-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcorr_test.go
348 lines (313 loc) · 6.72 KB
/
xcorr_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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package muse
import (
"math"
"math/rand"
"testing"
"gonum.org/v1/gonum/dsp/fourier"
"gonum.org/v1/gonum/floats"
)
func isPositive() func(float64) bool {
return func(x float64) bool { return x > 0 }
}
func isNegative() func(float64) bool {
return func(x float64) bool { return x < 0 }
}
func TestNextPowOf2(t *testing.T) {
data := []struct {
val float64
expected int
}{
{1.0, 1},
{1.5, 2},
{4.5, 8},
{15.9, 16},
{-5, 0},
{0, 0},
}
for _, d := range data {
if val := nextPowOf2(d.val); val != d.expected {
t.Errorf("Expected %d, but got %d", d.expected, val)
}
}
}
func TestZNormalize(t *testing.T) {
data := []struct {
ts []float64
}{
{[]float64{0, 1, 2, 3, 4, 5}},
{[]float64{0, 1, 2, 3, 4, 5, 6}},
{[]float64{3, 4, 3, 4, 3}},
{[]float64{99, 100, 101, 102, 103}},
}
for _, d := range data {
zNormalize(d.ts)
var ssum float64
for i := 0; i < len(d.ts); i++ {
ssum += d.ts[i] * d.ts[i]
}
if math.Abs(ssum-float64(len(d.ts)-1)) > 1e-8 {
t.Errorf("Expected a squared sum of %d, but got %.3f for %v", len(d.ts), ssum, d.ts)
}
}
}
func TestZeroPad(t *testing.T) {
dataset := []struct {
x []float64
n int
expectedx []float64
}{
{[]float64{1, 2, 3, 4}, 6, []float64{0, 0, 1, 2, 3, 4}},
{[]float64{1, 2, 3, 4}, 3, []float64{1, 2, 3, 4}},
{[]float64{1, 2, 3, 4}, 4, []float64{1, 2, 3, 4}},
}
for _, d := range dataset {
zpadx := zeroPad(d.x, d.n)
if len(zpadx) != len(d.expectedx) {
t.Fatalf("Expected length %d, but got length %d", len(d.expectedx), len(zpadx))
}
for i, v := range zpadx {
if d.expectedx[i] != v {
t.Fatalf("Expected value %v, but got %v", d.expectedx[i], v)
}
}
}
}
func TestXCorr(t *testing.T) {
datasets := []struct {
X []float64
Y []float64
Normalize bool
ExpectedXCorr []float64
ExpectedIdx int
ExpectedSign func(float64) bool
}{
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, 5, 0, 0},
false,
[]float64{10, 0, 0, 0, 0},
0,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, 0, 0, 5},
false,
[]float64{0, 0, 0, 10, 0},
-2,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{5, 0, 0, 0, 0},
false,
[]float64{0, 0, 10, 0, 0},
2,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, -5, 0, 0},
false,
[]float64{-10, 0, 0, 0, 0},
0,
isNegative(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{-5, 0, 0, 0, 0},
false,
[]float64{0, 0, -10, 0, 0},
2,
isNegative(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, 5, 0, 0},
true,
[]float64{1.00, -0.25, -0.25, -0.25, -0.25},
0,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, 0, 0, 5},
true,
[]float64{-0.25, -0.25, -0.25, 1.00, -0.25},
-2,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{5, 0, 0, 0, 0},
true,
[]float64{-0.25, -0.25, 1.00, -0.25, -0.25},
2,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, -5, 0, 0},
true,
[]float64{-1.00, 0.25, 0.25, 0.25, 0.25},
0,
isNegative(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{-5, 0, 0, 0, 0},
true,
[]float64{0.25, 0.25, -1.00, 0.25, 0.25},
2,
isNegative(),
},
{
[]float64{0, 0, 2, 2, 0},
[]float64{3, 3, 3, 3, 3},
true,
nil,
0,
func(x float64) bool { return x == 0 },
},
}
for _, ds := range datasets {
xcorr, mi, mv := xCorr(ds.X, ds.Y, len(ds.X), ds.Normalize)
if !prettyClose(xcorr, ds.ExpectedXCorr) {
t.Errorf("Expected cross correlation of %v, but got %v", ds.ExpectedXCorr, xcorr)
}
if mi != ds.ExpectedIdx {
t.Errorf("Expected max index to be at %d, but found it at %d", ds.ExpectedIdx, mi)
}
if !ds.ExpectedSign(mv) {
t.Errorf("Max value of, %f, sign evaluated to %t", mv, ds.ExpectedSign(mv))
}
}
}
func TestXCorrWithX(t *testing.T) {
datasets := []struct {
X []float64
Y []float64
ExpectedXCorr []float64
ExpectedIdx int
ExpectedSign func(float64) bool
}{
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, 5, 0, 0},
[]float64{1.00, -0.25, -0.25, -0.25, -0.25},
0,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, 0, 0, 5},
[]float64{-0.25, -0.25, -0.25, 1.00, -0.25},
-2,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{5, 0, 0, 0, 0},
[]float64{-0.25, -0.25, 1.00, -0.25, -0.25},
2,
isPositive(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{0, 0, -5, 0, 0},
[]float64{-1.00, 0.25, 0.25, 0.25, 0.25},
0,
isNegative(),
},
{
[]float64{0, 0, 2, 0, 0},
[]float64{-5, 0, 0, 0, 0},
[]float64{0.25, 0.25, -1.00, 0.25, 0.25},
2,
isNegative(),
},
{
[]float64{0, 0, 2, 2, 0},
[]float64{3, 3, 3, 3, 3},
nil,
0,
func(x float64) bool { return x == 0 },
},
}
for _, ds := range datasets {
n := len(ds.X)
ft := fourier.NewFFT(n)
x, err := zNormalize(ds.X)
if err != nil {
t.Errorf("%+v\n", err)
continue
}
floats.Scale(1/float64(len(x)-1), x)
refFT := ft.Coefficients(nil, zeroPad(x, n))
ftY := fourier.NewFFT(n)
coefScratch := make([]complex128, n/2+1)
seqScratch := make([]float64, n)
xcorr, mi, mv := xCorrWithX(refFT, ds.Y, ftY, coefScratch, seqScratch)
if !prettyClose(xcorr, ds.ExpectedXCorr) {
t.Errorf("Expected cross correlation of %v, but got %v", ds.ExpectedXCorr, xcorr)
}
if mi != ds.ExpectedIdx {
t.Errorf("Expected max index to be at %d, but found it at %d", ds.ExpectedIdx, mi)
}
if !ds.ExpectedSign(mv) {
t.Errorf("Max value of, %f, sign evaluated to %t", mv, ds.ExpectedSign(mv))
}
}
}
func BenchmarkZPad(b *testing.B) {
x := make([]float64, 480)
for i := 0; i < len(x); i++ {
x[i] = rand.Float64()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
zeroPad(x, 512)
}
}
func BenchmarkZNormalize(b *testing.B) {
x := make([]float64, 512)
for i := 0; i < len(x); i++ {
x[i] = rand.Float64()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
zNormalize(x)
}
}
func setupXCorrData() ([]float64, []float64, int) {
n := 16385
x := make([]float64, n)
y := make([]float64, n)
for i := 0; i < n; i++ {
x[i] = rand.Float64()
y[i] = rand.Float64()
}
return x, y, nextPowOf2(float64(n))
}
func BenchmarkXCorr(b *testing.B) {
x, y, n := setupXCorrData()
for i := 0; i < b.N; i++ {
xCorr(x, y, n, false)
}
}
func BenchmarkXCorrWithX(b *testing.B) {
x, y, n := setupXCorrData()
ft := fourier.NewFFT(n)
x, err := zNormalize(x)
if err != nil {
b.Fatalf("%+v\n", err)
}
X := ft.Coefficients(nil, zeroPad(x, n))
ftY := fourier.NewFFT(n)
coefScratch := make([]complex128, n/2+1)
seqScratch := make([]float64, n)
b.ResetTimer()
for i := 0; i < b.N; i++ {
xCorrWithX(X, y, ftY, coefScratch, seqScratch)
}
}