movmedian

Moving median over a sliding window.

Category: Data Import and Analysis > Preprocessing Data

Syntax

M = movmedian(A, wlen)

Median over a window of wlen elements centered on each element.

M = movmedian(A, [nb na])

Window of nb elements before and na after the current one, plus the element itself.

Description

The median of each window ignores extreme values, so movmedian removes isolated spikes while keeping sharp edges, something a moving average cannot do. It is a standard way to clean impulsive noise.

The window shrinks at the ends, so the output has the same size as the input.

Input arguments

A
Vector or matrix; operates along the first non-singleton dimension.
wlen
Window length. For an even length the window is centered between elements, with one more element before than after.

Output arguments

M
Array of the same size as A.

Example

Removing a spike

x = [1 2 3 40 5 6 7];
movmedian(x, 3)
movmean(x, 3)
Output
ans = 1×7

    1.5000    2.0000    3.0000    5.0000    6.0000    6.0000    6.5000
ans = 1×7

    1.5000    2.0000   15.0000   16.0000   17.0000    6.0000    6.5000

See also

movmean movmad smoothdata