Skip to content

Conversation

Aryan-Rajesh-Python
Copy link

@Aryan-Rajesh-Python Aryan-Rajesh-Python commented Nov 4, 2024

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Nov 4, 2024
if __name__ == "__main__":
import doctest

doctest.testmod()
Copy link
Contributor

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()

Copy link
Contributor

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

@MaximSmolskiy
Copy link
Member

I don't understand why high-shelf filter formulas should be changed.
Current formulas correspond to https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants