-
Notifications
You must be signed in to change notification settings - Fork 1
/
filters_test.go
224 lines (204 loc) · 5.38 KB
/
filters_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package pctl
import (
"math"
"math/rand"
"testing"
)
const biquadFilterCoefTol = 1e-8
func approxEqualAbs(a, b, tol float64) bool {
return math.Abs(a-b) <= tol
}
// these tests verify physical properties of the filters, but not e.g.
// that the -3dB point is correct, or any internal state variables are correct
func TestLowPassFilterAsymptotic(t *testing.T) {
lpf := NewLPF(1e6, 1e-3)
// 1Mhz low-pass filter has corner frequency of a MHz, corresponds to
// microseconds to reach nearly steady state
process := lpf.Update(0) // seed the filter
target := 1.
for i := 0; i < 5; i++ {
process = lpf.Update(target)
}
err := 1 - process
if math.Abs(err) > 1e-5 {
t.Errorf("process of %f has error of %f, expected to converge to 1 along step", process, err)
}
}
func TestHighPassRejectsOscillation(t *testing.T) {
hpf := NewHPF(1e6, 1e-3)
// cutoff of 1Mhz means that inputs at ms should be rejected
process := hpf.Update(0)
target := 1.
for i := 0; i < 5; i++ {
process = hpf.Update(target)
}
err := 0 - process
if math.Abs(err) > 1e-5 {
t.Errorf("process of %f has error of %f, expected to resist motion in ms domain", process, err)
}
}
func TestBiquadFilterAsymptotic(t *testing.T) {
// Bq at sample rate 1kHz, 250Hz corner, Q=sqrt(2)/2, -6dB gain
a0 := 0.2928920553392428
a1 := 0.5857841106784856
a2 := a0
b1 := -1.3007020142696517e-16
b2 := 0.17156822135697122
bq := NewBiquad(a0, a1, a2, b1, b2)
process := bq.Update(0)
target := 1.
for i := 0; i < 100; i++ {
process = bq.Update(target)
}
err := target - process
if math.Abs(err) > 1e-5 {
t.Errorf("process of %f has error of %f, expected to converge to target=1", process, err)
}
}
func TestStateSpaceReducesNoise(t *testing.T) {
A := [][]float64{
{2, -1},
{1, 0},
}
B := []float64{5e-5, 0}
C := []float64{4, 0.02}
D := 5e-5
filt := NewStateSpaceFilter(A, B, C, D, nil)
var maxIn float64
var maxOut float64
for i := 0; i < 100; i++ {
in := rand.Float64()
if in > maxIn {
maxIn = in
}
out := filt.Update(in)
if out > maxOut {
maxOut = out
}
}
if maxOut >= maxIn {
t.Errorf("state-space lowpass filter failed to reduce peak noise")
}
}
func TestStateSpaceNonzeroOutput(t *testing.T) {
A := [][]float64{
{2, -1},
{1, 0},
}
B := []float64{5e-5, 0}
C := []float64{4, 0.02}
D := 5e-5
filt := NewStateSpaceFilter(A, B, C, D, nil)
var maxOut float64
for i := 0; i < 100; i++ {
in := rand.Float64()
out := filt.Update(in)
if out > maxOut {
maxOut = out
}
}
if maxOut == 0 {
t.Errorf("state-space lowpass filter returned zero where it should not.")
}
}
func testBiquadvsEarLevel(t *testing.T, newF NewBiquadFunc, a0, a1, a2, b1, b2 float64) {
// assumes below parameters (default for biquad calculator v3)
// were used to compute a0..b2
b := newF(44100, 100, 0.7071, 6)
if !approxEqualAbs(b.a0, a0, biquadFilterCoefTol) {
t.Errorf("a0 %f != %f", b.a0, a0)
}
if !approxEqualAbs(b.a1, a1, biquadFilterCoefTol) {
t.Errorf("a1 %f != %f", b.a1, a1)
}
if !approxEqualAbs(b.a2, a2, biquadFilterCoefTol) {
t.Errorf("a2 %f != %f", b.a2, a2)
}
if !approxEqualAbs(b.b1, b1, biquadFilterCoefTol) {
t.Errorf("b1 %f != %f", b.b1, b1)
}
if !approxEqualAbs(b.b2, b2, biquadFilterCoefTol) {
t.Errorf("b2 %f != %f", b.b2, b2)
}
}
// these tests are regression against earlevel.com, where the C++ implementation
// was borrowed from
func TestLowPassBiquadCorrectCoefs(t *testing.T) {
testBiquadvsEarLevel(t, NewBiquadLowpass,
0.00005024141818873903,
0.00010048283637747806,
0.00005024141818873903,
-1.979851353142371,
0.9800523188151258)
}
func TestHighPassBiquadCorrectCoefs(t *testing.T) {
testBiquadvsEarLevel(t, NewBiquadHighpass,
0.9899759179893742,
-1.9799518359787485,
0.9899759179893742,
-1.979851353142371,
0.9800523188151258)
}
func TestBandPassBiquadCorrectCoefs(t *testing.T) {
testBiquadvsEarLevel(t, NewBiquadBandpass,
0.009973840592437116,
0,
-0.009973840592437116,
-1.979851353142371,
0.9800523188151258)
}
func TestNotchBiquadCorrectCoefs(t *testing.T) {
testBiquadvsEarLevel(t, NewBiquadNotch,
0.990026159407563,
-1.979851353142371,
0.990026159407563,
-1.979851353142371,
0.9800523188151258)
}
func TestPeakBiquadCorrectCoefs(t *testing.T) {
testBiquadvsEarLevel(t, NewBiquadPeak,
1.0099265876771597,
-1.979851353142371,
0.9701257311379663,
-1.979851353142371,
0.9800523188151258)
}
func TestLowShelfBiquadCorrectCoefs(t *testing.T) {
testBiquadvsEarLevel(t, NewBiquadLowShelf,
1.0041645480379269,
-1.9797515357244457,
0.9759879669583226,
-1.9798515425143588,
0.9800525082063363)
}
func TestHighShelfBiquadCorrectCoefs(t *testing.T) {
testBiquadvsEarLevel(t, NewBiquadHighShelf,
1.9894003627867007,
-3.95042317880182,
1.9612237817070965,
-1.9798515425143588,
0.9800525082063363)
}
func TestIdentityFIRFilterDoesNothing(t *testing.T) {
f := NewFIRFilter([]float64{1, 0, 0, 0})
input := []float64{3.14, 2.87, 1}
for i := 0; i < 3; i++ {
in := input[i]
out := f.Update(in)
if !approxEqualAbs(in, out, 1e-16) {
t.Errorf("sample %d had input-output mismatch %f != %f", i, in, out)
}
}
}
func TestLagFIRFilterLags(t *testing.T) {
f := NewFIRFilter([]float64{0, 1, 0, 0})
input := []float64{3.14, 2.87, 1}
f.Update(input[0])
for i := 1; i < 3; i++ {
in := input[i]
out := f.Update(in)
if !approxEqualAbs(input[i-1], out, 1e-16) {
t.Errorf("sample %d had input-output mismatch %f != %f", i, in, out)
}
}
}