# Doug's MATLAB Video Tutorials

## Using convolution to smooth data with a moving average in MATLAB

I teach the introduction to MATLAB classes for all new hires in the Technical Support group at MathWorks. One of the attendees wanted to know how to do a moving average in MATLAB. This can be useful for filtering, or smoothing, noisy data. I realized I had never covered that on the blog, so here we go! I show how to do this from scratch using conv. If you have the Curve Fitting Toolbox, you might want to check out smooth, which adds some fancier smoothing methods.

### 4 Responses to “Using convolution to smooth data with a moving average in MATLAB”

1. Ramesh replied on :

How to convolve the 1D array of complex numbers using a Gaussian Kernel?

2. Martin Offterdinger replied on :

How can I generate a moving average of a series of images of the form ImSer=(x,y,t); t being a time course. The series could eg be 512×512 pixels and 50 times points. The goal is to remove slow movements over time. I would require a way to specifiy a dimenson over which the average should be done. In this case a time average….

3. Doug replied on :

Try this:

im = rand(150,150,50);
mean(im,3)

4. Zoltan replied on :

Well, it is great to see how much different tasks the convolution can be used for. A real friend of engineers. :)

As a supplement to this post, here is an online demonstration to play around moving average: http://matlabtricks.com/post-4/online-demo-on-simple-moving-average

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 2 + 3 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

These postings are the author's and don't necessarily represent the opinions of MathWorks.