5.0

5.0 | 2 ratings Rate this file 7 Downloads (last 30 days) File Size: 4.55 KB File ID: #19504

fast running mean

by Neil Hodgson

 

08 Apr 2008 (Updated 10 Apr 2008)

fast recersive running mean in 2D or 3D.

| Watch this File

File Information
Description

[outmat] = fastrunmean(inmat,win,ptype);

fastrunmean computes the running mean of 'inmat' over a window of size 'win'. The argument 'ptype' controls the padding type, which decides how the edges are padded. An efficient recursive algorithm is used that is independent of the window size, so that the computational cost is only proportional to the size of the size input matrix.

Input
inmat - a 2D or 3D matrix
win - a matix of window lengths in each dimension. Windows should be odd in size!
ptype - a string describing how to treat edge padding
           'zeros' - pad with zeros
           'mean' - pad with the mean of inmat
           note: plan to add more options when I have time.

Output
outmat - boxcar filtered inmat.

Much faster than SMOOTHN for boxcar filters and extends RUNMEAN to 2 and 3 dimensions.

Example and speed test

m2D = randn([600 300]);
m3D = randn([100 100 100]);

2D

d = fastrunmean(m2D,[51 51],'zeros');

3D

d = fastrunmean(m3D,[51 21 73],'zeros');

Speed test

for n=1:100
tic;d=fastrunmean(m2D,[51 51],'zeros');t(n)=toc;
end
fprinft('Averaged time per call over 100 calls = %d s',mean(t))

for n=1:100
tic;d=fastrunmean(m3D,[51 51 51],'zeros');t(n)=toc;
end
fprinft('Averaged time per call over 100 calls = %d s',mean(t))

You can alter the size of the windows to see that mean(t) is independent of the window size. The time is averaged to allow for fluctuations in computer performance. The average time will give you a feel for the speed.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
smoothn

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
09 Apr 2008 Neil Hodgson

There are some bugs - updated 14:00 09-APR-08 but it takes a while for the update to come through.

08 Mar 2009 Andre  
24 Mar 2009 C Schwalm

I like this. A few suggestions:
Add functionality for 1D arrays (vectors), it would be nice to have one stop shopping, and runmeaning a vector is quite a common task.
Add a step parameter,for example: Let's say I want a 13-point running mean but I only want this every 2 points, so I'm halving the size of the array along that dimension.
Add more padding options.

Please login to add a comment or rating.
Updates
09 Apr 2008

Sorted out a couple of minor bugs. I submitted it too hastily!

10 Apr 2008

More silly bug fixes - there was and winx where there should have been a winz...

10 Apr 2008

Added some examples. Changed the error checking a little.

Tag Activity for this File
Tag Applied By Date/Time
filtering Neil Hodgson 22 Oct 2008 09:56:50
mean filter boxcar fast Neil Hodgson 22 Oct 2008 09:56:50

Contact us at files@mathworks.com