-
Notifications
You must be signed in to change notification settings - Fork 696
Add peaking equalizer filter #315
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,26 @@ def test_highpass(self): | |
# TBD - this fails at the 1e-4 level, debug why | ||
assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-3) | ||
|
||
def test_equalizer(self): | ||
""" | ||
Test biquad peaking equalizer filter, compare to SoX implementation | ||
""" | ||
|
||
CENTER_FREQ = 300 | ||
Q = 0.707 | ||
GAIN = 1 | ||
|
||
noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vincentqb instead of calling into sox while testing, should we write out reference files instead? especially since the asset is fixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree with you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although this could be done in a separate PR for all the other tests too. |
||
E = torchaudio.sox_effects.SoxEffectsChain() | ||
E.set_input_file(noise_filepath) | ||
E.append_effect_to_chain("equalizer", [CENTER_FREQ, Q, GAIN]) | ||
sox_output_waveform, sr = E.sox_build_flow_effects() | ||
|
||
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True) | ||
output_waveform = F.equalizer_biquad(waveform, sample_rate, CENTER_FREQ, GAIN, Q) | ||
|
||
assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) | ||
|
||
def test_perf_biquad_filtering(self): | ||
|
||
fn_sine = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"lfilter", | ||
"lowpass_biquad", | ||
"highpass_biquad", | ||
"equalizer_biquad", | ||
"biquad", | ||
'mask_along_axis', | ||
'mask_along_axis_iid' | ||
|
@@ -683,6 +684,33 @@ def lowpass_biquad(waveform, sample_rate, cutoff_freq, Q=0.707): | |
return biquad(waveform, b0, b1, b2, a0, a1, a2) | ||
|
||
|
||
def equalizer_biquad(waveform, sample_rate, center_freq, gain, Q=0.707): | ||
# type: (Tensor, int, float, float, float) -> Tensor | ||
r"""Designs biquad peaking equalizer filter and performs filtering. Similar to SoX implementation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure its necessary to mention that this is similar to Sox. Its a well-defined operation that stands by itself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. It's mentioned in the other transforms, so we'll have to remove it elsewhere too. |
||
|
||
Args: | ||
waveform (torch.Tensor): audio waveform of dimension of `(channel, time)` | ||
sample_rate (int): sampling rate of the waveform, e.g. 44100 (Hz) | ||
center_freq (float): filter’s central frequency | ||
gain (float): desired gain at the boost (or attenuation) in dB | ||
q_factor (float): https://en.wikipedia.org/wiki/Q_factor | ||
|
||
Returns: | ||
output_waveform (torch.Tensor): Dimension of `(channel, time)` | ||
""" | ||
w0 = 2 * math.pi * center_freq / sample_rate | ||
A = math.exp(gain / 40.0 * math.log(10)) | ||
alpha = math.sin(w0) / 2 / Q | ||
|
||
b0 = 1 + alpha * A | ||
b1 = -2 * math.cos(w0) | ||
b2 = 1 - alpha * A | ||
a0 = 1 + alpha / A | ||
a1 = -2 * math.cos(w0) | ||
a2 = 1 - alpha / A | ||
return biquad(waveform, b0, b1, b2, a0, a1, a2) | ||
|
||
|
||
@torch.jit.script | ||
def mask_along_axis_iid(specgrams, mask_param, mask_value, axis): | ||
# type: (Tensor, int, float, int) -> Tensor | ||
|
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.
Is there a chance that these values are special and miss something important?
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.
Good question! The specgram of the white noise signal is actually in the range of [0, 578], so 1000 is a bit large for this signal. Decrease it to 300.