@@ -10,13 +10,17 @@ class IIRFilter:
10
10
11
11
Implementation details:
12
12
Based on the 2nd-order function from
13
- https://en.wikipedia.org/wiki/Digital_biquad_filter,
13
+ https://en.wikipedia.org/wiki/Digital_biquad_filter,
14
14
this generalized N-order function was made.
15
15
16
16
Using the following transfer function
17
- H(z)=\frac{b_{0}+b_{1}z^{-1}+b_{2}z^{-2}+...+b_{k}z^{-k}}{a_{0}+a_{1}z^{-1}+a_{2}z^{-2}+...+a_{k}z^{-k}}
17
+ .. math:: H(z)=\frac{b_{0}+b_{1}z^{-1}+b_{2}z^{-2}+...+b_{k}z^{-k}}
18
+ {a_{0}+a_{1}z^{-1}+a_{2}z^{-2}+...+a_{k}z^{-k}}
19
+
18
20
we can rewrite this to
19
- y[n]={\frac{1}{a_{0}}}\left(\left(b_{0}x[n]+b_{1}x[n-1]+b_{2}x[n-2]+...+b_{k}x[n-k]\right)-\left(a_{1}y[n-1]+a_{2}y[n-2]+...+a_{k}y[n-k]\right)\right)
21
+ .. math:: y[n]={\frac{1}{a_{0}}}
22
+ \left(\left(b_{0}x[n]+b_{1}x[n-1]+b_{2}x[n-2]+...+b_{k}x[n-k]\right)-
23
+ \left(a_{1}y[n-1]+a_{2}y[n-2]+...+a_{k}y[n-k]\right)\right)
20
24
"""
21
25
22
26
def __init__ (self , order : int ) -> None :
@@ -34,17 +38,19 @@ def __init__(self, order: int) -> None:
34
38
35
39
def set_coefficients (self , a_coeffs : list [float ], b_coeffs : list [float ]) -> None :
36
40
"""
37
- Set the coefficients for the IIR filter. These should both be of size order + 1.
38
- a_0 may be left out, and it will use 1.0 as default value.
41
+ Set the coefficients for the IIR filter.
42
+ These should both be of size `order` + 1.
43
+ :math:`a_0` may be left out, and it will use 1.0 as default value.
39
44
40
45
This method works well with scipy's filter design functions
41
- >>> # Make a 2nd-order 1000Hz butterworth lowpass filter
42
- >>> import scipy.signal
43
- >>> b_coeffs, a_coeffs = scipy.signal.butter(2, 1000,
44
- ... btype='lowpass',
45
- ... fs=48000)
46
- >>> filt = IIRFilter(2)
47
- >>> filt.set_coefficients(a_coeffs, b_coeffs)
46
+
47
+ >>> # Make a 2nd-order 1000Hz butterworth lowpass filter
48
+ >>> import scipy.signal
49
+ >>> b_coeffs, a_coeffs = scipy.signal.butter(2, 1000,
50
+ ... btype='lowpass',
51
+ ... fs=48000)
52
+ >>> filt = IIRFilter(2)
53
+ >>> filt.set_coefficients(a_coeffs, b_coeffs)
48
54
"""
49
55
if len (a_coeffs ) < self .order :
50
56
a_coeffs = [1.0 , * a_coeffs ]
@@ -68,7 +74,7 @@ def set_coefficients(self, a_coeffs: list[float], b_coeffs: list[float]) -> None
68
74
69
75
def process (self , sample : float ) -> float :
70
76
"""
71
- Calculate y[n]
77
+ Calculate :math:` y[n]`
72
78
73
79
>>> filt = IIRFilter(2)
74
80
>>> filt.process(0)
0 commit comments