Shortcuts

torchaudio.functional.add_noise

torchaudio.functional.add_noise(waveform: Tensor, noise: Tensor, snr: Tensor, lengths: Optional[Tensor] = None) Tensor[source]

Scales and adds noise to waveform per signal-to-noise ratio.

Specifically, for each pair of waveform vector \(x \in \mathbb{R}^L\) and noise vector \(n \in \mathbb{R}^L\), the function computes output \(y\) as

\[y = x + a n \, \text{,} \]

where

\[a = \sqrt{ \frac{ ||x||_{2}^{2} }{ ||n||_{2}^{2} } \cdot 10^{-\frac{\text{SNR}}{10}} } \, \text{,} \]

with \(\text{SNR}\) being the desired signal-to-noise ratio between \(x\) and \(n\), in dB.

Note that this function broadcasts singleton leading dimensions in its inputs in a manner that is consistent with the above formulae and PyTorch’s broadcasting semantics.

This feature supports the following devices: CPU, CUDA This API supports the following properties: Autograd, TorchScript
Parameters:
  • waveform (torch.Tensor) – Input waveform, with shape (…, L).

  • noise (torch.Tensor) – Noise, with shape (…, L) (same shape as waveform).

  • snr (torch.Tensor) – Signal-to-noise ratios in dB, with shape (…,).

  • lengths (torch.Tensor or None, optional) – Valid lengths of signals in waveform and noise, with shape (…,) (leading dimensions must match those of waveform). If None, all elements in waveform and noise are treated as valid. (Default: None)

Returns:

Result of scaling and adding noise to waveform, with shape (…, L) (same shape as waveform).

Return type:

torch.Tensor

Tutorials using add_noise:
Torchaudio-Squim: Non-intrusive Speech Assessment in TorchAudio

Torchaudio-Squim: Non-intrusive Speech Assessment in TorchAudio

Torchaudio-Squim: Non-intrusive Speech Assessment in TorchAudio
Audio Data Augmentation

Audio Data Augmentation

Audio Data Augmentation

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources