-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft_complx.cpp
136 lines (117 loc) · 3.4 KB
/
fft_complx.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "fft_complx.h"
#include <stdio.h>
#include <complex>
#include <qdebug.h>
#include <qtimer.h>
#include <QTime>
fft_complx::fft_complx()
{
}
fft_complx::fft_fwd(std::complex<double> x[], int N)
{
// DFT
unsigned int k = N, n;
double thetaT = 3.14159265358979323846264338328L / N;
std::complex<double> phiT = {cos(thetaT), sin(thetaT)};
std::complex<double> T, y[N];
while (k > 1)
{
n = k;
k >>= 1;
phiT = phiT * phiT;
T = 1.0;
for (unsigned int l = 0; l < k; l++)
{
for (unsigned int a = l; a < N; a += n)
{
unsigned int b = a + k;
std::complex<double> t = x[a] - x[b];
x[a] += x[b];
x[b] = t * T;
}
T *= phiT;
}
}
// Decimate
unsigned int m = (unsigned int)log2(N);
for (unsigned int a = 0; a < N; a++)
{
unsigned int b = a;
// Reverse bits
b = (((b & 0xaaaaaaaa) >> 1) | ((b & 0x55555555) << 1));
b = (((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2));
b = (((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4));
b = (((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8));
b = ((b >> 16) | (b << 16)) >> (32 - m);
if (b > a)
{
std::complex<double> t = x[a];
x[a] = x[b];
x[b] = t;
}
}
for (unsigned int a = 0; a < N; a++){
if(a==0){y[a]=x[a];}else{y[a]=x[N-a];}}
for (unsigned int a = 0; a < N; a++){
x[a]=y[a];
}
//// Normalize (This section make it not working correctly)
// std::complex<double> f = 1.0 / sqrt(N);
// for (unsigned int i = 0; i < N; i++)
// x[i] *= f;
}
fft_complx::fft_bwd(std::complex<double> x[], int N)
{
// DFT
unsigned int k = N, n;
double thetaT = 3.14159265358979323846264338328L / N;
std::complex<double> phiT = {cos(thetaT), -sin(thetaT)};
std::complex<double> T, y[N];
while (k > 1)
{
n = k;
k >>= 1;
phiT = phiT * phiT;
T = 1.0;
for (unsigned int l = 0; l < k; l++)
{
for (unsigned int a = l; a < N; a += n)
{
unsigned int b = a + k;
std::complex<double> t = x[a] - x[b];
x[a] += x[b];
x[b] = t * T;
}
T *= phiT;
}
}
// Decimate
unsigned int m = (unsigned int)log2(N);
for (unsigned int a = 0; a < N; a++)
{
unsigned int b = a;
// Reverse bits
b = (((b & 0xaaaaaaaa) >> 1) | ((b & 0x55555555) << 1));
b = (((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2));
b = (((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4));
b = (((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8));
b = ((b >> 16) | (b << 16)) >> (32 - m);
if (b > a)
{
std::complex<double> t = x[a];
x[a] = x[b];
x[b] = t;
}
}
for (unsigned int a = 0; a < N; a++){
if(a==0){y[a]=x[a];}else{y[a]=x[N-a];}}
for (unsigned int a = 0; a < N; a++){
x[a]=y[a];
}
//// Normalize the reverse FFT
std::complex<double> Per = {N,0};
for (unsigned int i = 0; i < N; i++)
{
x[i] = x[i]/Per;
}
}