Functions for data filtering tasks.
filter_signal(data, cutoff, sample_rate, order=2, filtertype='lowpass', return_top=False)¶
Apply the specified filter
Function that applies the specified lowpass, highpass or bandpass filter to the provided dataset.
- data (1-dimensional numpy array or list) – Sequence containing the to be filtered data
- cutoff (int, float or tuple) – the cutoff frequency of the filter. Expects float for low and high types and for bandpass filter expects list or array of format [lower_bound, higher_bound]
- sample_rate (int or float) – the sample rate with which the passed data sequence was sampled
- order (int) – the filter order default : 2
- filtertype (str) – The type of filter to use. Available: - lowpass : a lowpass butterworth filter - highpass : a highpass butterworth filter - bandpass : a bandpass butterworth filter - notch : a notch filter around specified frequency range both the highpass and notch filter are useful for removing baseline wander. The notch filter is especially useful for removing baseling wander in ECG signals.
out – 1d array containing the filtered data
>>> import numpy as np >>> import heartpy as hp
Using standard data provided
>>> data, _ = hp.load_exampledata(0)
We can filter the signal, for example with a lowpass cutting out all frequencies of 5Hz and greater (with a sloping frequency cutoff)
>>> filtered = filter_signal(data, cutoff = 5, sample_rate = 100.0, order = 3, filtertype='lowpass') >>> print(np.around(filtered[0:6], 3)) [530.175 517.893 505.768 494.002 482.789 472.315]
Or we can cut out all frequencies below 0.75Hz with a highpass filter:
>>> filtered = filter_signal(data, cutoff = 0.75, sample_rate = 100.0, order = 3, filtertype='highpass') >>> print(np.around(filtered[0:6], 3)) [-17.975 -28.271 -38.609 -48.992 -58.422 -67.902]
Or specify a range (here: 0.75 - 3.5Hz), outside of which all frequencies are cut out.
>>> filtered = filter_signal(data, cutoff = [0.75, 3.5], sample_rate = 100.0, ... order = 3, filtertype='bandpass') >>> print(np.around(filtered[0:6], 3)) [-12.012 -23.159 -34.261 -45.12 -55.541 -65.336]
A ‘Notch’ filtertype is also available (see remove_baseline_wander).
>>> filtered = filter_signal(data, cutoff = 0.05, sample_rate = 100.0, filtertype='notch')
Finally we can use the return_top flag to only return the filter response that has amplitute above zero. We’re only interested in the peaks, and sometimes this can improve peak prediction:
>>> filtered = filter_signal(data, cutoff = [0.75, 3.5], sample_rate = 100.0, ... order = 3, filtertype='bandpass', return_top = True) >>> print(np.around(filtered[48:53], 3)) [ 0. 0. 0.409 17.088 35.673]
Detect outliers based on hampel filter
Funcion that detects outliers based on a hampel filter. The filter takes datapoint and six surrounding samples. Detect outliers based on being more than 3std from window mean. See: https://www.mathworks.com/help/signal/ref/hampel.html
- data (1d list or array) – list or array containing the data to be filtered
- filtsize (int) – the filter size expressed the number of datapoints taken surrounding the analysed datapoint. a filtsize of 6 means three datapoints on each side are taken. total filtersize is thus filtsize + 1 (datapoint evaluated)
array containing filtered data
>>> from .datautils import get_data, load_exampledata >>> data, _ = load_exampledata(0) >>> filtered = hampel_filter(data, filtsize = 6) >>> print('%i, %i' %(data, filtered)) 497, 496
apply altered version of hampel filter to suppress noise.
Function that returns te difference between data and 1-second windowed hampel median filter. Results in strong noise suppression characteristics, but relatively expensive to compute.
Result on output measures is present but generally not large. However, use sparingly, and only when other means have been exhausted.
- data (1d numpy array) – array containing the data to be filtered
- sample_rate (int or float) – sample rate with which data was recorded
out – array containing filtered data
1d numpy array
>>> from .datautils import get_data, load_exampledata >>> data, _ = load_exampledata(1) >>> filtered = hampel_correcter(data, sample_rate = 116.995)
smooth_signal(data, sample_rate, window_length=None, polyorder=3)¶
smooths given signal using savitzky-golay filter
Function that smooths data using savitzky-golay filter using default settings.
Functionality requested by Eirik Svendsen. Added since 1.2.4
- data (1d array or list) – array or list containing the data to be filtered
- sample_rate (int or float) – the sample rate with which data is sampled
- window_length (int or None) – window length parameter for savitzky-golay filter, see Scipy.signal.savgol_filter docs. Must be odd, if an even int is given, one will be added to make it uneven. default : 0.1 * sample_rate
- polyorder (int) – the order of the polynomial fitted to the signal. See scipy.signal.savgol_filter docs. default : 3
smoothed – array containing the smoothed data
Given a fictional signal, a smoothed signal can be obtained by smooth_signal():
>>> x = [1, 3, 4, 5, 6, 7, 5, 3, 1, 1] >>> smoothed = smooth_signal(x, sample_rate = 2, window_length=4, polyorder=2) >>> np.around(smoothed[0:4], 3) array([1.114, 2.743, 4.086, 5. ])
If you don’t specify the window_length, it is computed to be 10% of the sample rate (+1 if needed to make odd) >>> import heartpy as hp >>> data, timer = hp.load_exampledata(0) >>> smoothed = smooth_signal(data, sample_rate = 100)