-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Add Equal Loudness Filter #7019
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ef22068
Add Equal Loudness Filter
Martmists-GH 412b4b6
NoneType return on __init__
Martmists-GH 3cd4e81
Add data to JSON as requested by @CenTdemeern1 in a not very polite m…
Martmists-GH 702b6a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7d366bd
'modernize'
Martmists-GH e6c0fde
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bc19d88
Update audio_filters/equal_loudness_filter.py
Martmists-GH 63cfd22
Update equal_loudness_filter.py
cclauss 62441bd
Update equal_loudness_filter.py
cclauss 243dac3
Finally!!
cclauss ae66c19
Arrgghh
cclauss 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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from json import loads | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
from yulewalker import yulewalk | ||
|
||
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()) | ||
|
||
|
||
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 yulewalk 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 matlab implementation at https://bit.ly/3eqh2HU | ||
(url shortened for flake8) | ||
|
||
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 | ||
""" | ||
|
||
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) | ||
|
||
# Scipy's `yulewalk` function is a stub, so we're using the | ||
# `yulewalker` library instead. | ||
# This function computes the coefficients using a least-squares | ||
# fit to the specified curve. | ||
ya, yb = yulewalk(10, freqs_normalized, gains_normalized) | ||
self.yulewalk_filter.set_coefficients(ya, yb) | ||
|
||
def process(self, sample: float) -> float: | ||
""" | ||
Process a single sample through both filters | ||
|
||
>>> filt = EqualLoudnessFilter() | ||
>>> filt.process(0.0) | ||
0.0 | ||
""" | ||
Martmists-GH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tmp = self.yulewalk_filter.process(sample) | ||
return self.butterworth_filter.process(tmp) |
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,76 @@ | ||
{ | ||
"_comment": "The following is a representative average of the Equal Loudness Contours as measured by Robinson and Dadson, 1956", | ||
"_doi": "10.1088/0508-3443/7/5/302", | ||
"frequencies": [ | ||
0, | ||
20, | ||
30, | ||
40, | ||
50, | ||
60, | ||
70, | ||
80, | ||
90, | ||
100, | ||
200, | ||
300, | ||
400, | ||
500, | ||
600, | ||
700, | ||
800, | ||
900, | ||
1000, | ||
1500, | ||
2000, | ||
2500, | ||
3000, | ||
3700, | ||
4000, | ||
5000, | ||
6000, | ||
7000, | ||
8000, | ||
9000, | ||
10000, | ||
12000, | ||
15000, | ||
20000 | ||
], | ||
"gains": [ | ||
120, | ||
113, | ||
103, | ||
97, | ||
93, | ||
91, | ||
89, | ||
87, | ||
86, | ||
85, | ||
78, | ||
76, | ||
76, | ||
76, | ||
76, | ||
77, | ||
78, | ||
79.5, | ||
80, | ||
79, | ||
77, | ||
74, | ||
71.5, | ||
70, | ||
70.5, | ||
74, | ||
79, | ||
84, | ||
86, | ||
86, | ||
85, | ||
95, | ||
110, | ||
125 | ||
] | ||
} |
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
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.