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.
- 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
andnoise
, with shape (…,) (leading dimensions must match those ofwaveform
). IfNone
, all elements inwaveform
andnoise
are treated as valid. (Default:None
)
- Returns:
Result of scaling and adding
noise
towaveform
, with shape (…, L) (same shape aswaveform
).- Return type:
- Tutorials using
add_noise
: Torchaudio-Squim: Non-intrusive Speech Assessment in TorchAudio
Torchaudio-Squim: Non-intrusive Speech Assessment in TorchAudioAudio Data Augmentation