Med*Nap

ECG-Based Sleep Monitoring Project with Raspberry Pi Pico W 💤📊

1/13/20252 min read

Overview 📋

This project focuses on developing an ECG-based sleep monitoring system using the Raspberry Pi Pico W. The primary goal is to record uninterrupted ECG data for at least 15 minutes, process it to extract key heart rate variability (HRV) metrics, and correlate these metrics with sleep stages to understand sleep patterns better.

Motivation 💡

Sleep is an essential component of overall health and well-being. Monitoring sleep patterns through non-intrusive methods like ECG can provide valuable insights into heart activity during rest. Traditional sleep studies require complex setups, but this project aims to create a portable, cost-effective solution for sleep monitoring.

Key Components 🛠️

  1. Raspberry Pi Pico W: A microcontroller used to collect ECG data from sensors and transmit it to a computer for processing.

  2. ECG Sensor: Captures the electrical activity of the heart. ❤️

  3. MacBook: Used to receive data from the Raspberry Pi Pico W and process it using Python scripts. 💻

Methodology 📐
1. Data Collection 📥

ECG data is collected using a sensor connected to the Raspberry Pi Pico W. The microcontroller transmits the data to a MacBook via Wi-Fi. The data collection is designed to last at least 15 minutes to capture meaningful sleep patterns.

2. Data Processing 🔄

The collected data is processed using Python libraries such as pyedflib, pandas, heartpy, and neurokit2. The workflow involves the following steps:

  • EDF to DataFrame Conversion: The raw ECG data in EDF (European Data Format) is converted into a pandas DataFrame for easier manipulation. 🧩

  • Windowing: The ECG data is segmented into 30-second windows to facilitate stage-wise analysis. 🪟

  • Bandpass Filtering: A bandpass filter is applied to remove noise and focus on relevant frequencies (0.5-40 Hz). 🎛️

  • Z-Score Normalization: The data is normalized to ensure consistency in analysis. 📐

  • R-Peak Detection: Using the HeartPy library, R-peaks (points of maximum voltage during a heartbeat) are detected to calculate R-R intervals (time between consecutive R-peaks). 📈

3. Feature Extraction 📊

The processed data is used to compute various HRV metrics, including:

  • AVNN: Average NN interval (time between normal beats) ⏱️

  • SDNN: Standard deviation of NN intervals 📉

  • Mean HR: Average heart rate ❤️

  • RMSSD: Root mean square of successive differences between NN intervals 🔬

  • pNN50: Percentage of NN intervals that differ by more than 50 ms 📏

4. Sleep Stage Labeling 💤

The project uses sleep scoring annotations to label the ECG data with corresponding sleep stages. The labeling process involves matching 30-second windows with annotations provided in a text file. 📑

Challenges 🚧
  • Data Transmission Stability: Initially, the data transmission from the Raspberry Pi Pico W would stop after 5 minutes. This issue was resolved by optimizing the code and adjusting the Wi-Fi connection settings. 📶

  • Noise in ECG Data: Ensuring the accuracy of the ECG signal required implementing robust filtering techniques to remove noise and artifacts. 🎚️

Results 📈

The processed ECG data and HRV metrics provide a detailed view of heart activity during sleep. By correlating HRV features with sleep stages, we can better understand how heart rate changes across different sleep phases.

Example Output 🖥️
  • R-R Intervals Plot: Displays the time intervals between consecutive heartbeats. 📊

  • HRV Metrics Table: Summarizes the computed metrics for each window. 📋

Future Work 🔮
  • Real-Time Processing: Implementing real-time data analysis to provide immediate feedback on sleep patterns. ⏳

  • Mobile Application Integration: Developing a companion mobile app to visualize sleep data and HRV metrics. 📱

  • Improved Sleep Stage Classification: Using machine learning models to improve the accuracy of sleep stage detection. 🤖

Conclusion 🎉

This project demonstrates a practical approach to sleep monitoring using readily available hardware and open-source software. By leveraging ECG data, we can gain valuable insights into heart activity during sleep, potentially aiding in the diagnosis and management of sleep disorders. The project also highlights the importance of data preprocessing and feature extraction in achieving accurate and meaningful results.