-
Notifications
You must be signed in to change notification settings - Fork 13
/
camm.go
60 lines (49 loc) · 1.05 KB
/
camm.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
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package filters
import (
"fmt"
"github.com/bitmark-inc/logger"
"sync"
)
type Camm struct {
sync.RWMutex
Filter
nMedian uint64
nWMA uint64
f []Filter
current float64
}
func NewCamm(start float64, nMedian uint64, nWMA uint64) Filter {
filter := Camm{
nMedian: nMedian,
nWMA: nWMA,
}
filter.f = make([]Filter, 2)
filter.f[0] = NewSMM(start, nMedian)
filter.f[1] = NewWMA(start, nWMA)
return &filter
}
func (filter *Camm) Name() string {
filter.RLock()
defer filter.RUnlock()
return fmt.Sprintf("Camm %d,%d", filter.nMedian, filter.nWMA)
}
func (filter *Camm) Process(s float64) float64 {
filter.Lock()
defer filter.Unlock()
if s < 0 {
logger.Panicf("camm negative sample: %f", s)
}
for _, f := range filter.f {
s = f.Process(s)
}
filter.current = s
return filter.current
}
func (filter *Camm) Current() float64 {
filter.RLock()
defer filter.RUnlock()
return filter.current
}