Preprocessing

heartpy.preprocessing.scale_data(data, lower=0, upper=1024)[source]

scales passed sequence between thresholds

Function that scales passed data so that it has specified lower and upper bounds.

Parameters:
  • data (1-d array or list) – Sequence to be scaled
  • lower (int or float) – lower threshold for scaling default : 0
  • upper (int or float) – upper threshold for scaling default : 1024
Returns:

out – contains scaled data

Return type:

1-d array

Examples

When passing data without further arguments to the function means it scales 0-1024

>>> x = [2, 3, 4, 5]
>>> scale_data(x)
array([   0.        ,  341.33333333,  682.66666667, 1024.        ])

Or you can specify a range:

>>> scale_data(x, lower = 50, upper = 124)
array([ 50.        ,  74.66666667,  99.33333333, 124.        ])
heartpy.preprocessing.scale_sections(data, sample_rate, windowsize=2.5, lower=0, upper=1024)[source]

scales data using sliding window approach

Function that scales the data within the defined sliding window between the defined lower and upper bounds.

Parameters:
  • data (1-d array or list) – Sequence to be scaled
  • sample_rate (int or float) – Sample rate of the passed signal
  • windowsize (int or float) – size of the window within which signal is scaled, in seconds default : 2.5
  • lower (int or float) – lower threshold for scaling. Passed to scale_data. default : 0
  • upper (int or float) – upper threshold for scaling. Passed to scale_data. default : 1024
Returns:

out – contains scaled data

Return type:

1-d array

Examples

>>> x = [20, 30, 20, 30, 70, 80, 20, 30, 20, 30]
>>> scale_sections(x, sample_rate=1, windowsize=2, lower=20, upper=30)
array([20., 30., 20., 30., 20., 30., 20., 30., 20., 30.])
heartpy.preprocessing.enhance_peaks(hrdata, iterations=2)[source]

enhances peak amplitude relative to rest of signal

Function thta attempts to enhance the signal-noise ratio by accentuating the highest peaks. Note: denoise first

Parameters:
  • hrdata (1-d numpy array or list) – sequence containing heart rate data
  • iterations (int) – the number of scaling steps to perform default : 2
Returns:

out – array containing enhanced peaks

Return type:

1-d numpy array

Examples

Given an array of data, the peaks can be enhanced using the function

>>> x = [200, 300, 500, 900, 500, 300, 200]
>>> enhance_peaks(x)
array([   0.        ,    4.31776016,   76.16528926, 1024.        ,
         76.16528926,    4.31776016,    0.        ])
heartpy.preprocessing.enhance_ecg_peaks(hrdata, sample_rate, iterations=4, aggregation='mean', notch_filter=True)[source]

enhances ECG peaks

Function that convolves synthetic QRS templates with the signal, leading to a strong increase signal-to-noise ratio. Function ends with an optional Notch filterstep (default : true) to reduce noise from the iterating convolution steps.

Parameters:
  • hrdata (1-d numpy array or list) – sequence containing heart rate data
  • sample_rate (int or float) – sample rate with which the data is sampled
  • iterations (int) – how many convolutional iterations should be run. More will result in stronger peak enhancement, but over a certain point (usually between 12-16) overtones start appearing in the signal. Only increase this if the peaks aren’t amplified enough. default : 4
  • aggregation (str) – how the data from the different convolutions should be aggregated. Can be either ‘mean’ or ‘median’. default : mean
  • notch_filter (bool) – whether to apply a notch filter after the last convolution to get rid of remaining low frequency noise. default : true
Returns:

output – The array containing the filtered data with enhanced peaks

Return type:

1d array

Examples

First let’s import the module and load the data

>>> import heartpy as hp
>>> data, timer = hp.load_exampledata(1)
>>> sample_rate = hp.get_samplerate_mstimer(timer)

After loading the data we call the function like so:

>>> filtered_data = enhance_ecg_peaks(data, sample_rate, iterations = 3)

By default the module uses the mean to aggregate convolutional outputs. It is also possible to use the median.

>>> filtered_data = enhance_ecg_peaks(data, sample_rate, iterations = 3,
... aggregation = 'median', notch_filter = False)

In the last example we also disabled the notch filter.

heartpy.preprocessing.interpolate_clipping(data, sample_rate, threshold=1020)[source]

interpolate peak waveform

Function that interpolates peaks between the clipping segments using cubic spline interpolation. It takes the clipping start +/- 100ms to calculate the spline.

Parameters:
  • data (1d list or numpy array) – data section to be evaluated
  • sample_rate (int or float) – sample rate with which the data array is sampled
  • threshold (int or float) – the threshold for clipping, recommended to be a few data points below ADC or sensor max value, to compensate for signal noise default : 1020
Returns:

out – the output is an array with clipping segments replaced by interpolated segments

Return type:

array

Examples

First let’s load some example data:

>>> import heartpy as hp
>>> data, _ = hp.load_exampledata(example=2)
>>> x = data[2000:3000]
>>> x[425:445]
array([948, 977, 977, 977, 977, 978, 978, 977, 978, 977, 977, 977, 977,
       914, 820, 722, 627, 536, 460, 394])

And interpolate any clipping segments as such:

>>> intp = interpolate_clipping(x, sample_rate=117, threshold=970)
>>> intp[425:445]
array([ 972, 1043, 1098, 1138, 1163, 1174, 1173, 1159, 1134, 1098, 1053,
        998,  934,  848,  747,  646,  552,  470,  402,  348])
heartpy.preprocessing.flip_signal(data, enhancepeaks=False, keep_range=True)[source]

invert signal waveforms.

Function that flips raw signal with negative mV peaks to normal ECG. Required for proper peak finding in case peaks are expressed as negative dips.

Parameters:
  • data (1d list or numpy array) – data section to be evaluated
  • enhance_peaks (bool) – whether to apply peak accentuation default : False
  • keep_range (bool) – whether to scale the inverted data so that the original range is maintained
Returns:

out

Return type:

1d array

Examples

Given an array of data

>>> x = [200, 300, 500, 900, 500, 300, 200]

We can call the function. If keep_range is False, the signal will be inverted relative to its mean.

>>> flip_signal(x, keep_range=False)
array([628.57142857, 528.57142857, 328.57142857, -71.42857143,
       328.57142857, 528.57142857, 628.57142857])

However, by specifying keep_range, the inverted signal will be put ‘back in place’ in its original range.

>>> flip_signal(x, keep_range=True)
array([900., 800., 600., 200., 600., 800., 900.])

It’s also possible to use the enhance_peaks function:

>>> flip_signal(x, enhancepeaks=True)
array([1024.        ,  621.75746332,  176.85545623,    0.        ,
        176.85545623,  621.75746332, 1024.        ])