Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Using a Moving Average Filter on Audio (Read 6462 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Using a Moving Average Filter on Audio

Hi, I've been reading up on how to implement digital filters in software - mostly using Smith's "The Scientist's and Engineer's Guide to DSP".  Making a Moving Average Filter is my first attempt at implementing a filter.  The idea behind the filter is very straight forward: the output of a sample will be an average of the given sample and it's neighbor samples. 

For example, using a 5 point moving average filter where "x" is the input data and "y" as the output data, sample 80 is calculated as:

Code: [Select]
y[80] = (x[78] + x[79] + x[80] + x[81] + x[82]) / 5;


So, in C++ I made a simple Moving Average Filter.  For testing input data I recorded some 16 bit mono PCM audio of me just talking and then I randomly generated some static and mixed the two together.  My expected outcome was that the static noise would be lessened but this was not the case.  After trying many different values as the number of points for my moving average filter I always pretty much got the same output: a low volume static and my vocals had disappeared.

So, my question: Is it possible that moving average filters are not really meant to be applied to audio, or time domain signals in general?  The algorithm is very straightforward and I've reviewed my code many times and I'm not noticing any problem with the calculations.  So, I'm starting to think that this filter and audio just don't mix do well. 

Any comments or advice?  Thanks.

Zeke

Using a Moving Average Filter on Audio

Reply #1
What you are trying to do should work fine. It will basically be a poor quality lowpass filter and your voice should still be fine. If your voice is gone, there is something wrong with your implementation. Make sure that when you sum the values you don't do it in a 16bit variable because you'll get overflow, and remember that everything is signed.

I would try something even simpler first, like divide every sample by 2 (which should just reduce the volume). Then try working with more than one sample for input.

Oh, and if you're not on a PC you might have to deal with the endianess of the samples (which are little-endian in wav files).

Good luck! 

edit - added endian issue

Using a Moving Average Filter on Audio

Reply #2
Make a test signal to feed your filter, consisting of a ramp from -32768 to +32767, and analyze filter output in waveform editor (I suspect overflow on 5 step addition, or signed unsigned mix up)

I  played around with software audio filters as well.For best quality I used:
-convert input data to (long) floats. (extra headroom, don't worry about overflow, don't create multiple rounding errors)
-do arithmetic
-clip peeks to 32767 and -32767
-convert back to 16 bit (for Hi Quality use dither)

Using a Moving Average Filter on Audio

Reply #3
Doh!  You guys are absolutely right.  My buffer for the 16 bit data is made up of short ints.  I've done sample manipulation a million times and just happened to overlook the overflow this time - too little sleep while programming this last night.  Apologies for my error and thanks for the feedback concerning this.

Zeke

Using a Moving Average Filter on Audio

Reply #4
What you are building is called a FIR-filter, one of only two basic digital filter architectures.

On a simplified level, FIR-filters are the basis for stuff like artficial reverbrators, filters, convolution boxes, (multitap) delay boxes, just about anything you can think of that statically modify frequency response/impulse-response in an audio signal (linear processing function) can be implemented as a (potentially large) number of taps just like you describe.

http://www.bores.com/courses/intro/filters/4_fir.htm

Using a Moving Average Filter on Audio

Reply #5
A moving average filter is nothing but a bad lowpass filter with a rectangular window response.

See, among other things, http://www.aes.org/sections/pnw/ppt/filters/filtutv1.ppt for more information than you may have ever wanted.
-----
J. D. (jj) Johnston

 

Using a Moving Average Filter on Audio

Reply #6
my bad

-k