#!/usr/bin/env python # coding: utf-8 # # ECG-Derived Respiration (EDR) Analysis # ECG-derived respiration (EDR) is the extraction of respiratory information from ECG and is a noninvasive method to monitor respiration activity under instances when respiratory signals are not recorded. In clinical settings, this presents convenience as it allows the monitoring of cardiac and respiratory signals simultaneously from a recorded ECG signal. This example shows how to use Neurokit to perform EDR analysis. # # This example can be referenced by [citing the package](https://github.com/neuropsychology/NeuroKit#citation). # In[2]: # Load NeuroKit and other useful packages import neurokit2 as nk import numpy as np import pandas as pd import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # In[3]: plt.rcParams['figure.figsize'] = [15, 5] # Bigger images # ## Download ECG Data # In this example, we will download a dataset containing an ECG signal sampled at 1000 Hz. # In[3]: # Get data ecg = np.array(pd.read_csv("https://raw.githubusercontent.com/neuropsychology/NeuroKit/dev/data/ecg_1000hz.csv"))[:, 1] # Visualize signal nk.signal_plot(ecg) # ## Extraction of ECG Features # Now you can extract the R peaks of the signal using `ecg_peaks()` and compute the heart period using `ecg_rate()`. # *Note: As the dataset has a frequency of 1000Hz, make sure the `sampling_rate` is also set to 1000Hz. It is critical that you specify the correct sampling rate of your signal throughout all the processing functions.* # In[4]: # Extract peaks rpeaks, info = nk.ecg_peaks(ecg, sampling_rate=1000) # Compute rate ecg_rate = nk.ecg_rate(rpeaks, sampling_rate=1000, desired_length=len(ecg)) # ## Analyse EDR # Now that we have an array of the heart period, we can then input this into `ecg_rsp()` to extract the EDR. # # Default method is by `Van Gent et al. 2019` ; see the full reference in documentation (run : `nk.ecg_rsp?`) # In[5]: edr = nk.ecg_rsp(ecg_rate, sampling_rate=1000) # Visual comparison nk.signal_plot(edr) # The function `ecg_rsp()` incorporates different methods to compute EDR. For a visual comparison of the different methods, we can create a dataframe of EDR columns each of which are produced by different methods, and then plot it, like so: # In[16]: edr_df = pd.DataFrame({"Van Gent et al.": nk.ecg_rsp(ecg_rate, sampling_rate=1000), "Charlton et al." : nk.ecg_rsp(ecg_rate, sampling_rate=1000, method="charlton2016"), "Soni et al.": nk.ecg_rsp(ecg_rate, sampling_rate=1000, method="soni2019"), "Sarkar et al.": nk.ecg_rsp(ecg_rate, sampling_rate=1000, method="sarkar2015")}) # In[17]: nk.signal_plot(edr_df, subplots=True)