ChatPPG Editorial

Median Filter for PPG: When It Helps and When It Hurts Waveform Features

Practical guide to median filter use on PPG: remove impulse spikes, protect waveform morphology, choose kernel sizes, and combine with accelerometer gates.

ChatPPG Research Team
6 min read
Median Filter for PPG: When It Helps and When It Hurts Waveform Features

The median filter ppg approach removes short impulse spikes and baseline glitches from photoplethysmography signals while preserving sharp edges better than a moving average. However, if the kernel is too large or applied without attention to sampling rate it will blunt important morphological features such as the dicrotic notch and second derivative peaks.

Quick answer

Use a small median window (3 to 11 samples at common PPG sampling rates) for impulse removal and baseline spike correction, combine with a narrow bandpass for cardiac band isolation, and check morphology after filtering with derivative-based metrics. When you need accurate waveform shape for feature extraction, prefer adaptive or model-based approaches instead of a large median filter.

Why median filters are used on PPG

Median filters are robust to outliers. For PPG, common short-duration artifacts include micro-motion spikes, LED flicker, and transient contact losses. A median filter replaces each sample with the median of the neighborhood, which removes single-sample or short cluster spikes without averaging across a rising or falling edge.

This behavior contrasts with linear smoothing such as a moving average, which reduces high-frequency noise but also attenuates peaks and widens sharp transitions. For tasks that tolerate small timing shifts but not amplitude outliers, the median filter is a good first-line tool.

Related reading: see our guides on adaptive filtering for PPG and PPG reading and interpretation for context on feature extraction.

When a median filter helps

Impulse and spike removal

If the raw PPG has isolated samples far from the local baseline, a median filter with a short kernel (odd length, typically 3, 5, or 7) will remove those spikes without changing the waveform baseline. This is useful when LEDs produce momentary transient peaks or when a finger shift produces a high-amplitude spike.

Robust baseline wander correction in short segments

Cascaded median filters of different window sizes can estimate baseline trend without fitting sinusoids. Two-stage median filtering is a common approach: a long-window median extracts baseline drift, and a short-window median removes local spikes before baseline subtraction. This can be safer than high-order polynomial detrending on short recordings.

Preserving edge features compared to mean filters

Because the median is an order statistic, it preserves sharp edges better than a linear smoother. For heartbeat detection where peak timing is more important than exact amplitude, a small median kernel can improve peak detection sensitivity while reducing false positives caused by spikes.

When a median filter hurts waveform features

Morphology blunting and peak distortion

Median filters can remove or shift small morphological features. The dicrotic notch, small inflections used in augmentation index calculations, and the fine structure of the systolic upstroke can be flattened if the median window length approaches the duration of those features. At 100 Hz sampling, a 21-sample median covers 210 ms, which can blunt a dicrotic notch that is often within 100 to 200 ms after the systolic peak.

Phase and timing shifts

Although median filters are non-linear, they can introduce sample-level timing changes for inflection points. This is especially relevant when the downstream task measures time differences, such as pulse transit time or inter-beat interval alignment against ECG. Always validate timing-sensitive metrics after any median filtering step.

Hidden bias for amplitude-based metrics

Feature calculations that rely on relative amplitude ratios, such as the b/a ratio from the second derivative of the PPG, are sensitive to even small amplitude distortions. An overaggressive median filter reduces b and d wave amplitudes differently, biasing derived vascular indices.

Practical rules for using a median filter on PPG

1) Match kernel length to sampling rate and target features

  • For sampling rates 50 to 125 Hz: start with window = 3 to 11 samples.
  • For high-resolution clinical traces (250 Hz and above): a slightly larger kernel can be used for impulse noise removal, but keep it below 15 samples unless testing shows no morphology loss.

Rule of thumb: keep kernel less than 10% of the cardiac cycle duration at expected heart rate. For 60 BPM (1 s cycle) that means kernel should cover less than 100 ms.

2) Use odd-length windows

Median filters require odd-length windows to center the median. Use 3, 5, 7, 9, 11, etc.

3) Combine with a bandpass

A narrow bandpass (for example 0.7 to 8 Hz for adult heart rates) isolates the cardiac band so the median filter has fewer slow trends to fight. See our adaptive filtering primer for designs that use reference signals.

