Gradient Algorithm

As you know, Garmin's internal GRADE metric is not exposed to us.

I've toyed with several algorithms and am pretty happy now. I run a 12 second rolling linear average. It causes a lag in reporting gradient changes, but better than the noise of the value bouncing around a lot. Recording Elevation and Distance and simply doing 100*deltaElev/deltaDist. I added some logic to fast start the calc when I detect 3 consecutive elevation points in the opposite direction of the trend. And I reset the rolling avg when speed drops below 1mph (walking slow or stopped).

That seems to finally give me a decent stable and reasonable gradient metric that I combine with Vertical rate (feet or meters) when grade > 2.5%.

Anyway, I was considering getting a bit more sophisticated and implementing a 20 point exponentially weighted moving average (EWMA). What that does is count the more recent points more than the older points (say from 10 or 15 seconds ago), and in theory would respond quicker to a change in the road's grade, but also tame the noise compared with just looking at the last few seconds. But depending on the decay rate of the weights, that could just make things noisy again.

Curious if anyone has tried EWMA for grade? I know - #nerdAlert But that's what happens when an engineer rides a bike ;-)
  • Dave,

    that will be a tricky task especially because the CPU's of the watches are lame ducks. Maybe some kind of a low pass filter could be done to ignore errornous (?) peak values but maybe you can simplify the algorithm from David H. Douglas and Thomas K. Peucker which is doing a great job by reducing data without eliminating it's characteristics.

    It was used to reduce data points for the cartography but I have used the algorithm for different things already (data reduction, line smoothing).

  • I figured it out! Duh.... The problem is that ALTITUDE is not updated every second... So on, say, a 12% grade.... the altitude might stay at 5200 ft for 4 seconds, then jump to 5273 ft... So grade appears to vary between, say 7% and 20%. Which sucks.

    The obvious solution is to monitor for the CHANGE in altitude and only then regenerate the current grade. That will stabilize the metric. And explains why Garmin's built in grade is stable.

    So, I'm now thinking I don't need to get crazy with filters and weightings... and may be able to reduce the rolling window duration so lag is reduced.