gastropy.hampel_filter#
- gastropy.hampel_filter(data, k=3, n_sigma=3.0)[source]#
Remove spike artifacts using the Hampel median identifier.
Slides a window of length
2k+1across the signal. Any sample that deviates from the local median by more thann_sigma * 1.4826 * MADis replaced by the local median.The factor 1.4826 makes the scaled MAD a consistent estimator of the standard deviation under a Gaussian assumption (Davies & Gather, 1993).
- Parameters:
data (array_like) – EGG signal(s). Accepts shape
(n_samples,)or(n_channels, n_samples).k (int, optional) – Half-window size: each sample is compared to its
kneighbours on either side (window length =2k+1). Default is 3.n_sigma (float, optional) – Outlier threshold in scaled MAD units. Samples further than
n_sigmafrom the local median are replaced. Default is 3.0.
- Returns:
cleaned – Signal with spike outliers replaced by local medians. Same shape as input.
- Return type:
np.ndarray
References
Davies, P. L., & Gather, U. (1993). The identification of multiple outliers. Journal of the American Statistical Association, 88, 782–792.
Dalmaijer, E. S. (2025). electrography v1.1.1. esdalmaijer/electrography
Examples
>>> import numpy as np >>> from gastropy.signal import hampel_filter >>> sig = np.sin(2 * np.pi * 0.05 * np.arange(0, 300, 0.1)) >>> sig[50] = 100.0 # inject a spike >>> cleaned = hampel_filter(sig) >>> np.abs(cleaned[50]) < 5.0 # spike removed True