The purpose of this lab is to learn about measurement of photoplethysmogram (PPG) using a pulse oximeter. We will learn about driving a pulse oximeter using ESP32, and using Python serial reader to receive the signal at PC. We will also use filters to reduce the noise and study the affect of the filters on the signal.
-
A photoplethysmogram (PPG) is an optically obtained plethysmogram that can be used to detect blood volume changes in the microvascular bed of tissue. A PPG is often obtained by using a pulse oximeter which illuminates the skin and measures changes in light absorption. A conventional pulse oximeter monitors the perfusion of blood to the dermis and subcutaneous tissue of the skin.
For more details, please see Wikipedia – Photoplethysmogram.
-
Low pass filters can help with reducing the noise in the signals of sensors. Low pass filters exist in many different forms. For hardware side, as introduced in Lab 3, an RC circuit can be used as a low pass filter when output is connected to the capacitor. In this lab, we will look at the software side where filters are applied by code and algorithms.
-
A moving average filter is applied by averaging several previous data points. As the image below shows, each point of the filtered signal is the average of k previous near points in the original signal.
For more details, please see Wikipedia- Moving average.
-
An exponential decay filter (also called exponential smoothing) is a rule of thumb method to filter out noise in time series signals. It can be implemented by the following equation:
Sk = αxk + (1 - α) Sk-1
where S is the filtered signal, x is the original signal and α is the exponential factor. For more details, please see Wikipedia - Exponential smoothing.
Pre-lab computations:
- None but see Computer Preparation below
We will use Python for signal processing in this Lab. If you are not familiar with Python, please follow the steps to prepare your Python workspace here.
For more information on setting up Anaconda and Spyder click here.
All the code in this Lab is provided in the 'src' folder, but you are welcome to write you own code for any part of this lab.
-
Setup hardware
-
Connect the pulse oximeter sensor to your ESP32: red pin to 3.3V, black pin to GND and purple pin to an Analog input.
-
Connect the ESP32 to your PC.
-
-
-
Use two fingers to hold the sensor or use tape to fix the sensor on one of your fingers or on your ear. Make sure the front side of the sensor is touching your skin.
-
In Thonny, click View at the top menu and select plotter, if everything is set correctly, you should see similar plots as the following figure and a yellow LED on your ESP32 blinking on your heartbeat. You may need to adjust the threshold for the led to blink based on your readings, make sure to have a good threshold as this will be important later. If you see that signal reaches the upper limit or the peak is not obvious, you may need to adjust the pressure applied on the sensor.
-
✏️ Take a photo of your sensor setup and a screenshot of the serial plotter.
-
Open Spyder, make sure the virtual environment is correct.
-
In Spyder, open the file serial_reader_PPG.py. Before running serial_reader_PPG.py, save the pulse sensor code from the previous section to your ESP32 as code.py. Close Thonny, since it is connected to the serial port of your ESP32. Reset your ESP32 and get readings from the sensor, using the blinking LED to determine if you are getting proper readings.
-
Run the serial_reader_PPG.py Python script, making sure the sensor is correctly placed on your skin. It will record the raw PPG sensor reading for 10s. The data will be saved as a '.txt' file in the same folder as 'serial_reader_PPG.py'.
-
If the recording is successful, you can see a plot pop up as the following figure shows.
-
✏️ Take a screenshot of this figure.
-
Repeat this step to record 1 trial for each member in your group. Make sure to change the file name in the script. Otherwise, it will overwrite the existing file.
-
If you find any of the following errors, just close the current Console and run again.
-
In medical practice, data such as PPG or ECG are usually considered private. So you are also welcome to use anonymous recording in this lab, for example, by writing 'member 1' instead of your name.
-
In Spyder, open moving_average_filter.py, remember to change the file name of your recorded data.
-
Run this script, a figure should pop up.
-
✏️ Take a screenshot of the figure and describe what you find when applying moving average and with different window size k. You can use 'Zoom' button to Zoom in and get a better observation.
-
Repeat the steps for each trial you just recorded.
- In Spyder, open exponential_decay_filter.py, enter the file name of your recorded data.
- Run this script, a figure should pop up.
- ✏️ Take a screenshot of the figure and describe what you find when applying the filter and with different exponential factors.
- Repeat the steps for each trial you just recorded.
-
In Spyder, open heart_rate_estimate.py, enter the file name of your recorded data.
-
Run this script, a figure should pop up and show the filtered signal by exponential decay.
-
You can check the coordinates of the peaks by pointing your cursor on the peak and the coordinate is on the top right corner.
-
Find the time interval between 2 peaks, as the following figure shows. Repeat 5 times and calculate the average of t.
-
Use the following equation, where
hris the heart rate andtis time in seconds.hr = 60 / t -
✏️ Write down the heart rate for each member of your group.
-
Do some exercises, such as jumping jacks. Then take measurement again and estimate your heart rate. You need to be quick as your heart rate may recover to baseline quickly.
-
✏️ Write down the heart rate after exercises and compare with the heart rate before exercises.
-
✏️ Verify the result by counting pulses on your wrist (use this video for reference). It is always important to validate your sensor readings with a known accurate source.
Same instructions as previous TECHIN 512 labs.
Q: I have Python installed on my PC, why should I use Anaconda?
A: If you have Python already installed, it could work well in this lab. However, Anaconda can be very useful in the future when you work on several Python projects simultaneously. Anaconda helps you to create and manage multiple virtual Python environments, which could prevent conflict of library dependencies.
Q: If I don't have Spyder installed, can I also create a virtual environment and run the script?
A: You can also use terminal.
- Create a folder anywhere you like, for example,
~/home/Desktop/lab7_env - Open a terminal.
- Create a virtual environment named
venvin your terminal:python3 -m venv ~/home/Desktop/lab7_env, remember to replace the dir to the correct folder if you created the virtual in other dirs. - Activate the virtual environment.
source ~/home/Desktop/lab7_env/bin/activateif using macOS, orC:\> ~\home\Desktop\lab7_env\Scripts\activate.batif using windows. - In your virtual environment, you can install required packages in the script.
Refer to this about creating python virtual environments.















