Denoising and outliers
Mean, Gaussian and median smoothing, detrending, the Teager–Kaiser energy operator, and how to find the samples that do not belong.
- Choose between mean, Gaussian and median smoothing for a given kind of noise
- Remove trends before analysing a signal
- Detect outliers with global and local thresholds, and recognise when a threshold is fooled
Most noise is one of two kinds: a little everywhere (broadband noise), or a lot in a few places (spikes and glitches). The right tool differs.
Smoothing broadband noise
mean_smooth_filter.m replaces each sample with the mean of its neighbours:
k = 20; % window is 2k + 1 = 41 samples
for i = k+1:n-k-1
filtsig(i) = mean(signal(i-k:i+k));
end
That is a convolution with a flat kernel — a crude low-pass filter. Its loop stops one sample early (the last full window is centred on \(n - k\), not \(n - k - 1\)), and both edges are left at zero; the comment says “from 21 to 2080”, but with \(n = 3001\) it runs to 2980. gaussian_filter.m swaps the flat kernel for a Gaussian, whose smooth edges avoid the ripples a box kernel adds in the frequency domain.
Spikes: the median
An average is dragged by every extreme value in its window. The median — the middle value after sorting — ignores a minority of them entirely.
median_filter.m uses it surgically: it applies the median only at samples above a threshold read off a histogram, leaving the rest of the signal untouched. Custom Filter/1_median_filter.py instead runs signal.medfilt with 3, 11 and 21 points over the whole 1 kHz + 15 kHz test signal. A median is not a low-pass filter, so its effect on the 15 kHz ripple is uneven: its DFT bin drops from 80 to about 12 with 3 or 11 points and to 3 with 21 points, while the 1 kHz wave is also flattened at its peaks.
Trends and energy
A slow drift can swamp everything else in a spectrum. 0_detrend.py shows signal.detrend removing a constant or a straight line, and linear_detrending.m does the same with MATLAB’s detrend on a random walk plus a ramp. nonlinear_detrend.m goes further, fitting polynomials with polyfit and subtracting the best one. Lesson 12 shows what happens when a trend is left in.
TKEO_filter.m computes the Teager–Kaiser energy operator, \(\Psi[n] = x[n]^2 - x[n-1]\,x[n+1]\), on an EMG recording. It responds to both amplitude and frequency, so a muscle burst jumps out of the baseline far more clearly than in the raw signal. (Its data file, emg4TKEO.mat, is not in the repository.)
Outliers
std_thresholding.m flags every sample above mean + 3 standard deviations, then replaces it by interpolating its neighbours:
threshold = mean(signal) + 3*std(signal);
outliers = signal > threshold;
F = griddedInterpolant(time(~outliers), signal(~outliers));
signal(outliers) = F(time(outliers));
The weakness is that the outliers are included when the threshold is computed, and they inflate the standard deviation — so some of them hide below it. Simulating the script’s setup 200 times (10,000 samples, 50 outliers), the rule caught 86% of the outliers on average and as few as 72% in the worst run. Computing the threshold from robust statistics — the median and the median absolute deviation — removes that blind spot.
sliding_RMS.m handles the opposite case, where a whole segment is bad: it computes the RMS in a sliding window and blanks the stretches where it exceeds a threshold — a local test rather than a global one.
Try it yourself. Port std_thresholding.m to NumPy and count how many of the 50 planted outliers it catches. Then replace the threshold with np.median(s) + 3 * 1.4826 * np.median(abs(s - np.median(s))) and count again.