Unverified Commit 7da61a4b authored by Tomás Osório's avatar Tomás Osório Committed by GitHub
Browse files

Add Functional DB_to_amplitude (#469)

* add functional DB_to_amplitude

* add test scriptmodule

* add test db_to_amplitude

* add tests

* improve docstrings, move ref for easier use
parent 3cffddbd
...@@ -581,6 +581,54 @@ class TestFunctional(unittest.TestCase): ...@@ -581,6 +581,54 @@ class TestFunctional(unittest.TestCase):
_test_torchscript_functional(F.amplitude_to_DB, spec, multiplier, amin, db_multiplier, top_db) _test_torchscript_functional(F.amplitude_to_DB, spec, multiplier, amin, db_multiplier, top_db)
def test_torchscript_DB_to_amplitude(self):
x = torch.rand((1, 100))
ref = 1.
power = 1.
_test_torchscript_functional(F.DB_to_amplitude, x, ref, power)
def test_DB_to_amplitude(self):
# Make some noise
x = torch.rand(1000)
spectrogram = torchaudio.transforms.Spectrogram()
spec = spectrogram(x)
amin = 1e-10
ref = 1.0
db_multiplier = math.log10(max(amin, ref))
# Waveform amplitude -> DB -> amplitude
multiplier = 20.
power = 0.5
db = F.amplitude_to_DB(torch.abs(x), multiplier, amin, db_multiplier, top_db=None)
x2 = F.DB_to_amplitude(db, ref, power)
self.assertTrue(torch.allclose(torch.abs(x), x2, atol=5e-5))
# Spectrogram amplitude -> DB -> amplitude
db = F.amplitude_to_DB(spec, multiplier, amin, db_multiplier, top_db=None)
x2 = F.DB_to_amplitude(db, ref, power)
self.assertTrue(torch.allclose(spec, x2, atol=5e-5))
# Waveform power -> DB -> power
multiplier = 10.
power = 1.
db = F.amplitude_to_DB(x, multiplier, amin, db_multiplier, top_db=None)
x2 = F.DB_to_amplitude(db, ref, power)
self.assertTrue(torch.allclose(torch.abs(x), x2, atol=5e-5))
# Spectrogram power -> DB -> power
db = F.amplitude_to_DB(spec, multiplier, amin, db_multiplier, top_db=None)
x2 = F.DB_to_amplitude(db, ref, power)
self.assertTrue(torch.allclose(spec, x2, atol=5e-5))
@unittest.skipIf(not IMPORT_LIBROSA, 'Librosa not available') @unittest.skipIf(not IMPORT_LIBROSA, 'Librosa not available')
def test_amplitude_to_DB(self): def test_amplitude_to_DB(self):
spec = torch.rand((6, 201)) spec = torch.rand((6, 201))
......
...@@ -401,6 +401,21 @@ def amplitude_to_DB(x, multiplier, amin, db_multiplier, top_db=None): ...@@ -401,6 +401,21 @@ def amplitude_to_DB(x, multiplier, amin, db_multiplier, top_db=None):
return x_db return x_db
def DB_to_amplitude(x, ref, power):
# type: (Tensor, float, float) -> Tensor
r"""Turn a tensor from the decibel scale to the power/amplitude scale.
Args:
x (torch.Tensor): Input tensor before being converted to power/amplitude scale.
ref (float): Reference which the output will be scaled by.
power (float): If power equals 1, will compute DB to power. If 0.5, will compute DB to amplitude.
Returns:
torch.Tensor: Output tensor in power/amplitude scale.
"""
return ref * torch.pow(torch.pow(10.0, 0.1 * x), power)
def create_fb_matrix(n_freqs, f_min, f_max, n_mels, sample_rate): def create_fb_matrix(n_freqs, f_min, f_max, n_mels, sample_rate):
# type: (int, float, float, int, int) -> Tensor # type: (int, float, float, int, int) -> Tensor
r"""Create a frequency bin conversion matrix. r"""Create a frequency bin conversion matrix.
......
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