Locate P, Q, S and T waves in ECG#

This example can be referenced by citing the package.

This example shows how to use NeuroKit to delineate the ECG peaks in Python using NeuroKit. This means detecting and locating all components of the QRS complex, including P-peaks and T-peaks, as well their onsets and offsets from an ECG signal.

# Load NeuroKit and other useful packages
import neurokit2 as nk
import numpy as np
import pandas as pd

In this example, we will use a short segment of ECG signal with sampling rate of 3000 Hz.

Find the R peaks#

# Retrieve ECG data from data folder
ecg_signal = nk.data(dataset="ecg_1000hz")
# Extract R-peaks locations
_, rpeaks = nk.ecg_peaks(ecg_signal, sampling_rate=1000)

The ecg_peaks() function will return a dictionary contains the samples at which R-peaks are located.

Let’s visualize the R-peaks location in the signal to make sure it got detected correctly.

# Visualize R-peaks in ECG signal
plot = nk.events_plot(rpeaks['ECG_R_Peaks'], ecg_signal)
../../_images/cf854beead3f241822cb01a1e2ebe95f77ba2b2ee6c279525bdd77e973dbabeb.png
# Zooming into the first 5 R-peaks
plot = nk.events_plot(rpeaks['ECG_R_Peaks'][:5], ecg_signal[:6000])
../../_images/01c1a067ae172211c3cd06bab0547b441f387c9ebf1478249ee273c8e8a6168e.png

Visually, the R-peaks seem to have been correctly identified. You can also explore searching for R-peaks using different methods provided by NeuroKit ecg_peaks().

Locate other waves (P, Q, S, T) and their onset and offset#

In ecg_delineate(), NeuroKit implements different methods to segment the QRS complexes. There are the derivative method and the other methods that make use of Wavelet to delineate the complexes.

Peak method#

First, let’s take a look at the ‘peak’ method and its output.

# Delineate the ECG signal
_, waves_peak = nk.ecg_delineate(ecg_signal, rpeaks, sampling_rate=1000, method="peak")
# Zooming into the first 3 R-peaks, with focus on T_peaks, P-peaks, Q-peaks and S-peaks
plot = nk.events_plot([waves_peak['ECG_T_Peaks'][:3], 
                       waves_peak['ECG_P_Peaks'][:3],
                       waves_peak['ECG_Q_Peaks'][:3],
                       waves_peak['ECG_S_Peaks'][:3]], ecg_signal[:4000])
../../_images/d4d0c54474b089f98c0fc4940b96f174ba959122225d5f2f950da1bce7735aff.png

Visually, the ‘peak’ method seems to have correctly identified the P-peaks, Q-peaks, S-peaks and T-peaks for this signal, at least, for the first few complexes. Well done, peak!

However, it can be quite tiring to be zooming in to each complex and inspect them one by one. To have a better overview of all complexes at once, you can make use of the show argument in ecg_delineate() as below.

# Delineate the ECG signal and visualizing all peaks of ECG complexes
_, waves_peak = nk.ecg_delineate(ecg_signal, 
                                 rpeaks, 
                                 sampling_rate=1000, 
                                 method="peak", 
                                 show=True, 
                                 show_type='peaks')
../../_images/f9baffb54eac82946f530bae16c8678d6c84cee00f285370173e99038499898d.png

The ‘peak’ method is doing a glamorous job with identifying all the ECG peaks for this piece of ECG signal.

On top of the above peaks, the peak method also identify the wave boundaries, namely the onset of P-peaks and offset of T-peaks. You can vary the argument show_type to specify the information you would like plot.

Let’s visualize them below:

# Delineate the ECG signal and visualizing all P-peaks boundaries
signal_peak, waves_peak = nk.ecg_delineate(ecg_signal, 
                                           rpeaks, 
                                           sampling_rate=1000,
                                           method="peak", 
                                           show=True, 
                                           show_type='bounds_P')
../../_images/7e26da04027ed61475a2517bbfd04ac3e5d52af456938614822cbf2114dec1b8.png
# Delineate the ECG signal and visualizing all T-peaks boundaries
signal_peaj, waves_peak = nk.ecg_delineate(ecg_signal, 
                                           rpeaks, 
                                           sampling_rate=1000, 
                                           method="peak", 
                                           show=True, 
                                           show_type='bounds_T')
