Android – How to calculate moving average speed from GPS

androidgps

I'm developing an android application using GPS. I'd like to implement a feature that displays the users average speed over the 1/5/15 minute. Something like the CPU load on unix. I can calculate average easily by cumulating the distance traveled second by second and divide it by the elapsed time, but I can't think of a smart way of calculating the moving average.

Obviously I can get id done by putting the distance between the last and the current position in an array every second while deleting the oldest value.

I'm looking for a neat way of doing this.

Best Answer

Heres one way to go about it that is pretty straight forward:

If you are sampling position every second keep 901 samples in a queue, thats 15 mins worth (and 1 extra). Position 0 is the most recent measurement, effectively your current position.

For an average speed over the last X minutes:

s = X * 60;
point1 = postion_queue[0]; // this is your current position
point2 = postion_queue[s]; // this is your position s seconds ago
d = distance_between_points(point1, point2);
speed = d / s;

speed is now distance units per second, convert to mph, or kph or whatever units you need. Different values of X can be used for any average between 1 and 15 minutes.

Related Topic