# Averaging Floating Numbers

## Recommended Posts

Hey All,

Anyone have an elegant way to do an average on floating (MF) numbers?

I am reading a float value from a device every 10 seconds, but saving that value once a minute to a Data Table, I would like to keep a running average of the 6 reads per minute and store that instead of whatever the value is at that instant. Some sort of FIFO arrangement where the newest value comes in and the oldest value drops off with an output value of the running average.

I've used the Filter FB before with integers, but that doesn't work with floats (unless I'm missing something).

Any Ideas?

Regards,
JohnR

##### Share on other sites

• MVP 2022

Can't you just use a vector of the 6 MFs? Store the new value using an indirect address for the MF, increment that indirect value, then sum and divide for your moving average. This should work fine if you only have 6. If you have a hundred or a thousand values to average, I have developed an elegant solution for that, but it's only an approximate calculation.

##### Share on other sites

• MVP 2022

Flex's answer is correct, but there is also the question that needs to be answered that affects the way you do the maths.  Do you want to average only the last 6 readings, or do you want to leverage in the rolling average?  Both ways are just different maths, therefore you need to know which one you want to use.

cheers, Aus

##### Share on other sites

• MVP 2022
12 minutes ago, Ausman said:

Yay! I'm 1 for 1 today!

##### Share on other sites

• MVP 2022

Flex.....😀

I should add that if you need to do the leveraged average, depending on how you do this it can take ages to get it up to your ideal reading amount.  This can be a problem on a full init and reset, or power cycle of sensors that need time to stabilise properly.  You either need to add in a suitable delay on any actions associated with the average, or have the average over-ridden for a set time with an amount close to the normal running situation.

cheers, Aus

##### Share on other sites

16 hours ago, Flex727 said:

Can't you just use a vector of the 6 MFs? Store the new value using an indirect address for the MF, increment that indirect value, then sum and divide for your moving average.

Flex, this is the direction I was leaning, something like a Vector-Shift-Left, plug a value into the starting address, which then bumps the rest of the values down, then do the math.

I see the Store Direct under Math/Float/Basic functions, but (unless I'm missing the obvious), you can't do  vector functions with MF's.

So, are you thinking that I should have a net with 6 of these Float Store Directs in series, each moving the previous value to the next, then doing the math?

14 hours ago, Ausman said:

Do you want to average only the last 6 readings, or do you want to leverage in the rolling average?

Aus, I was thinking that if I plugged one value in and dropped one off, then did a sum & divide, that would be a rolling average....

JohnR

##### Share on other sites

• MVP 2022

JohnR, I think that you could look at using a Formula to do the maths.  I haven't physically tried your need and it's time for zzzzzzz, but Formulas work with MFs and it should be much easier.

Along the lines of (A+B+C+D+E+F)/6.

Have a go and I think you'll find it ok.

Re my average comments, brain too tired to explain tonight.  I'll try and do it in the morning.

cheers, Aus

##### Share on other sites

• MVP 2022
1 hour ago, JohnR said:

I see the Store Direct under Math/Float/Basic functions, but (unless I'm missing the obvious), you can't do  vector functions with MF's.

I was talking about Store Indirect, but you are correct that you can't do that with MFs. Not sure why.

It looks like your solution may be the most straightforward way.

##### Share on other sites

1 hour ago, Ausman said:

but Formulas work with MFs and it should be much easier.

Aus, you are correct, that is much simpler then my net full of math......

1 hour ago, Flex727 said:

I was talking about Store Indirect, but you are correct that you can't do that with MFs. Not sure why.

Flex, I'm not sure why MF's are excluded from a lot of stuff either, but at least you bumped my mind into looking at the Float functions available....

We are a little closer to an Elegant solution

JohnR

##### Share on other sites

• MVP 2022

I'm working on my "methods" pros and cons essay.   Might take a bit longer.   🐢

Almost done but I'll revisit it a few times before posting.  Stay tuned!

cheers, Aus

##### Share on other sites

• MVP 2022

I know, I know, taking longer than I wanted.  Other things cropping up.  I am doing a subroutine (export/import) to show examples.  Hopefully able to do this homework tonight.

cheers, Aus

##### Share on other sites

• MVP 2022

Sorry...other things on the go.  Almost there...will be worth the wait.  "Elegance Extraordinaire Monsewer"

Well...maybe.  Certainly will get your brain cells firing.

Cheers, Aus

##### Share on other sites

• MVP 2022

Sorry All....Real work has prevailed over volunteer stuff.    It is coming and you'll like it.

cheers, Aus

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×