Skip to main content

The remote health monitoring system (hardware development and measurement)

Introduction

Heart disease is a commonly-known disease in the world. Patients suffering a heart attack may lead to stroke or even death. One early symptom of heart attack is arrhythmia. If arrhythmia occurs frequently, patients will have a higher chance of having a heart attack.

When our heart beats, there is a small change in electrical potential across the heart which can be transformed into voltage waveform. This waveform is commonly named as Electrocardiography (ECG). The ECG waveform contains three parts, i.e. P, QRS and T wave. The shape of the waveform would look similar to Figure 1 for a healthy person.

Photo_1_9111b07889a769488af24bc2bf28f02128770277.jpg

Figure 1. Normal ECG waveform

Irregular heartbeat means the heart is having abnormal movements and this generates abnormal ECG waveforms.

There are different types of arrhythmia such as premature ventricular contractions and ventricular ectopic beats. Each type of arrhythmia has a unique shape such that doctors can determine patents’ type of heart disease according to the ECG waveform.

 

Photo_2_5ef7224a17938e8941f9325a2399f2e4da8a6842.jpg

Figure 2. Premature ventricular contraction

Photo_3_9afc3a7d562b231f7ce5bf73f37bd7f53aced760.jpg

Figure 3. Ventricular ectopic beats

One major reason that heart diseases have a high risk of death is that arrhythmias are hard to discover. In s traditional ECG examination, electrodes are placed on the patient’s body and then connected to professional ECG monitoring machine. Patients cannot be monitored on a 24/7 basis as such ECG monitoring can only be performed in the clinic. Therefore, such a traditional approach cannot predict heart attack effectively.

The ultimate goal of this project is to provide a cost-effective portable ECG monitoring solution with automatic diagnostic function.

Design and Implementation

System overview

The new generation remote ECG monitor system is formed by three parts. They are:

  • Wearable ECG measurement hardware signal filtering and signal improvement
    BLE technology to replace 3G module for low-power consumption portable with battery charging function more local processing with deep learning classifier on MCU.
  • User mobile APP connects to a measurement device via BLE technology real-time information display and communicates with an online server and database.
  • Online server with ML classifier as reference for MCU classifier.

Photo_4_272b301d121d931e05f94ff1f158148e04f0450a.jpg

Figure 4. System Overview

Analogue front end (AFE) design

The analogue front end is a part of the circuit which responses to capture, filter and amplifier analogue signal. Usually, AFE will apply on small-signal capturing such as in ECG application. Some advanced AFE will include Analogue-Digit Convert (ADC) and Digit Signal Processing (DSP) functions.

In this project, AD8232 is chosen to the ECG AFE. AD8232 is mainly formed by an instrumentation amplifier, an operation amplifier, a right leg drive amplifier. These three components are highly related to ECG signal amplifying. AD8232 also includes electrodes leadoff detection and automatic fast restore circuit.

Photo_5_9704cfca28efc573c029ce32c99dd6d8817211f7.jpg

Figure 5. AD8232 block diagram

As the human body signal strength is very weak. The signal needs to be filtered and amplified to convert back an ECG signal. AD8232 is flexible that engineers can custom design analogue filters to adjust the ECG signal bandwidth and control the signal gain. There are four steps in an ECG signal reproduction procedure. Firstly, the human body signal will pass through an instrumentation amplifier. The instrumentation amplifier will amplify the voltage differential between two electrodes (IN+ - IN-) around 100 times. Then, the signal will pass through a high-pass filter. The low frequency (below 0.5Hz) noise which is generated by muscle movement and respiration will be filtered out. Then, the signal will pass through a low-pass filter in order to filter out the high-frequency noise such as 60Hz interference. Finally, the filtered signal will be amplified again by an operation amplifier and output with an analog signal.

Photo_6_9b215b9888654ee6549c2a53f5d46076579ebef9.jpg

Figure 6. ECG signal reproduce procedure

One of the most popular ECG AFE solutions on the market is AD8232 module from Sparkfun. The module can capture ECG perfectly while the patient is at still during the measurement. However, the ECG signal is distorted due to body movement. Therefore, this module is not suitable for portable use.

Photo_7_839384a1637ce70b64e3666da3ee1fb2e8d0b6c9.jpg

Figure 7. AD8232 module from spark fun

Photo_8_9c672d6a462c929af8cc7a72ad14fa1ca0ca464a.jpg

Figure 8. ECG signal distorted

Therefore, I decided to redesign the analogue filter for portable use with the AD8232.

Analogue Filter Design

High-Pass Filter selection

AD8232 datasheet provides three types of high pass filter and that’s mean AD8232 maximum can form a three-pole high pass filter. Higher order filter is proportional to better performance of the filter. However, higher-order filters will result in the need to place more components on the PCB. To have a balance between the PCB space and the performance of a filter, a two-pole high pass filter is selected in this project.

The first high pass filter is formed by a RC network which is connected to the output of the instrumentation amplifier through HPSENSE and HPDRIVE. This RC network acts like an integrator that feeds DC signals back into the instrumentation amplifier to eliminate the low-frequency signal. The performance of this high pass filter is strong and stable. It provides a stable 20dB per decade decline rate to the low-frequency side. This part of the circuit is applied to the AFE.

Photo_9_2eae8682c2e0e9548a65785bd4bf759daf32441e.jpg

Figure 9. 1st High pass filter

The second high-pass filter is adding a pole to the DC blocking circuit to form a high-pass filter. The advantage of using this high-pass filter is that it provides a narrow pass band with the maximum pass band fatness. However, the performance of this filter is reduced in the low-frequency region and there is no effect on signals below 0.1 Hz. The ECG module is using this design and we would like to explain why it is not good under bodily movements.

Photo_10_06770ccaefec16c2eac8360de1ef506e8c59aafb.jpg

Figure 10. High-pass filter 2

Photo_11_5595de5bcf1aa09f1b5cc8fcd49f7527653266a6.jpg

Figure 11. 2nd filter become invalid under 0.1Hz

Another high-pass filter is considered and added in this ECG measurement circuit to improve the ability of anti-motion effect. An RC network is connected to the output of the instrumentation amplifier. Combined with two high-pass filters, this two-pole high-pass filter provides stable and powerful performance with + 40dB per decade.

Photo_12_ed73908367cf1ede752a20ebb75198aa418eea76.jpg

Figure 12. Two pole high-pass filter

Photo_13_06baf925856731faa74cb2e89f8aa2f896c5e43c.jpg

Figure 13. +40dB per decade

Low-Pass Filter selection and 2nd stage gain

The Sallen-Key [5] two-pole low-pass filter is chosen in this project. The filter provides a sharp cut off point and the following equations show the gain and Q value of this circuit:

Photo_14_450cefd4b25f440eaebbc2221787b97ac5ce0a90.jpg

Photo_15_760761a70f6d7913072fe43d6db97191bdd12b35.jpg

Figure 14. Sallen-Key low-pass filter

Right-Leg Drive circuit

The right-leg drive circuit can improve the common-mode rejection of the system. The right leg drive amplifier inverts the common-mode signal from the instrumentation amplifier input and output to the user's body in order to provide feedback to reduce common-mode noise.

Photo_16_1701db381f65691651e7e563e23b8abfd6761b54.jpg

Figure 15. Right-Leg Drive circuit

Performance investigation of ECG circuit

To show the difference between the new ECG circuit and the previous one, an experiment is conducted. The new module filter is designed by AD8232 Filter Design Tool which is produced by Analog Devices. With this tool, users can calculate the resistor and capacitor value to adjust the filter bandwidth.

Photo_18_11159dc6bf4da3387115a7f76d7ed707b4ecf31d.jpg

Figure 16. AD8232 Filter Design Tool by Analog Devices

Photo_19_ce4d14117651b8cf22d00a80e4e575070a8c1cf0.jpg

Figure 17. Module Layout

 

Photo_20_353a5c0500cac00747ed81c2e96b116fa56c661b.jpg

Figure 18. The new module schematic

Photo_21_5f4d332a389258b35b2bd3319f2cf3693e18fdca.jpg

Figure 19. soldered testing circuit

 

SparkFun ECG circuit

New ECG circuit

Still

Photo_22_0cc497352affffe62938e8c12241aea98eeb9ac4.jpg Photo_23_a8dfd9edc41f21bf5b22f142d19d56062c547e49.jpg

Walk

Photo_24_3494d6e49f242a1cff2ff475dc0e660c1aa9e23e.jpg Photo_25_d1606aba823cece31ba9069c24fcbeb7134b6afd.jpg

Run

Photo_26_27df290bf9b2007bd70b062149df4d709e747417.jpg Photo_271_f38f9b2b200ddc2a1adc28bd0f3bc22d1c0495d7.jpg

Table 2. Comparison of New and Old ECG circuit

From Table 2, we can see the performance of two circuits is similar when the user keeps still. However, once the user walks during the ECG measurement, this motion distorts the P, T peak of ECG signal due to low-frequency noise. When the user jogs during the measurement, the ECG signal is completely distorted. On the contrary, when the user walks, the new ECG circuit can reproduce the ECG signal to a large extent. When the user is jogging, the circuit can retain the QRS complex part. We can conclude that the new ECG circuit with the function of elimination of motion is more suitable for remote ECG monitoring.

ECG hardware

The hardware is based on Microcontroller – STM32F4. There are four major components that connect to the microcontroller;

  1. STM32F4 connects to an external 16-bit ADC via 3 wire communication and it is compatible with SPI communication protocol. And the external ADC takes part in converting the amplified analogue signal from the AD8232 ECG circuit. Also, the ECG lead-off detection pins are connected to STM32 GPIO to check the electrons connections state.
  2. STM32F4 connects to an MPU9250 accelerometer via SPI.
  3. STM32F4 connects to a BLE Bluetooth module JDY16 via UART.
  4. STM32F4 is responsible to monitor the battery voltage level. Therefore, the battery voltage input is connected to an STM32F4 ADC pin.

18350 Li-ion rechargeable battery, which has a very large capacity (1000mAh) is chosen to be the system battery. Therefore, a charging circuit and li-ion battery protection circuit are included on PCB.

The cost of the ECG hardware is around USD $ 17/HKD $130.

2417_aa4f5f5ec41f1299ff9d1f401ff4c6e803b55e35.png

Figure 24. Hardware system block diagram

2524_372f58f0bd91dfab5c449722f10cb3b5458ed779.jpg

Figure 25. PCB Schematic diagram

2614_c6a0fc1f923a474e9f7b225ec8a0f34be1c73b64.png

Figure 26. PCB Layout diagram

2711_498ee491323ac5fcb986e44ba3e1a324d183f604.png

Figure 27. Soldered PCB

There are three ECG electrons pad sockets on the PCB. User can install three electrode pads and mount the ECG hardware on top of the chest. Besides, there is a 3D-printed case of the ECG hardware.

288_d4224c7ee189fe99ed51399d00f4d19673a37a8b.png

Figure 28. mount on top of the chest

295_36a2b357d0a7af874a596dc146a65d2910510045.png

Figure 29. 3D-printed case

Microcontroller

STM32F405RGT6 is based on high-performance ARM Cortex-M4 core at 168Mhz. Also, it has 192 Kbytes of SRAM and 1Mbtye of Flash. The memory is large enough to store and run different applications. Also, there are a variety of peripherals that is suitable for controlling purpose.

Core

ARM® 32-bit Cortex® - M4 CPU

Dimension

10mm * 10mm

Flash memory size

1MB

SRAM size

192KB

Timers

Twelve 16-bits and two 32-bit timers up to 168MHz

Debug mode

Serial wire debug (SWD)

Communication interface

I2C, SPI, UART etc.

Table 3. the specification of STM32F405RGT6

Bluetooth module

JDY-16 is a BLE module by a mainland company JDY. The advantage of this component is that JDY-16 is tiny and PCB antenna is embedded on the module. A tiny component can make the final product become more portable. In addition, it is low-power consumption and JDY-16 consumes at most 10mA under maximum power setting.

3115_d98fddb9d8dd0bc9951376e09eb4781a6325784e.png

Figure 31.JDY-16 BLE module

Transmission power

≤ 0dB

Communication interface

I2C, UART

Working voltage

1.8V - 3.6V

Antenna

PCB antenna

Operating distance

≤ 60 meters

Dimension

14.94mm * 19.6mm * 2.6mm

Table 4. the specification of JDY-16

AD8232 Analogue Front End

The analogue circuit part already mentioned in Section 2.2. AD8232 has electrons lead-off detection. Once any electrons fall off, a GPIO interrupt is generated by the AD8232. The lead-off detection GPIO is connected to an STM32 GPIO pin.

3216_901cd6eaf5e4ea40d8cc201d8f08f2084f5302ef.png

Figure 32. MPU9250 Accelerometer

An MPU9250 from InvenSense is a motion tracking IC and it is chosen as an accelerometer sensor. It is a very tiny sensor and the dimension is around 3 x 3 x 1 mm. An MPU9250 supports the full-scale range of ±2g, ±4g, ±8g, and ±16g, which is suitable for our application.

Accelerometer Features

Programmable full-scale range ±2g, ±4g, ±8g and ±16g and integrated 16-bit ADCs

Communication interface

I2C, SPI

Working voltage

2.4V-3.6V

Dimension

3mm * 3mm *1mm

Table 5. the specification of MPU9250

ADS8866 16-bit ADC

The ADS8866 is a 16-bit, single-ended input, analogue-to-digital converter (ADC) manufactured from Texas Instruments. Although the maximum sampling rate supported by the ADS8866 is 100k samplings per second and our application needs 360Hz only, the power consumption directly variates to the application sampling rate. Therefore, ADS8866 can keep in low-power consumption. MCU can access ADS8866 via SPI or three-wire interface.

3315_a90539deb0b119f9236050334646c05859f84a55.png

Figure 33. ADS8866

