forked from whoozle/clunk
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfft_context.h
152 lines (115 loc) · 2.89 KB
/
fft_context.h
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef FFT_CONTEXT_H__
#define FFT_CONTEXT_H__
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#include <complex>
#include <assert.h>
namespace clunk {
template<int N, typename T>
struct danielson_lanczos {
typedef danielson_lanczos<N / 2, T> next_type;
template<int SIGN>
static void apply(std::complex<T>* data) {
next_type::template apply<SIGN>(data);
next_type::template apply<SIGN>(data + N / 2);
T a = (T)(-2 * M_PI / N * SIGN);
T wtemp = sin(a / 2);
std::complex<T> wp(-2 * wtemp * wtemp, sin(a)), w(1, 0);
for (unsigned i = 0; i < N / 2 ; ++i) {
int j = i + N / 2;
std::complex<T> temp = data[j] * w;
data[j] = data[i] - temp;
data[i] += temp;
w += w * wp;
}
}
};
template<typename T>
struct danielson_lanczos<8, T> {
typedef danielson_lanczos<4, T> next_type;
static inline void rotate(std::complex<T>* data, int i, const std::complex<T>& w) {
int j = i + 4;
std::complex<T> temp = data[j] * w;
data[j] = data[i] - temp;
data[i] += temp;
}
template<int SIGN>
static inline void apply(std::complex<T>* data) {
next_type::template apply<SIGN>(data);
next_type::template apply<SIGN>(data + 4);
rotate(data, 0, std::complex<T>(1, 0));
rotate(data, 1, std::complex<T>(M_SQRT1_2, -M_SQRT1_2 * SIGN));
rotate(data, 2, std::complex<T>(0, -1));
rotate(data, 3, std::complex<T>(-M_SQRT1_2, -M_SQRT1_2 * SIGN));
}
};
template<typename T>
struct danielson_lanczos<4, T> {
typedef danielson_lanczos<2, T> next_type;
template<int SIGN>
static inline void apply(std::complex<T>* data) {
next_type::template apply<SIGN>(data);
next_type::template apply<SIGN>(data + 2);
std::complex<T> temp = data[2];
data[2] = data[0] - temp;
data[0] += temp;
temp = data[3] * std::complex<T>(0, -SIGN);
data[3] = data[1] - temp;
data[1] += temp;
}
};
template<typename T>
struct danielson_lanczos<2, T> {
template<int SIGN>
static inline void apply(std::complex<T>* data) {
std::complex<T> temp = data[1];
data[1] = data[0] - temp;
data[0] += temp;
}
};
template<typename T>
struct danielson_lanczos<1, T> {
template<int SIGN>
static inline void apply(std::complex<T>*) {}
};
template<int BITS, typename T = float>
class fft_context {
public:
enum { N = 1 << BITS };
typedef std::complex<T> value_type;
value_type data[N];
inline void fft() {
scramble(data);
next_type::template apply<1>(data);
}
inline void ifft() {
scramble(data);
next_type::template apply<-1>(data);
for(unsigned i = 0; i < N; ++i) {
data[i] /= N;
}
}
private:
typedef danielson_lanczos<N, T> next_type;
static inline void scramble(std::complex<T> * data) {
int j = 0;
for(int i = 0; i < N; ++i) {
if (i > j) {
std::swap(data[i], data[j]);
}
int m = N / 2;
while(j >= m && m >= 2) {
j -= m;
m >>= 1;
}
j += m;
}
}
};
}
#ifdef CLUNK_USES_SSE
# include "sse_fft_context.h"
#endif
#endif