This repository provides a custom implementation of the sequential analysis method detailed by Martens et al. (2007) for non-invasive fetal electrocardiogram (fECG) extraction. The primary challenge this project addresses is isolating the weak fetal cardiac signal from maternal abdominal recordings, which are heavily contaminated by maternal ECG (mECG), power-line interference, and baseline drift.
Fetal cardiac monitoring is essential for evaluating fetal health and detecting distress. While Doppler ultrasound is widely used, non-invasive abdominal fECG provides several unique benefits:
- Continuous and ambulatory monitoring without frequent probe repositioning.
- Detailed morphological analysis of cardiac cycles (P, QRS, and T waves) enabling deeper physiological insights.
- Accurate Fetal Heart Rate (FHR) variability assessment.
This project implemets a 5 stage sequential filtering pipeline based on established signal processing techniques:
Raw Abdominal Signal (1000 Hz)
β
1. Baseline Wander Removal
β
2. Power Line Interference Cancellation
β
3. Anti Aliased Upsampling (2000 Hz)
β
4. Maternal ECG Cancellation (MECG)
β
5. Fetal ECG Extraction & Synchronous Averaging
Challenge: Patient movement and respiration induce low frequency baseline drift that corrupts the FECG spectrum.
Implementation:
- High-pass FIR filter (3 Hz cutoff). Tap count auto-scales with the sampling rate to preserve filter sharpness, equivalent to 1000 taps at 400 Hz, which works out to ~2500 taps for this project's native 1000 Hz data.
- Designed strictly using the Window Method (Hamming window).
- Applied via zero-phase filtering (
scipy.signal.filtfilt) to prevent any nonlinear phase distortion of the QRS complexes.
Challenge: 50 Hz power line noise and its harmonics frequently corrupt clinical recordings.
Implementation:
- Adaptive noise cancellation via a configurable Phase Locked Loop (PLL), each tracked component with its own independently tracked amplitude and phase. Defaults to standard single tone cancellation of the 50 Hz mains fundamental only; can be configured to additionally cancel a chosen number of harmonics (e.g. 100/150/200 Hz), up to a 200 Hz limit. The main analysis notebook configures 4 harmonic cancellation (fundamental + 100/150/200 Hz) for all reported results.
- Features an amplitude-based blocking mechanism that suspends filter adaptation during high energy QRS complexes, strictly protecting the cardiac morphology from being filtered out.
Challenge: Accurate removal of the maternal ECG requires sub millisecond precision alignment.
Implementation:
- The signal is upsampled from its native rate (1000 Hz for this dataset) to 2000 Hz.
- Relies on polyphase filtering (
resample_poly) to ensure strict anti aliasing while increasing temporal resolution for optimal template matching.
Challenge: The maternal heartbeat dominates the abdominal recording and must be precisely subtracted without damaging the underlying fetal signal.
Implementation:
- Channel Combination: Principal Component Analysis (PCA) extracts the dominant maternal cardiac axis.
- QRS Detection: Employs a Matched Filter (Cross-correlation) to detect R peaks.
- Robust Template Generation: Calculates a moving average of the last 10 maternal beats, implementing a trimming technique (discarding maximum and minimum amplitude beats) to reject outliers and prevent fetal QRS contamination.
- Subtraction: Segments the template into P, QRS, and T waves, fitting each independently to the raw signal via Ridge regularised least squares (a small Tikhonov penalty is added to the normal equations for numerical stability).
Challenge: The residual signal contains the isolated but noisy FECG, requiring precise detection and enhancement.
Implementation:
- Fetal QRS Detection: Re applies the Matched Filter technique on the MECG free residual to locate fetal R peaks.
- Synchronous Averaging: Computes the FHR and performs ensemble averaging over 150 consecutive fetal beats to dramatically increase the Signal to Noise Ratio (SNR) and reveal the clean fetal cardiac morphology.
BSP_project/
βββ data/ # Raw PhysioNet records, organised as data/set_a/ with matching .fqrs ground-truth annotations
β
βββ notebooks/ # Jupyter notebooks
β βββ main_analysis.ipynb # Pipeline execution and validation
βββ results/ # Auto generated figures (recreated by running the notebook)
βββ src/ # Core Python modules
βββ filtering.py # BWR + PLIC
βββ preprocessing.py # Upsampling (1000 Hz --> 2000 Hz)
βββ mecg_canceller.py # MECG detection and least squares subtraction
βββ fecg_extractor.py # Fetal QRS detection and synchronous averaging
βββ utils.py # FHR/reliability/success rate metrics and ground truth validation
βββ plotting.py # Visualisations
The main_analysis.ipynb notebook includes:
- Welch's Periodogram: Compares the Power Spectral Density (PSD) before and after filtering.
- ICA Benchmarking: Uses
FastICA(fromscikit-learn) as a baseline BSS method to demonstrate the robustness of the sequential approach in noisy environments. - Ground-Truth Validation: Matches detected fetal QRS locations against the reference
.fqrsannotations shipped with the dataset to compute real precision, recall and F1.
numpyscipyscikit-learnmatplotlibwfdbpandasjupyter
- Place your raw
.dat/.heafiles, plus their matching.fqrsground truth annotation files, intodata/set_a/. - Open
notebooks/main_analysis.ipynb. - Run the pipeline cells sequentially to process the signals, extract the fECG, and visualise the results.
Martens, S. M. M., Rabotti, C., Mischi, M., & Sluijter, R. J. (2007). A robust fetal ECG detection method for abdominal recordings. Physiological Measurement, 28(4), 373-388.