Selected Reading

SciPy - electrocardiogram() Method



The SciPy electrocardiogram() method is a part of scipy.misc module which represent the signal processing along with analysis. This is used to represent the electrical activity of the heart. In brief, we can say this method as ECG which helps to build the medical devices and equipment.

Syntax

Following is the syntax of the SciPy electrocardiogram() Method −

electrocardiogram()

Parameters

This method does not take any parameter.

Return value

This method does not return any value.

Example 1

Following is the basic example of SciPy electrocardiogram() method that illustrate the plot of ECG signal.

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram

# load the ECG signal
ecg = electrocardiogram()

# Plot the ECG signal
plt.plot(ecg)
plt.title('Electrocardiogram (ECG) Signal')
plt.xlabel('Number')
plt.ylabel('Amplitude')
plt.show()

Output

The above code produces the following output −

scipy_electrocardium_method_one

Example 2

Here, you can see the demonstration of low-pass filter in the ECG signal using the functions butter and filtfilt from the module scipy.signal. After filtering, it removes the noise(high frequency) and plots two signals in parallel mode.

import numpy as np
from scipy.signal import butter, filtfilt
from scipy.misc import electrocardiogram

# load the ECG signal
ecg = electrocardiogram()

# design a low-pass filter
fs = 360  # Sampling frequency (assumed)
cutoff = 50  # Desired cutoff frequency of the filter, Hz
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(5, normal_cutoff, btype='low', analog=False)

# apply the filter to the ECG signal
filtered_ecg = filtfilt(b, a, ecg)

# Plot the original and filtered ECG signals
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 6))
plt.plot(ecg, label='Original ECG')
plt.plot(filtered_ecg, label='Filtered ECG', linestyle='--')
plt.title('Low-pass Filtered Electrocardiogram (ECG) Signal')
plt.xlabel('Number')
plt.ylabel('Amplitude')
plt.legend()
plt.show()

Output

The above code produces the following output −

scipy_electrocardium_method_two

Example 3

from scipy.signal import find_peaks
from scipy.misc import electrocardiogram
import matplotlib.pyplot as plt

# Load the ECG signal
ecg = electrocardiogram()

# Detect R-peaks in the ECG signal
peaks, _ = find_peaks(ecg, distance=150)

# Plot the ECG signal with detected R-peaks
plt.plot(ecg, label='ECG Signal')
plt.plot(peaks, ecg[peaks], "x", label='R-peaks')
plt.title('R-peak Detection in Electrocardiogram (ECG) Signal')
plt.xlabel('Number')
plt.ylabel('Amplitude')
plt.legend()
plt.show()

Output

The above code produces the following output −

scipy_electrocardium_method_three
scipy_reference.htm
Advertisements