-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.go
299 lines (252 loc) · 5.94 KB
/
fft.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package godsp
import "errors"
import "math"
import "math/cmplx"
func isPowerOf2(n int) bool {
return ((n != 0) && !((n & (n - 1)) != 0))
}
func FFT(data []complex128) error {
//could special case a few more multiples here like 4 which is particularily efficient
//Also could exploit the concurrent nature of go a little more to get implicit threading
//could also check for perfect squares because bluestein can be done very efficient there
n := len(data)
if n == 0 {
return nil
}
if n == 1 {
return nil
}
if n > 1e6 { //tune this number against the goroutine overhead
if n%4 == 0 {
return FFTmod4(data)
}
if n%2 == 0 {
return FFTmod2(data)
}
}
if isPowerOf2(n) {
return FFTradix2(data)
}
return FFTBluestein(data)
}
func IFFT(data []complex128) error {
swapped := make([]complex128, len(data))
for i, v := range data {
swapped[i] = complex(imag(v), real(v))
}
err := FFT(swapped)
if err != nil {
return err
}
for i, v := range swapped {
data[i] = complex(imag(v)/float64(len(data)), real(v)/float64(len(data)))
}
return nil
}
func bitReverse(x, levels int) int {
result := 0
for i := 0; i < levels; i++ {
result = (result << 1) | (x & 1)
x = x >> 1
}
return result
}
func getLevels(size int) int {
// Compute levels = floor(log2(n))
levels := 0
for size > 1 {
levels++
size = size >> 1
}
return levels
}
func FFTradix2(data []complex128) error {
// Variables
n := len(data)
levels := getLevels(n)
// Bit-reversed addressing permutation
for i := range data {
j := bitReverse(i, levels)
if j > i {
data[i], data[j] = data[j], data[i]
}
}
factors := make([]complex128, n)
for i := range factors {
arg := -2.0 * math.Pi * float64(i) / float64(n)
val := complex(math.Cos(arg), math.Sin(arg))
factors[i] = val
}
// Cooley-Tukey decimation-in-time radix-2 FFT
for size := 2; size <= n; size *= 2 {
halfsize := size / 2
tablestep := n / size
for i := 0; i < n; i += size {
k := 0
for j := i; j < i+halfsize; j++ {
t := data[j+halfsize] * factors[k]
data[j+halfsize] = data[j] + t
data[j] += t
k += tablestep
}
}
}
return nil
}
func getMLevel(input int) (int, error) {
out := 0
for input != 1 {
if input%4 != 0 {
return 0, errors.New("input not a power of 4")
}
input = input >> 2
out++
}
return out, nil
}
func FFTRadix4(data []complex128) error {
//public static void FFTR4(double[] X, double[] Y, int N, int M) {
M, err := getMLevel(len(data))
if err != nil {
return err
}
// N = 4 ^ M
// N = 1 << (M+M);
N := len(data)
N1 := 0
N2 := len(data)
I1 := 0
I2 := 0
I3 := 0
twiddles := make([]complex128, N)
for i := range twiddles {
arg := 2 * math.Pi * float64(i) / float64(N)
twiddles[i] = complex(math.Cos(arg), math.Sin(arg))
}
for K := 0; K < M; K++ {
N1 = N2
N2 = N2 / 4
for J := 0; J < N2; J++ {
//Should be pre-calculated for optimization
ind := J << uint(K)
Tw1 := twiddles[ind%N]
Tw2 := twiddles[(2*ind)%N]
Tw3 := twiddles[(3*ind)%N]
CO1 := real(Tw1)
CO2 := real(Tw2)
CO3 := real(Tw3)
SI1 := imag(Tw1)
SI2 := imag(Tw2)
SI3 := imag(Tw3)
for I := J; I < N; I += N1 {
I1 = I + N2
I2 = I1 + N2
I3 = I2 + N2
C1 := data[I] + data[I2]
C3 := data[I] - data[I2]
C2 := data[I1] + data[I3]
C4 := data[I1] - data[I3]
data[I] = C1 + C2
//These adds and subtracts are probably efficient enough
C2 = C1 - C2
C1 = complex(real(C3)-imag(C4), imag(C3)+real(C4))
C3 = complex(real(C3)+imag(C4), imag(C3)-real(C4))
//These mults should be able to be converted into the complex domain
data[I1] = complex(CO1*real(C3)+SI1*imag(C3), CO1*imag(C3)-SI1*real(C3))
data[I2] = complex(CO2*real(C2)+SI2*imag(C2), CO2*imag(C2)-SI2*real(C2))
data[I3] = complex(CO3*real(C1)+SI3*imag(C1), CO3*imag(C1)-SI3*real(C1))
}
}
}
// Radix-4 bit-reverse
J := 0
N2 = N >> 2
for I := 0; I < N-1; I++ {
if I < J {
data[I], data[J] = data[J], data[I]
}
N1 = N2
for J >= 3*N1 {
J -= 3 * N1
N1 >>= 2
}
J += N1
}
return nil
}
func FFTmod4(data []complex128) error {
return FFTmod2(data)
}
func FFTmod2(data []complex128) error {
FirstPart := data[:len(data)/2]
SecondPart := data[len(data)/2:]
for i := range FirstPart {
tw := cmplx.Exp(complex(0, 2*math.Pi*float64(i)/float64(len(data))))
t := tw * (FirstPart[i] - SecondPart[i])
FirstPart[i] = FirstPart[i] + SecondPart[i]
SecondPart[i] = t
}
//Parallelize the decomp here, mod2 should only be called for very large data
evenResult := make(chan error)
oddResult := make(chan error)
go func() { evenResult <- FFT(FirstPart) }()
go func() { oddResult <- FFT(SecondPart) }()
if err := <-evenResult; err != nil {
return err
}
if err := <-oddResult; err != nil {
return err
}
levels := getLevels(len(data))
// Bit-reversed addressing permutation
for i := range data {
j := bitReverse(i, levels)
if j > i {
data[i], data[j] = data[j], data[i]
}
}
return nil
}
func nextPower(value int) int {
ret := 1
for ret < value {
ret = ret * 2
}
return ret
}
func FFTBluestein(data []complex128) error {
PadLen := nextPower(2*len(data) - 1)
chirpedData := make([]complex128, PadLen)
chirp := make([]complex128, len(data))
wrappedChirp := make([]complex128, PadLen)
//Prepare chirps
for i, v := range data {
chirp[i] = cmplx.Exp(complex(0, -1*math.Pi*float64(i*i)/float64(len(data))))
wrappedChirp[i] = cmplx.Conj(chirp[i])
if i != 0 {
wrappedChirp[PadLen-i] = -1 * cmplx.Conj(chirp[i])
}
chirpedData[i] = chirp[i] * v
}
err := FFT(chirpedData)
if err != nil {
return err
}
//This can be precomputed and should be... but I'm lazy
//This would save alot fo memory and increase speed by a factor
err = FFT(wrappedChirp)
if err != nil {
return err
}
for i, v := range wrappedChirp {
chirpedData[i] = v * wrappedChirp[i]
}
err = IFFT(chirpedData)
if err != nil {
return err
}
for i, v := range chirpedData[0:len(data)] {
data[i] = v * chirp[i]
}
return nil
}