-
-
Notifications
You must be signed in to change notification settings - Fork 45.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update butterworth_filter.py #12348
Open
Aryan-Rajesh-Python
wants to merge
2
commits into
TheAlgorithms:master
Choose a base branch
from
Aryan-Rajesh-Python:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update butterworth_filter.py #12348
Aryan-Rajesh-Python
wants to merge
2
commits into
TheAlgorithms:master
from
Aryan-Rajesh-Python:patch-1
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for more information, see https://pre-commit.ci
imSanko
suggested changes
Nov 6, 2024
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
"""
def make_highshelf(frequency, samplerate, gain_db, q_factor=0.707):
"""
Creates a high-shelf filter based on the specified parameters.
Args:
frequency (float): Center frequency of the filter.
samplerate (float): Sampling rate.
gain_db (float): Gain in decibels.
q_factor (float): Quality factor.
Returns:
IIRFilter: Configured high-shelf filter.
Examples:
>>> filter = make_highshelf(1000, 48000, 6)
>>> filter.a_coeffs # doctest: +NORMALIZE_WHITESPACE
[2.2229172136088806, -3.9587208137297303, 1.7841414181566304]
>>> filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
[4.295432981120543, -7.922740859457287, 3.6756456963725253]
"""
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
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
filt = IIRFilter(2)
filt.set_coefficients([a0, a1, a2], [b0, b1, b2])
return filt
if __name__ == "__main__":
import doctest
doctest.testmod()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try to implement these change
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe your change:
Checklist: