Scipy – Display electrocardiogram
Last Updated :
17 Feb, 2022
Electrocardiography is the practice of using electrodes mounted on the skin to produce an electrocardiogram, which is a graph of voltage versus time of the electrical activity of the heart.
Applications:
- Heartbeat analysis
- Cancer treatment
In python using scipy we can generate electrocardiogram by using scipy.misc.electrocardiogram()
It is used to load an electrocardiogram and will return only 1-D signal.
The signal which is returned is a 5-minute-long electrocardiogram (ECG), which is a medical recording of the heart’s electrical activity, it basically returns an n -Dimensional array.
Approach:
- Import numpy and scipy modules.
- Create an ecg model.
- Calculate time data with frequency.
- Display the graph.
Implementations:
Python
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
import numpy as np
ecg = electrocardiogram()
frequency = 360
time_data = np.arange(ecg.size) / frequency
plt.plot(time_data, ecg)
plt.xlabel( "time in seconds" )
plt.ylabel( "ECG in milli Volts" )
plt.show()
|
Output:
- Change the x, y limits for clarity visualization.
Python3
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
import numpy as np
ecg = electrocardiogram()
frequency = 360
time_data = np.arange(ecg.size) / frequency
plt.plot(time_data, ecg)
plt.xlabel( "time in seconds" )
plt.ylabel( "ECG in milli Volts" )
plt.xlim( 9 , 10.2 )
plt.ylim( - 1 , 1.5 )
plt.show()
|
Output:
Output:
[-0.245 -0.215 -0.185 ... -0.405 -0.395 -0.385]
- Change the frequency to 1
Python3
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
import numpy as np
ecg = electrocardiogram()
frequency = 1
time_data = np.arange(ecg.size) / frequency
plt.plot(time_data, ecg)
plt.xlabel( "time in seconds" )
plt.ylabel( "ECG in milli Volts" )
plt.xlim( 9 , 10.2 )
plt.ylim( - 1 , 1.5 )
plt.show()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...