Confused by smoothing code - Am I being thick?

I am having difficulty understanding the smoothing code on the arduino site (can't link, forum won't let me). I'm smoothing some IR sensor readings and it seems to do it quite well, I just haven't the foggiest idea on how it works. I think I might be being a bit thick.

Could someone please explain how it works to me? I can't even get it from examining the comments. :frowning:

My main concern is that since I am making a segway type thing, that if one of the numbers rolls over or something I'll end up being curbstomped by my own creation. Could this happen, and if it could would it take so long that it wouldn't happen within the battery life anyway?

I tried to write some of my own code that used an array which repeatedly updated and was averaged out, but that failed miserably. If you have any simpler smoothing code that I could have a look at, that would help me loads.


It's an "exponentially weighted moving average"; aka EWMA. It helps me to understand how it works if I expand it six or seven times and put the equation into a spreadsheet. Now that you know what it's called, I suspect you can find a good explanation through Google. :wink:

An answer to your other question: I don't think you're being thick. I had trouble getting my head around EWMA until I worked through a few problems by hand. ::slight_smile:

Very quick and dirty smoothing algorithm in one line :

rollav = rollav * 0.9 + float(analogRead(0)) * 0.1;

rollav is defined as a float.

Wow, that must be one noisy component :wink:

Quicker and dirtier:

const int SMOOTH_FACTOR = 3;
rollav = (rollav - (rollav >> SMOOTH_FACTOR)) + (analogRead(0) >> SMOOTH_FACTOR);