Having a 16-bit high-resolution ADC, the ECG hardware can capture the ECG in detail. Comparing to the device with the 12-bit resolution, the signal converted by our 16-bit ADC has a better Signal-Noise-Ratio (SNR) and less distortion. In Figure 34 and Figure 35, we can recognize the P wave, the T wave and the QRS complex of the ECG signal in a better way under the 16-bit signal resolution.

3414_98be6b8219eddf6483ab9f37ace75c30610990ba.png

Figure 34. 16-bit resolution ADC

3512_13d86be58ccb7416b3409c7a9925a20250854651.png

Figure 35. 12-bit resolution ADC

Battery and Charging Circuit

As previously mentioned, 18350 li-ion rechargeable battery is chosen to be the system power supply. There is a li-ion battery charging circuit built by TP4054 IC on the PCB. Users can plug the mini USB cable to the ECG hardware to charge the battery.

369_31be4ab8f0a30b0fa2be01cba3ad00c2fee55cfc.png

Figure 36. TP4054 Li-ion battery charging circuit

Battery Protection Circuit

Li-ion battery can provide large current (around 3A) output. Once there is a short circuit, the high output current will generate heat which may be harmful to the user. In addition, over-discharge, over-charge and over-current will degrade the battery health. Therefore, battery protection must be considered in this project.

A DW01, a li-ion battery protection IC is added. DW01 limit the li-ion battery voltage in the range of 3.0V to 4.3V in order to prevent an over-discharge and over-charge situation. Also, there are fuses on both the input of the charging circuit and the input of battery to limit the current usage under 500mA and 120mA.

374_0ede1dc00035595d860135c6d21d290cf0321dfe.png

Figure 37. DW01 protection circuit

384_8baa93d3f6f6b45aeb2352a0f887753eb605cecb.png

Figure 38. Fuses on PCB

There are positive and negative markings on the battery socket and the PCB in order to indicate the battery installation direction. Also, a reverse protection diode is added. Once the user installs the battery in the reverse direction, the reverse protect diode limits the input voltage to -0.7V rather than -VCC in order to protect the circuit.

394_55942bdb027421b8ea182606d169e500a25b5366.png

Figure 39. Reverse Protect Diode

404_078fb3ee5931dae852a2b3731e4573ffcbec0d78.png

Figure 40. Clear battery markings

 

Voltage regulation and grounding

The NCP1529 step-down DC-DC converter is chosen as the system power regulator. Having a high switching frequency 1.7Mhz, NCP1529 provides up to 96% power efficiency and less power consumption can help to extend the battery life. The NCP1529 steps down and regulate the li-ion battery voltage to 3.0V to system power.

4113_da24eb6d12a1d6085d55e06abf8226d40ed3a4dd.png

Figure 41. NCP1529 DC-DC converter circuit

To provide a better noiseless environment to the analogue circuit, a linear regulator is added. An XC6206-2.8V is chosen as the analogue circuit power supply i.e., the external AD8866 16-bit ADC and AD8232 AFE. Besides, an LC low pass filter is added to the output of XC6206-2.8V to eliminate the power noise.

4213_bc016d41e89b5941b636915ae90a9c082ac46047.png

Figure 42. XC6206 linear regulator

 

To minimise the ground noise, separated grounds [6] and single-point grounding design are considered. There are three grounding in the system and they are power ground, (Digit) ground and analogue ground. Digital ground means that the ground connects to digital signal components such as the accelerometer and Bluetooth module. For analogue ground, it is connected to the AD8232 ECG AFE and the external ADC. The reason for separating ground is that digital signal is a square wave and includes a lot of high-frequency component in it. Therefore, separating ground can help to eliminate the digital noise affecting the analogue components. Both analogue ground and digital ground connect to power ground with zero Ohm resistors near to the battery ground negative side. Besides, ground plane design is applied on the digital ground and power ground to reduce the ground impedance.

4313_ff5a473392a0978adb337d94bafaab49e74fe3f1.png

Figure 43. separated grounds and single-point grounding design

Battery Voltage Sensing

A battery voltage sensing circuit is designed in order to indicate the battery level. As Li-ion battery maximum voltage is 4.2V which exceed the STM32 internal ADC range (maximum 3.3V), a voltage divider is added to drop the voltage. Also, an external OP Amp buffer is added and it is driven by the system power supply (3.0V). Once the input voltage exceeds its supply voltage, the OP Amp buffer will limit the output voltage up to its supply voltage (3.0V) in order to protect the STM32 ADC.

4412_c098b66358d1b45e82f46476b989953662b07313.png

Figure 44. Voltage divider and Op AMP buffer

Firmware

The firmware bases on FreeRTOS [7]. FreeRTOS is a real-time operating system kernel for embedded system. FreeRTOS supports multiple threads, tasks, mutexes and software timers. Using FreeRTOS can handle multiple tasks with a single microcontroller.

