Main Content

Remove Spikes from a Signal

Sometimes data exhibit unwanted transients, or spikes. Median filtering is a natural way to eliminate them.

Consider the open-loop voltage across the input of an analog instrument in the presence of 60 Hz power-line noise. The sample rate is 1 kHz.

load openloop60hertz

fs = 1000;
t = (0:numel(openLoopVoltage) - 1)/fs;

Corrupt the signal by adding transients with random signs at random points. Reset the random number generator for reproducibility.

rng default

spikeSignal = zeros(size(openLoopVoltage));
spks = 10:100:1990;
spikeSignal(spks+round(2*randn(size(spks)))) = sign(randn(size(spks)));

noisyLoopVoltage = openLoopVoltage + spikeSignal;


xlabel('Time (s)')
ylabel('Voltage (V)')
title('Open-Loop Voltage with Added Spikes')

Figure contains an axes. The axes with title Open-Loop Voltage with Added Spikes contains an object of type line.

yax = ylim;

The function medfilt1 replaces every point of a signal by the median of that point and a specified number of neighboring points. Accordingly, median filtering discards points that differ considerably from their surroundings. Filter the signal using sets of three neighboring points to compute the medians. Note how the spikes vanish.

medfiltLoopVoltage = medfilt1(noisyLoopVoltage,3);


xlabel('Time (s)')
ylabel('Voltage (V)')
title('Open-Loop Voltage After Median Filtering')

Figure contains an axes. The axes with title Open-Loop Voltage After Median Filtering contains an object of type line.

See Also

Related Topics