EEG Filters

Predefined filter.

class openbci_stream.utils.filters.Filter[source]

Generic filter.

__call__(eeg, axis=- 1, timestamp=None, fs=None, padtype='even', padlen=None, method='pad', irlen=None)[source]

Apply a digital filter forward and backward to a signal.

This function applies a linear digital filter twice, once forward and once backwards. The combined filter has zero phase and a filter order twice that of the original.

The function provides options for handling the edges of the signal.

Parameters
  • eeg (array) – Numpy array to filter.

  • axis (int, optional) – The axis of x to which the filter is applied. Default is None, in this case is selected automatically, with the larger dimension as time.

  • timestamp (array, optional.) – The time vector for calculate the sample frequency.

  • fs (float, optional.) – The sample frequency if timestamp is not defined.

  • padtype (str or None, optional) – Must be ‘odd’, ‘even’, ‘constant’, or None. This determines the type of extension to use for the padded signal to which the filter is applied. If padtype is None, no padding is used. The default is ‘odd’.

  • padlen (int or None, optional) – The number of elements by which to extend x at both ends of axis before applying the filter. This value must be less than x.shape[axis] - 1. padlen=0 implies no padding. The default value is 3 * max(len(a), len(b)).

  • method (str, optional) – Determines the method for handling the edges of the signal, either “pad” or “gust”. When method is “pad”, the signal is padded; the type of padding is determined by padtype and padlen, and irlen is ignored. When method is “gust”, Gustafsson’s method is used, and padtype and padlen are ignored.

  • irlen (int or None, optional) – When method is “gust”, irlen specifies the length of the impulse response of the filter. If irlen is None, no part of the impulse response is ignored. For a long signal, specifying irlen can significantly improve the performance of the filter.

Returns

y – The filtered output with the same shape as x.

Return type

ndarray

See also

sosfiltfilt, lfilter_zi, lfilter, lfiltic, savgol_filter, sosfilt

Notes

When method is “pad”, the function pads the data along the given axis in one of three ways: odd, even or constant. The odd and even extensions have the corresponding symmetry about the end point of the data. The constant extension extends the data with the values at the end points. On both the forward and backward passes, the initial condition of the filter is found by using lfilter_zi and scaling it by the end point of the extended data.

When method is “gust”, Gustafsson’s method [1]_ is used. Initial conditions are chosen for the forward and backward passes so that the forward-backward filter gives the same result as the backward-forward filter.

The option to use Gustaffson’s method was added in scipy version 0.16.0.

_fit_fs(eeg=None, timestamp=None, fs=None)[source]

Compile filters for new sample frequency.

class openbci_stream.utils.filters.FiltersSet(*filters)[source]

Join a set of filters and call one after the other.

class openbci_stream.utils.filters.GenericButterBand(f0, f1, fs=250, N=5)[source]

Generic bandpass

__init__(f0, f1, fs=250, N=5)[source]

Butterworth digital and analog filter design.

Design an Nth-order digital or analog Butterworth filter and return the filter coefficients.

Parameters
  • f0 (float) – Low cutoff frequency.

  • f1 (float) – Hight cutoff frequency.

  • fs (float) – Sample frequency.

  • N (int) – The order of the filter.

_fit(fs)[source]

Get the numerator and denominator of new sample frequency.

Parameters

fs (float, optional) – The sampling frequency of the digital system.

Returns

  • b, a (ndarray, ndarray) – Numerator (b) and denominator (a) polynomials of the IIR filter. Only returned if output='ba'.

  • z, p, k (ndarray, ndarray, float) – Zeros, poles, and system gain of the IIR filter transfer function. Only returned if output='zpk'.

  • sos (ndarray) – Second-order sections representation of the IIR filter. Only returned if output=='sos'.

class openbci_stream.utils.filters.GenericNotch(f0, fs=250, Q=3)[source]

Neneric notch.

__init__(f0, fs=250, Q=3)[source]

Design second-order IIR notch digital filter.

A notch filter is a band-stop filter with a narrow bandwidth (high quality factor). It rejects a narrow frequency band and leaves the rest of the spectrum little changed.

Parameters
  • f0 (float) – Frequency to remove from a signal. If fs is specified, this is in the same units as fs. By default, it is a normalized scalar that must satisfy 0 < w0 < 1, with w0 = 1 corresponding to half of the sampling frequency.

  • Q (float) – Quality factor. Dimensionless parameter that characterizes notch filter -3 dB bandwidth bw relative to its center frequency, Q = w0/bw.

  • fs (float, optional) – The sampling frequency of the digital system.

_fit(fs)[source]

Get the numerator and denominator of new sample frequency.

Parameters

fs (float) – The sampling frequency of the digital system.

Returns

b, a – Numerator (b) and denominator (a) polynomials of the IIR filter.

Return type

ndarray, ndarray

openbci_stream.utils.filters.compile_filters(FS, N, Q)[source]

Compile filter.

All filters must be setted for a specified frequnecy sample. Since this driver recommend the calculation of sample rate each time the filters must be generated before to use them.