-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfft.cpp
More file actions
41 lines (37 loc) · 702 Bytes
/
fft.cpp
File metadata and controls
41 lines (37 loc) · 702 Bytes
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
#include "fft.hpp"
void separate(Complex * X, size_t N)
{
Complex * b;
b = new Complex[N/2];
for (size_t i = 0; i < N / 2; ++i)
{
b[i] = X[2 * i + 1];
X[i] = X[2 * i];
}
for (size_t i = 0; i < N / 2; ++i)
{
X[i + N / 2] = b[i];
}
delete [] b;
}
void fft(Complex * X, size_t N)
{
if (N < 2)
{
// Do nothing
}
else
{
separate(X, N); //even numbers to the left, odd numbers to the left
fft(X, N / 2);
fft(X + N / 2, N / 2);
for (size_t k = 0; k < N / 2; ++k)
{
Complex even = X[k];
Complex odd = X[k + N / 2];
Complex w = std::exp(Complex(0.0, -2.*PI*k / N));
X[k] = even + w * odd;
X[k + N / 2] = even - w * odd;
}
}
}