How do you calculate a rolling average speed?

1k Views Asked by At

I am making a speedometer for my bike with an arduino and I have got it to display speed now I want to display average speed. But I'm struggling to think of how to do it. The speed is checked 1000 times a second.

If anyone could help that would be great. Thanks!

4

There are 4 best solutions below

0
On

If you want the average speed over the last $5$ minutes you can just add up the last $300,000$ speed values and divide by $300,000$. Once you get the sum started you can just update it by adding the new value and subtracting the old one. Storing the speed data in a circular buffer will make this easy. You might want to add the speed data in blocks of $1000$ to get a speed for the second first.

0
On

Calculate the integral wrt. Time using Trapezoidal Rule- high frequency of readings, so this is accurate enough.

Calculate between -T and +T , which is your interval of interest, dividing by 2*T to get the average.

For the next moment discard the earliest reading(s) , adding the same number of fresh ones at the 'new' end, keeping the width 2T, and always dividing by 2T.

Hence your rolling average.

0
On

You can use a ring buffer of appropriate size. Then the average speed is the sum of the numbers in the ring buffer divided by the count of numbers currently in the buffer. The sum of the numbers at time $t+1$ is the sum at time $t$ plus the number added to the ring buffer minus the number that leaves the ring buffer (or zero if the buffer is not full yet).

However this probably not a mathematics question.

0
On

Cheers for your help guys.

I did it on my own using a spreadsheet and patience. The formula is below, Avmph is the current average speed and inc is incremented after each time the speed is measured.

Avmph =((Avmph*(inc-1))+mph)/inc