-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
62 lines (49 loc) · 1.29 KB
/
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
package gdsp
// Filter performs a 1-dimensional digital filter.
func Filter(b Vector, a Vector, x Vector, z Vector) (Vector, Vector) {
n := len(a)
zOut := z.Copy()
if len(zOut) < n {
zOut = zOut.PaddedTrailing(0.0, n-len(zOut))
}
y := MakeVector(0.0, len(x))
bn := VSDiv(b, a[0])
an := VSDiv(a, a[0])
for m := 0; m < len(y); m++ {
y[m] = bn[0]*x[m] + zOut[0]
for i := 1; i < n; i++ {
zOut[i-1] = bn[i]*x[m] + zOut[i] - an[i]*y[m]
}
}
return y, zOut[:len(zOut)-1]
}
// FilterC performs a 1-dimensional digital filter.
func FilterC(b VectorComplex, a VectorComplex, x VectorComplex, z VectorComplex) (VectorComplex, VectorComplex) {
n := len(a)
zOut := z.Copy()
if len(zOut) < n {
zOut = zOut.PaddedTrailing(0.0, n-len(zOut))
}
y := MakeVectorComplex(0.0, len(x))
bn := VSDivC(b, a[0])
an := VSDivC(a, a[0])
for m := 0; m < len(y); m++ {
y[m] = bn[0]*x[m] + zOut[0]
for i := 1; i < n; i++ {
zOut[i-1] = bn[i]*x[m] + zOut[i] - an[i]*y[m]
}
}
return y, zOut[:len(zOut)-1]
}
// IIR performs an IIR filter on input with the given response.
func IIR(input Vector, response float64) Vector {
output := make([]float64, len(input))
for i, v := range input {
if i == 0 {
output[i] = v
} else {
output[i] = output[i-1]*(1.0-response) + v*response
}
}
return output
}