4Author: J.A. de Jong - ASCEE V.O.F.
6Description: Filter design implementation of common biquad filters that are
7often used in parametric equalizers.
9Major source is Audio EQ Cookbook:
10 https://archive.is/20121220231853/http://www.musicdsp.org/
11 files/Audio-EQ-Cookbook.txt
13The definition of the BiQuad filter coefficients as coming out of these
14functions defines the filter as:
16y[n] = 1/ba[3] * ( ba[0] * x[n] + ba[1] * x[n-1] + ba[2] * x[n-2] +
17 + ba[4] * y[n-1] + ba[5] * y[n-2]
20*Note that all filters are normalized such that ba[3] is by definition equal to
25__all__ = [
'peaking',
'biquadTF',
'notch',
'lowpass',
'highpass',
26 'highshelf',
'lowshelf',
'LP1compensator',
'LP2compensator',
27 'HP1compensator',
'HP2compensator']
29from numpy
import array, cos, pi, sin, sqrt
30from scipy.interpolate
import interp1d
31from scipy.signal
import sosfreqz, bilinear_zpk, zpk2sos
36 Design of peaking biquad filter
39 fs: Sampling frequency [Hz]
41 Q: Quality factor (~ inverse of bandwidth)
42 gain: Increase in level at the center frequency [dB]
44 A = sqrt(10**(gain/20))
54 return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
59 Notch filter, parameter gain not used
62 fs: Sampling frequency [Hz]
63 f0: Center frequency [Hz]
64 Q: Quality factor (~ inverse of bandwidth)
74 return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
79 Second order low pass filter, parameter gain not used
82 fs: Sampling frequency [Hz]
83 f0: Cut-off frequency [Hz]
84 Q: Quality factor (~ inverse of bandwidth)
94 return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
99 Second order high pass filter, parameter gain not used
102 fs: Sampling frequency [Hz]
103 f0: Cut-on frequency [Hz]
104 Q: Quality factor (~ inverse of bandwidth)
115 return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
123 fs: Sampling frequency [Hz]
124 f0: Cut-on frequency [Hz]
125 Q: Quality factor (~ inverse of bandwidth)
126 gain: Increase in level w.r.t. "wire" [dB]
131 b0 = A*((A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha)
132 b1 = -2*A*((A-1) + (A+1)*cos(w0))
133 b2 = A*((A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha)
134 a0 = (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha
135 a1 = 2*((A-1) - (A+1)*cos(w0))
136 a2 = (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha
137 return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
145 fs: Sampling frequency [Hz]
146 f0: Cut-on frequency [Hz]
147 Q: Quality factor (~ inverse of bandwidth)
148 gain: Increase in level w.r.t. "wire" [dB]
153 b0 = A*((A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha)
154 b1 = 2*A*((A-1) - (A+1)*cos(w0))
155 b2 = A*((A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha)
156 a0 = (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha
157 a1 = -2*((A-1) + (A+1)*cos(w0))
158 a2 = (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha
159 return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
163 Shelving type filter that, when multiplied with a first-order low-pass
164 filter, alters the response of that filter to a different first-order
168 fs: Sampling frequency [Hz]
169 f0o: Cut-off frequency of the original filter [Hz]
170 f0n: Desired cut-off frequency [Hz]
180 zd, pd, kd = bilinear_zpk(z, p, k, fs)
182 sos = zpk2sos(zd,pd,kd)
188 Shelving type filter that, when multiplied with a second-order low-pass
189 filter, alters the response of that filter to a different second-order
193 fs: Sampling frequency [Hz]
194 f0o: Cut-off frequency of the original filter [Hz]
195 Qo: Quality factor of the original filter (~inverse of bandwidth)
196 f0n: Desired cut-off frequency [Hz]
197 Qn: Desired quality factor(~inverse of bandwidth)
204 zIm = omg0o*sqrt(1-1/(4*Qo**2))
209 pIm = omg0n*sqrt(1-1/(4*Qn**2))
215 k = (pRe**2 + pIm**2)/(zRe**2 + zIm**2)
217 zd, pd, kd = bilinear_zpk(z, p, k, fs)
219 sos = zpk2sos(zd,pd,kd)
225 Shelving type filter that, when multiplied with a first-order high-pass
226 filter, alters the response of that filter to a different first-order
230 fs: Sampling frequency [Hz]
231 f0o: Cut-on frequency of the original filter [Hz]
232 f0n: Desired cut-on frequency [Hz]
242 zd, pd, kd = bilinear_zpk(z, p, k, fs)
244 sos = zpk2sos(zd,pd,kd)
250 Shelving type filter that, when multiplied with a second-order high-pass
251 filter, alters the response of that filter to a different second-order
255 fs: Sampling frequency [Hz]
256 f0o: Cut-on frequency of the original filter [Hz]
257 Qo: Quality factor of the original filter (~inverse of bandwidth)
258 f0n: Desired cut-on frequency [Hz]
259 Qn: Desired quality factor(~inverse of bandwidth)
266 zIm = omg0o*sqrt(1-1/(4*Qo**2))
271 pIm = omg0n*sqrt(1-1/(4*Qn**2))
279 zd, pd, kd = bilinear_zpk(z, p, k, fs)
281 sos = zpk2sos(zd,pd,kd)
287 Computes the transfer function of the biquad.
289 Interpolates the frequency response to `freq`
292 fs: Sampling frequency [Hz]
293 freq: Frequency array to compute the
294 ba: Biquad filter coefficients in common form.
296 TODO: This code is not yet tested
298 freq2, h = sosfreqz(sos, worN=freq.size, fs=fs)
299 interpolator = interp1d(freq2, h, kind=
'quadratic')
300 return interpolator(freq)
biquadTF(fs, freq, sos)
Computes the transfer function of the biquad.
lowshelf(fs, f0, Q, gain)
Low shelving filter.
LP2compensator(fs, f0o, Qo, f0n, Qn)
Shelving type filter that, when multiplied with a second-order low-pass filter, alters the response o...
notch(fs, f0, Q, gain=None)
Notch filter, parameter gain not used.
HP1compensator(fs, f0o, f0n)
Shelving type filter that, when multiplied with a first-order high-pass filter, alters the response o...
LP1compensator(fs, f0o, f0n)
Shelving type filter that, when multiplied with a first-order low-pass filter, alters the response of...
highshelf(fs, f0, Q, gain)
High shelving filter.
lowpass(fs, f0, Q, gain=None)
Second order low pass filter, parameter gain not used.
peaking(fs, f0, Q, gain)
Design of peaking biquad filter.
highpass(fs, f0, Q, gain=None)
Second order high pass filter, parameter gain not used.
HP2compensator(fs, f0o, Qo, f0n, Qn)
Shelving type filter that, when multiplied with a second-order high-pass filter, alters the response ...