-
-
Notifications
You must be signed in to change notification settings - Fork 51k
Clean up the audio_filters directory #15087
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
Open
priya-sundaram-dev
wants to merge
2
commits into
TheAlgorithms:master
Choose a base branch
from
priya-sundaram-dev:audio-filters-tlc
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,58 @@ | ||
| # Audio Filter | ||
| # Audio Filters | ||
|
|
||
| Audio filters work on the frequency of an audio signal to attenuate unwanted frequency and amplify wanted ones. | ||
| They are used within anything related to sound, whether it is radio communication or a hi-fi system. | ||
| Audio filters work on the frequency of an audio signal to attenuate unwanted | ||
| frequencies and amplify wanted ones. They are used within anything related to | ||
| sound, whether it is radio communication or a hi-fi system. If you have ever | ||
| turned up the bass or cut the treble on a stereo, tuned a radio to a station, or | ||
| removed the background hum from a recording, you have used an audio filter. | ||
|
|
||
| Curious to learn more? These are great starting points: | ||
|
|
||
| * <https://www.masteringbox.com/filter-types/> | ||
| * <http://ethanwiner.com/filters.html> | ||
| * <https://en.wikipedia.org/wiki/Audio_filter> | ||
| * <https://en.wikipedia.org/wiki/Electronic_filter> | ||
| * <https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html> | ||
|
|
||
| ## What's in this directory | ||
|
|
||
| | File | Description | | ||
| | ---- | ----------- | | ||
| | [`iir_filter.py`](iir_filter.py) | A generic N-order [Infinite Impulse Response (IIR)](https://en.wikipedia.org/wiki/Infinite_impulse_response) filter. This is the engine every filter below runs on: give it a set of coefficients and it processes a stream of samples one at a time. | | ||
| | [`butterworth_filter.py`](butterworth_filter.py) | A collection of second-order [Butterworth](https://en.wikipedia.org/wiki/Butterworth_filter) / biquad filter designs from the RBJ Audio EQ Cookbook. Each function returns a ready-to-use `IIRFilter`. | | ||
| | [`equal_loudness_filter.py`](equal_loudness_filter.py) | An [equal-loudness](https://en.wikipedia.org/wiki/Equal-loudness_contour) filter that compensates for the human ear's non-linear response to sound by cascading a Yule-Walker filter and a Butterworth high-pass filter. Includes a dependency-free `yulewalk` implementation. | | ||
| | [`show_response.py`](show_response.py) | Helpers to plot the [magnitude and phase response](https://en.wikipedia.org/wiki/Frequency_response) of any filter so you can *see* what it does. | | ||
| | [`loudness_curve.json`](loudness_curve.json) | The Robinson-Dadson equal-loudness contour data used by the equal-loudness filter. | | ||
|
|
||
| ## Filter designs in `butterworth_filter.py` | ||
|
|
||
| | Function | Effect | | ||
| | -------- | ------ | | ||
| | `make_lowpass` | Passes frequencies below the cutoff, attenuates those above it. | | ||
| | `make_highpass` | Passes frequencies above the cutoff, attenuates those below it. | | ||
| | `make_bandpass` | Passes a band of frequencies around the center (constant skirt gain). | | ||
| | `make_bandpass_peak` | Passes a band of frequencies around the center (constant 0 dB peak gain). | | ||
| | `make_notch` | Rejects a narrow band around the center — great for removing mains hum. | | ||
| | `make_allpass` | Passes all frequencies but changes their phase relationship. | | ||
| | `make_peak` | Boosts or cuts a band around the center by a given gain (parametric EQ). | | ||
| | `make_lowshelf` | Boosts or cuts everything below the cutoff. | | ||
| | `make_highshelf` | Boosts or cuts everything above the cutoff. | | ||
|
|
||
| ## Try it out | ||
|
|
||
| ```python | ||
| from audio_filters.butterworth_filter import make_lowpass | ||
| from audio_filters.show_response import show_frequency_response | ||
|
|
||
| # A 5 kHz low-pass filter for CD-quality audio (44.1 kHz sample rate) | ||
| filt = make_lowpass(5000, 44100) | ||
|
|
||
| # Process samples one at a time... | ||
| filtered = [filt.process(sample) for sample in my_audio_samples] | ||
|
|
||
| # ...or visualise what the filter does to the spectrum: | ||
| show_frequency_response(make_lowpass(5000, 44100), 44100) | ||
| ``` | ||
|
|
||
| Every module has runnable doctests — read them for concrete, copy-pasteable | ||
| examples of each filter in action. |
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from json import loads | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| from scipy.linalg import toeplitz | ||
| from scipy.signal import lfilter, unit_impulse | ||
|
|
||
| from audio_filters.butterworth_filter import make_highpass | ||
| from audio_filters.iir_filter import IIRFilter | ||
|
|
||
| data = loads((Path(__file__).resolve().parent / "loudness_curve.json").read_text()) | ||
|
|
||
|
|
||
| def _polystab(poly: np.ndarray) -> np.ndarray: | ||
| """ | ||
| Stabilize a polynomial by reflecting any roots that lie outside the unit | ||
| circle back inside it. This keeps the resulting IIR filter stable without | ||
| changing its magnitude response. | ||
|
|
||
| https://en.wikipedia.org/wiki/Minimum_phase | ||
|
|
||
| >>> np.round(_polystab(np.array([1.0, 2.0, 1.0])), 6) | ||
| array([1., 2., 1.]) | ||
| >>> np.round(_polystab(np.array([1.0, 2.0, 1.01])), 6) | ||
| array([1. , 1.980198, 0.990099]) | ||
| """ | ||
| if poly.size <= 1: | ||
| return poly | ||
| roots = np.roots(poly) | ||
| nonzero = np.where(roots != 0)[0] | ||
| outside = 0.5 * (np.sign(np.abs(roots[nonzero]) - 1) + 1) | ||
| roots[nonzero] = (1 - outside) * roots[nonzero] + outside / np.conj(roots[nonzero]) | ||
| stabilized = np.poly(roots) | ||
| if not np.imag(poly).any(): | ||
| stabilized = np.real(stabilized) | ||
| return stabilized | ||
|
|
||
|
|
||
| def _numerator( | ||
| impulse_response: np.ndarray, denominator: np.ndarray, numerator_order: int | ||
| ) -> np.ndarray: | ||
| """ | ||
| Least-squares estimate of the numerator polynomial of a transfer function | ||
| given its impulse response and (already known) denominator polynomial. | ||
|
|
||
| >>> num = _numerator(np.array([1.0, 0.0, 0.0]), np.array([1.0, 0.0, 0.0]), 1) | ||
| >>> np.round(num, 6) | ||
| array([1., 0.]) | ||
| """ | ||
| length = impulse_response.size | ||
| impulse = lfilter([1.0], denominator.ravel(), unit_impulse(length)) | ||
| toep = toeplitz(impulse, unit_impulse(numerator_order + 1)) | ||
| return np.linalg.lstsq(toep.conj(), impulse_response.ravel().conj(), rcond=None)[ | ||
| 0 | ||
| ].conj() | ||
|
|
||
|
|
||
| def yulewalk( | ||
| order: int, frequencies: np.ndarray, magnitudes: np.ndarray, npt: int = 512 | ||
| ) -> tuple[np.ndarray, np.ndarray]: | ||
| """ | ||
| Design a recursive (IIR) digital filter that approximates an arbitrary | ||
| frequency response using the modified Yule-Walker method. This is a | ||
| dependency-free re-implementation of MATLAB/Octave's ``yulewalk`` so that | ||
| the equal-loudness filter below no longer relies on a third-party package. | ||
|
|
||
| https://en.wikipedia.org/wiki/Autoregressive_model#Yule%E2%80%93Walker_equations | ||
|
|
||
| :param order: order of the filter to design | ||
| :param frequencies: sample points on ``[0, 1]`` where 1 is the Nyquist | ||
| frequency, in increasing order and starting at 0 | ||
| :param magnitudes: desired (linear) magnitude at each point in ``frequencies`` | ||
| :param npt: number of points used to estimate the frequency response | ||
| :return: ``(a_coeffs, b_coeffs)``, the denominator and numerator polynomials | ||
|
|
||
| >>> a, b = yulewalk(4, np.array([0.0, 0.5, 1.0]), np.array([1.0, 0.5, 0.0])) | ||
| >>> len(a), len(b) | ||
| (5, 5) | ||
| >>> bool(np.all(np.abs(np.roots(a)) < 1)) # the designed filter is stable | ||
| True | ||
|
|
||
| Mismatched inputs and non-increasing frequencies are rejected: | ||
|
|
||
| >>> yulewalk(4, np.array([0.0, 1.0]), np.array([1.0])) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: frequencies and magnitudes must have the same length | ||
| >>> yulewalk(4, np.array([0.0, 1.0, 0.5]), np.array([1.0, 0.5, 0.0])) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: frequencies must be in increasing order | ||
| """ | ||
| frequencies = np.asarray(frequencies, dtype=float).ravel() | ||
| magnitudes = np.asarray(magnitudes, dtype=float).ravel() | ||
| if frequencies.size != magnitudes.size: | ||
| msg = "frequencies and magnitudes must have the same length" | ||
| raise ValueError(msg) | ||
| if np.any(np.diff(frequencies) < 0): | ||
| msg = "frequencies must be in increasing order" | ||
| raise ValueError(msg) | ||
|
|
||
| npt = npt + 1 | ||
| # Linearly interpolate the target response onto a dense grid, then mirror it | ||
| # to build the full (symmetric) magnitude spectrum. | ||
| response = np.interp(np.linspace(0, 1, npt), frequencies, magnitudes) | ||
| response = np.concatenate([response, response[-2:0:-1]]) | ||
|
|
||
| total = response.size | ||
| half = (total + 1) // 2 | ||
| window_len = 4 * order | ||
| index = np.arange(window_len) | ||
|
|
||
| # Autocorrelation from the power spectrum, tapered with a Hamming window. | ||
| correlation = np.real(np.fft.ifft(response * response)) | ||
| correlation = correlation[:window_len] * ( | ||
| 0.54 + 0.46 * np.cos(np.pi * index / (window_len - 1)) | ||
| ) | ||
| cepstral_window = np.concatenate([[0.5], np.ones(half - 1), np.zeros(total - half)]) | ||
|
|
||
| # Solve the Yule-Walker normal equations for the denominator coefficients. | ||
| rmat = toeplitz(correlation[order : window_len - 1], correlation[order:0:-1]) | ||
| rhs = -correlation[order + 1 : window_len] | ||
| denominator = np.concatenate([[1.0], np.linalg.lstsq(rmat, rhs, rcond=None)[0]]) | ||
| denominator = _polystab(denominator) | ||
|
|
||
| half_correlation = correlation.copy() | ||
| half_correlation[0] = correlation[0] / 2 | ||
| numerator = _numerator(half_correlation, denominator, order) | ||
|
|
||
| padded_num = np.zeros(total) | ||
| padded_num[: numerator.size] = numerator | ||
| padded_den = np.zeros(total) | ||
| padded_den[: denominator.size] = denominator | ||
|
|
||
| spectrum = 2 * np.real(np.fft.fft(padded_num) / np.fft.fft(padded_den)) | ||
| complex_log = np.log(np.abs(spectrum)) + 1j * np.angle(spectrum) | ||
| cepstrum = np.fft.ifft( | ||
| np.exp(np.fft.fft(cepstral_window * np.fft.ifft(complex_log))) | ||
| ) | ||
| numerator = np.real(_numerator(cepstrum[:window_len], denominator, order)) | ||
| return denominator, numerator | ||
|
|
||
|
|
||
| class EqualLoudnessFilter: | ||
| r""" | ||
| An equal-loudness filter which compensates for the human ear's non-linear | ||
| response to sound. This filter corrects this by cascading a Yule-Walker | ||
| filter and a Butterworth filter. | ||
|
|
||
| Designed for use with samplerate of 44.1kHz and above. If you're using a | ||
| lower samplerate, use with caution. | ||
|
|
||
| Code based on the matlab implementation at https://bit.ly/3eqh2HU | ||
| (url shortened for ruff) | ||
|
|
||
| Target curve: https://i.imgur.com/3g2VfaM.png | ||
| Yulewalk response: https://i.imgur.com/J9LnJ4C.png | ||
| Butterworth and overall response: https://i.imgur.com/3g2VfaM.png | ||
|
|
||
| Images and original matlab implementation by David Robinson, 2001 | ||
|
|
||
| https://en.wikipedia.org/wiki/Equal-loudness_contour | ||
|
|
||
| >>> filt = EqualLoudnessFilter() | ||
| >>> isinstance(filt.yulewalk_filter, IIRFilter) | ||
| True | ||
| """ | ||
|
|
||
| def __init__(self, samplerate: int = 44100) -> None: | ||
| self.yulewalk_filter = IIRFilter(10) | ||
| self.butterworth_filter = make_highpass(150, samplerate) | ||
|
|
||
| # pad the data to nyquist | ||
| curve_freqs = np.array(data["frequencies"] + [max(20000.0, samplerate / 2)]) | ||
| curve_gains = np.array(data["gains"] + [140]) | ||
|
|
||
| # Convert to angular frequency | ||
| freqs_normalized = curve_freqs / samplerate * 2 | ||
| # Invert the curve and normalize to 0dB | ||
| gains_normalized = np.power(10, (np.min(curve_gains) - curve_gains) / 20) | ||
|
|
||
| # Compute the coefficients using a least-squares fit to the curve with | ||
| # the built-in ``yulewalk`` implementation above (no third-party deps). | ||
| ya, yb = yulewalk(10, freqs_normalized, gains_normalized) | ||
| self.yulewalk_filter.set_coefficients(ya.tolist(), yb.tolist()) | ||
|
|
||
| def process(self, sample: float) -> float: | ||
| """ | ||
| Process a single sample through both filters | ||
|
|
||
| >>> filt = EqualLoudnessFilter() | ||
| >>> filt.process(0.0) | ||
| 0.0 | ||
| """ | ||
| tmp = self.yulewalk_filter.process(sample) | ||
| return self.butterworth_filter.process(tmp) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Can we move make_notch() and make_bandpass_peak() to their own files? Or is there some reason that they should remain in this file?make_notch() contains many short, cryptic variable names that make the algorithm read like a chemistry formula. Experts might be comfortable with that, but it might be difficult for new developers to understand. Can any of these be renamed to more self-documenting variable names that would help visitors follow the complexity?
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.
Moving them to a new file is not necessary. I now see that this file contains lots of filters, so we can continue that approach.