|
1 | 1 | from math import cos, sin, sqrt, tau
|
2 |
| - |
3 | 2 | from audio_filters.iir_filter import IIRFilter
|
4 | 3 |
|
5 | 4 | """
|
6 | 5 | Create 2nd-order IIR filters with Butterworth design.
|
7 | 6 |
|
8 | 7 | Code based on https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html
|
9 |
| -Alternatively you can use scipy.signal.butter, which should yield the same results. |
| 8 | +Alternatively, you can use scipy.signal.butter, which should yield the same results. |
10 | 9 | """
|
11 | 10 |
|
12 |
| - |
13 | 11 | def make_lowpass(
|
14 | 12 | frequency: int,
|
15 | 13 | samplerate: int,
|
@@ -208,27 +206,32 @@ def make_highshelf(
|
208 | 206 |
|
209 | 207 | >>> filter = make_highshelf(1000, 48000, 6)
|
210 | 208 | >>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
|
211 |
| - [2.2229172136088806, -3.9587208137297303, 1.7841414181566304, 4.295432981120543, |
212 |
| - -7.922740859457287, 3.6756456963725253] |
| 209 | + [2.2229172136088806, -3.616080374557819, 1.4122939941901636, 0.5690651073937642, |
| 210 | + -1.3335115824061685, 0.7666141956641752] |
213 | 211 | """
|
214 | 212 | w0 = tau * frequency / samplerate
|
215 | 213 | _sin = sin(w0)
|
216 | 214 | _cos = cos(w0)
|
217 | 215 | alpha = _sin / (2 * q_factor)
|
218 | 216 | big_a = 10 ** (gain_db / 40)
|
219 |
| - pmc = (big_a + 1) - (big_a - 1) * _cos |
220 |
| - ppmc = (big_a + 1) + (big_a - 1) * _cos |
221 |
| - mpc = (big_a - 1) - (big_a + 1) * _cos |
222 |
| - pmpc = (big_a - 1) + (big_a + 1) * _cos |
| 217 | + pmc = (big_a + 1) + (big_a - 1) * _cos |
| 218 | + ppmc = (big_a + 1) - (big_a - 1) * _cos |
| 219 | + mpc = (big_a - 1) + (big_a + 1) * _cos |
| 220 | + pmpc = (big_a - 1) - (big_a + 1) * _cos |
223 | 221 | aa2 = 2 * sqrt(big_a) * alpha
|
224 | 222 |
|
225 |
| - b0 = big_a * (ppmc + aa2) |
226 |
| - b1 = -2 * big_a * pmpc |
227 |
| - b2 = big_a * (ppmc - aa2) |
228 |
| - a0 = pmc + aa2 |
229 |
| - a1 = 2 * mpc |
230 |
| - a2 = pmc - aa2 |
| 223 | + b0 = big_a * (pmc - aa2) |
| 224 | + b1 = -2 * big_a * mpc |
| 225 | + b2 = big_a * (pmc + aa2) |
| 226 | + a0 = ppmc - aa2 |
| 227 | + a1 = 2 * pmpc |
| 228 | + a2 = ppmc + aa2 |
231 | 229 |
|
232 | 230 | filt = IIRFilter(2)
|
233 | 231 | filt.set_coefficients([a0, a1, a2], [b0, b1, b2])
|
234 | 232 | return filt
|
| 233 | + |
| 234 | + |
| 235 | +if __name__ == "__main__": |
| 236 | + import doctest |
| 237 | + doctest.testmod() |
0 commit comments