ECG
Measure Biopotential / ECG
Warning: This application note is for learning and development only. It is not for Medical Diagnostic Use. Should be used solely by or under the supervision of qualified engineers and technicians who are familiar with the risks associated with handling electrical circuits. The Python program is for experienced Python users.
An electrocardiogram (ECG or EKG, abbreviated from the German Elektrokardiogramm) records the electrical voltage in the heart in the form of a graph. It is the prime tool in cardiac electrophysiology, and its function is in the screening and diagnosis of cardiovascular diseases. This note uses the EMANT380 Bluetooth DAQ module with Python running on a PC to measure ECG.
Requirements
- Python for Windows - Python 2.6.6
- PyBluez 0.19
- Scipy 0.9.0
- Numpy 1.6.1rc3
- Matplotlib 1.0.1
Note:
- Pair the EMANT380 to PC.
- You must modify the bluetooth MAC address (Python) for your EMANT380 module before running the example.
import matplotlib.pyplot as plt
import time
import random
import math
import emant
from scipy import fft, ifft
MAINS_FREQ = 50
NOTCH_LENGTH_AVERAGE = 4
ScanRate = MAINS_FREQ * NOTCH_LENGTH_AVERAGE
NumSamples = ScanRate * 2 + NOTCH_LENGTH_AVERAGE
def HighPass(ECGdata):
fftECG = fft(ECGdata)
for i in range(len(fftECG)):
if i<8 or i>(NumSamples-NOTCH_LENGTH_AVERAGE-8): fftECG[i]=0
return ifft(fftECG)
def NotchMains(ECGdata):
filteredECG=[]
for i in range(len(ECGdata)-NOTCH_LENGTH_AVERAGE):
filteredECG.append((sum(ECGdata[i:(i+NOTCH_LENGTH_AVERAGE)])/NOTCH_LENGTH_AVERAGE))
return filteredECG
m = emant.Emant300()
#change this mac address to your EMANT380 Bluetooth DAQ module
m.Open("00:06:66:00:A1:D8",True)
print "Hardware Id: " + m.HwId()
r,actScanRate = m.ConfigAnalog(1, emant.Emant300.Bipolar, ScanRate)
print "Actual Scan Rate: " + str(actScanRate)
fig = plt.figure()
ax = fig.add_subplot(3, 1, 1)
bx = fig.add_subplot(3, 1, 2)
cx = fig.add_subplot(3, 1, 3)
# Create a new timer object. Interval set to 2.5 secs
timer = fig.canvas.new_timer(interval=2500)
loop = True
def update_graph(axesa,axesb,axesc):
global timer, loop
if loop:
ECGdata = m.ReadAnalogWaveform(emant.Emant300.AIN2,emant.Emant300.AIN3,NumSamples)
Notchfiltered = NotchMains(ECGdata)
HighPassed = HighPass(Notchfiltered)
axesa.cla()
axesa.plot(ECGdata[10:NumSamples-10]) # update the data
axesb.cla()
axesb.plot(Notchfiltered[10:NumSamples-10])
axesc.cla()
axesc.plot(HighPassed[10:NumSamples-10])
axesa.figure.canvas.draw()
else:
plt.close()
def onclick(event):
global loop
loop = not(event.button==3)
# right mouse click on graph to end program
cid = fig.canvas.mpl_connect('button_press_event', onclick)
# tell the timer what function should be called
timer.add_callback(update_graph, ax, bx, cx)
update_graph(ax,bx,cx)
timer.start()
plt.show()
timer.stop()
m.Close()
External Links
Transform Based Approach for ECG Period Normalization
An Ultra-Wearable, Wireless, Low Power ECG Monitoring System
Detection of Power-Line Interferences in ECG signal using Frequency-Domain Analysis
Wireless non-contact ECG and EEG for unobtrusive cardiac and brain monitoring
Nonintrusive Biological Signal Measurement for Ubiquitous Healthcare