In this article, Gaurav Singh from Embedded Engineering website shows you how to implement the simplest possible digital filter “moving average filter”.
Though it is very easy to implement but still in many applications this is more than good enough. for example reducing random noise from signal. ofcourse when it is very simple it does have issues like , it does no have very sharp filter Response.
there not much need to be done to calculate output signal. you just need to decide how many point average you want to take for each output point. the more cutoff frequency will go less and less as you increase number of point to be take for average. A 5 point average will have pretty high cutoff frequency than a 11 point average.
for a point a 5 point average is calculated with this simple formula, we take two samples from back and two from front and calculate average for current sample.
o u t [ i ] = i n p u t [ i − 2 ] + i n p u t [ i − 1 ] + i n p u t [ i ] + i n p u t [ i + 1 ] + i n p u t [ i + 2 ] 5
little issue come up when i < half of average lenght , because it will make few of the index for input “negative”, >as there are very few programming language that support negative index we need to a little adjustment the calculation. we will only take sample from ahead of the current position , this arrangement not going to just going to shift the signal a little ahead in time , other property will remain same.
o u t [ i ] = i n p u t [ i ] + i n p u t [ i + 1 ] + i n p u t [ i + 2 ] + i n p u t [ i + 3 ] + i n p u t [ i + 4 ] 5
if you wanted to look at more of mathematical equation
o u t [ i ] = 1 M ∑ j = 0 M − 1 i n p u t [ i + j ]
M is number of sample taken for averages ,
this whole process is implemented using convolution operation. if you look at filter kernel for 3 point kernel , it will look like ….0,0,0,1/3,1/3,1/3.0,0,0……
Frequency Response o u t [ f ] = s i n ( π f M ) M s i n ( π f )
|FFT of Input Sinal|
|Frequency domain , Pass(1 point ‘no avg’)|
Fimrware is quite simple , i use ezdsp5502 board with on board TMS320VC5502 and TLV320AIC23B codec. TLV320AIC23B codec is used to sample input signal at 48Khz rate and same is used to play back filtered signal , due to limitation you can see the maximum amplitude is limited.
mcBSP is used to transfer sampled data between dsp and TLV320AIC23B, TLV320AIC23B works as I2S master and send data to DSP , DSP has mcBSP receive interrupt enabled , mcBSP isr receive data and put into a three part ring buffer.
Ring buffer is maintained as three 64 sample long array , one 64 sample long array for input anther for processing and last one for output. these buffer keep on cycle through different stages.
firmware can handle negative array index. though you can easily getaway without this
all source code is available on this github repo.