In order to create an algorithm that is able to accurately extract HRV and use it in a way which is useful to a clinician, the signal processing is broken down into the following steps:
- Obtaining ECG data from archives
- Using this data to plot a typical ECG waveform
- Removal of noise
- Detection of the R wave peak
- Extrapolation of R wave peak to HRV in the time domain
- Linking this is changes in physiological signal to predict medical events
More steps may be added through the process depending on what is found at each stage.
The team will be using Python to accomplish this.
Evolution of our Code
A more detailed guide to the steps included in the code can be seen in the diagram below.
Opening a .csv File
Opening a .csv file and printing the ECG data, https://github.com/cay15/hrv/blob/master/52.py The ECG dataset which was used to produce these plots is the MIT-BIH Normal Sinus Rhythm Sample No.16265.
Resampling our input file to 1000Hz, so that it is consistent over different inputs, https://github.com/cay15/hrv/blob/master/upsample_ecg.py. This also splits the signal so a smaller, more manageable chunk is passed to the other modules. The ECG dataset which was used to produce these plots is the MIT-BIH Normal Sinus Rhythm Sample No.16265 .
Creating an artificial ECG to test out the filtering and peak detection modules, https://github.com/cay15/hrv/blob/master/fakeecg.py. The ECG QRS segment is modelled by a Daubechies wavelet, while the rest of the wave is given by an interval of rest. Gaussian noise and sinusoids of various frequencies are added to simulate mains noise, electromyogram noise, and baseline offset.
Testing out filtering methods including a notch filter, high pass and low pass, https://github.com/cay15/hrv/blob/master/filter.py . The notch filter is designed to remove mains interference at 50Hz. The low pass filter is designed to remove electromyogram noise and other high-frequency noise with a cutoff of 150Hz. The high pass filter is designed to remove baseline drift and other low-frequency noise with a cutoff of 0.5Hz.
R Peak Detection
- Peak detection module which finds peaks based on whether surrounding samples have greater amplitudes than it, mirrors these peaks, for the case where peaks are inverted from medical conditions or lead placement. It then uses boundaries which can be specified to rule out peaks that are too close together by specifying a minimum amplitude, https://github.com/cay15/hrv/blob/master/peak_detection.py.
The application of boundary conditions is not perfect and needs tweaks in order to be as accurate as possible.
These functions have been combined to one code which takes in the .csv file, resamples it, removes noise and detects the peaks, https://github.com/cay15/hrv/blob/master/main.py
The previous code has been amended to allow more user input in terms of deciding whether to sample an artificial ECG or a raw .csv file and if it is a raw file, which lead of the ECG to process, https://github.com/cay15/hrv/blob/master/main12.py.
Results for Normal ECG
Results for Arrythmia
Results for Noise Stress Test
Results for Artificial ECG
- Goldberger AL, Amaral LAN, Glass L, Hausdorff JM, Ivanov PCh, Mark RG, Mietus JE, Moody GB, Peng C-K, Stanley HE. PhysioBank, PhysioToolkit, and PhysioNet: Components of a New Research Resource for Complex Physiologic Signals. Circulation 101(23):e215-e220 [Circulation Electronic Pages; http://circ.ahajournals.org/content/101/23/e215.full]; 2000 (June 13).
- T. A. L. The Beth Israel Deaconess Medical Center, ‘The MIT-BIH Normal Sinus Rhythm Database’. physionet.org, 1990, doi: 10.13026/C2NK5R.
- S. D. Greenwald, ‘The MIT-BIH Malignant Ventricular Arrhythmia Database’. physionet.org, 1992, doi: 10.13026/C22P44.