plot ECG from .mat file

Viewed 38

I'm new to python, how we can plot an ECG signal from a .mat file in python?

Code:

import matplotlib.pyplot as plt
import ecg_plot
import scipy.io as sio
import numpy as np
from scipy.misc import electrocardiogram
ecg=sio.loadmat('/content/hc001.mat')
print(ecg)

Answer:

{'val': array([[ 21,  22,  24, ..., 236, 174,  84]], dtype=int16)}
2 Answers

This is one fairly easy method using the ecg_plot package:

Install in terminal: pip install ecg_plot

Then in Python:

import ecg_plot
    
ecg=sio.loadmat('/content/hc001.mat')
ecg_plot.plot(ecg, sample_rate = 500)
ecg_plot.show()

For the following example, I have downloaded the ECG mat files from here.

You can plot your ECG data with the commonly used matplotlib library in the following way:

import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio

ecg = sio.loadmat('100m (0).mat')['val'].reshape((-1))

fs = 360
time = np.arange(ecg.size) / fs
plt.plot(time, ecg)
plt.xlabel("time in s")
plt.ylabel("ECG in mV")
plt.show()

enter image description here

For further details, take a look at this example from scipy.

Related