|
| 1 | +--- |
| 2 | +description: >- |
| 3 | + Build a x. |
| 4 | +--- |
| 5 | + |
| 6 | +# x - x |
| 7 | + |
| 8 | +Created By: Roni Bandini |
| 9 | + |
| 10 | +Public Project Link: []() |
| 11 | + |
| 12 | +GitHub Repo: []() |
| 13 | + |
| 14 | +## Introduction |
| 15 | + |
| 16 | +During the Apollo missions astronauts wore ECG sensors to monitor their stress levels and assess their ability to perform complex tasks. These ECG signals were manually monitored from Earth, over a distance of 238,855 miles. This process was challenging due to delays and data transmission errors. Today, advancements in technology have led to the development of small, inexpensive, offline wearable devices that utilize Machine Learning to determine stress levels. These modern devices can alert when a worker is unable to perform a critical task, ensuring safety and efficiency without the need for remote monitoring. |
| 17 | + |
| 18 | +How complicated is it to obtain ECG data, extract specific features with the Edge Impulse HR/HRV Processing Block, train a model, and deploy the model to create an Arduino wearable? |
| 19 | + |
| 20 | +{% hint style="danger" %} |
| 21 | +**Disclaimer**: this project is experimental and it is not intended to diagnose or treat any medical conditions. |
| 22 | +{% endhint %} |
| 23 | + |
| 24 | +## Requirements |
| 25 | + |
| 26 | +1 x Arduino Portenta board H7 |
| 27 | +1 x AD8232 SparkFun Single Lead Heart Rate Monitor |
| 28 | +5 x female-female jumper cables |
| 29 | +Edge Impulse Enterprise Account |
| 30 | + |
| 31 | +> Note: All Edge Impulse users (Community, Professional, and Enterprise) can extract heart rate and HRV features using this block for testing purposes. However, the deployment option is only available for Enterprise users. |
| 32 | +
|
| 33 | +## Circuit |
| 34 | + |
| 35 | +Connect AD8232 Output to A0 |
| 36 | +Connect AD8232 Lo – to D2 |
| 37 | +Connect AD8232 Lo + to D3 |
| 38 | + |
| 39 | +Lo means Leads off detection. The AD8232 also includes a shutdown pin (SDN) that is not used for this case. A logic level signal can be applied to SDN to switch to shutdown mode, even when the supply is still on. |
| 40 | + |
| 41 | +Connect the pads jack right all way to the end, disconnect the AC from the notebook and move away all radio devices before sampling. Use good quality new pads for each measurement. |
| 42 | + |
| 43 | +ECG Intervals |
| 44 | + |
| 45 | +The ECG is separated into two basic intervals: the PR Interval and the QT Interval. The PR interval is the initial wave generated by an electrical impulse traveling from the right atrium to the left. Then inside the QT is there a complex process that generates the signature "beep" in cardiac monitors. During QRS both ventricles begin to pump. After the initial contraction comes the ST segment (the time where the ventricals waiting to be "re-polarized"). Finally the T wave becomes present to actively "re-ploarize", or relax the ventricles. |
| 46 | + |
| 47 | +## Data Acquisition |
| 48 | + |
| 49 | +For this project, the data will be the electrical activity of the heart. The AD8232 sensor is designed to extract, amplify, and filter small biopotential signals in the presence of noisy conditions, such as those created by motion or remote electrode placement. |
| 50 | + |
| 51 | +The ECG signal can also be combined with data from an accelerometer for enhanced diagnostics. Some Arduino boards, like the Arduino BLE 33 Sense, have an onboard accelerometer, so with a small code modification, that data can also be used to train the model. |
| 52 | + |
| 53 | +The pads should be connected as follow: |
| 54 | + |
| 55 | +Yellow pad to the left |
| 56 | +Red pad to the right |
| 57 | +Green pad below the red pad |
| 58 | + |
| 59 | +Remember to disconnect the AC from the notebook before sampling. |
| 60 | + |
| 61 | +Upload the Data Forwarding acquisition script to the Arduino Portenta using the Arduino IDE. |
| 62 | + |
| 63 | +Check that the values being obtained are stable in serial monitor (Center value of about ~500 with spikes between +300/-200) |
| 64 | + |
| 65 | +Note: even with all these precautions, the AD8232 module could still produce a noisy signal. For a real case scenario it is recommended to use professional equipment like the Elemyo EMG/ECG module MYO v1.5 For this public project synthetic data was used. |
| 66 | + |
| 67 | +Close serial monitor and run edge-impulse-data-forwarder |
| 68 | +Select the Edge Impulse project and check that the frequency shows [SER] Detected data frequency: 50Hz |
| 69 | + |
| 70 | +Go to https://studio.edgeimpulse.com/studio/YOURPROJECTIDHERE/acquisition/training |
| 71 | + |
| 72 | +Select length 120.000 ms and take around 6 samples for each category. Example: regular working versus stress. Select around 10% of the samples for testing. |
| 73 | + |
| 74 | +## The HR-HRV Block |
| 75 | + |
| 76 | +Edge Impulse recently added a learning block specifically designed for this scenario. |
| 77 | + |
| 78 | +The HR/HRV Features block processes physiological signals like the electrocardiogram (ECG), with optional accelerometer inputs for enhanced accuracy in motion-prone applications, to extract key metrics such as heart rate (HR) and heart rate variability (HRV). HR measures the number of beats per minute, while HRV measures the time variance between successive heartbeats, also known as the interbeat interval (IBI). The block offers real-time HR estimation and HRV analysis on resource-constrained edge devices and leverages cutting-edge algorithms for precise feature extraction. The extracted features can be used on their own or to inform downstream machine learning tasks such as stress detection or heart health analysis. |
| 79 | + |
| 80 | +For this project, stress detection is performed with a classification algorithm, as the model has already been trained with ECG data from both regular working conditions and stress conditions. However, if you extract all the HRV features, you can also determine stress by analyzing frequency domain features such as Raw VLF Energy, Raw LF Energy, Raw HF Energy, Relative VLF Energy, and the LF/HF Ratio Peak Energy. Just note that you will need a window size of 90 seconds (10 seconds for RMSSD and 30 seconds for time domain analysis). |
| 81 | + |
| 82 | +Time Domain features are: IBI Slope, HR Mean, HR Slope, RMSSD Slope, RMSSD, AVNN, SDNN, Range NN, MAD NN and pNN50 |
| 83 | + |
| 84 | +Frequency-domain features are: Raw VLF Energy, Raw LF Energy, Raw HF Energy, Raw Total Energy, Relative VLF Energy, Relative LF Energy, Relative HF Energy, LF/HF Ratio, Peak VLF Energy, Peak LF Energy, Peak HF Energy |
| 85 | + |
| 86 | +I have used ECG, filter preset 1, window size 40 and no HRV features. |
| 87 | + |
| 88 | +## Model Training |
| 89 | + |
| 90 | +The training could require some parameters to be modified from the defaults. I have found the following parameter to work well for my dataset with a 89.3% accuracy. Training cycles 40, learning rate 0.005, bacth size 30 and no auto weight. |
| 91 | + |
| 92 | +## Deployment |
| 93 | + |
| 94 | +For projects with Edge Impulse HRV DSP block a license to deploy is required, so there is a button to authorize this feature. |
| 95 | + |
| 96 | +After the authorization you will be able to get the model as an Arduino library. |
| 97 | + |
| 98 | +But an intermediate step is still required. |
| 99 | + |
| 100 | +1. Unzip the deployment file into a local folder |
| 101 | +2. Download a zip containing all files from https://github.com/edgeimpulse/example-hr-lib-arm |
| 102 | +3. Unzip the files and copy inside the local folder of step 1 |
| 103 | +4. Zip the folder of step 1 (now with the additional files) |
| 104 | +5. Add the new zip as a library in Arduino IDE (Skectch, Include Library, Add Zip library) |
| 105 | + |
| 106 | +Now you will be able to use the model library with your own code. A sample ECG monitor script using the compiled ZIP can be found at https://github.com/ronibandini/hr-hrv/blob/main/ecg2.ino |
| 107 | + |
| 108 | +Note 1: If Arduino Portenta shows exit status 74, double click reset, select the port |
| 109 | + |
| 110 | +## Final Notes |
| 111 | + |
| 112 | +Thanks to Machine Learning, monitoring ECG signals no longer requires transmitting data to a remote computer for expert analysis. Instead, subtle health conditions can be detected by small, offline wearable devices equipped with Machine Learning capabilities. These devices can identify over-stressed workers who may be unable to perform their tasks effectively, thus preventing serious consequences. |
| 113 | + |
| 114 | +## Files and Project Link |
| 115 | + |
| 116 | +https://studio.edgeimpulse.com/studio/558080 |
| 117 | +https://github.com/ronibandini/hr-hrv/ |
| 118 | + |
| 119 | +## References |
| 120 | + |
| 121 | +https://docs.edgeimpulse.com/docs/edge-impulse-studio/processing-blocks/hr-hrv-block |
| 122 | +https://docs.edgeimpulse.com/docs/edge-ai-hardware/mcu/arduino-portenta-h7 |
| 123 | +https://www.edgeimpulse.com/blog/unraveling-the-depths-of-hr-and-hrv-edge-impulses-technical-insights/ |
| 124 | +https://learn.sparkfun.com/tutorials/ad8232-heart-rate-monitor-hookup-guide |
| 125 | +https://pmc.ncbi.nlm.nih.gov/articles/PMC10098909/ |
| 126 | +https://www.theatlantic.com/science/archive/2019/07/apollo-11-moon-heart-rates/593971/ |
| 127 | +https://elemyo.com/support/start_info/myo_v1_4 |
0 commit comments