-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathyin.go
60 lines (50 loc) · 1.01 KB
/
yin.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
package yin
func ACF(x *[]float64) []float64 {
N := len(*x)
r := make([]float64, N)
for tau := 0; tau < N; tau++ {
entry := 0.0
for j := 1; j < N-tau; j++ {
entry += (*x)[j] * (*x)[j+tau]
}
r[tau] = entry
}
return r
}
func DF(x *[]float64) []float64 {
N := len(*x)
d := make([]float64, N)
for tau := 0; tau < N; tau++ {
entry := 0.0
for j := 0; j < N-tau; j++ {
entry += ((*x)[j] - (*x)[j+tau]) * ((*x)[j] - (*x)[j+tau])
}
d[tau] = entry
}
return d
}
func CMNDF(df *[]float64) {
(*df)[0] = 1
runningSum := 0.0
for tau := 1; tau < len(*df); tau++ {
runningSum += (*df)[tau]
(*df)[tau] *= float64(tau) / runningSum
}
}
var YIN_DEFAULT_THRESHOLD = 0.20
func AbsoluteThreshold(cmndf *[]float64) float64 {
N := len(*cmndf)
tau := 0
for tau = 2; tau < N; tau++ {
if (*cmndf)[tau] < YIN_DEFAULT_THRESHOLD {
for tau+1 < N && (*cmndf)[tau+1] < (*cmndf)[tau] {
tau++
}
break
}
}
if tau == N || (*cmndf)[tau] >= YIN_DEFAULT_THRESHOLD {
return -1
}
return float64(tau)
}