add_band_noise
- selfeeg.augmentation.functional.add_band_noise(x: ArrayLike, bandwidth: list[tuple[float, float], str, float], samplerate: float = 256, noise_range: float = None, std: float = None, get_noise: bool = False) tuple[ArrayLike, ArrayLike | None][source]
adds random noise filtered at specific bandwidths.
Given a set of bandwidths or a set of specific frequencies, add_band_noise creates a noise whose spectrum is bigger than zero only on the specified bands. It can be used to alter only specific frequency components of the original signal. By default, the noise generated will have the same standard deviation as x, but it can be rescaled so to be within a specific range of values or to have a specific standard deviation.
- Parameters:
x (ArrayLike) – The input Tensor or Array.
bandwidth (list) –
The frequency components which the noise must have. Must be a list with the following values:
strings: add noise to specific EEG components. Can be any of “delta”, “theta”, “alpha”, “beta”, “gamma”, “gamma_low”, “gamma_high”.
scalar: add noise to a specific component.
tuple with 2 scalar: add noise to a specific band set with the tuple (start_component,end_component).
samplerate (float, optional) –
The sampling rate, given in Hz. Remember to change this value according to the signal sampling rate.
Default = 256
noise_range (float, optional) –
The range within the noise is scaled. Must be a single scalar or a two element list. If given as a single scalar, then the range is considered the interval [-noise_range, noise_range]. If this parameter is given, then std value is ignored, since it is not possible to guarantee that the two conditions will be satisfied at the same time. To rescale, the following formula is applied:
noise_new = ((noise - max(noise))/(max(noise)-min(noise))) *(target_range_max - target_range_min) + target_range_min
Default = None
std (float, optional) –
The desired standard deviation of the noise. If noise_range is given, this argument is ignored. It simply scale the noise by applying:
noise_new = noise * (target_std / std(noise))Default = None
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.
Example
>>> import torch >>> import numpy as np >>> from scipy.signal import periodogram >>> import selfeeg.augmentation as aug >>> x = torch.zeros(16,32,1024) + torch.sin(torch.linspace(0, 8*torch.pi,1024)) >>> xaug, noise = aug.add_band_noise(x, "beta", 128, noise_range=0.2, get_noise=True) >>> f, per = periodogram(noise, 128) >>> index= np.where(per>1e-12)[0] >>> print( len(np.where( ((f[index]<13) | (f[index]>30)))[0])==0 ) #should return True