-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.go
61 lines (47 loc) · 999 Bytes
/
sw.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 horus
import "fmt"
// SWindow defines attributes for a fixed slide window
type SWindow struct {
window int
values []float64
}
// NewSWindow provides a fixed slide window operation that
// exposes average, max and min statistics.
// The given window must be an integer higher than 0.
func NewSWindow(window int) (*SWindow, error) {
if window == 0 {
return nil, fmt.Errorf("window must be higher than 0")
}
return &SWindow{
window: window,
values: make([]float64, window),
}, nil
}
func (s *SWindow) Add(value float64) {
s.values = append(s.values[1:s.window], value)
}
func (s *SWindow) Average() float64 {
avg := 0.0
for _, v := range s.values {
avg = avg + v
}
return avg / float64(s.window)
}
func (s *SWindow) Max() float64 {
max := s.values[0]
for _, v := range s.values {
if v > max {
max = v
}
}
return max
}
func (s *SWindow) Min() float64 {
min := s.values[0]
for _, v := range s.values {
if v <= min {
min = v
}
}
return min
}