forked from bemasher/fftw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfftw_test.go
259 lines (214 loc) · 6.9 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
package fftw
import (
"fmt"
"math"
"testing"
)
const (
Tolerance = 1e-15
)
var TestInputVectorF64 []float64 = []float64{1, 1, 1, 1, 0, 0, 0, 0}
var TestInputVectorC128 []complex128 = []complex128{1, 1, 1, 1, 0, 0, 0, 0}
var TestOutputVectorC128 []complex128
var TestOutputVector []uint64 = []uint64{
0x4010000000000000, 0x0000000000000000,
0x3FF0000000000000, 0xC003504F333F9DE6,
0x0000000000000000, 0x0000000000000000,
0x3FF0000000000000, 0xBFDA827999FCEF34,
0x0000000000000000, 0x0000000000000000,
0x3FF0000000000000, 0x3FDA827999FCEF34,
0x0000000000000000, 0x0000000000000000,
0x3FF0000000000000, 0x4003504F333F9DE6,
}
var Localities []Locality = []Locality{InPlace, OutOfPlace}
func TestDFT1D(t *testing.T) {
for _, locality := range Localities {
// Plan forward and backward transforms for current locality
dftForward := NewDFT1D(8, nil, nil, Forward, locality, Measure)
dftBackward := NewDFT1D(8, nil, nil, Backward, locality, Measure)
defer dftForward.Close()
defer dftBackward.Close()
// Copy test vector to input and execute
copy(dftForward.In, TestInputVectorC128)
dftForward.Execute()
// Check output against test output vector
for i := range dftForward.Out {
diff := dftForward.Out[i] - TestOutputVectorC128[i]
if math.Abs(real(diff)) > Tolerance || math.Abs(imag(diff)) > Tolerance {
t.Fatalf("failed: %+0.3f %+0.3f", dftForward.Out[i], TestOutputVectorC128[i])
}
}
// Copy back to input for backward transform and execute
copy(dftBackward.In, dftForward.Out)
dftBackward.Execute()
// Normalize the output data, FFTW doesn't by default
for i, b := range dftBackward.Out {
dftBackward.Out[i] = complex(real(b)/8, imag(b)/8)
}
// Check output against test input vector
for i := range dftBackward.Out {
diff := dftBackward.Out[i] - TestInputVectorC128[i]
if math.Abs(real(diff)) > Tolerance || math.Abs(imag(diff)) > Tolerance {
t.Fatalf("failed: %+0.3f %+0.3f", dftBackward.Out[i], TestInputVectorC128[i])
}
}
}
}
func ExampleDFT1DPlan() {
dft := NewDFT1D(8, nil, nil, Forward, OutOfPlace, Measure)
defer dft.Close()
// Modifying the location of the input or output arrays will cause FFTW to
// fail. Instead, copy data into and out of arrays.
data := []complex128{1, 1, 1, 1, 0, 0, 0, 0}
copy(dft.In, data)
dft.Execute()
fmt.Println(dft)
fmt.Printf("%+0.3f\n", dft.In)
fmt.Printf("%+0.3f\n", dft.Out)
// Output:
// {Direction:Forward Locality:OutOfPlace PlanFlags:NA In:[8]complex128 Out:[8]complex128}
// [(+1.000+0.000i) (+1.000+0.000i) (+1.000+0.000i) (+1.000+0.000i) (+0.000+0.000i) (+0.000+0.000i) (+0.000+0.000i) (+0.000+0.000i)]
// [(+4.000+0.000i) (+1.000-2.414i) (+0.000+0.000i) (+1.000-0.414i) (+0.000+0.000i) (+1.000+0.414i) (+0.000+0.000i) (+1.000+2.414i)]
}
func TestHCDFT1D(t *testing.T) {
for _, locality := range Localities {
// Plan forward and backward transforms for current locality
dftForward := NewHCDFT1D(8, nil, nil, Forward, locality, Measure)
dftBackward := NewHCDFT1D(8, nil, nil, Backward, locality, Measure)
defer dftForward.Close()
defer dftBackward.Close()
// Copy test vector to input and execute
copy(dftForward.Real, TestInputVectorF64)
dftForward.Execute()
// Check output against test output vector
for i := range dftForward.Complex {
diff := dftForward.Complex[i] - TestOutputVectorC128[i]
if math.Abs(real(diff)) > Tolerance || math.Abs(imag(diff)) > Tolerance {
t.Fatalf("failed: %+0.3f %+0.3f", dftForward.Complex[i], TestOutputVectorC128[i])
}
}
// Copy back to input for backward transform and execute
copy(dftBackward.Complex, dftForward.Complex)
dftBackward.Execute()
// Normalize the output data, FFTW doesn't by default
for i := range dftBackward.Real {
dftBackward.Real[i] /= 8
}
// Check output against test input vector
for i := range dftBackward.Real {
diff := dftBackward.Real[i] - TestInputVectorF64[i]
if math.Abs(diff) > Tolerance {
t.Fatalf("failed: %+0.3f %+0.3f", dftBackward.Real[i], TestInputVectorF64[i])
}
}
}
}
func ExampleHCDFT1DPlan() {
dft := NewHCDFT1D(8, nil, nil, Forward, OutOfPlace, Measure)
defer dft.Close()
// Modifying the location of the input or output arrays will cause FFTW to
// fail. Instead, copy data into and out of arrays.
copy(dft.Real, []float64{1, 1, 1, 1, 0, 0, 0, 0})
dft.Execute()
fmt.Println(dft)
fmt.Printf("%+0.3f\n", dft.Real)
fmt.Printf("%+0.3f\n", dft.Complex)
// Output:
// {Direction:Forward Locality:OutOfPlace PlanFlags:NA Real:[8]float64 Complex:[5]complex128}
// [+1.000 +1.000 +1.000 +1.000 +0.000 +0.000 +0.000 +0.000]
// [(+4.000+0.000i) (+1.000-2.414i) (+0.000+0.000i) (+1.000-0.414i) (+0.000+0.000i)]
}
const (
Bits = 12
BlockSize = 1 << Bits
)
func BenchmarkDFTInPlaceForward(b *testing.B) {
dft := NewDFT1D(BlockSize, nil, nil, Forward, InPlace, Measure)
defer dft.Close()
b.SetBytes(BlockSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dft.Execute()
}
}
func BenchmarkDFTOutOfPlaceForward(b *testing.B) {
dft := NewDFT1D(BlockSize, nil, nil, Forward, OutOfPlace, Measure)
defer dft.Close()
b.SetBytes(BlockSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dft.Execute()
}
}
func BenchmarkDFTInPlaceBackward(b *testing.B) {
dft := NewDFT1D(BlockSize, nil, nil, Backward, InPlace, Measure)
defer dft.Close()
b.SetBytes(BlockSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dft.Execute()
}
}
func BenchmarkDFTOutOfPlaceBackward(b *testing.B) {
dft := NewDFT1D(BlockSize, nil, nil, Backward, OutOfPlace, Measure)
defer dft.Close()
b.SetBytes(BlockSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dft.Execute()
}
}
func BenchmarkHCDFTInPlaceForward(b *testing.B) {
dft := NewHCDFT1D(BlockSize, nil, nil, Forward, InPlace, Measure)
defer dft.Close()
b.SetBytes(BlockSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dft.Execute()
}
}
func BenchmarkHCDFTOutOfPlaceForward(b *testing.B) {
dft := NewHCDFT1D(BlockSize, nil, nil, Forward, OutOfPlace, Measure)
defer dft.Close()
b.SetBytes(BlockSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dft.Execute()
}
}
func BenchmarkHCDFTInPlaceBackward(b *testing.B) {
dft := NewHCDFT1D(BlockSize, nil, nil, Backward, InPlace, Measure)
defer dft.Close()
b.SetBytes(BlockSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dft.Execute()
}
}
func BenchmarkHCDFTOutOfPlaceBackward(b *testing.B) {
dft := NewHCDFT1D(BlockSize, nil, nil, Backward, OutOfPlace, Measure)
defer dft.Close()
b.SetBytes(BlockSize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
dft.Execute()
}
}
func init() {
// Allocate and initialize test output data vector
TestOutputVectorC128 = make([]complex128, len(TestOutputVector)>>1)
for i := range TestOutputVectorC128 {
TestOutputVectorC128[i] = complex(
math.Float64frombits(TestOutputVector[i<<1]),
math.Float64frombits(TestOutputVector[i<<1+1]),
)
}
}