# Is this code for doing a moving average correct?

Is this code for doing a moving average correct?

y=conv(x, ones(1,100)/100);


I want to filter my speech signal with moving average filter in code. I think that I can use convolution for this.

• @JRE: Just edit it, as you have done. No need for <Pedantic jerk mode /> :-) – Peter K. May 24 '15 at 17:50

Presuming you are using Matlab or GNU Octave, then yes, that should be OK. Compare with this tutorial on doing a moving average with convolution.

You've done the division on your kernel, but it makes no mathematical difference whether you do it there or after the convolution. Practically, it takes less time if you do it your way (scaling the kernel.)

You may find that a moving average is not adequate for audio processing. You will then need to look into creating filters with the appropriate methods in Matlab. Some keywords to search for together with Matlab: filter, butter.

Good suggestion from @jojek. The smooth does a moving average by convolution, but has other options as well. It can also make use of a GPU to speed things up if you have a truly massive amount of data to be filtered.

Yes, this code is correct for implementing a moving average filter.

Nevertheless I do recommend to use the in-built smooth function in MATLAB.

y = smooth(x, 100, 'moving')