From Figure 45, there are 5 tasks: one for ADC converting, one for ECG signal processing, one for Bluetooth UART transmitting, one for motion detection and one for battery voltage sensing.

4510_5d65973b54d0bde4a17482dd298b79d59d6ac9ba.png

Figure 45. microcontroller program flow chart

 

Besides, there are timers running in the background for signal converting. DMAs (Direct Memory Access) is widely used for controlling peripherals in order to save CPU loads

From Figure 46, Timer 2 is running at 360Hz which matches the ECG sampling rate. Every 1/360 second, Timer 2 Channel 2 triggers the DMA to send a SPI3 frame to ADS8866 ADC and it returns a 16-bit data frame. When 3600 data frames have been received through SPI3, an interrupt triggers the DMA to indicate that it is the end of the ten-second ECG sampling period and the measurement of the period is completed.

468_ceab63a27816a215a9f309626203333f6e11b041.png

Figure 46. Timer 2 and SPI3 receive DMA program flow chart

 

Similarly, from Figure 47 we can see that Timer 1 is running at 20Hz and generates a timer interrupt in every cycle. Inside the interrupt service routine, STM32 sends seven-byte data frames via DMA and SPI1 to an MPU9250 to get its X-axis, Y-axis, and Z-axis accelerations. When 420 data frames are received, an interrupt triggers the DMA to indicate that it is the end of the three-second motion tracking sampling period and the measurement of the period is completed.

475_2bfb5ec5befe1f3deb317b1ff93362aa3425fbe9.png

Figure 47. Timer 1 and SPI1 receive DMA program flow chart

 

From Figure 48, Timer 3 is running at 20Hz and keeps scanning the ECG electron lead-off detection GPIO from AD8232. Once the electron drops, STM32 updates the ECG electron status to users via Bluetooth.

484_5f6485e95e48551dc7f55621fbde40e9438a83cc.png

Figure 48. Timer 3 program flow chart

Deep Learning Model and Deep Learning Classifier on MCU

The deep learning model was provided by Dr Sean Xu, a Ph.D. graduate working in deep learning for heart arrhythmia. The deep learning model is designed for classifying arrhythmia from the ECG signal with a 360Hz sampling rate. The Deep Neural Network requires 417 sequential ECG sampling points as input for the input layer. From Table 6, there are five output labels in the output layer, which show the ECG health status. They are Normal beat, Supraventricular ectopic beat, Ventricular ectopic beat, Fusion beat, and Unknown beat. For more details, please refer to Sean’s journal papers - I-Vector Based Patient Adaptation of Deep Neural Networks for Automatic Heartbeat Classification [8] and Towards End-to-End ECG Classification with Raw Signal Extraction and Deep Neural Networks [9].

Output labels

Description

0

Normal beat

1

Supraventricular ectopic beat

2

Ventricular ectopic beat

3

Fusion beat

4

Unknown beat

Table 6. Output class description of the deep learning model

In this project, STM32f405RBT6 from STMicroelectronics is chosen to be the system microcontroller. STM32CubeMX [10] is a graphical tool that is provided by STMicroelectronics for setting up STM32 MCU peripheral parameters and generating C libraries. Users can add different C libraries such as the DSP library by accessing the GUI on STM32CubeMX. Undoubtedly, STM32CubeMX facilitates project initiation and project management.

493_aeebe2cada2e62813d14e9e6957bb0d2c9279ab5.png

Figure 49. STM32CubeMX

503_1f005c78ef46b4431fc8eccdacc82be7bfc2ef95.png

Figure 50 X-CUBE-AI

In STM32CubeMX, there is an expansion package called X-CUBE-AI [11], which is designed for converting pre-trained Neural Network models to an STM32 C library. From Figure 50, we can use X-CUBE-AI to import an AI model into it. Sean’s deep learning model uses Keras with Tensorflow. In the Keras of X-CUBE-AI, a model compression is provided to reduce the size of the C library. In this project, the model compression is not selected to maximize the classification accuracy.

After importing the model to X-CUBE-AI, we can analyze the model. In Figure 51, we can see the analysis result. Besides, we can validate the C library on X-CUBE-AI. We can validate the C library by using a random number or custom data on X-CUBE-AI. X-CUBE-AI generates a report to show the error rate on each model layer compared to the original model. Figure 52 shows the validation result.

5112_c57f09a9c7676c0fec07a6819b872a7c3f77fdc3.png

Figure 51. Analyze model in X-CUBE-AI

5212_bf20c11907fce4a96dfa597ee1acbb4b219ff2e5.png

Figure 52. Validation in X-CUBE-AI

In Figure 53, we can see the model in detail. It includes the information of each hidden layer and bias information. On the other hand, we can import some sample data or using random data to validate the model in X-CUBE-AI.

5312_e9076ad3133d04e0fbc16754055dfdf95b12eec9.png

