Average value

3 posts in this topic



Is there any FB for average value calculation?


I did add Value to int every secend and devide it value by passed secends. Only problem is that by the time goes on int will be full?




Share this post

Link to post
Share on other sites

maybe... did you check the instruction list?

and you should always post your code and expectations. (resolution, time span, accuracy...)


if i read this right you have a problem due summation since average is computed by:

AVG(n) = (X1+X2+...Xn)/n

it appears that you are attempting to store top side of the fraction into one variable and of course this will overflow pretty soon.

solution to not do that... note that the very next average will be

AVG(n+1) = (X1+X2+...Xn+1)/(n+1)

but this can also be written as this (isolate last term)

AVG(n+1) = (X1+X2+...Xn)/(n+1) + Xn+1/(n+1)


AVG(n+1) = (AVG(n)*n + Xn+1)/(n+1)

but that will overflow the same way as before due multiplication AVG(n)*n.

however this can be rewritten as

AVG(n+1) = AVG(n)*n/(n+1) + Xn+1/(n+1)

where AVG(n) is previous average, Xn+1 is the latest sample, and "n+1" is the latest sample count.

as long as you compute n/(n+1) before multiplying AVG(n), there should be no problem since n/(n+1) will grow but never exceed 1.

ultimately this will settle at some average value since limit of  Xn/n goes to zero as n goes to infinity so adding more samples will just add zeroes and average will stop changing. 

this begs question... are you really really sure you need total average over indeterminate and always increasing time interval and not average of several most recent samples? one would normally look at average only over fixed number of samples. that number can be small or large but it should be fixed.

if you really want to cover "endless" interval, you need to define what is "endless".


1 person likes this

Share this post

Link to post
Share on other sites

I think you are looking for the function MovingAverage. Often called a floating average. Essentially a FIFO of values that is divide by the number of values.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now