shift_frequency

selfeeg.augmentation.functional.shift_frequency(x: ArrayLike, shift_freq: float, Fs: float, forward: bool = None, random_shift: bool = False, batch_equal: bool = True) ArrayLike[source]

shifts the frequency components of the signals included in the ArrayLike object.

Shift will be performed as reported in [shiftFT1] (see section 4 of the reference paper).

Parameters:
  • x (ArrayLike) – The input Tensor or Array. The last two dimensions must refer to the EEG (Channels x Samples).

  • shift_freq (float) – The desired shift, given in Hz. It must be a positive value. If random_shift is set to True, shift_freq is used to extract a random value from a uniform distribution between [-shift_freq, shift_freq], i.e. it becomes the maximum value of the distribution.

  • Fs (float`) – the EEG sampling rate in Hz.

  • forward (bool, optional) –

    Whether to shift the EEG frequencies forward (True) or backward (False). If left to None, a random selection of the shift direction will be performed.

    Default = None

  • random_shift (bool, optional) –

    Wheter to choose a random shift from a uniform distribution between [-shift_freq, shift_freq] or not.

    Default = False

  • batch_equal (bool, optional) –

    Whether to apply the same shift to all EEG record or not.

    Default = True

Returns:

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

Note

If random shift is set to False and forward is None, then batch_equal will be set to True since no differences in the shift can be applied.

References

[shiftFT1]

Rommel, Cédric, et al. “Data augmentation for learning predictive models on EEG: a systematic comparison.” Journal of Neural Engineering 19.6 (2022): 066020.

Example

>>> import torch
>>> import math
>>> import selfeeg.augmentation as aug
>>> from scipy.signal import periodogram
>>> Fs= 128
>>> x = torch.zeros(16,32,1024) + torch.sin(torch.linspace(0, 48*torch.pi,1024))
>>> x = x + torch.sin(torch.linspace(0, 8*torch.pi,1024))
>>> xaug = aug.shift_frequency(x, 10, Fs, True)
>>> f, per1 =periodogram(x[0,0], fs=Fs)
>>> per2 =periodogram(xaug[0,0], fs=Fs)[1]
>>> # check if spectrogram is really shfted
>>> print(math.isclose(per1[4],per2[84], rel_tol=1e-5)) # should return True
>>> print(math.isclose(per1[24],per2[104],rel_tol=1e-5)) # should return True

plot the augmentations (require matplotlib to be installed)

>>> import matplotlib.pyplot as plt
>>> plt.style.use('seaborn-v0_8-white')
>>> plt.rcParams['figure.figsize'] = (15.0, 6.0)
>>> plt.plot(f,per1)
>>> plt.plot(f,per2)
>>> plt.tick_params(axis='both', which='major', labelsize=12)
>>> plt.title(
...     'Spectrogram of original and frequency shifted signal', fontsize=15)
>>> plt.legend(['original sinusoide', 'shifted sinusoide'])
>>> plt.show()