Generation of Tachogram from ECG
Difficulty Level:
Tags pre-process☁ecg☁tachogram

Like described on another Jupyter Notebook entitled "Event Detection - R Peaks (ECG)" , periodicity is one of the distinctive characteristics of electrocardiographic signals.

For studying periodicity it is necessary to detect a specific event in all the cardiac cycles. The QRS complex is the most prominent structure of ECG signal, being the event commonly chosen.

After detecting each R peak the time interval between them can be determined. With each time interval a time series may be generated, named Tachogram .

Essentially, the tachogram describes how the cardiac period changes along time and is the inverse of heart rate time series.

Here, it will be presented a simple way to generate a tachogram from the acquired data.


1 - Importation of the needed packages

In [1]:
# biosignalsnotebooks own package for loading and plotting the acquired data
import biosignalsnotebooks as bsnb

# Scientific packages
import numpy

2 - Load of acquired ECG data

In [2]:
# Load of data
data, header = bsnb.load("/signal_samples/ecg_4000_Hz.h5", get_header=True);

3 - Identification of mac address of the device and the channel used during acquisition

Both data and header are store in Python dictionaries , so, we must retrieve the data contained in those dictionaries.

In [3]:
# Data is in dictionary format and channel is one of the keys. This line gets the first key of the dictionary.
channel = list(data.keys())[0]

# The mac address of the acquiring device corresponds to the device name of the dictionary returned in the header, that was previously stored in a variable.
mac_address = str(header["device name"])
In [4]:
print ("Mac Address: " + mac_address + " Channel: " + str(channel))
Mac Address: b'00:07:80:D8:A7:F9' Channel: CH1

4 - Storage of sampling frequency and acquired data inside variables

In [5]:
# Sampling frequency of acquired data
fs = header["sampling rate"]

# Signal Samples
signal = data[channel]

# Generate the time axis of the signal given its sampling frequency
time = bsnb.generate_time(signal, fs)

5 - Detection of the time instants where R peaks are located

The detection of the R peaks is executed using the Pan-Tompkins algorithm , which is computationally inexpensive while offering high accuracy for the detection. This algorithm is implemented in the biosignalsnotebooks Python package, facilitating its application.

In [6]:
# R peak detection.
time_r_peaks, amp_r_peaks = bsnb.detect_r_peaks(signal, fs, time_units=True)

6 - Determination of the time between consecutive R peaks (cardiac cycle)

The tachogram corresponds to the derivative of the signal, i.e., to the interval duration of peaks over time, in order to get the variation of the interval duration of peaks over time (or the variation of the heartbeats period over time). The function diff , provided in numpy, computes the difference between successive data in an array. Thus, the tachogram can be computed by applying the diff function to the time of the detected peaks, which results in the interval between consecutive R peaks.

In [7]:
tachogram = numpy.diff(time_r_peaks)

# The tachogram time can be obtained by shifting each point of heartbeat duration to the center of the two corresponding peaks.
tachogram_time = (time_r_peaks[1:] + time_r_peaks[:-1]) / 2

7 - Representation of tachogram samples and highlighting of the respective cardiac cycle in the bottom plot

The next figures illustrate the tachogram and the corresponding ECG signal that originated it. The areas of the plots with different colors correspond to different heartbeats and indicate the interval considered to calculate the duration of each point in the tachogram.

In [8]:
list_figures = bsnb.plot_ecg_tachogram(time, signal, tachogram_time, tachogram, time_r_peaks)

This procedure can be automatically done by tachogram function in detect module of biosignalsnotebooks package .

In [9]:
tachogram_data, tachogram_time = bsnb.tachogram(signal, fs, signal=True, out_seconds=True)

As demonstrated in this Jupyter Notebook , the simplicity of generating a tachogram as a visualisation support to analyse cardiac events, namely, heart period over time, allows to access information that is not directly observable from the ECG signal, but that may still be relevant.

Using the biosignalsnotebooks Python package, the procedure can even be reduced to one line of code!

We hope that you have enjoyed this guide. biosignalsnotebooks is an environment in continuous expansion, so don"t stop your journey and learn more with the remaining Notebooks !

In [10]:
from biosignalsnotebooks.__notebook_support__ import css_style_apply
css_style_apply()
.................... CSS Style Applied to Jupyter Notebook .........................
Out[10]: