Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Introduction

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.

Background

  1. 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.

    • Below are images showing and ICU monitor where "Pleth" is PPG data as well as a pulse oximeter used at the UW Medical Center.

      ICU Monitor

      UW pulse oximeter

  2. 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.

  3. 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.

    Moving Average Example

  4. 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.

Preparation

Pre-lab computations:

  • None but see Computer Preparation below

Computer Preparation

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.

Parts, tools, supplies required:

  • ESP32

  • Pulse oximeter (PPG) sensor

    PPG Sensor

Procedure

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.

  1. Setup hardware

    1. Connect the pulse oximeter sensor to your ESP32: red pin to 3.3V, black pin to GND and purple pin to an Analog input.

    2. Connect the ESP32 to your PC.


Test the sensor

  1. 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.

  2. 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.

    Serial Plotter Example

  3. ✏️ Take a photo of your sensor setup and a screenshot of the serial plotter.

Record data on your PC (You can also use alternative methods instead of Spyder, please refer Q&A)

  1. Open Spyder, make sure the virtual environment is correct.

    Spyder Enviroment Check

  2. 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.

  3. 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'.

    Text Output File

  4. If the recording is successful, you can see a plot pop up as the following figure shows.

    PPG Data

  5. ✏️ Take a screenshot of this figure.

  6. 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.

    Change filename

  7. If you find any of the following errors, just close the current Console and run again.

    Python error 1

    Python error 2

    Python error 3

  8. 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.

Filter the noise using moving average

  1. In Spyder, open moving_average_filter.py, remember to change the file name of your recorded data.

    Moving Average Filter

  2. Run this script, a figure should pop up.

  3. ✏️ 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.

    Zooming in

  4. Repeat the steps for each trial you just recorded.

Filter the noise using exponential decay

  1. In Spyder, open exponential_decay_filter.py, enter the file name of your recorded data.
  2. Run this script, a figure should pop up.
  3. ✏️ Take a screenshot of the figure and describe what you find when applying the filter and with different exponential factors.
  4. Repeat the steps for each trial you just recorded.

Estimate your heart rate

  1. In Spyder, open heart_rate_estimate.py, enter the file name of your recorded data.

  2. Run this script, a figure should pop up and show the filtered signal by exponential decay.

  3. You can check the coordinates of the peaks by pointing your cursor on the peak and the coordinate is on the top right corner.

    Heart Rate Estimation 1

  4. Find the time interval between 2 peaks, as the following figure shows. Repeat 5 times and calculate the average of t.

    Heart Rate Estimation 2

Estimate your heart rate

  1. Use the following equation, where hr is the heart rate and t is time in seconds.

    hr = 60 / t

  2. ✏️ Write down the heart rate for each member of your group.

  3. 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.

  4. ✏️ Write down the heart rate after exercises and compare with the heart rate before exercises.

  5. ✏️ 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.

Write-Up

Same instructions as previous TECHIN 512 labs.

Frequently Asked Questions

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.

  1. Create a folder anywhere you like, for example, ~/home/Desktop/lab7_env
  2. Open a terminal.
  3. Create a virtual environment named venv in 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.
  4. Activate the virtual environment. source ~/home/Desktop/lab7_env/bin/activate if using macOS, or C:\> ~\home\Desktop\lab7_env\Scripts\activate.bat if using windows.
  5. In your virtual environment, you can install required packages in the script.

Refer to this about creating python virtual environments.