-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocorrelation.go
63 lines (56 loc) · 1.33 KB
/
autocorrelation.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
package asap
import (
"math"
"github.com/mjibson/go-dsp/fft"
)
const acfCorrThreshold = 0.2
func acf(in []float64, maxLag int) ([]int, []float64, float64) {
pow := float64(int(math.Log2(float64(len(in))) + 1))
l := int(math.Pow(2.0, pow))
fftv := make([]float64, len(in)+(l-len(in)))
copy(fftv, in)
if len(fftv) != l {
panic("fftv wtf")
}
f_f := fft.FFTReal(fftv)
s_f := make([]float64, len(fftv))
for i, x := range f_f {
s_f[i] = real(x)*real(x) + imag(x)*imag(x)
}
r_t := fft.IFFTReal(s_f)
correlations := make([]float64, maxLag)
r_t0 := real(r_t[0])
for i := range correlations {
if i < 1 {
continue
}
correlations[i] = real(r_t[i]) / r_t0
}
var peaks []int
var maxAcf float64
if len(correlations) < 2 {
return peaks, correlations, maxAcf
}
positive := correlations[1] > correlations[0]
max := 1
for i := range correlations {
if i < 2 {
continue
}
if !positive && correlations[i] > correlations[i-1] {
max = i
positive = !positive
} else if positive && correlations[i] > correlations[max] {
max = i
} else if positive && correlations[i] < correlations[i-1] {
if max > 1 && correlations[max] > acfCorrThreshold {
peaks = append(peaks, max)
}
if correlations[max] > maxAcf {
maxAcf = correlations[max]
}
positive = !positive
}
}
return peaks, correlations, maxAcf
}