random_FT_phase

selfeeg.augmentation.functional.random_FT_phase(x: ArrayLike, value: float = 1, batch_equal: bool = True) ArrayLike[source]

randomizes the phase of all signals in the input ArrayLike object.

For more info, see [ftphase1].

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

  • value (float, optional) –

    The magnitude of the phase perturbation. It must be a value between (0,1], which will be used to rescale the interval [0, 2* ‘pi’] in [0, value * 2 * ‘pi’]

    Default = 1

  • batch_equal (bool, optional) –

    Whether to apply the same perturbation on all signals or not. Note that all channels of the same records will be perturbed in the same way to preserve cross-channel correlations.

    Default = True

Returns:

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

References

[ftphase1]

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 selfeeg.augmentation as aug
>>> x = torch.zeros(16,32,1024) + torch.sin(torch.linspace(0, 8*torch.pi,1024))
>>> xaug = aug.random_FT_phase(x, 0.8)
>>> # see https://dsp.stackexchange.com/questions/87343/
>>> phase_shift = torch.arccos( 2*((x[0,0,0:512]*xaug[0,0,:512]).mean()) )
>>> a=torch.sin(torch.linspace(0, 8*torch.pi,1024) + phase_shift)
>>> if (a[0] - xaug[0,0,0]).abs()>0.1:
...     a=torch.sin(torch.linspace(0, 8*torch.pi,1024) - phase_shift)
>>> print((a - xaug[0,0]).mean()<1e-3)

plot the results (required matplotlib to be installed)

>>> plt.plot(x[0,0])
>>> plt.plot(xaug[0,0])
>>> plt.plot(a)
>>> plt.show()