Signal#

Tools for digital signal processing: fft filtering, low-frequency filtering utilizing downsampling etc.

class brainmaze_utils.signal.LowFrequencyFilter(fs=None, cutoff=None, n_decimate=1, n_order=None, dec_cutoff=0.3, filter_type='lp', ftype='fir')#
Parameters:
  • fs (float) – sampling frequency

  • cutoff (float) – frequency cutoff

  • n_decimate (int) – how many times the signal will be downsampled before the low frequency filtering

  • n_order (int) – n-th order filter used for filtration

  • dec_cutoff (float) – relative frequency at which the signal will be filtered when downsampled

  • filter_type (str) – ‘lp’ or ‘hp’

  • ftype (str) – ‘fir’ or ‘iir’

LFFilter = LowFrequencyFilter(fs=fs, cutoff=cutoff_low, n_decimate=2, n_order=101, dec_cutoff=0.3, filter_type='lp')
X_inp = np.random.randn(1e4)
X_outp = LFilter(X_inp)
decimate(X)#
design_filters()#
filter_signal(X)#
upsample(X)#
brainmaze_utils.signal.PSD(x: ndarray, fs: float, nperseg=None, noverlap=0, nfft=None)#

Estimates PSD of an input signal or signals using Welch’s method. If nperseg is None, the spectrum is estimated from the whole signal in a single window.

Parameters:
  • x (np.ndarray) – A single signal with a shape (n_samples) or set of signals with a shape (n_signals, n_shapes)

  • fs (float) – Sampling frequency

  • nperseg (int) – Number of samples for a segment

  • noverlap (int) – Number of overlap samples.

Returns:

  • freq (np.ndarray) – Frequency axis for estimated PSD

  • psd (np.ndarray) – Power spectral density estimate

brainmaze_utils.signal.buffer(x: ndarray, fs: float = 1, segm_size: float = None, overlap: float = 0, drop: bool = True)#

Buffer signal into matrix

Parameters:
  • x (np.ndarray) – Signal to be

  • fs (float) – Sampling frequency

  • segm_size (float) – Segment size in seconds

  • overlap (float) – Overlap size in seconds

  • drop (bool) – Drop last segment if True, else append zeros

Returns:

buffered_signal

Return type:

np.ndarray

brainmaze_utils.signal.decimate(x, fs, fs_new, cutoff=None, datarate=False)#

Downsample signal with anti-aliasing filter (Butterworth - 16th order). Works for signals with NaN values - replaced by zero. Can return also data-rate. Can take matrix where signals are stacked in 0th dim.

Parameters:
  • x (np ndarray) – shape[n_signal, n_sample], shape[n_sample] - can process multiple signals.

  • fs (float) – Signals fs

  • fs_new (float) – Downsample to fs_new

  • cutoff (float) – Elective cutoff freq. of anti-alias. filter. By default 2/3 of 0.5*fs_new

  • datarate (bool) – If return datarate of signals

Return type:

numpy ndarray / tuple

brainmaze_utils.signal.detrend(y, x=None, y2=None)#

Detrends the input signal by removing the linear trend.

Parameters:
  • y (numpy.ndarray) – The input signal to be detrended.

  • x (numpy.ndarray, optional) – The x-values corresponding to the y-values. If None, a linear space is used.

  • y2 (numpy.ndarray, optional) – An additional signal to be detrended using the same trend as y.

Returns:

  • numpy.ndarray – The detrended signal.

  • tuple of numpy.ndarray – The detrended signals if y2 is provided.

brainmaze_utils.signal.fft_filter(X: ndarray, fs: float, cutoff: float, type: str = 'lp')#

FFT filter

Parameters:
  • X (numpy.ndarray)

  • fs (float)

  • cutoff (float)

  • type (str) – ‘lp’ or ‘hp’

Return type:

numpy.ndarray

brainmaze_utils.signal.find_peaks(y)#

Finds the peaks in a given signal.

Parameters:

y (numpy.ndarray) – The input signal in which to find peaks.

Returns:

  • position (numpy.ndarray) – The positions of the peaks in the input signal.

  • value (numpy.ndarray) – The values of the peaks in the input signal.

brainmaze_utils.signal.get_datarate(x)#

Calculates datarate based on NaN values

Parameters:

x (numpy ndarray / list)

Returns:

  • numpy ndarray

  • flaot values 0-1 with relative NaN count per signal.

brainmaze_utils.signal.nandecimate(x, fs, fs_new, cutoff=None, datarate=False)#

Downsample signal with anti-aliasing filter (Butterworth - 16th order). Works for signals with NaN values - replaced by zero. Can return also data-rate. Can take matrix where signals are stacked in 0th dim.

Parameters:
  • x (np ndarray) – shape[n_signal, n_sample], shape[n_sample] - can process multiple signals.

  • fs (float) – Signals fs

  • fs_new (float) – Downsample to fs_new

  • cutoff (float) – Elective cutoff freq. of anti-alias. filter. By default 2/3 of 0.5*fs_new

  • datarate (bool) – If return datarate of signals

Return type:

numpy ndarray / tuple

brainmaze_utils.signal.resample(x, fsamp_orig, fsamp_new)#

Resample a signal from an old sampling frequency to a new sampling frequency. Works with nans.

Parameters:
  • x (numpy.ndarray) – The input signal to be resampled.

  • fsamp_orig (float) – The original sampling frequency of the signal.

  • fsamp_new (float) – The new sampling frequency to resample the signal to.

Returns:

The resampled signal.

Return type:

numpy.ndarray

brainmaze_utils.signal.unify_sampling_frequency(x: list, sampling_frequency: list, fs_new=None) tuple#

Takes list of signals and list of frequencies and downsamples to the same sampling frequency. If all frequencies are same and fs_new is not specified, no operation performed. If not all frequencies are the same and fs_new is not specified, downsamples all signals on the lowest fs present in the list. If fs_new is specified, signals will be processed and downsampled on that frequency. If all sampling frequencies == fs_new, nothing is performed.

Parameters:
  • x (list) – list of numpy signals for downsampling

  • sampling_frequency (list) – for each signal

  • fs_new (float) – new sampling frequency

Return type:

tuple - (numpy ndarray, new_freq)