Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calculation of SNR value in tutorial #2285

Closed
wants to merge 1 commit into from

Conversation

hagenw
Copy link
Contributor

@hagenw hagenw commented Mar 22, 2022

The calculation of the SNR in tha data augmentation examples seems to be wrong to me:

image

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 scale would change from 7.39, 2.72, 1.35 to 10.0, 3.16, 1.41.

@facebook-github-bot
Copy link
Contributor

Hi @hagenw!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@hagenw hagenw force-pushed the fix-snr-calculation branch from c47fef5 to 34e443e Compare March 22, 2022 13:19
@hagenw hagenw changed the title Fix calculation of SNR value Fix calculation of SNR value in tutorial Mar 22, 2022
Copy link
Collaborator

@mthrok mthrok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hagenw

Thanks for pointing out. The proposed fix seems to be correct.

@facebook-github-bot
Copy link
Contributor

@mthrok has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@github-actions
Copy link

Hey @mthrok.
You merged this PR, but labels were not properly added. Please add a primary and secondary label (See https://github.com/pytorch/audio/blob/main/.github/process_commit.py)

xiaohui-zhang pushed a commit to xiaohui-zhang/audio that referenced this pull request May 4, 2022
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: pytorch#2285

Reviewed By: nateanl

Differential Revision: D35047737

Pulled By: mthrok

fbshipit-source-id: ac24c8fd48ef06b4b611e35163084644330a3ef3
mthrok added a commit to mthrok/audio that referenced this pull request Jul 29, 2022
In pytorch#2285, the SNR calculation was fixed,
but there was still one that was not fixed. This commit fixes it.

Also following the feedback pytorch/tutorials#1930 (comment), update the variable name.
facebook-github-bot pushed a commit that referenced this pull request Aug 1, 2022
Summary:
In #2285, the SNR calculation was fixed,
but there was still one that was not fixed. This commit fixes it.

Also following the feedback pytorch/tutorials#1930 (comment), update the variable name.

Pull Request resolved: #2595

Reviewed By: carolineechen

Differential Revision: D38314672

Pulled By: mthrok

fbshipit-source-id: b2015e2709729190d97264aa191651b3af4ba856
mthrok added a commit that referenced this pull request Aug 1, 2022
Summary:
In #2285, the SNR calculation was fixed,
but there was still one that was not fixed. This commit fixes it.

Also following the feedback pytorch/tutorials#1930 (comment), update the variable name.

Pull Request resolved: #2595

Reviewed By: carolineechen

Differential Revision: D38314672

Pulled By: mthrok

fbshipit-source-id: b2015e2709729190d97264aa191651b3af4ba856
@hagenw hagenw deleted the fix-snr-calculation branch September 8, 2022 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants