I have a GPS that I am using to get the speed of an implement and then control come solenoids based on the speed and a switch case programmed into the ardunio. I am trying to smooth the GPS responses by averaging the numbers. I understand that I could simply make them and int or take less readings but .25 MPH matters to a point (660 mistakes per hour which is within tolerance).
I have looked at endless sketches and read as many things that DEV has had to say about GPS.
I found this code in one of him examples:
static uint16_t averageKnots = 0;
averageKnots = (averageKnots * 3) / 4 + knots;
knots = averageKnots / 4;
It isnt making mathematical sense to me. Can someone explain this.
I feel like something isn't quite write with the code. It seems backwards a bit but I am the newbie and you all have tons of experience so I'm asking before being told Im a know it all......
The snippet may be out of context, but it makes no sense.
Here is the general approach for a moving average (digital low pass or exponential filter) with fixed fraction alpha = 1/4 applied to the incoming data. You can choose any fraction between 0 and 1.0. Filter response is slower, with lower values of alpha.
The code is all in my message (except, of course, "rollingSumKnots" has to be initialized to 0, as in the sketch you gave.
My two versions do the same thing. What I am calling rollingSumKnots is the sum of all the speeds up to the present speed, with past speeds discounted by 3/4 at each step. That is
That's a pretty wasteful and limited implementation:
It would be much more efficient to keep a running sum, rather than adding all elements on each iteration. The modulo operation to wrap around is unnecessarily expensive as well.