A Two-Lead ECG Using DAQ and Python
By
Biplab Dutta and Anshuman Dey
Report submitted to the Faculty of the Indian Institute of Technology, Kanpur in partial fulfilment of the requirements for the course PHY692
APPROVED APPROVED :
———————————— Dr. Dr. K. P. P. Rajeev
Course Instructor
April 30, 2012 Kanpur, Uttar Pradesh India
Contents 1 Introduction
2
2 Pro ject Ob jective
2
3 A bit of Electrical Theory
3.1 3.2 3.3 3.4
Measurement . . Amplification . . Noise . . . . . . . Data Acquisition
. . . .
. . . .
. . . .
. . . .
2
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
2 2 3 3
4 Parts and Cost
3
5 ECG in the Making
3
5.1 5.2 5.3
ECG Circuit Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Electrodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Up the Complete Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 4 4
6 ECG Visualization
4
7 Diagrams and Pictures
5
8 ECG Setup
6
9 Plots of ECG
7
1
1
Intr Introdu oduct ctio ion n
Electr Electrocar ocardio diogra graph( ph(ECG ECG)) is a device device that can be used used to amplif amplify y, mea measur suree and record record the natura naturall electrical electrical activity activity of the heart. It generally generally measures measures and records records the electrical electrical potential potential generated generated by the natural pacemaker - Sinoatrial Node(SAN). Although all of the heart’s cells have the ability to generate the electrical impulses(or action potentials) that trigger cardiac contraction, the SAN normally initiates it, simply because it generates impulses slightly faster than the other areas. The ECG(sometimes EKG) is today used world over as a relatively simpler way of diagnosing heart conditions. The fundamental function of the ECG as known today was developed by the Dutch scientist Willem Einthoven in the beginning of the 20th century for which he was awarded the Nobel prize in Physiology or Medicine ”...for his discovery of the mechanism of the electrocardiogram.” One should note that the cardiac cardiac electrical electrical signals are different different from than the heart b eats. ECG is used to study the electrical activity of the heart and heart sounds are listened to with a stethoscope. SAN initiates the electrical signal which serve to command and coordinate contraction of the four chambers at the heart at the appropriate intervals, and their analysis reveals a wealth of information about cardiac regulation, as well insights into the pathological conditions. Each Each heartbeat produces a periodic pattern pattern in the ECG signal, called a PQRST wave. wave. The smooth curve in the ECG(P) is caused by the simulation of the atria via the SAN in the right atrium. There is a brief pause, as the electrical impulse is slowed by the Atrioventricular node (AVN) and Purkinje fibers in the bundle of His. The prominent spike in the ECG(the QRS complex) is caused by this step, where the electrical impulse travels through the inter-ventricular septum and up through the outer walls of the ventricles. The sharp peak is the R component, and exact heart rate can be calculated as the inverse of R -to-R interval(RRi).
2
Projec Projectt Object Objectiv ive e
Our objective was to build a portable and extremely cheap ECG machine using a data acquisition (DAQ) card and Python (of course excluding excluding the cost of a computer computer and a DAQ DAQ card!). There There are some reasons reasons which made us select this project: 1. Firstly Firstly,, we wanted wanted to do something different different and very cool! 2. Secondly Secondly,, we wanted wanted to visualize visualize and analyze analyze our own heartbeat. heartbeat. This project can be divided into two main parts: 1. Hardware Hardware - This consist consist of an Instrument Instrumentation ation amplifier, amplifier, a DAQ DAQ card and a computer. 2. Softwar Softwaree - Data and post-processing post-processing has been done using Python. Before going into the details of the our own ECG machine, let us consider some of challenges which had to be overcome in order to build the ECG.
3 3.1 3.1
A bit bit of Elec Electr tric ical al Theo Theory ry Meas Measur urem emen entt
The electrical signals generated in the heart is transmitted along with blood throughout the whole body. These These signals signals can be detect detected ed on the surface surface of the skin. One can simply simply see these these signals signals as voltage voltage change as their heartbeats if one holds the two leads of a multimeter, one on each hand. These fluctuations are rapid and the signals reaching the skin become extremely weak(of the order of few millivolts) and difficult to be detected without proper amplification.
3.2 3.2
Ampl Amplifi ifica cati tion on
One can simply use an op-amp to amplify amplify the signal. signal. But due to too much backgrou background nd noise we need something something which had a very very high commo common-mode n-mode rejection rejection ratio. In the beginning beginning of the project we were were
2
to use in-chip instrumentation amplifier which are built specially for medical instrumentation(AD624, AD620) but these chips were not easily available. So we decided to go chips which are easily available in an electronics electronics lab. Our instructor instructor suggested us to use op-amp op-amp OP07 for instrumentation instrumentation amplifier amplifier and op-amp 741 for post amplification. These chips also operate at low voltages.
3.3
Noise
This was the biggest challenge challenge of all! It is unfortunate unfortunate that the heart is not the only source source of voltage voltage on the skin. Radiation Radiation from a variety variety of electrical electrical devices devices is absorbed by by our skin. Our body is a very very good receiver. receiver. These signals signals are also measured measured with our ECG, thus thus burying the ECG signal in a sea of electrical noise. One can use either hardware(bandpass filters) or software(post processing using python). We did not use any hardware filter. All the filtering is done using python.
3.4 3.4
Data Data Acqu Acquis isit itio ion n
For data acquisition acquisition we used PMD-1208FS PMD-1208FS - low-cost low-cost USB-based USB-based Persona Personall Measureme Measurement nt device. This DAQ DAQ card has 8 channels channels,, 12 bit input. For acquiring data we use PyUniversal PyUniversalLibr Library ary (PyUL) which is a Python wrapper for Measurement Computing’s Universal Library for data acquisition on Microsoft Windows operating systems.
4
Parts and Cost
This section accounts for the parts and cost in making only the instrumentation amplifier and the skin electrodes. Most of the parts we required were available in our Electronics lab. The list of parts used: •
3× low voltage op-amp OP07
•
1× low voltage op-amp 741
•
6× 1kΩ resistors
•
1× 18kΩ resistor
•
1× 33kΩ resistor
•
Electrodes - 2 coins with shampoo (Rs. 5) and 16 × Skin Electrodes (Rs. 240)
•
2×Universal PCB (Rs. 90) - Although we didn’t use them in our final demo.
•
5
1×Stereo pin (Rs. 15) - To check our earlier signal from the instrumentation amplifier connecting it the sound card of the computer and using a sound editor (Goldwave) to process the signal.
ECG ECG in in th the Ma Making
In the beginning we tried setting up the circuit on a Universal PCB but due some error arising every time we decided to use breadboard instead. The circuit for instrumentation amplifier was borrowed from Paul Horowitz and Winfield Hill(see References). The schematics (Section 8) shows the circuit layout in details. The gain for the instrumentation amplifier is given by [11] Gain =
vout v2
−
v1
= 1+
2R1 Rgain
R3 R2
(1)
where vout - output voltage, v2 - +ve input, v1 - -ve input, R1 , R2 , R3 , Rgain - resistors. In our case we obtained a gain of 3 from the instrumentation amplifier since we used R1 = R2 = R3 = Rgain and then fed its output to the inverting amplifier(using op-amp 741) with a gain of 51. The total gain achieved is 153. This is enough for us to visualize the signal coming from the skin.
3
5.1 5.1
ECG ECG Circ Circui uitt Dia Diagr gram am
The circuit shown in the figure is divided into parts - an instrumentation amplifier and an inverting amplifi amplifier. er. The instrume instrumenta ntatio tion n amplifi amplifier er with a gai gain n of 3 is used used to amplif amplify y and reduce reduce noise in the signal signal coming from the skin. We could have used only the instrumen instrumentation tation amplifier amplifier to get the output of desired gain but using all equal resistors in the instrumentation amplifier made it easy to troubleshoot. In the beginning we had trouble with loose connections since the breadboards weren’t good enough. Taking the output from the instrumentation amplifier with a gain of 3 and feeding it to the input of an inverting amplifier with a gain of 51 made it easier to troubleshoot.
5.2 5.2
The The Elec Electr trode odess
In the beginni beginning ng we got the skin electr electrodes odes from a medica medicall store. store. But those weren weren’t ’t good enough enough as they were use-and-thro use-and-throw w type. We had think of something something which could be used over over and over over again. Although the skin electrodes gave the best results. For our preliminary testing purposes we used two One rupee coins along with crocodile clips as our electrodes. Shampoo was applied to the skin and electrode interface to provide better conductivity. Electrical tapes were used to attach the electrodes to the skin.
5.3
Settin Setting g Up the Comple Complete te Circ Circuit uit
Finally the output from the electronic circuit is connected to the input of DAQ card and the other input of DAQ card is grounded. The circuit diagram shows the connections made.
6
ECG ECG Visu Visual aliz izat atio ion n
The ECG signal is acquired by DAQ and a code written in python (ecg.py). This piece of code does the following things: •
Acquires data from the card
•
Plots the raw data in realtime
•
Processes the raw data and outputs the filtered and smoothed data along with the heart rate.
The filtering filtering is done digitally digitally using a low pass filter code. The low pass filter reduces reduces the influence of 60 Hz interfere interference nce and other backgrou background nd noise. The desirable desirable passband to maximize maximize the QRS energy is approximatel approximately y 5-15 Hz [1]. But since our hardware hardware is very very simple with only two leads we cannot use a bandpass at that range(it completely completely distorts distorts the ECG wavefo waveform). rm). So we have to go with a low pass filter with cutoff frequency of 40 Hz. The electrodes have to be properly connected to skin otherwise we end up getting a bad signal. The following steps were implemented to get the final ECG signal and the heart rate: •
Acquire the raw data and plot its time series.
•
Filter the data using low pass (cutoff frequency 40 Hz) using scipy.signal module.
•
Using Savitzky-Golay filter to smooth the filtered output.
All the plots are plotted in realtime with a time lag of the total post processing time of the raw data.
4
7
Diag Diagra rams ms and and Pic Pictu ture ress
(a) Heart and an ECG trace
(b) ECG Circuit Diagram (R1 = R2 = R3 = Rgain = R4 = 1kΩ, R5 = 51kΩ)
5
8
ECG Setup
(c) ECG Signal Amplifier
(d) Complete ECG Setup
6
9
Plots of ECG
Figure 1: Plots of Original Signal, Filtered Signal, Smoothed Signal (xlabel: Time (in seconds), ylabel: Voltage (in Volts))
7
Acknowledgements:
The authors acknowledge Dr. K. P. Rajeev for his support and guidance.
References [1] J. Pan, W. J. Tompkis, “A Real-Time QRS Detection Algorithm”, IEEE Transactions Transactions on BioMedical Engineering, Vol. BME-32, No. 3 (1985). [2] P. K. Gakare, Gakare, A. M. Patel, Patel, A. N. Cheeran “Real “Real Time Analysis and Diagnosis Diagnosis of ECG Signal for Tachycardia Condition”, International Journal of Computer Applications, (2012). [3] P. Horowitz, W. Hill, “The Art of Electronics”, Second Edition, Cambridge University University Press (2011) [4] Electrocardiography, Electrocardiography, http://en.wikipedia.org/wiki/El http://en.wikipedia.org/wiki/Electrocardiography ectrocardiography. [5] DIY
ECG
Machine
on
the
Cheap,
http://www.swharden.com/blog/
2009-08-14-diy-ecg-machine-on-the-cheap/ .
[6] Measure Measure Biopotential Biopotential / ECG, http://www.emant.com/ecg.page. [7] [7] S. Raja, Raja, “QRS “QRS dete detect ctio ion n in ECGs ECGs”, ”, http://ggeek.googlecode.com/svn http://ggeek.googlecode.com/svn/trunk/qrsdetect /trunk/qrsdetect/ / ecgpy.py. [8] Savitz Savitzkyky-Gol Golay ay-Sm -Smooth oothing ing filter.py.
with with
Python Python,,
http://public.procoders.net/sg_filter/sg_
[9] PyUniversalLibrary PyUniversalLibrary,, https://code.astraw.com/project https://code.astraw.com/projects/PyUniversalLib s/PyUniversalLibrary/ rary/ [10] Cook ookboo book
/
Data
Acquisition
with
PyUL,
http://www.scipy.org/Cookbook/Data_
Acquisition_with_PyUL
[11] Instrumentation Amplifier, http://en.wikipedia.org/wiki/In http://en.wikipedia.org/wiki/Instrumentation_am strumentation_amplifier plifier
8