From 981fa9bafd41f1bb8e1cc82b5a2fa2512b46791d Mon Sep 17 00:00:00 2001 From: Aryan Rajesh Date: Mon, 4 Nov 2024 10:45:17 +0530 Subject: [PATCH 1/2] Update butterworth_filter.py --- audio_filters/butterworth_filter.py | 33 ++++++++++++++++------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/audio_filters/butterworth_filter.py b/audio_filters/butterworth_filter.py index 4e6ea1b18fb4..788283f4a296 100644 --- a/audio_filters/butterworth_filter.py +++ b/audio_filters/butterworth_filter.py @@ -1,15 +1,13 @@ from math import cos, sin, sqrt, tau - from audio_filters.iir_filter import IIRFilter """ Create 2nd-order IIR filters with Butterworth design. Code based on https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html -Alternatively you can use scipy.signal.butter, which should yield the same results. +Alternatively, you can use scipy.signal.butter, which should yield the same results. """ - def make_lowpass( frequency: int, samplerate: int, @@ -208,27 +206,32 @@ def make_highshelf( >>> filter = make_highshelf(1000, 48000, 6) >>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE - [2.2229172136088806, -3.9587208137297303, 1.7841414181566304, 4.295432981120543, - -7.922740859457287, 3.6756456963725253] + [2.2229172136088806, -3.616080374557819, 1.4122939941901636, 0.5690651073937642, + -1.3335115824061685, 0.7666141956641752] """ w0 = tau * frequency / samplerate _sin = sin(w0) _cos = cos(w0) alpha = _sin / (2 * q_factor) big_a = 10 ** (gain_db / 40) - pmc = (big_a + 1) - (big_a - 1) * _cos - ppmc = (big_a + 1) + (big_a - 1) * _cos - mpc = (big_a - 1) - (big_a + 1) * _cos - pmpc = (big_a - 1) + (big_a + 1) * _cos + pmc = (big_a + 1) + (big_a - 1) * _cos + ppmc = (big_a + 1) - (big_a - 1) * _cos + mpc = (big_a - 1) + (big_a + 1) * _cos + pmpc = (big_a - 1) - (big_a + 1) * _cos aa2 = 2 * sqrt(big_a) * alpha - b0 = big_a * (ppmc + aa2) - b1 = -2 * big_a * pmpc - b2 = big_a * (ppmc - aa2) - a0 = pmc + aa2 - a1 = 2 * mpc - a2 = pmc - aa2 + b0 = big_a * (pmc - aa2) + b1 = -2 * big_a * mpc + b2 = big_a * (pmc + aa2) + a0 = ppmc - aa2 + a1 = 2 * pmpc + a2 = ppmc + aa2 filt = IIRFilter(2) filt.set_coefficients([a0, a1, a2], [b0, b1, b2]) return filt + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 5717e7cc8f2e75ef8f85ccde8de5c7401aa7c939 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Nov 2024 05:16:09 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- audio_filters/butterworth_filter.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/audio_filters/butterworth_filter.py b/audio_filters/butterworth_filter.py index 788283f4a296..f223cd59b33b 100644 --- a/audio_filters/butterworth_filter.py +++ b/audio_filters/butterworth_filter.py @@ -8,6 +8,7 @@ Alternatively, you can use scipy.signal.butter, which should yield the same results. """ + def make_lowpass( frequency: int, samplerate: int, @@ -234,4 +235,5 @@ def make_highshelf( if __name__ == "__main__": import doctest + doctest.testmod()