From 4132e11e35e9d60cfae1997112abbe388bb45b85 Mon Sep 17 00:00:00 2001 From: moto <855818+mthrok@users.noreply.github.com> Date: Mon, 1 Aug 2022 07:22:54 -0700 Subject: [PATCH] Fix audio data augmentation tutorial (#1987) Addresses: #1930 Ref: https://github.com/pytorch/audio/pull/2595 --- beginner_source/audio_data_augmentation_tutorial.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/beginner_source/audio_data_augmentation_tutorial.py b/beginner_source/audio_data_augmentation_tutorial.py index f3ac7917da..8bcc30cbf5 100644 --- a/beginner_source/audio_data_augmentation_tutorial.py +++ b/beginner_source/audio_data_augmentation_tutorial.py @@ -239,14 +239,14 @@ def plot_specgram(waveform, sample_rate, title="Spectrogram", xlim=None): noise, _ = torchaudio.load(SAMPLE_NOISE) noise = noise[:, : speech.shape[1]] -speech_power = speech.norm(p=2) -noise_power = noise.norm(p=2) +speech_rms = speech.norm(p=2) +noise_rms = noise.norm(p=2) snr_dbs = [20, 10, 3] noisy_speeches = [] for snr_db in snr_dbs: snr = 10 ** (snr_db / 20) - scale = snr * noise_power / speech_power + scale = snr * noise_rms / speech_rms noisy_speeches.append((scale * speech + noise) / 2) ###################################################################### @@ -376,7 +376,7 @@ def plot_specgram(waveform, sample_rate, title="Spectrogram", xlim=None): noise = noise[:, : rir_applied.shape[1]] snr_db = 8 -scale = math.exp(snr_db / 10) * noise.norm(p=2) / rir_applied.norm(p=2) +scale = (10 ** (snr_db / 20)) * noise.norm(p=2) / rir_applied.norm(p=2) bg_added = (scale * rir_applied + noise) / 2 plot_specgram(bg_added, sample_rate, title="BG noise added")