Skip to content

Commit 8076ad0

Browse files
committed
iir: Separate out filter design from benchmark
1 parent 6e19609 commit 8076ad0

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

benchmarks/iir/iir_benchmark.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@
2121
print(e)
2222
pass
2323

24-
# https://stackoverflow.com/a/20932062
25-
def butter2_lowpass(f, sr):
26-
ff = f / sr
27-
ita = 1.0/ math.tan(math.pi*ff)
28-
q = math.sqrt(2.0)
29-
b0 = 1.0 / (1.0 + q*ita + ita*ita)
30-
b1 = 2 * b0
31-
b2 = b0
32-
a1 = 2.0 * (ita*ita - 1.0) * b0
33-
a2 = -(1.0 - q*ita + ita*ita) * b0
34-
35-
# Return in biquad / Second Order Stage format
36-
# to be compatible with scipy, a1 and a2 needed to be flipped??
37-
sos = [ b0, b1, b2, 1.0, -a1, -a2 ]
38-
39-
return sos
4024

4125
def make_two_sines(f1 = 2.0, f2 = 20.0, sr = 100, dur = 1.0):
4226
n = int(dur * sr)

benchmarks/iir/simplefilters.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
"""
3+
Filter design for simple low/high/band-pass filters
4+
"""
5+
6+
import math
7+
8+
# https://stackoverflow.com/a/20932062
9+
def butter2_lowpass(f, sr):
10+
ff = f / sr
11+
ita = 1.0/ math.tan(math.pi*ff)
12+
q = math.sqrt(2.0)
13+
b0 = 1.0 / (1.0 + q*ita + ita*ita)
14+
b1 = 2 * b0
15+
b2 = b0
16+
a1 = 2.0 * (ita*ita - 1.0) * b0
17+
a2 = -(1.0 - q*ita + ita*ita) * b0
18+
19+
# Return in biquad / Second Order Stage format
20+
# to be compatible with scipy, a1 and a2 needed to be flipped??
21+
sos = [ b0, b1, b2, 1.0, -a1, -a2 ]
22+
23+
return sos
24+
25+
# https://stackoverflow.com/a/39240059
26+
def butter2_highpass(f, sr):
27+
28+
# low-pass filter prototype
29+
ff = f / sr
30+
ita = 1.0/ math.tan(math.pi*ff)
31+
q = math.sqrt(2.0)
32+
b0 = 1.0 / (1.0 + q*ita + ita*ita)
33+
b1 = 2 * b0
34+
b2 = b0
35+
a1 = 2.0 * (ita*ita - 1.0) * b0
36+
a2 = -(1.0 - q*ita + ita*ita) * b0
37+
38+
# convert to high-pass
39+
b0 = b0*ita*ita;
40+
b1 = -b1*ita*ita;
41+
b2 = b2*ita*ita;
42+
43+
# Return in biquad / Second Order Stage format
44+
# to be compatible with scipy, a1 and a2 needed to be flipped??
45+
sos = [ b0, b1, b2, 1.0, -a1, -a2 ]
46+
47+
return sos
48+
49+
def butter2_bandpass(low, high, fs):
50+
51+
hp = butter2_highpass(low, fs)
52+
lp = butter2_lowpass(high, fs)
53+
bp = hp + lp
54+
55+
return bp
56+
57+
58+
59+

0 commit comments

Comments
 (0)