-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilter1.go
65 lines (56 loc) · 1.21 KB
/
filter1.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
/* Copyright (c) 2020, Serhat Şevki Dinçer.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package butter
// First order filter
type filter1 struct {
s, a1, b0, b1 float64
}
func (f *filter1) Next(u float64) float64 {
t := u - f.a1*f.s // Direct Form 2
y := f.b0*t + f.b1*f.s
f.s = t // shift memory
return y
}
func (f *filter1) NextS(u, y []float64) {
n := len(u)
if n > len(y) {
n = len(y)
}
for i := 0; i < n; i++ {
y[i] = f.Next(u[i])
}
}
func (f *filter1) Reset(u, y float64) {
f.s = (y - f.b0*u) / (f.b1 - f.b0*f.a1) // division is safe
}
// NewLowPass1 creates first order Low-Pass filter
func NewLowPass1(wc float64) Filter {
if !valid(wc) {
return nil
}
var f filter1
wat := prewarp(wc) // wa * dt
a0 := wat + 2
f.a1 = (wat - 2) / a0
f.b0 = wat / a0
f.b1 = f.b0
f.s = 0
return &f
}
// NewHighPass1 creates first order High-Pass filter
func NewHighPass1(wc float64) Filter {
if !valid(wc) {
return nil
}
var f filter1
wat := prewarp(wc) // wa * dt
a0 := wat + 2
f.a1 = (wat - 2) / a0
f.b0 = 2 / a0
f.b1 = -f.b0
f.s = 0
return &f
}