add_noise_SNR

selfeeg.augmentation.functional.add_noise_SNR(x: ArrayLike, target_snr: float = 5.0, get_noise: bool = False) tuple[ArrayLike, ArrayLike | None][source]

add noise to the input ArrayLike object such that its SNR (Signal to Noise Ratio) will be the one desired.

Since the signal is supposed to be already noisy, it makes more sense to say that this function scales the SNR by a factor equal to 1/P_noise_new, where P_noise_new is the power of the new added noise. Check [snr1] for more info.

Parameters:
  • x (ArrayLike) – The input Tensor or Array.

  • target_snr (float, optional) –

    The target SNR.

    Default = 5.

  • get_noise (bool, optional) –

    Whether to return the generated noise or not.

    Default = False

Returns:
  • x (ArrayLike) – The augmented version of the input Tensor or Array.

  • noise (ArrayLike, optional) – The generated noise. Returned only if get_noise is set to True

References

[snr1]

created using the following reference: https://stackoverflow.com/questions/14058340/

Example

>>> import torch
>>> import numpy as np
>>> import selfeeg.augmentation as aug
>>> x = torch.zeros(16,32,1024) + torch.sin(torch.linspace(0, 8*np.pi,1024))
>>> xaug, noise = aug.add_noise_SNR(x, 10, get_noise=True)
>>> SNR = 10*np.log10(((x**2).sum().mean())/((noise**2).sum().mean()))
>>> print(math.isclose( SNR ,10,rel_tol=1e-2)) # should return True