From 8395fe65a676471eafaa55e8c2ce9f303093ce70 Mon Sep 17 00:00:00 2001 From: Hagen Wierstorf Date: Tue, 22 Mar 2022 09:42:08 -0700 Subject: [PATCH] Fix calculation of SNR value in tutorial (#2285) Summary: The calculation of the SNR in tha data augmentation examples seems to be wrong to me: ![image](https://user-images.githubusercontent.com/173624/159487032-c60470c6-ef8e-48a0-ad5e-a117fcb8d606.png) If we start from the definition of the signal-to-noise ratio using the root mean square value we get: ``` SNR = 20 log10 ( rms(scale * speech) / rms(noise) ) ``` this can be transformed to ``` scale = 10^(SNR/20) rms(noise) / rms(speech) ``` In the example not `rms` is used but `lambda x: x.norm(p=2)`, but as we have the same length of the speech and noise signal, we have ``` rms(noise) / rms(speech) = noise.norm(p=2) / speech.norm(p=2) ``` this would lead us to: ``` 10^(SNR/20) = e^(SNR / 10) ``` which is not true. Hence I changed `e^(SNR / 10)` to `10^(SNR/20)`. For the proposed SNR values of 20 dB, 10 dB, 3 dB the value of the scale would change from 7.39, 2.72, 1.35 to 10.0, 3.16, 1.41. Pull Request resolved: https://github.com/pytorch/audio/pull/2285 Reviewed By: nateanl Differential Revision: D35047737 Pulled By: mthrok fbshipit-source-id: ac24c8fd48ef06b4b611e35163084644330a3ef3 --- examples/tutorials/audio_data_augmentation_tutorial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tutorials/audio_data_augmentation_tutorial.py b/examples/tutorials/audio_data_augmentation_tutorial.py index 3129d04392..61382c1671 100644 --- a/examples/tutorials/audio_data_augmentation_tutorial.py +++ b/examples/tutorials/audio_data_augmentation_tutorial.py @@ -350,7 +350,7 @@ def get_noise_sample(*, resample=None): snr_dbs = [20, 10, 3] noisy_speeches = [] for snr_db in snr_dbs: - snr = math.exp(snr_db / 10) + snr = 10 ** (snr_db / 20) scale = snr * noise_power / speech_power noisy_speeches.append((scale * speech + noise) / 2)