forked from clandry94/agraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfir_filter.go
74 lines (59 loc) · 1.27 KB
/
fir_filter.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
package agraph
import "container/list"
/*
FIR moving average filter
*/
type FIR struct {
source chan []uint16
sink chan []uint16
Name string
tapBuffer *list.List // maybe needs to be a pointer
tapCount int
}
func newFIR(name string, maSize int) (Node, error) {
return &FIR{
source: make(chan []uint16, SOURCE_SIZE),
sink: nil,
Name: name,
tapBuffer: list.New(),
tapCount: maSize,
}, nil
}
func (n *FIR) SetSink(c chan []uint16) {
n.sink = c
}
func (n *FIR) Source() chan []uint16 {
return n.source
}
func (n *FIR) Sink() chan []uint16 {
return n.sink
}
func (n *FIR) Process() error {
for {
select {
case data := <-n.source:
var filteredData, err = n.do(data)
//fmt.Printf("Data processed from %v, here it is: %v\n", n.Name, filteredData)
if err != nil {
panic("Could not filter!")
}
n.sink <- filteredData
}
}
return nil
}
func (n *FIR) do(data []uint16) ([]uint16, error) {
var movAvg float64
movAvg = 0.0
if n.tapBuffer.Len() >= n.tapCount {
n.tapBuffer.Remove(n.tapBuffer.Back())
}
n.tapBuffer.PushFront(data)
p := n.tapBuffer.Front()
for p.Next() != nil {
movAvg += float64(p.Value.([]uint16)[0]) * (1 / float64(n.tapCount))
p = p.Next()
}
data[0] = uint16(movAvg)
return data, nil
}