Figure 53. AI model detail shown in X-CUBE-AI

After setting up all the peripheral and AI model in STM32CubeMX, we can generate the microcontroller project file to Keil µVision IDE.

549_d363a9bc6eb72aeebe9bbaece1a24778a813279d.png

Figure 54. C library in Keil µvision5.0 IDE

From figure 54, we can see the STM32CubeMX generated project file with all MCU C library including the deep learning classifier library – “app_x-cube-ai.c”, “ecg.c” and “ecg_data.c”. There is a performance comparison of the pre-train model classification on STM32 and Python. In this performance comparison, it is expected that the classification results and output label values from Python are more accurate than that in STM32. 20 sets of sample data are stored in the classifiers at Python and STM32.

556_4462afebe4b6d47ac75427ef85102e5dcae22f41.png

Figure 55. Raspberry Pi 4

563_186b115c915800447c716b28ef85cb1ed594929a.png

Figure 56. STM32 Nucleo Board

From Table 7, we can see that result of the classifier in STM32 is very close to that in Python. The largest difference is around 0.005%. It shows that the Deep learning classification library generated by X-CUBE-AI in STM32 is almost equivalent to that in Python.

Table_7_e264899231f6e46c8fba99253c7bb39fe6ec6a4c.png

Table 7. STM32 Output label error on a 20 set of sample data

Pan-Tompkin algorithm

We need to detect the R peak position of an ECG waveform before inputting the data to the neural network classifier. It is because Sean’s deep neural network is designed for input 417 sampling each time. It is expected that the R peak should be found in the middle of the samples (i.e., 209th). If the number of samples between two successive R peaks is less than 417 samples, we claim these arrays as an “overlapping array”. In an overlapping array, we should assign zero-value samples to keep the number of samples in each ECG waveform is the same before processing the classification.

Pan-Tompkins [12] algorithm is an algorithm which is proposed by Jiapu Pan and Wills J. Tompkins for detecting QRS complexes. This algorithm was tested in MIT ECG database and the performance are outstanding. The testing report pointed out that more than 99% of QRS complexes were detected with Pan-Tompkins algorithm.

The block diagram of Pan-Tompkins is shown in Figure 57.

573_5581217f206f39fa6495e93b383e8c9ff3fe50cd.png

Figure 57. Pan-Tompkins block diagram

2.7 Digital Filter Design

In the previous section, it tells that an ECG signal is required to be passed into a 5-15 Hz band-pass filter before running the R peak catching process. Therefore, we need to design a digital filter for this purpose in the STM32 microcontroller. In this project, a Finite Impulse Response (FIR) filter is selected because the FIR filter is simple and stable. Also, its linear phase characteristics ensure that the ECG signal is not distorted.

583_e372e412d296d7e0eced4ccb9db587a7816c1b0a.png

Figure 58. Filter Designer in Matlab

Filter Designer in Matlab [13] is a great tool to design an FIR filter. From Figure 58, We can control the UI to input the cut-off frequency and windows selection into it. It shows the corresponding filter frequency response and phase response. Also, it can generate a C header for the coefficients of the FIR filter.

After importing the C header to the STM32 project file, we need to run an FIR filter function in STM32. The simplest way to build an FIR filter is to make use of the Cortex Microcontroller Software Interface Standard (CMSIS) library [14]. STM32F4 is based on Cortex-M4 and the CMSIS library to provide the DSP library to STM32. Appling the DSP library, we can build up the FIR filter easily.

593_2d287ba0859b7e16870bd7fa3e68fd9c1c068726.png

Figure 59. CMSIS FIR filter function applied on stm32

We can see that some high-frequency noises are removed after applying the band-pass filter in Figure 60 and Figure 61.

602_acc84812c4984505ef01113267dc0b25e7a4a8dc.png

Figure 60. before band-pass filter

6112_6267203b63453a9058aba5352301ffd2a2d463ae.png

Figure 61. after band-pass filter

 

2.8 Real life ECG classification on MCU

This section shows the process of ECG classification in an MCU. A sequence of ECG data with arrhythmia from the MIT arrhythmia database (Figure 62) is selected as the sample data.

6213_03e3956e3804959f9979c4650336f37830a49788.png

Figure 62. a sequence of ECG data with arrhythmia from MIT arrhythmia database

639_a449324f6e467c5fbbdf04cfc68a941841f50644.png

Figure 63. the sample data after derivative

643_fad227543a6928f4b2709f4560e8b523bb2bddd7.png

Figure 64. the sample data after square function

653_1606107e480bd3e9bc03754d26ada4a512cd092a.png

Figure 65. the sample data after window integration

663_d087fb638c421af018ae1f87560c115a0d09dd62.png

Figure 66. the sample data after adjusting thresholding