../../_images/bd2b06c1c204bd95779747fd03b2630b0dc7b6ed68ffda2559619d4b1a405be6.png

Both the onsets of P-peaks and the offsets of T-peaks appears to have been correctly identified here. This information will be used to delineate cardiac phases in ecg_phase().

Let’s next take a look at the continuous wavelet method.

Continuous Wavelet Method (CWT)#

# Delineate the ECG signal
signal_cwt, waves_cwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=1000, 
                                         method="cwt", 
                                         show=True, 
                                         show_type='all')
../../_images/4e87bf972fc6e8984e38b864f9ffa620b5ef5a085170c1a3ab9b03cdf2d3723a.png

By specifying ‘all’ in the show_type argument, you can plot all delineated information output by the cwt method. However, it could be hard to evaluate the accuracy of the delineated information with everyhing plotted together. Let’s tease them apart!

# Visualize P-peaks and T-peaks
signal_cwt, waves_cwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=1000, 
                                         method="cwt", 
                                         show=True, 
                                         show_type='peaks')
../../_images/4438c3c179ede147cc0ece321707f4708f1527b800de39e7486fce4177f671b8.png
# Visualize T-waves boundaries
signal_cwt, waves_cwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=1000, 
                                         method="cwt", 
                                         show=True, 
                                         show_type='bounds_T')
../../_images/bb4ccef9f91ea7633fb583f8e7b92fdb38ce300abf3a2dcf57a16f86f8cfbc0c.png
# Visualize P-waves boundaries
signal_cwt, waves_cwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=1000, 
                                         method="cwt", 
                                         show=True, 
                                         show_type='bounds_P')
../../_images/53a4c6985c5200b81c16f1f61e20bca501cf63c94bb1044ed8e468c9131f59d7.png
# Visualize R-waves boundaries
signal_cwt, waves_cwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=1000, 
                                         method="cwt", 
                                         show=True, 
                                         show_type='bounds_R')
../../_images/652d9012006311d3f1f4faf0806c6a5e56fa2bde8b8db7a8990ab8f3e77d58eb.png

Unlike the peak method, the continuous wavelet method does not idenfity the Q-peaks and S-peaks. However, it provides more information regarding the boundaries of the waves

Visually, except a few exception, CWT method is doing a great job. However, the P-waves boundaries are not very clearly identified here.

Last but not least, we will look at the third method in NeuroKit ecg_delineate() function: the discrete wavelet method.

Discrete Wavelet Method (DWT) - default method#

# Delineate the ECG signal
signal_dwt, waves_dwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=3000, 
                                         method="dwt", 
                                         show=True, 
                                         show_type='all')
../../_images/bb180658a7b7584b8a6b5087e1f3bb6832cbd38ceef8d3b1cd3101e1f53c55d3.png
# Visualize P-peaks and T-peaks
signal_dwt, waves_dwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=3000, 
                                         method="dwt", 
                                         show=True, 
                                         show_type='peaks')
../../_images/d68352513f4ca2dacfe5e80b4a49753b0409ff5951df40e899f965627cb71620.png
# visualize T-wave boundaries
signal_dwt, waves_dwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=3000, 
                                         method="dwt", 
                                         show=True, 
                                         show_type='bounds_T')
../../_images/e9aa9d83e3d067fa3d088c1f5413d607c2ea9e689ac973805eb5c4577313caad.png
# Visualize P-wave boundaries
signal_dwt, waves_dwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=3000, 
                                         method="dwt", 
                                         show=True, 
                                         show_type='bounds_P')
../../_images/aa1fd8ded73f8ffc6b3ef5e8f04a6eb72436a0c7382a40474c224af3a45f84a8.png
# Visualize R-wave boundaries
signal_dwt, waves_dwt = nk.ecg_delineate(ecg_signal, 
                                         rpeaks, 
                                         sampling_rate=3000, 
                                         method="dwt", 
                                         show=True, 
                                         show_type='bounds_R')
../../_images/7a428bd7eb19887bdde33c8043d29c1fd516d392def3532cdfacad118cd3d0fa.png

Visually, from the plots above, the delineated outputs of DWT appear to be more accurate than CWT, especially for the P-peaks and P-wave boundaries.

Overall, for this signal, the peak and DWT methods seem to be superior to the CWT.