Signal

Signal

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

class best.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)
best.signal.PSD(x: numpy.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

best.signal.buffer(x: numpy.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

best.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

Returns

Return type

numpy ndarray / tuple

best.signal.detrend(y, x=None, y2=None)
best.signal.fft_filter(X: numpy.ndarray, fs: float, cutoff: float, type: str = 'lp')

FFT filter

Parameters
  • X (numpy.ndarray) –

  • fs (float) –

  • cutoff (float) –

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

Returns

Return type

numpy.ndarray

best.signal.find_peaks(y)
best.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.

best.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

Returns

Return type

numpy ndarray / tuple

best.signal.resample(x, fsamp_old, fsamp_new)
Parameters
  • x – input signal

  • fsamp_old – original sampling frequency

  • fsamp_new – new sampling frequency

Returns

interpolated signal at required sampling positions

best.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

Returns

Return type

tuple - (numpy ndarray, new_freq)