movmean

Moving average over a sliding window.

Category: Data Import and Analysis > Preprocessing Data

Syntax

M = movmean(A, wlen)

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

M = movmean(A, [nb na])

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

M = movmean(A, wlen, 'Endpoints', 'discard')

Drops the positions where the window does not fit entirely.

Description

movmean is the classic moving average, used to smooth noise and reveal a trend. A longer window smooths more but blurs fast changes. A centered window does not shift the signal in time, while a one-sided window such as [k 0] follows the data with a delay.

By default the window shrinks near the ends so that the output has the same length 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.
'Endpoints'
How to treat the ends: 'shrink' (default) uses the available elements; 'discard' removes the incomplete positions.

Output arguments

M
Smoothed array. Same size as A unless endpoints are discarded.

Example

Centered, trailing and discarded ends

x = [1 2 3 4 5];
movmean(x, 3)
movmean(x, [1 0])
movmean(x, 3, 'Endpoints', 'discard')
Output
ans = 1×5

    1.5000    2.0000    3.0000    4.0000    4.5000
ans = 1×5

    1.0000    1.5000    2.5000    3.5000    4.5000
ans = 1×3

     2     3     4

See also

movmedian movsum smoothdata filter conv