Jump to content

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

Link to comment
Share on other sites

  • MVP 2023

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.

Link to comment
Share on other sites

  • MVP 2023

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

Link to comment
Share on other sites

  • MVP 2023

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

Link to comment
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?

image.thumb.png.00d6637f0238b6e8145c6cf53d740dbd.png

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

Link to comment
Share on other sites

  • MVP 2023

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

Link to comment
Share on other sites

  • MVP 2023
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.

Link to comment
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......

image.thumb.png.2a1acde36176b49c8e116b7647431abf.png

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

×
×
  • Create New...