4) Cascade short and long medians for baseline removal

A robust baseline estimate can be produced by two-stage median filtering: short median to remove spikes followed by a long median to estimate slowly varying baseline. Subtract the baseline and then, if needed, reapply a short median for residual spikes.

5) Use diagnostic metrics after filtering

Always compute a morphology preservation score. Practical tests include:

  • Peak height ratio before and after filtering averaged across a 30-second segment.
  • Second derivative b/a ratio change.
  • Cross-correlation of 1-second beat windows to detect shape drift.

If these metrics change beyond preset thresholds, revert to a smaller median kernel or use an alternative method.

Alternatives that preserve morphology

Robust smoothing splines

Smoothing splines with tuning on the L1 loss are robust to outliers while preserving the overall waveform shape. They can be configured to reject short spikes while maintaining finer morphological detail.

Savitzky-Golay filtering

Savitzky-Golay filters perform local polynomial fitting and can denoise without blunting peaks as much as moving averages. Use small frame lengths and choose polynomial order to match the local curvature you want to preserve.

Wavelet denoising

Wavelet thresholding can remove high-frequency noise while retaining transient morphology. For morphology-sensitive applications, transform-based denoising tuned to cardiac frequencies is often superior.

Implementation recipe (Python)

This recipe shows a safe median filter pipeline for wearable PPG sampled at 100 Hz.

import numpy as np
from scipy.signal import medfilt, butter, filtfilt

# x: raw PPG array sampled at fs
fs = 100
# Short median for spikes
x1 = medfilt(x, kernel_size=3)
# Baseline via long median
baseline = medfilt(x1, kernel_size=201)  # 2 sec baseline window at 100 Hz
x2 = x1 - baseline
# Bandpass for cardiac band
b, a = butter(2, [0.7/(fs/2), 8/(fs/2)], btype='band')
x_filt = filtfilt(b, a, x2)

Adjust kernel_size and filter bands for your device.

Validation checklist

  • Plot raw and filtered beats over multiple heart rates and movement levels.
  • Confirm inter-beat interval timing error against ECG or chest strap under resting conditions.
  • Quantify second derivative feature drift across 60-second windows.
  • Run downstream algorithm on unfiltered and filtered data and compare stability of estimates.

For QA under motion, see our article on PPG HRV and motion artifacts which covers signal quality indices and repair strategies.

References

FAQ

What kernel size should I use for median filter on wrist PPG? Start small. For 50 to 125 Hz sampling, try 3 to 11 samples and validate morphology preservation metrics.

Does median filtering change heart rate? If the kernel is small and focused on spikes, heart rate should not change. Large kernels that span a substantial fraction of the cardiac cycle can shift or remove peaks and affect heart rate estimation.

Can I apply median filtering in real time on-device? Yes. Median filtering is cheap and often implemented on microcontrollers. Use small kernels and test CPU/memory tradeoffs.

When should I avoid median filters? Avoid large median windows when you need precise morphology for vascular indices, blood pressure surrogate features, or second derivative metrics.

Is median filtering enough for motion artifacts? No. Median filters handle impulse spikes and short glitches. Motion artifacts that overlap with the cardiac band require adaptive filters, accelerometer fusion, or spectral methods. See adaptive filtering for PPG for a deeper approach.

Frequently Asked Questions

What kernel size should I use for median filter on wrist PPG?
Start small. For 50 to 125 Hz sampling, try 3 to 11 samples and validate morphology preservation metrics.
Does median filtering change heart rate?
If the kernel is small and focused on spikes, heart rate should not change. Large kernels that span a substantial fraction of the cardiac cycle can shift or remove peaks and affect heart rate estimation.
Can I apply median filtering in real time on-device?
Yes. Median filtering is cheap and often implemented on microcontrollers. Use small kernels and test CPU/memory tradeoffs.
When should I avoid median filters?
Avoid large median windows when you need precise morphology for vascular indices, blood pressure surrogate features, or second derivative metrics.
Is median filtering enough for motion artifacts?
No. Median filters handle impulse spikes and short glitches. Motion artifacts that overlap with the cardiac band require adaptive filters, accelerometer fusion, or spectral methods.