We can see that the Pan-Tompkins algorithm can correctly detect all R peaks in a sequence of ECG data in Figure 66. After that, each ECG signal is packaged as a 417 array and it is passed to the deep learning classifier on STM32. Based on the result, the classifier identifies an arrhythmia at 1886th sample, which matches the real situation.

673_be04c1be3d0aff54e4246c55bb31e7358a819dc0.png

Figure 67. classifier can classify arrhythmia happen

683_99b931851bf067e75b315c8ae4ed46674e673823.png

Figure 68. arrhythmia happen on 1886 array position

2.9 Heart rate calculation

In the previous mentions, it tells that the Pan-Tompkins algorithm can detect all R-peak positions in a sequence of ECG signal. Therefore, once we get N R-peaks, we get N – 1 intervals. Then, the calculation of the average heart rate is shown below.

formula1_c2ae366a0ffb06aa98d0579fb3c9752e86b3d154.png

where

AP(i) means the array position of i-th R-peak

N means the total number of R-peak inside 10 seconds

360 means 360Hz, it is the ECG signal sampling rate

2.10 Motion detection

The accelerometer is used to capture the X-axis, Y-axis, Z-axis acceleration in 20Hz, and stored in an array buffer. Every three seconds, the microcontroller processes a total of 180 sets of data.

Root mean square calculation is a method to estimate the vibration from users. The following three pictures show the accelerometer data sequence in three different cases respectively. They are standing still, walking, and running. The green line, red line and the blue line represents X-axis acceleration, Y-axis acceleration, and Z-axis acceleration respectively. We can see that when the user moves violently, the ripple amplitude becomes larger in Figure 69, Figure 70 and Figure 71.

693_3524045f6a6d2ee6264ab77d05ae35064bf8bfa1.png

Figure 69. standing still

702_4cb3424b3773b2ee667c5c37ab5916f8366b5cb1.png

Figure 70. Walking

7112_3d14c51cd16171e9291c7fad71195656830bf23d.png

Figure 71. running

 

The accelerometer is used to measure the offset angles of the motion of the user on the three axes. Every set of data needs to be normalized based on the offset angles. Then the root-mean-square of the magnitudes of the three axes is calculated and it is used to determine the user’s motion.

7212_ff256a9593d0e94cf248b8dcf4fec039ea950fab.png

Figure 72. Motion detection calculation process

 

Mobile App

The phone application was developed on the Android platform.

The phone app has three main functions:

  • to receive data from the ECG measurement hardware via Bluetooth,
  • to display information showing the real-time ECG waveform, and
  • to transmit ECG raw data to server developed for deep AI classification.

The reason why AI classification in both MCU and on the online server was that the development of online classification is mature. This solution was proven last year. Therefore, the performance of MCU classification can be compared to that of the online server.

In order to speed up the development process, different android libraries were adopted in the phone app:

  • FastBle: to handle the Bluetooth communication,
  • MPChart: to generate real-time ECG waveform diagram and display, and
  • Async-http: to make and receive http requests

738_5b091695b307bd38c4453bd5f1232c630edb3fc8.png

Figure 73. The ECG phone app UI introduction

743_fe6e0d1c749a7b9cd6e697dc2dfd9eae63fdef69.png

Figure 74. Mobile app program logic diagram

Discussion

3.1 Performance investigation

This section contains the study of ECG hardware performance in real-life.
First, the hardware performance would be demonstrated understanding still, walk and run conditions. Followed by the performance test on the heart rate calculation.

3.1.1 ECG sensing function

As mentioned in the previous section, the ECG hardware is mounted on top of the user’s chest with three-electrode pads.

751_9da3c18ea9bcfb98ca21a9f1837d0631254101f9.png

Figure 75. ECG hardware is mounted on user’s body

Standing Still

When the user stands still, the ECG hardware can get the ECG waveform with the minimum distortion. In Figure 76, the ECG waveform is noise-less and we can recognize the P wave, QRS complex, and T wave of ECG waveform clearly. Also, the hardware can identify correctly that the user is standing still.

761_3e52cf05ec902df7ef2ed16c9c736a67cc7a1865.png

Figure 76. ECG hardware performance under user standing still

Walking

When the user walks during the measurement, the heart rate increases and thus the ECG hardware can recognize that the user is walking. In Figure 77, we can see that there are some ripples inside the ECG waveform due to the body movements. In this part, ECG AFE is used to eliminate the motion effects and thus most of the ECG signals can be figured out. Although some of the P waves are distorted, most of the QRS complexes and T waves can still be observed clearly.

771_04840779e504a27f28cb0e9e610f70464a809354.png

Figure 77. ECG hardware performance under user walking

Jogging

