🩺
🫀 004. BitaHealth: End-to-End HRV Analysis Pipeline
TL;DR
- Comprehensive pipeline for Heart Rate Variability (HRV) analysis using BitaHealth data.
- From raw signal preprocessing, filtering, feature extraction, to visualization.
- Ready-to-use Python code snippets with NeuroKit2 and Matplotlib.
Welcome to our deep dive into HRV analysis with the BitaHealth dataset! Whether you’re a researcher, developer, or health enthusiast, understanding the full process—from raw ECG signals to actionable metrics—can be daunting. Today, we break it down into clear, step-by-step stages, complete with code examples, so you can reproduce or adapt the pipeline to your own data.
We’ll cover everything: loading raw ECG, cleaning and filtering the signal, detecting R-peaks, computing time- and frequency-domain HRV features, and finally visualizing results. By the end, you’ll have a robust Python script leveraging NeuroKit2
and Matplotlib
to extract insights from heart rate variability.
Pipeline Overview
- Data Loading: Import raw ECG signals from CSV or PDF source.
- Preprocessing: Bandpass filtering to remove baseline wander and noise.
- Peak Detection: R-peak extraction using Pan–Tompkins algorithm.
- Feature Computation: Compute time-domain (e.g., SDNN, RMSSD) and frequency-domain (LF/HF) metrics.
- Visualization: Plot HRV time series, power spectral density, and summary table.
Python Example
# HRV Analysis with NeuroKit2
import neurokit2 as nk
import pandas as pd
import matplotlib.pyplot as plt
# 1. Load data (CSV export from BitaHealth)
df = pd.read_csv('bitahealth_ecg.csv')
ecg_signal = df['ECG'].values
# 2. Preprocess: Bandpass filter between 0.5–40 Hz
filtered = nk.signal_filter(ecg_signal, lowcut=0.5, highcut=40, method='butterworth', order=5)
# 3. Peak detection
signals, info = nk.ecg_process(filtered, sampling_rate=500)
# 4. Compute HRV features
time_features = nk.hrv_time(signals['ECG_R_Peaks'], sampling_rate=500)
freq_features = nk.hrv_frequency(signals['ECG_R_Peaks'], sampling_rate=500)
# 5. Visualize results
plt.figure(figsize=(10, 4))
plt.plot(filtered, label='Filtered ECG')
plt.scatter(signals['ECG_R_Peaks'], filtered[signals['ECG_R_Peaks']], color='r')
plt.title('ECG with R-Peaks')
plt.legend()
plt.show()
print('Time-Domain HRV Features:', time_features)
print('Frequency-Domain HRV Features:', freq_features)
Key Insights
Processing physiological signals requires careful filtering to preserve peak morphology. Here, a Butterworth bandpass filter (0.5–40 Hz) effectively removes drift and powerline noise. Peak detection via nk.ecg_process
simplifies R-peak extraction and artifact correction.
Time-domain metrics like SDNN and RMSSD capture overall variability and short-term fluctuations. Frequency analysis (LF/HF ratio) provides insight into autonomic balance between sympathetic and parasympathetic activity.