DiscreteFourierTransform.cc
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "DiscreteFourierTransform.hh"
#include <bits/stdc++.h>
template <class T, class E>
DiscreteFourierTransform<T, E>::DiscreteFourierTransform(int nbEchantillons_, vector<T> signal_, double frequenceEchantillonnage_)
{
nbEchantillons = nbEchantillons_;
frequenceEchantillonnage = frequenceEchantillonnage_;
signal = signal_;
}
template <class T, class E>
void DiscreteFourierTransform<T, E>::compute()
{
for (int k = 0; k < nbEchantillons; k++)
{
E xR = 0;
E yI = 0;
E fk = (E)(k * frequenceEchantillonnage) / (E)nbEchantillons;
// E fk = (E)(k * 1.0 / nbEchantillons);
for (int n = 0; n < signal.size(); n++)
{
const T val = signal[n];
if (!isNAN(val))
{
const double angle = 2 * PI * n * fk;
xR += (E)cos(angle) * val;
yI += (E)sin(angle) * val;
}
}
E amplitude = (E)sqrt(xR * xR + yI * yI);
E phase = (E)atan2(yI, xR);
amplitudes.push_back(amplitude);
phases.push_back(phase);
frequences.push_back(fk);
if (fk == (E)0)
periodes.push_back((E)0);
else
periodes.push_back((E)(1.0 / fk));
sort(periodes.begin(), periodes.end());
}
}