When the user is running, the quick body movements generate noise and cause motion effects. In Figure 78, we can only recognize the QRS complex clearly when the user is jogging. Motion effects generate a lot of large-scale ripples and distort the P wave and T wave. The good thing is the calculation of the heart rate is still valid because the QRS peak can still be recognized properly.

781_bddfd6d7339e04d64cd8c8938c96b8e3f2bfb2d0.png

Figure 78. ECG hardware performance under user jogging

3.1.2 Deep learning classification

To test the classification performance in the ECG measurement device, another ECG experiment is carried out in a real human body.

791_219a3ea68abe3089154a6387d325e136509bded1.png

Figure 79. ECG hardware is mounted on my chest

In Figure 80, we can see that 12 heart beats are measured in 10 seconds. With the 360Hz sampling frequency, the MCU receives total 3600 sample data points and they are stored in an array.

80_a7bf1ca89427a9ef39004db4ec1cd6dc0bf86358.png

Figure 80. ECG hardware is mounted on my chest

After that, the MCU locates all R-peak positions. In Figure 81, the Pan-Tompkins algorithm is used to find out 11  of them. Noted that, the first R-peak is neglected. It is because this R-peak position is in 190 and thus it cannot form a complete 417-sample array of ECG data for the algorithm to locate the peak.

8110_103c9c39b688235d5dcef52e080b7d9d9d607e23.png

Figure 81. Pan-Tompkins algorithm find out 11 valid ECG samples

Then, all ECG waveforms are loaded into the classifier and output labels are generated by the classifier. The classification result is listed in Table 8. It shows that the MCU can classify all 11 ECG samples as class 0 (normal beats).

table_8_7e1c97dc9bb01fedd4d7f07c147a57097eb34a0e.png

Table 8. The classification result in detail

Then, the ECG waveforms and the classification results are sent to the mobile app. There are no abnormal beats in this sample set and the mobile app shows the message “No Arrhythmias found” in Figure 82. Meanwhile, the mobile app sends the Raw ECG data to the online server and gets back the classification result via HTTP request. The classification results from the online server are also shown in the mobile app.

8210_d99c480ceaded2de3800bcfe874abb6f4751d424.png

Figure 82. Pan-Tompkins algorithm find out 11 valid ECG samples

 

3.1.3 Heart rate calculation

This section will focus on testing the heart rate calculation accuracy. The setting of this test is that users use ECG hardware and Mi Band 3 at the same time. Mi Band 3 is an electronic smart band with heart rate tracking function. Then, there is a heart rate compression to these two devices.

83_84_a41ec74537414d86e2633f6ad034ca3a01f49d78.png

Figure 83. ECG hardware heart rate calculation and the Mi Band 3

Test Number

ECG hardware

Mi Band

Difference between the two devices.

0

64

64

0.00%

1

65

68

-4.41%

2

67

66

+1.52%

3

71

72

-1.39%

4

71

70

+1.43%

5

84

88

-4.55%

6

83

88

-5.68%

7

83

79

+5.06%

8

87

85

+2.35%

9

84

80

+5.00%

10

80

83

-3.61%

11

96

90

+6.67%

12

100

104

-4.00%

13

103

100

+3.00%

14

100

95

+5.26%

Table 9. Heart rate by ECG hardware and Mi Band

From Table 9, we can see that the heart rate detection performance of these two devices is roughly the same. For ECG hardware, it will measure heartrate in every 10 seconds, while Mi Band 3 takes 20 seconds to complete a heart rate checking. Therefore, there are errors in the result due to the different sampling periods in the two devices.

  • ECG lead-off detection

As section 2.2 mentioned, there is ECG electrode lead-off detection function inside AD8232. Therefore, once any electrode comes off, there will be a notification in the phone App to remind the user to install the electrode properly.

851_2e1645e330e13dda1da87a378bb440fb912e166a.png

Figure 85. ECG electrode lead-off notification on phone App

 

3.1.5 Battery charging

The ECG measurement device requires 5V and 0.5A for charging. When the user plugs in the device to the mini USB cable, an LED indicates that the device is charging. Once the battery is charged to 4.2V, the device stops the charging process automatically and switches off the LED.

86_87_34ede72760860806545ed1eaf6aa24a88eebd9a6.png

Figure 86. ECG hardware is charging and ECG hardware finishes charging

3.1.6 Power Consumption

The ECG measurement device is highly energy-efficient and consumes only about 75mA and 0.3W of power. Using a 1000-mAh 18350 rechargeable li-ion battery, the ECG measurement can last for more than 13 hours.

Conclusion

To conclude, I successfully developed a new generation ECG hardware with great ECG function and compact design. In addition, I have enriched the local processing functions on the ECG hardware and developed a phone application that can provide users with more real-time information. More importantly, the whole system can remain low cost with automatic diagnostic function. I hope it can help people detect heart disease early.

15066681D has not written a bio yet…