-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeat.go
61 lines (51 loc) · 1.02 KB
/
beat.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
package max3010x
type beat struct {
filterFIR *fir
signal struct {
dc movingAverage
ac struct {
max float64
min float64
prev float64
}
rising bool
}
}
func newBeat() *beat {
return &beat{
filterFIR: newFIR(),
}
}
// check receives a normalized (0.0 - 1.0) signal input and checks for
// beats. It returns true on rising edges (positive zero crossings) or false
// otherwise.
func (b *beat) check(signal float64) bool {
beat := false
b.signal.dc.add(signal)
ac := b.filterFIR.lowPass(signal - b.signal.dc.mean)
// Rising edge
if b.signal.ac.prev < 0 && ac >= 0 {
delta := b.signal.ac.max - b.signal.ac.min
if delta > 0.5 && delta < 50 {
beat = true
}
b.signal.rising = true
b.signal.ac.max = 0
}
// Falling edge
if b.signal.ac.prev > 0 && ac <= 0 {
b.signal.rising = false
b.signal.ac.min = 0
}
if b.signal.rising {
if ac > b.signal.ac.prev {
b.signal.ac.max = ac
}
} else {
if ac < b.signal.ac.prev {
b.signal.ac.min = ac
}
}
b.signal.ac.prev = ac
return beat
}