Running Average in Comp

More
23 Apr 2009 11:45 - 23 Apr 2009 12:52 #136 by BigJohnT
I'm writing a component using comp and I need to get a running average of a float input.

I see in the comp section of the hal manual that I can create the array by doing:

variable float myarray[10];

Is there a slick way to push a new number into an array then get the average?

I'm looking to cut out some of the noise on the velocity output from the 5i20 encoder.
So if I'm way off base with a running average please let me know.

Thanks
John
Last edit: 23 Apr 2009 12:52 by BigJohnT.

Please Log in or Create an account to join the conversation.

More
23 Apr 2009 13:17 #138 by BigJohnT
Replied by BigJohnT on topic Re:Running Average in Comp
Chris suggested that I look at lowpass.

Thanks
John

Please Log in or Create an account to join the conversation.

More
24 Apr 2009 06:51 #141 by alex_joni
BigJohnT wrote:

variable float myarray[10];
Is there a slick way to push a new number into an array then get the average?


Usually you use another variable (lets call it last) which point to where you last entered a value.
so you do
myarray[last++] = newvalue;
if (last >= 10) last = 0; // should only need ==10, but it can't hurt to be a bit more paranoid ;)

For the average you have to add them, then divide by 10. There are other "smarter" algorithms, but for 10 items it's not worth the extra effort.

otoh, the lowpass filter should be ok for you ;)

Regards,
Alex

Please Log in or Create an account to join the conversation.

More
24 Apr 2009 12:55 #142 by BigJohnT
Replied by BigJohnT on topic Re:Running Average in Comp
Thanks Alex,

Looks like the math and the programming on the lowpass is simpler and limits the change to what ever the gain is set to from input to input which should smooth things out.

out += (in - out) * gain

Took me a second or three to remember what the assignment by addition did but once that clicked...

John

Please Log in or Create an account to join the conversation.

More
25 Apr 2009 12:18 #152 by BigJohnT
Replied by BigJohnT on topic Re:Running Average in Comp
Using the lowpass on the input smoothed out the velocity in real nice.

John

Please Log in or Create an account to join the conversation.

More
09 Mar 2010 14:08 #2255 by tom3p
Replied by tom3p on topic Re:Running Average in Comp
i've done running averages with weighting of the new information
like 20 parts of old value plus 1 part of new divided by 21
the more old stuff in the recipe makes new stuff matter less
less stuff in the recipe makes new stuff matter more
by adjusting the the number of samples, you vary between very responsive to sluggish
its easy to imagine the new value influencing the result a lot with (1old + 1 new) /2
and not making a big diff with (20 old + 1 new)/21
maybe this is what lowpass does, but 'lowpass' sure doesnt remind me of this concept
if so, make an alias called 'runningAvg' ;)

Please Log in or Create an account to join the conversation.

More
09 Mar 2010 14:12 #2256 by tom3p
Replied by tom3p on topic Re:Running Average in Comp
btw: a running average has to 'charge up'
the output value is garbage untill its accumulated n samples
where n is the denominator in ( (n-1)*oldval + newval) / n

Please Log in or Create an account to join the conversation.

Time to create page: 0.074 seconds
Powered by Kunena Forum