Heartbeat detection
sleepecg.compare_heartbeats(detection, annotation, max_distance=0)
Determine correctness of detection results.
Determine true positives (TP), false positives (FP) and false negatives (FN) for an
array of detected heartbeat indices based on an array of annotated heartbeat indices.
Since neither annotations nor automated detectors usually hit the peak perfectly,
detected peaks no further than max_distance
in both directions from an annotated peak
are considered true positives.
Parameters:

detection
(ndarray
) –Detected heartbeat indices.

annotation
(ndarray
) –Annotated heartbeat indices.

max_distance
(int
) –Maximum distance between indices to consider as the same peak, by default
0
.
Returns:

TP
(ndarray
) –True positives, i.e. actual heartbeats detected as heartbeats.

FP
(ndarray
) –False positives, i.e. nonheartbeats detected as heartbeats.

FN
(ndarray
) –False negatives, i.e. actual heartbeats not detected as heartbeats.
Source code in sleepecg/heartbeats.py
sleepecg.detect_heartbeats(ecg, fs, backend='c')
Detect heartbeats in an ECG signal.
This is a modified version of the beat detection algorithm using adaptive thresholds described by Pan & Tompkins in 1985.
Modifications/additions to the original algorithm are:
 Instead of a hardware filter adjusted to the sampling frequency of the MITBIH
Arrhythmia Database, a second order bandpass with cutoff frequencies 5 and 30 Hz
created via
scipy.signal.butter()
is used.  A bidirectional filter is used to remove filter delay.
 The integration window is centered on the filtered signal, i.e. a peak in the filtered signal corresponds to a plateau in the integrated signal, not a saddle in the rising edge. This lets the adaptive threshold for the integrated signal remain at a higher level, which is less susceptible to noise.
 Learning phase 1 is not described in detail in the original paper. This implementation
uses maximum and mean values inside the first two seconds to initialize
SPKI/SPKF/NPKI/NPKF. Details are provided in the
_thresholding
code.  In addition to the original searchback criterion, a searchback is also performed if no peak is found during the first second of the signal or no second peak is found 1.5 s after the first one. This ensures correct behaviour at signal start in case an unusually large peak during learning phase 1 messes up threshold initialization.
 After an unsuccessful searchback, the procedure is repeated in the same interval with further reduced thresholds, up to 16 times.
Parameters:

ecg
(ndarray
) –ECG signal. Note that the unit of the data does not matter (the algorithm will return similar results regardless of the scaling of the data).

fs
(float
) –Sampling frequency in Hz. For best results, a sampling frequency of at least 100 Hz is recommended.

backend
((c, numba, python)
) –Which implementation of the squared moving integration and thresholding algorithm to use. If available,
'c'
is the fastest implementation,'numba'
is about 25% slower, and'python'
is about 20 times slower but provided as a fallback. By default'c'
.
Returns:

ndarray
–Indices of detected heartbeats.
Examples:
Detect heartbeats in a short electrocardiogram:
>>> from sleepecg import detect_heartbeats, get_toy_ecg
>>> ecg, fs = get_toy_ecg() # 5 min of ECG data at 360 Hz
>>> heartbeats = detect_heartbeats(ecg, fs)
>>> print(f"{len(heartbeats)} heartbeats detected")
478 heartbeats detected
Source code in sleepecg/heartbeats.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 

sleepecg.rri_similarity(detection, annotation, fs_resample=4)
Calculate measures of similarity between RR intervals.
RR intervals are calculated from detected and annotated heartbeat indices. The RR time
series is then resampled to frequency fs_resample
in the timespan common to both
detection and annotation. Pearson's and Spearman's correlation coefficients as well as
the root mean square error are returned.
Parameters:

detection
(ndarray
) –Detected heartbeat indices.

annotation
(ndarray
) –Annotated heartbeat indices.
Returns:

pearsonr
(float
) –Pearson correlation coefficient between resampled RR time series.

spearmanr
(float
) –Spearman correlation coefficient between resampled RR time series.

rmse
(float
) –Root mean square error between resampled RR time series.
Source code in sleepecg/heartbeats.py
sleepecg.get_toy_ecg()
Load a 5 minute long electrocardigram sampled at 360 Hz.
Data taken from scipy.datasets.electrocardiogram: https://docs.scipy.org/doc/scipy/reference/generated/scipy.datasets.electrocardiogram.html
Returns: