How to calculate moving average speed from GPS?

别说谁变了你拦得住时间么 提交于 2019-12-03 09:10:54
Florian Brucker

You will need to store all the values for the whole time span, as you already suggested. The reason is that you somehow need to "forget" the contributions of the old values to the moving average. You can't do that exactly if you don't know what these values where (i.e. if you do not store them).

In your case, 1 value each second for 15 minutes amounts to 15 * 60 = 900 data points, that should be OK.

Note that you do not need to perform a sum over the whole array each time you update: You can calculate the new moving average from the number of data points, the new value and the value you are "forgetting" at that moment:

new_average = (n * old_average - x_forget + x_new) / n

Here, n is the number of data points (900 in your case), x_forget is the value you are "forgetting" and x_new is the latest value. You then drop x_forget from the front of your array and store x_new at the end. Instead of an array you might want to use a queue implemented via a linked list.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!