Skip to content

Commit 6d5f0b4

Browse files
xinyang0vincentqb
authored andcommitted
Add peaking equalizer filter (#315)
Add peaking equalizer filter in functional.py and test it in test_functional_filter.py.
1 parent 2489cbb commit 6d5f0b4

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

test/test_functional_filtering.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ def test_highpass(self):
136136
# TBD - this fails at the 1e-4 level, debug why
137137
assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-3)
138138

139+
def test_equalizer(self):
140+
"""
141+
Test biquad peaking equalizer filter, compare to SoX implementation
142+
"""
143+
144+
CENTER_FREQ = 300
145+
Q = 0.707
146+
GAIN = 1
147+
148+
noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3")
149+
E = torchaudio.sox_effects.SoxEffectsChain()
150+
E.set_input_file(noise_filepath)
151+
E.append_effect_to_chain("equalizer", [CENTER_FREQ, Q, GAIN])
152+
sox_output_waveform, sr = E.sox_build_flow_effects()
153+
154+
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
155+
output_waveform = F.equalizer_biquad(waveform, sample_rate, CENTER_FREQ, GAIN, Q)
156+
157+
assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4)
158+
139159
def test_perf_biquad_filtering(self):
140160

141161
fn_sine = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3")

torchaudio/functional.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"lfilter",
2020
"lowpass_biquad",
2121
"highpass_biquad",
22+
"equalizer_biquad",
2223
"biquad",
2324
'mask_along_axis',
2425
'mask_along_axis_iid'
@@ -685,6 +686,33 @@ def lowpass_biquad(waveform, sample_rate, cutoff_freq, Q=0.707):
685686
return biquad(waveform, b0, b1, b2, a0, a1, a2)
686687

687688

689+
def equalizer_biquad(waveform, sample_rate, center_freq, gain, Q=0.707):
690+
# type: (Tensor, int, float, float, float) -> Tensor
691+
r"""Designs biquad peaking equalizer filter and performs filtering. Similar to SoX implementation.
692+
693+
Args:
694+
waveform (torch.Tensor): audio waveform of dimension of `(channel, time)`
695+
sample_rate (int): sampling rate of the waveform, e.g. 44100 (Hz)
696+
center_freq (float): filter’s central frequency
697+
gain (float): desired gain at the boost (or attenuation) in dB
698+
q_factor (float): https://en.wikipedia.org/wiki/Q_factor
699+
700+
Returns:
701+
output_waveform (torch.Tensor): Dimension of `(channel, time)`
702+
"""
703+
w0 = 2 * math.pi * center_freq / sample_rate
704+
A = math.exp(gain / 40.0 * math.log(10))
705+
alpha = math.sin(w0) / 2 / Q
706+
707+
b0 = 1 + alpha * A
708+
b1 = -2 * math.cos(w0)
709+
b2 = 1 - alpha * A
710+
a0 = 1 + alpha / A
711+
a1 = -2 * math.cos(w0)
712+
a2 = 1 - alpha / A
713+
return biquad(waveform, b0, b1, b2, a0, a1, a2)
714+
715+
688716
@torch.jit.script
689717
def mask_along_axis_iid(specgrams, mask_param, mask_value, axis):
690718
# type: (Tensor, int, float, int) -> Tensor

0 commit comments

Comments
 (0)