How to calculate moving average speed from GPS?

前端 未结 2 1715
悲哀的现实
悲哀的现实 2021-02-11 08:14

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 u

2条回答
  •  余生分开走
    2021-02-11 09:17

    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.

提交回复
热议问题