Filtering/Averaging Question

Hi everybody!

My most recent project is reading servo signals with an Arduino. This works quite fine at this point but I was wondering how I could increase the accuracy of the detected pulselengths. As I am not in need of an enormous reaction speed, I thought about filtering/averaging the values.

My first thought was this:

averagedValue = ( averagedValue + newValue ) / 2;

Of course this could be tweaked to what fits best, by simply writing:

averagedValue = ( (ratio * averagedValue) + newValue ) / ( ratio + 1 );

Now, my question is: Are there any other filtering/avering methods that work better/are more feasable for this project?

Thanks in Advance!!!

Are there any other filtering/avering methods that work better/are more feasable for this project?

You can write whole books about digital filtering. The more complex they are the more processing time they take up.

One way it to take n samples, where n is any number greater than 3 so you might like to start with 10. You have these samples in a circular buffer but also have a running total. Each time you take a new sample you subtract the old sample from the running total and store the new sample in its place. Then you add this new sample to the running total. Then to find the average divide the running total by n. This messing about reduces the number of arithmetic operations to three, one add, one subtract and a divide. The alternative is to add up all the samples in the circular buffer and then divide by n, but that has n additions and so will take longer.

This filter will respond slower to changes in the input and so will smooth more.

Thanks for the quick answer!

But one thing is unclear to me:

Grumpy_Mike: Each time you take a new sample you subtract the old sample from the running total and store the new sample in its place.

What do you mean by saying "old sample"?

Do you mean the oldest or the second newest? If the second is the case, you'll have to explain it in more detail to me, otherwise I think I won't get it ;)

Probably I would reduce the number of samples to 5 or so, since the period of a servo signal is 20 ms and I dont want the controls to be too sluggisch, but I'll see when the hardware for the project is done.

Otherwise, it sounds like a good idea, thanks!

What do you mean by saying “old sample”?

The sample you took n samples ago, that is the oldest. So your array only has n of the most recent samples.

Ok, that makes sense. Thanks!