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

Posted by Doug Hull,

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.

## 5 CommentsOldest to Newest

Ramesh replied on : 1 of 5
How to convolve the 1D array of complex numbers using a Gaussian Kernel?
Martin Offterdinger replied on : 2 of 5
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 512x512 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....
Doug replied on : 3 of 5
Try this: im = rand(150,150,50); mean(im,3)
Zoltan replied on : 4 of 5
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
Bharath replied on : 5 of 5
Dear Doug, I think something that is missing in the video is that the "theoretical background" that is necessary before sliding the mask function (e.g. [a b c]). The sliding/mask function g(t) has to be reversed before sliding it over the main function h(t). Conv = Int_{x}^{y} [h)(t) * g(tau - t)] dt So, if you go by this definition, my mask function (mask = [a b c]) has to be - new mask = [c b a] before sliding from left. Or the original mask function can be used if the sliding is done from right end. In the example considered in the video, the values of a and c in the mask function are same, i.e. 1. Hence time reversal of this particular mask function is not necessary (although done, the results are same). For more understanding, an ex. a = [1 2 3 4 5] and mask = [1 2] can be tried. Please correct me if I am wrong.

This site uses Akismet to reduce spam. Learn how your comment data is processed.