Unverified Commit 933f6037 authored by Bhargav Kathivarapu's avatar Bhargav Kathivarapu Committed by GitHub
Browse files

Add bandpass and bandreject to functional (#464)



* Add bandpass to functional.py

* Add bandpass and bandreject to functional

* change name to const_skirt_gain
Co-authored-by: default avatarBhargav Kathivarapu <ka387861@L-156168835.local>
Co-authored-by: default avatarVincent Quenneville-Belair <vincentqb@gmail.com>
parent 61bbd133
...@@ -168,6 +168,68 @@ class TestFunctionalFiltering(unittest.TestCase): ...@@ -168,6 +168,68 @@ class TestFunctionalFiltering(unittest.TestCase):
assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4)
_test_torchscript_functional(F.allpass_biquad, waveform, sample_rate, CENTRAL_FREQ, Q) _test_torchscript_functional(F.allpass_biquad, waveform, sample_rate, CENTRAL_FREQ, Q)
def test_bandpass_with_csg(self):
"""
Test biquad bandpass filter, compare to SoX implementation
"""
CENTRAL_FREQ = 1000
Q = 0.707
CONST_SKIRT_GAIN = True
noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3")
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(noise_filepath)
E.append_effect_to_chain("bandpass", ["-c", CENTRAL_FREQ, str(Q) + 'q'])
sox_output_waveform, sr = E.sox_build_flow_effects()
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
output_waveform = F.bandpass_biquad(waveform, sample_rate, CENTRAL_FREQ, Q, CONST_SKIRT_GAIN)
assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4)
_test_torchscript_functional(F.bandpass_biquad, waveform, sample_rate, CENTRAL_FREQ, Q, CONST_SKIRT_GAIN)
def test_bandpass_without_csg(self):
"""
Test biquad bandpass filter, compare to SoX implementation
"""
CENTRAL_FREQ = 1000
Q = 0.707
CONST_SKIRT_GAIN = False
noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3")
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(noise_filepath)
E.append_effect_to_chain("bandpass", [CENTRAL_FREQ, str(Q) + 'q'])
sox_output_waveform, sr = E.sox_build_flow_effects()
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
output_waveform = F.bandpass_biquad(waveform, sample_rate, CENTRAL_FREQ, Q, CONST_SKIRT_GAIN)
assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4)
_test_torchscript_functional(F.bandpass_biquad, waveform, sample_rate, CENTRAL_FREQ, Q, CONST_SKIRT_GAIN)
def test_bandreject(self):
"""
Test biquad bandreject filter, compare to SoX implementation
"""
CENTRAL_FREQ = 1000
Q = 0.707
noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3")
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(noise_filepath)
E.append_effect_to_chain("bandreject", [CENTRAL_FREQ, str(Q) + 'q'])
sox_output_waveform, sr = E.sox_build_flow_effects()
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
output_waveform = F.bandreject_biquad(waveform, sample_rate, CENTRAL_FREQ, Q)
assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4)
_test_torchscript_functional(F.bandreject_biquad, waveform, sample_rate, CENTRAL_FREQ, Q)
def test_equalizer(self): def test_equalizer(self):
""" """
Test biquad peaking equalizer filter, compare to SoX implementation Test biquad peaking equalizer filter, compare to SoX implementation
......
...@@ -23,6 +23,8 @@ __all__ = [ ...@@ -23,6 +23,8 @@ __all__ = [
"lowpass_biquad", "lowpass_biquad",
"highpass_biquad", "highpass_biquad",
"allpass_biquad", "allpass_biquad",
"bandpass_biquad",
"bandreject_biquad",
"equalizer_biquad", "equalizer_biquad",
"biquad", "biquad",
'mask_along_axis', 'mask_along_axis',
...@@ -824,6 +826,67 @@ def allpass_biquad(waveform, sample_rate, central_freq, Q=0.707): ...@@ -824,6 +826,67 @@ def allpass_biquad(waveform, sample_rate, central_freq, Q=0.707):
return biquad(waveform, b0, b1, b2, a0, a1, a2) return biquad(waveform, b0, b1, b2, a0, a1, a2)
def bandpass_biquad(waveform, sample_rate, central_freq, Q=0.707, const_skirt_gain=False):
# type: (Tensor, int, float, float, bool) -> Tensor
r"""Design two-pole band-pass filter. Similar to SoX implementation.
Args:
waveform(torch.Tensor): audio waveform of dimension of `(..., time)`
sample_rate (int): sampling rate of the waveform, e.g. 44100 (Hz)
central_freq (float): central frequency (in Hz)
q_factor (float): https://en.wikipedia.org/wiki/Q_factor
const_skirt_gain (bool) : If ``True``, uses a constant skirt gain (peak gain = Q).
If ``False``, uses a constant 0dB peak gain. (Default: ``False``)
Returns:
output_waveform (torch.Tensor): Dimension of `(..., time)`
References:
http://sox.sourceforge.net/sox.html
https://www.w3.org/2011/audio/audio-eq-cookbook.html#APF
"""
w0 = 2 * math.pi * central_freq / sample_rate
alpha = math.sin(w0) / 2 / Q
temp = math.sin(w0) / 2 if const_skirt_gain else alpha
b0 = temp
b1 = 0.
b2 = -temp
a0 = 1 + alpha
a1 = -2 * math.cos(w0)
a2 = 1 - alpha
return biquad(waveform, b0, b1, b2, a0, a1, a2)
def bandreject_biquad(waveform, sample_rate, central_freq, Q=0.707):
# type: (Tensor, int, float, float) -> Tensor
r"""Design two-pole band-reject filter. Similar to SoX implementation.
Args:
waveform(torch.Tensor): audio waveform of dimension of `(..., time)`
sample_rate (int): sampling rate of the waveform, e.g. 44100 (Hz)
central_freq (float): central frequency (in Hz)
q_factor (float): https://en.wikipedia.org/wiki/Q_factor
Returns:
output_waveform (torch.Tensor): Dimension of `(..., time)`
References:
http://sox.sourceforge.net/sox.html
https://www.w3.org/2011/audio/audio-eq-cookbook.html#APF
"""
w0 = 2 * math.pi * central_freq / sample_rate
alpha = math.sin(w0) / 2 / Q
b0 = 1.
b1 = -2 * math.cos(w0)
b2 = 1.
a0 = 1 + alpha
a1 = -2 * math.cos(w0)
a2 = 1 - alpha
return biquad(waveform, b0, b1, b2, a0, a1, a2)
def equalizer_biquad(waveform, sample_rate, center_freq, gain, Q=0.707): def equalizer_biquad(waveform, sample_rate, center_freq, gain, Q=0.707):
# type: (Tensor, int, float, float, float) -> Tensor # type: (Tensor, int, float, float, float) -> Tensor
r"""Design biquad peaking equalizer filter and perform filtering. Similar to SoX implementation. r"""Design biquad peaking equalizer filter and perform filtering. Similar to SoX implementation.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment