# Moving/Rolling Average

## Recommended Posts

Hi guys,

I was wondering if anyone had an idea on how to create a moving average in visilogic?? The formula is:

Moving Average = X1 + X2 + X3 .....Xi

i

This is updating every minute so I am using an increment FB with a 1 minute timer to increase i. The problem I have is I don't know where or how to store the previous averages as my dataset increases.

Can they be stored somewhere so I can retrieve them each time I make the calculation?

Brian

##### Share on other sites

• MVP 2022

How many values do you want to average? I did this once the "crowbar" way for 100 values, storing the newest value in a data table and continually bumping all the other values down. You add the new values until your pointer reaches 100 and then you keep adding the newest one while subtracting the oldest and dividing by 100. ML's are involved. There's probably a better way, but it did work and I understood it.

It would be easy to store the old calculated averages periodically in another data table.

Let me know if you want some sample code.

Joe T.

##### Share on other sites

Hi,

A good idea is to use vector to save values. Then you will use vector fill, vector copy etc.

Before adding a last value to vector, shift (n-1) vector values to free space.

then calculate average.

And do it in cicle.

##### Share on other sites

Hi,

A good idea is to use vector to save values. Then you will use vector fill, vector copy etc.

Before adding a last value to vector, shift (n-1) vector values to free space.

then calculate average.

And do it in cicle.

Hi Alex,

Thanks for the replies.

I had some contact with support and managed to implement it the way you described. The issue I have now is that when my MB (controlling the vector fill,copy, etc;) is off, I am servicing a dump load and when I engage the MB again, the value that is for the dump load is storing in the vector that is used for my primary load. Is there any way I can stop this value from entering the vector as I am getting an inaccurate calculation. This probably doesn't make much sense without seeing the code but any help is greatly appreciated,

Brian

• MVP 2022

Joe T.

##### Share on other sites

Hi Brian,

Save you value in a separate MI, aside of vector.

Then use it when you need.

##### Share on other sites

Hi Brian,

Save you value in a separate MI, aside of vector.

Then use it when you need.

Thank you Alex....pretty simple once I hear it!

I am using a filter to sample 12 values in a minute but I don't think I have it configured correctly.

Can you shed some light on how to configure it so I sample every 5 sec and output the average after a minute ?

I have attached my configuration.

Many thanks

Brian

##### Share on other sites

• 5 years later...
• MVP 2022

I hate to dig up a 5 year old post, but I'm wondering if anyone has used this potential shortcut:

This would be for a 100 point moving average:

ML 0 is the final moving average and ML 1 is each new point added to the moving average. This appears to me to be a bit smoother than an actual moving average, but with a slower response to large changes, but possibly good enough. I think it's obvious how to change it for any length moving average. Thoughts? I'm not a math whiz, so maybe I'm missing something important.

##### Share on other sites

• MVP 2022

Flex -

You don't have any provision to remove old data.  Your moving average will keep growing.  Or if your new data is less than 100 it will be ignored.

On 11/2/2017 at 12:59 PM, Flex727 said:

, but with a slower response to large changes

That's why you filter a lot of values.  A couple of big-ish values shouldn't affect your average.  Unless they're ridiculously large (bigger than the filter total), in which case you need to filter more values or put in logic to handle them.

This has come up before so I chopped some code up from an old project and put it in a stand-alone file.  I ran it with a data size of 100 values, but it can go as big as the MI table above 1000 will allow.

Joe T.

Edited by Joe Tauser
Re-submitted file in new version
##### Share on other sites

• MVP 2022
9 hours ago, Joe Tauser said:

You don't have any provision to remove old data.  Your moving average will keep growing.

It doesn't keep growing because the sum is multiplied by 99 then divided by 100. In other words, you take 99% of the 100 point sum and add the new value (which is 1% of the total). As I said, it appears to me to be a bit smoother and more laggy than a genuine 100 point moving average (which sometimes can be a good thing), but it is only 3 steps and two operands to create a (pseudo) moving average of any number of points - to the limits of a 32-bit integer.

However, I'm going to study your LargeFilterJT a bit.

By the way, that vlp file appears to have been saved with 9.8.64, but you seem to be using the version from before Unitronics fixed the database issue to prevent it from being opened in 9.8.31. You should remove that version and re-download & install.

##### Share on other sites

• MVP 2022

Re-saved in 9.8.65.

Joe T.

Large Filter JT.vlp

## 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.

×
×