Jump to content

Recommended Posts

Posted

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

  • MVP 2023
Posted

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.

  • MVP 2023
Posted

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

  • MVP 2023
Posted

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

Posted

 

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

  • MVP 2023
Posted

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

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

Posted
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

  • MVP 2023
Posted

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

  • MVP 2023
Posted

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

  • MVP 2023
Posted

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

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
×
×
  • Create New...

Important Information

This site uses cookies. By clicking I accept, you agree to their use.