forked from runningwild/go-fftw
/
fftw_test.go
281 lines (262 loc) · 8.56 KB
/
fftw_test.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
package fftwf
import (
"github.com/orfjackal/gospec/src/gospec"
//. "github.com/orfjackal/gospec/src/gospec"
"math"
)
func Alloc1dSpec(c gospec.Context) {
d10 := Alloc1d(10)
d100 := Alloc1d(100)
d1000 := Alloc1d(1000)
c.Specify("Allocates the appropriate memory for 1d arrays.", func() {
c.Expect(len(d10), gospec.Equals, 10)
c.Expect(len(d100), gospec.Equals, 100)
c.Expect(len(d1000), gospec.Equals, 1000)
})
}
// Make sure that the memory allocated by fftw is getting properly GCed
func GCSpec(c gospec.Context) {
tot := 0.0
for i := 0; i < 1000; i++ {
d := Alloc1d(100000000) // Allocate a bunch of memory
d[10000] = complex(float32(i), 0) // Do something stupid with it so
tot += float64(real(d[10000])) // hopefully it doesn't get optimized out
}
}
func Alloc2dSpec(c gospec.Context) {
d100x50 := Alloc2d(100, 50)
c.Specify("Allocates the appropriate memory for 2d arrays.", func() {
c.Expect(len(d100x50), gospec.Equals, 100)
for _, v := range d100x50 {
c.Expect(len(v), gospec.Equals, 50)
}
counter := float32(0.0)
for i := range d100x50 {
for j := range d100x50[i] {
d100x50[i][j] = complex(counter, 0)
counter += 1.0
}
}
counter = 0.0
for i := range d100x50 {
for j := range d100x50[i] {
c.Expect(real(d100x50[i][j]), gospec.Equals, counter)
counter += 1.0
}
}
})
}
func Alloc3dSpec(c gospec.Context) {
d100x20x10 := Alloc3d(100, 20, 10)
c.Specify("Allocates the appropriate memory for 3d arrays.", func() {
c.Expect(len(d100x20x10), gospec.Equals, 100)
for _, v := range d100x20x10 {
c.Expect(len(v), gospec.Equals, 20)
for _, v := range v {
c.Expect(len(v), gospec.Equals, 10)
}
}
counter := float32(0.0)
for i := range d100x20x10 {
for j := range d100x20x10[i] {
for k := range d100x20x10[i][j] {
d100x20x10[i][j][k] = complex(counter, 0)
counter += 1.0
}
}
}
counter = 0.0
for i := range d100x20x10 {
for j := range d100x20x10[i] {
for k := range d100x20x10[i][j] {
c.Expect(real(d100x20x10[i][j][k]), gospec.Equals, counter)
counter += 1.0
}
}
}
})
}
func peakVerifier(s []complex64, c gospec.Context) {
c.Expect(real(s[0]), gospec.IsWithin(1e-6), 0.0)
c.Expect(imag(s[0]), gospec.IsWithin(1e-6), 0.0)
c.Expect(real(s[1]), gospec.IsWithin(1e-6), float32(len(s))/2)
c.Expect(imag(s[1]), gospec.IsWithin(1e-6), 0.0)
for i := 2; i < len(s)-1; i++ {
c.Expect(real(s[i]), gospec.IsWithin(1e-6), 0.0)
c.Expect(imag(s[i]), gospec.IsWithin(1e-6), 0.0)
}
c.Expect(real(s[len(s)-1]), gospec.IsWithin(1e-6), float32(len(s))/2)
c.Expect(imag(s[len(s)-1]), gospec.IsWithin(1e-6), 0.0)
}
func FFT1dSpec(c gospec.Context) {
signal := Alloc1d(16)
new_in := Alloc1d(16)
new_out := Alloc1d(16)
for i := range signal {
signal[i] = complex(float32(i), float32(-i))
new_in[i] = signal[i]
}
forward := PlanDft1d(signal, signal, Forward, Estimate)
c.Specify("Creating a plan doesn't overwrite an existing array if fftw.Estimate is used.", func() {
for i := range signal {
c.Expect(signal[i], gospec.Equals, complex(float32(i), float32(-i)))
}
})
// A simple real cosine should result in transform with two spikes, one at S[1] and one at S[-1]
// The spikes should be real and have amplitude equal to len(S)/2 (because fftw doesn't normalize)
for i := range signal {
signal[i] = complex(float32(math.Cos(float64(i)/float64(len(signal))*math.Pi*2)), 0)
new_in[i] = signal[i]
}
forward.Execute()
c.Specify("Forward 1d FFT works properly.", func() {
peakVerifier(signal, c)
})
// This should also be the case when using new arrays...
forward.ExecuteNewArray(new_in, new_out)
c.Specify("New array Forward 1d FFT works properly", func() {
peakVerifier(new_out, c)
})
}
func FFT2dSpec(c gospec.Context) {
signal := Alloc2d(64, 8)
for i := range signal {
for j := range signal[i] {
signal[i][j] = complex(float32(i+j), float32(-i-j))
}
}
forward := PlanDft2d(signal, signal, Forward, Estimate)
c.Specify("Creating a plan doesn't overwrite an existing array if fftw.Estimate is used.", func() {
for i := range signal {
for j := range signal[i] {
c.Expect(signal[i][j], gospec.Equals, complex(float32(i+j), float32(-i-j)))
}
}
})
// As long as fx < dx/2 and fy < dy/2, where dx and dy are the lengths in each dimension,
// there will be 2^n spikes, where n is the number of dimensions. Each spike will be
// real and have magnitude equal to dx*dy / 2^n
dx := len(signal)
fx := float64(dx) / 4
dy := len(signal[0])
fy := float64(dy) / 4
for i := range signal {
for j := range signal[i] {
cosx := float32(math.Cos(float64(i) / float64(dx) * fx * math.Pi * 2))
cosy := float32(math.Cos(float64(j) / float64(dy) * fy * math.Pi * 2))
signal[i][j] = complex(cosx*cosy, 0)
}
}
forward.Execute()
c.Specify("Forward 2d FFT works properly.", func() {
for i := range signal {
for j := range signal[i] {
if (i == int(fx) || i == dx-int(fx)) &&
(j == int(fy) || j == dy-int(fy)) {
c.Expect(real(signal[i][j]), gospec.IsWithin(1e-7), float32(dx*dy/4))
c.Expect(imag(signal[i][j]), gospec.IsWithin(1e-7), 0.0)
} else {
c.Expect(real(signal[i][j]), gospec.IsWithin(1e-7), 0.0)
c.Expect(imag(signal[i][j]), gospec.IsWithin(1e-7), 0.0)
}
}
}
})
}
func FFT3dSpec(c gospec.Context) {
signal := Alloc3d(32, 16, 8)
for i := range signal {
for j := range signal[i] {
for k := range signal[i][j] {
signal[i][j][k] = complex(float32(i+j+k), float32(-i-j-k))
}
}
}
forward := PlanDft3d(signal, signal, Forward, Estimate)
c.Specify("Creating a plan doesn't overwrite an existing array if fftw.Estimate is used.", func() {
for i := range signal {
for j := range signal[i] {
for k := range signal[i][j] {
c.Expect(signal[i][j][k], gospec.Equals, complex(float32(i+j+k), float32(-i-j-k)))
}
}
}
})
// As long as fx < dx/2, fy < dy/2, and fz < dz/2, where dx,dy,dz are the lengths in
// each dimension, there will be 2^n spikes, where n is the number of dimensions.
// Each spike will be real and have magnitude equal to dx*dy*dz / 2^n
dx := len(signal)
fx := float64(dx) / 4
dy := len(signal[0])
fy := float64(dy) / 4
dz := len(signal[0][0])
fz := float64(dz) / 4
for i := range signal {
for j := range signal[i] {
for k := range signal[i][j] {
cosx := float32(math.Cos(float64(i) / float64(dx) * fx * math.Pi * 2))
cosy := float32(math.Cos(float64(j) / float64(dy) * fy * math.Pi * 2))
cosz := float32(math.Cos(float64(k) / float64(dz) * fz * math.Pi * 2))
signal[i][j][k] = complex(cosx*cosy*cosz, 0)
}
}
}
forward.Execute()
c.Specify("Forward 3d FFT works properly.", func() {
for i := range signal {
for j := range signal[i] {
for k := range signal[i][j] {
if (i == int(fx) || i == dx-int(fx)) &&
(j == int(fy) || j == dy-int(fy)) &&
(k == int(fz) || k == dz-int(fz)) {
c.Expect(real(signal[i][j][k]), gospec.IsWithin(1e-7), float32(dx*dy*dz/8))
c.Expect(imag(signal[i][j][k]), gospec.IsWithin(1e-7), 0.0)
} else {
c.Expect(real(signal[i][j][k]), gospec.IsWithin(1e-7), 0.0)
c.Expect(imag(signal[i][j][k]), gospec.IsWithin(1e-7), 0.0)
}
}
}
}
})
}
func FFTR2CSpec(c gospec.Context) {
signal := make([]float32, 16)
F_signal := make([]complex64, 9)
for i := range signal {
signal[i] = float32(math.Sin(float64(i) / float64(len(signal)) * math.Pi * 2))
}
forward := PlanDftR2C1d(signal, F_signal, Estimate)
forward.Execute()
c.Specify("Running a R2C transform doesn't destroy the input.", func() {
for i := range signal {
c.Expect(signal[i], gospec.Equals, float32(math.Sin(float64(i)/float64(len(signal))*math.Pi*2)))
}
})
c.Specify("Forward 1d Real to Complex FFT works properly.", func() {
c.Expect(real(F_signal[0]), gospec.IsWithin(1e-6), 0.0)
c.Expect(imag(F_signal[0]), gospec.IsWithin(1e-6), 0.0)
c.Expect(real(F_signal[1]), gospec.IsWithin(1e-6), 0.0)
c.Expect(imag(F_signal[1]), gospec.IsWithin(1e-6), -float32(len(signal))/2)
for i := 2; i < len(F_signal)-1; i++ {
c.Expect(real(F_signal[i]), gospec.IsWithin(1e-6), 0.0)
c.Expect(imag(F_signal[i]), gospec.IsWithin(1e-6), 0.0)
}
})
}
func FFTC2RSpec(c gospec.Context) {
signal := make([]float32, 16)
F_signal := make([]complex64, 9)
forward := PlanDftR2C1d(signal, F_signal, Estimate)
backward := PlanDftC2R1d(F_signal, signal, Estimate)
for i := range signal {
signal[i] = float32(i)
}
forward.Execute()
backward.Execute()
c.Specify("Forward 1d Complx to Real FFT works properly.", func() {
for i := 0; i < len(signal); i++ {
c.Expect(signal[i], gospec.IsWithin(1e-5), float32(i*len(signal)))
}
})
}