File Exchange

image thumbnail

Moving Average Function

version 1.0 (5.5 KB) by

Calculates a moving average in an n-dimensional matrix in any dimension.

135 Downloads

Updated

View License

result=movingmean(data,window,dim,option) computes a centered moving average of the data matrix "data" using a window size specified in "window" in "dim" dimension, using the algorithm specified in "option". Dim and option are optional inputs and will default to 1.

Dim and option optional inputs can be skipped altogether or can be replace with a []. For example movingmean(data,window) will give the same results as movingmean(data,window,1,1) or movingmean(data,window,[],1).

Input data matrix size and dimension is only limited by the maximum matrix size for you platform. Window must be an integer and should be odd. If window is even then it is rounded down to the next lower odd number.

Function computes the moving average incorporating a center point and (window-1)/2 elements before and after in the specified dimension. At the edges of the matrix the number of elements before or after are reduced so that the actual window size is less than the specified window.

The function is broken into two parts, a 1d-2d algorithm and a 3d+ algorithm. This was done to optimize solution speed, especially in smaller matrices (i.e. ~1000 x 1). Further, several different algorithms to the 1d-2d and 3d+ problem are provided as in certain cases the default algorithm is not the fastest. This typically happens when the matrix is very wide (i.e. 100 x 100000 or 10 x 1000 x 1000) and the moving average is being computed in the shorter dimension. The size where the default algorithm is slower will depend on the computer.

Comments and Ratings (11)

Glen

Glen (view profile)

Melissa,
The function only works in 1-D (i.e. it treats any matrix as a collection of vectors) so it would not work for calculating an average over a circle centered over each data point in a grid of data.

A simple, albeit computationally slow approach would be to step through each data point in your grid (h,k) and determine which data points (x, y) are within the specified radius, i.e. look for all x, y where (x-h)^2+(y-k)^2 <= radius^2, then average the values of the corresponding data points. Since x, y, h, and k are all integers your "circle" will have a jagged edge, but you may be able to deal with that with some rounding. Vectorizing this code for computational speed might be a challenge, especially with the case of data points near the edge of the input matrix where you don't have a full circle of available data points around your central data point (the number of valid data points incorporated into each average can vary, which complicates vectorizing the code).

I will think about this and see if any specific solutions come to mind.

Glen

Hello,

I might be missing something, so I want to clarify real quick. I'm looking for a moving window average that will incorporate all the data values that are within a spatial circle from my point of interest. My data is currently in grid format (or raster format if you are more familiar with that terminology). Will this function calculate the moving window average within a circle around each point? If not, do you know a good approach to take to do this?

Thanks!

Kari Vienola

Glen

Glen (view profile)

Patrick,

The function deals with ends by clipping the trailing or leading portion of the window and transitioning to a leading or trailing moving average instead of a centered one. To go with the example you gave in your comment if the window size is 3 then at a center of 1 the function averages data from points 1 and 2; at a center of 2 points 1, 2, and 3 are averaged; at a center of 9 points 8, 9, and 10 are averaged; and at a center of 10 (lets assume the vector has 10 entries) points 9 and 10 are averaged.

How does movingmean deal with the ends? Does it start with a window size encompassing only point 1 at 1, then 3 points at point 2, then increasing in window size until the window size is that specified in the function input? Thanks.

Hause Lin

Radim

Radim (view profile)

Nice and simple. Thank you.

Good job! Very useful as Stephan Wolf said.

Barbara

Barbara (view profile)

Stephan Wolf

Just what i was lookin for. Centered moving average that is able to work in a plot over the whole width, without having to look for window size of the filter and moving the beginning. Great!

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today