-
Notifications
You must be signed in to change notification settings - Fork 2
/
fft_test.go
265 lines (227 loc) Β· 7.03 KB
/
fft_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
package fourier
import (
"fmt"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var epsilon = 0.0000001
func TestBitReversal(t *testing.T) {
b16 := "%016b"
require.Equal(t, "0000000000000100", fmt.Sprintf(b16, reverseBits(2, 4)))
require.Equal(t, "0000000001000000", fmt.Sprintf(b16, reverseBits(2, 8)))
require.Equal(t, "0000000010000000", fmt.Sprintf(b16, reverseBits(1, 8)))
require.Equal(t, "0000000000100000", fmt.Sprintf(b16, reverseBits(1, 6)))
require.Equal(t, "1000000000000000", fmt.Sprintf(b16, reverseBits(1, 16)))
require.Equal(t, "0000000000000001", fmt.Sprintf(b16, reverseBits(1, 1)))
require.Equal(t, "0000000000000001", fmt.Sprintf(b16, reverseBits(1, 0)))
}
func TestLog2(t *testing.T) {
require.Equal(t, uint(1), log2(3))
require.Equal(t, uint(4), log2(16))
require.Equal(t, uint(5), log2(32))
}
func TestIsPowerOf2(t *testing.T) {
require.True(t, isPowerOfTwo(2))
require.True(t, isPowerOfTwo(4))
require.True(t, isPowerOfTwo(8))
require.True(t, isPowerOfTwo(1))
require.False(t, isPowerOfTwo(5))
require.False(t, isPowerOfTwo(7))
}
func TestNextPowerOfTwo(t *testing.T) {
require.Equal(t, 1, nextPowerOfTwo(1))
require.Equal(t, 8, nextPowerOfTwo(5))
require.Equal(t, 64, nextPowerOfTwo(50))
require.Equal(t, 64, nextPowerOfTwo(64))
}
func TestButterflyReorder(t *testing.T) {
buf := []complex128{
complex(1, 0),
complex(2, 0),
complex(3, 0),
complex(4, 0),
complex(5, 0),
complex(6, 0),
complex(7, 0),
complex(8, 0),
}
reorder(buf)
require.Equal(t, []complex128{
complex(1, 0),
complex(5, 0),
complex(3, 0),
complex(7, 0),
complex(2, 0),
complex(6, 0),
complex(4, 0),
complex(8, 0),
}, buf)
}
func TestForwardTransform(t *testing.T) {
buf := make([]complex128, 8)
for i := 0; i < 4; i++ {
buf[i] = complex(1, 0)
}
Forward(buf)
cmplxEqualEpsilon(t, []complex128{
(4 + 0i),
(1 - 2.414213562373095i),
(0 + 0i),
(1 - 0.4142135623730949i),
(0 + 0i),
(0.9999999999999999 + 0.4142135623730949i),
(0 + 0i),
(0.9999999999999997 + 2.414213562373095i),
}, buf, epsilon)
}
func TestRoundTripTransform(t *testing.T) {
buf := make([]complex128, 4)
for i := range buf {
buf[i] = complex(float64(i)+1, 0)
}
Forward(buf)
cmplxEqualEpsilon(t, []complex128{
(10 + 0i),
(-2 + 2i),
(-2 + 0i),
(-2 - 2i),
}, buf, epsilon)
Inverse(buf)
cmplxEqualEpsilon(t, []complex128{
(1 + 0i),
(2 + 0i),
(3 + 0i),
(4 + 0i),
}, buf, epsilon)
}
func TestFrequencyDomainZeroPaddingResample(t *testing.T) {
for _, tt := range []struct {
src []float64
scale int
expected []float64
}{
{
src: []float64{1, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5},
scale: 1,
expected: []float64{1, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5},
},
{
src: []float64{1, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5},
scale: 2,
expected: []float64{1, 0.75, 0.5, 0.75, 1.0, 0.75, 0.5, 0.75, 1.0, 0.75, 0.5, 0.75, 1.0, 0.75, 0.5, 0.75},
},
{
src: []float64{1, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5},
scale: 8,
expected: []float64{1, 0.9809698831278217, 0.9267766952966369, 0.8456708580912724, 0.75, 0.6543291419087276, 0.5732233047033631, 0.5190301168721783, 0.5, 0.5190301168721783, 0.5732233047033631, 0.6543291419087276, 0.75, 0.8456708580912724, 0.9267766952966369, 0.9809698831278217, 1, 0.9809698831278217, 0.9267766952966369, 0.8456708580912724, 0.75, 0.6543291419087276, 0.5732233047033631, 0.5190301168721783, 0.5, 0.5190301168721783, 0.5732233047033631, 0.6543291419087276, 0.75, 0.8456708580912724, 0.9267766952966369, 0.9809698831278217, 1, 0.9809698831278217, 0.9267766952966369, 0.8456708580912724, 0.75, 0.6543291419087276, 0.5732233047033631, 0.5190301168721783, 0.5, 0.5190301168721783, 0.5732233047033631, 0.6543291419087276, 0.75, 0.8456708580912724, 0.9267766952966369, 0.9809698831278217, 1, 0.9809698831278217, 0.9267766952966369, 0.8456708580912724, 0.75, 0.6543291419087276, 0.5732233047033631, 0.5190301168721783, 0.5, 0.5190301168721783, 0.5732233047033631, 0.6543291419087276, 0.75, 0.8456708580912724, 0.9267766952966369, 0.9809698831278217},
},
} {
var (
l = len(tt.src)
ln = l * tt.scale
csrc = make([]complex128, l)
cpadded = make([]complex128, ln)
out = make([]float64, ln)
)
for i, v := range tt.src {
csrc[i] = complex(v*float64(tt.scale), 0)
}
require.NoError(t, Forward(csrc))
// Split the spectral content down the middle and copy both ends into the
// ends of a new upscaled buffer. This will leave the zeros in the center.
for i := 0; i < l/2; i++ {
cpadded[i] = csrc[i]
cpadded[len(cpadded)-1-i] = csrc[len(csrc)-1-i]
}
require.NoError(t, Inverse(cpadded))
for i, v := range cpadded {
out[i] = real(v)
}
assert.Equal(t, tt.expected, out)
}
}
func TestMagnitude(t *testing.T) {
const fc = 10.0
const fs = 32.0 * fc
const size = 256
carrier := make([]complex128, size)
cmplxCarrier(carrier, fc, fs)
err := Forward(carrier)
require.NoError(t, err)
abs := make([]float64, len(carrier))
err = Magnitude(abs, carrier)
require.NoError(t, err)
// Carrier Frequency = 10Hz
// Resolution = 1.25Hz
// Spike @ Carrier Frequency / Resolution = 8
assert.Equal(t, 1.0, abs[8])
// Spike in negative frequencies
assert.Equal(t, 1.0, abs[248])
// Other parts should be close to zero
assert.Equal(t, 0.0, math.Round(abs[0]))
assert.Equal(t, 0.0, math.Round(abs[10]))
}
func cmplxEqualEpsilon(t *testing.T, expected, actual []complex128, epsilon float64) {
t.Helper()
for i := range expected {
if real(expected[i]) == 0 {
assert.Equal(t, 0.0, real(actual[i]))
} else {
assert.InEpsilon(t, real(expected[i]), real(actual[i]), epsilon)
}
if imag(expected[i]) == 0 {
assert.Equal(t, 0.0, imag(actual[i]))
} else {
assert.InEpsilon(t, imag(expected[i]), imag(actual[i]), epsilon)
}
}
}
func BenchmarkFFT(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
src := make([]complex128, 64)
for i := range src {
src[i] = complex(float64(i)+1, 0)
}
buf := make([]complex128, len(src))
for i := 0; i < b.N; i++ {
copy(buf, src)
b.StartTimer()
Forward(buf)
b.StopTimer()
}
}
func BenchmarkIFFT(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
src := make([]complex128, 64)
for i := range src {
src[i] = complex(float64(i)+1, 0)
}
Forward(src)
buf := make([]complex128, len(src))
for i := 0; i < b.N; i++ {
copy(buf, src)
b.StartTimer()
Inverse(buf)
b.StopTimer()
}
}
func ExampleForward_roundtrip() {
buf := make([]complex128, 8)
for i := range buf {
buf[i] = complex(float64(i+1), 0)
}
fmt.Println("time:", buf)
// Transform to the frequency domain
Forward(buf)
fmt.Println("frequency:", buf)
// Transform back to the time domain
Inverse(buf)
fmt.Println("time:", buf)
// Output: time: [(1+0i) (2+0i) (3+0i) (4+0i) (5+0i) (6+0i) (7+0i) (8+0i)]
// frequency: [(36+0i) (-4+9.65685424949238i) (-4+4i) (-4+1.6568542494923797i) (-4+0i) (-3.9999999999999996-1.6568542494923797i) (-3.9999999999999996-4i) (-3.9999999999999987-9.65685424949238i)]
// time: [(1.0000000000000002+0i) (2+0i) (3+0i) (4+0i) (5+0i) (6+0i) (7+0i) (8+0i)]
}