2
$\begingroup$

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.

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

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.

$\endgroup$
2
$\begingroup$

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')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.