average FIFO or just add and divide

controlconcepts

Lifetime Supporting Member
Join Date
Oct 2008
Location
boston
Posts
300
I have read a lot on average's and different methods on how it is done. what is the advantage of using a fiof or the Avg function as to just a ADD and divide? what is the point of storing data into a file when it is only used once? what is the point of keeping a lot of data files fulll?/?
Thanks Matt
 
Last edited:
ADD and divide is fine for things like averaging an analog signal and you don't need to record the history of those readings. But what if you wanted to calculate standard deviation of those readings? You would store them in a structure like a FIFO so you could run through the readings for the 2nd pass. Its often easier than keeping track of storing them in an array.

A FIFO can be used for a lot of things, another I can think of is tracking pallets ID sent into a storage cilo or conveyor loop, etc.
 
Do you need a true average or a running average?

Take the following 10 samples that you might see at some hypothetical process. The numbers could represent anything that varies over some arbitrary sampling period.

[5.56, 5.6, 5.51, 5.49, 5.59, 6.12, 8.5, 8.4, 8.45 , 8.5 ]

The average of those numbers is 6.77. However if you use the add/divide method instead of storing the values you will get 8.31. Thats a significant difference.

So it depends on what you really want.
 
Do you need a true average or a running average?

Take the following 10 samples that you might see at some hypothetical process. The numbers could represent anything that varies over some arbitrary sampling period.

[5.56, 5.6, 5.51, 5.49, 5.59, 6.12, 8.5, 8.4, 8.45 , 8.5 ]

The average of those numbers is 6.77. However if you use the add/divide method instead of storing the values you will get 8.31. Thats a significant difference.

So it depends on what you really want.

How is that possible? What am I missing?
 
Don't know what you're using the average for, but if you are just trying to smooth a signal then a low pass filter often works well with no arrays, no FIFO's, no real storage.

The two methods (averaging vs Filtering) are not interchangeable for all applications, but for many of my applications, I've found that they produce similar results. Additionally, if I know that my signal is not continous or suddenly changes when a certain event occurs, I can easily pre-load my filter by setting the "memory" variable equal to the current process signal - it beats filling an array or FIFO with the inital value. I can also easily change my dampening with one variable versus expanding my FIFO or Array.

There are some good simple examples of low pass filters in old posts of this forum (my thanks to all who contributed).
 
How is that possible? What am I missing?

(5.56 + 5.6 + 5.51 + 5.49 + 5.59 + 6.12 + 8.5 + 8.4 + 8.45 + 8.5)/10 = 6.77.

To get this average you have to store the 10 samples in either a FIFO or a circular queue.

If you don't store any data samples then the only data you have is the current sample and the last computed average. If you just add the current measured variable to the last average and divide by 2 then you get:

(5.56 + 5.6) / 2 = 5.58
(5.58 + 5.51) / 2 = 5.454
(5.454 + 5.49) / 2 = 5.5175
(5.5175 + 5.59) / 2 = 5.554
(5.554 + 6.12) / 2 = 5.837
(5.837 + 8.5) / 2 = 7.169
(7.169 + 8.4) / 2 = 7.785
(7.785 + 8.45) / 2 = 8.118
(8.118 + 8.5) / 2 = 8.31

The suitability of either method to the OPs question is unknown and he wasn't specific about any particular application. I'm just illustrating the difference to answer his question about why to store data with a simple example.

If you are batch averaging and you start the averaging over with each new batch then you can keep a running sum and a running count and get an accurate average as long as you don't overflow your sum word. But if you attempt to make a rolling average at some point you need to know past data in order to get an accurate average.
 
Last edited:
That's not a running average calculation I've seen before. You're effectively giving the weight of the current/last reading 5 times (10 / 2) the amount of the previous readings? I think it deserves a new name; after the author perhaps? (y)
 
Bingo. It doesn't work. That's the point I was trying to illustrate. If the program doesn't store any data then the only thing it has is the current average and the current sample, so whats it going to do?

((Average * (numSamples-1))+ NewSample)/numSamples is close enough for Estes rocket science, but I don't think that's what the OP was thinking.

I've posted solutions to get around having to sum the entire stored data array every time the average is computed and solutions to get around shifting large chunks of data around in memory, but these solutions still rely on stored data. A search for averaging and circular queue will find them.
 
Last edited:
Sorry for the lack of information in my first post. The average is for temperature probes hourly and daily. The methods that I am using are FIFO (Storage), curricular with indexed addresses (Storage) and then just add current sample and divide by how many samples. The first two I used the same logic that has been posted here before. Named FIFO RUNNING AVERAGE

I wanted to know if there as an advantage to tracking the samples. So I added rungs to compare running total of all three methods. And a compare of the average of all three with a latch bit to set if any method was off. And they were all the same! So are the examples I have wrong and I don't see where any of the stored data comes into the final average.
I can post the code if necessary.


Again Thank you all,
Matt
 
Are you restarting the average every hour or every day? What I mean is are you computing the average between fixed times, say 10:00 am and 11:00 am, then starting over and averaging the next hour. This is the kind of application I meant when I mentioned batch averaging. If so then you don't need the extra data, you just need a running sum and running count.

If you need a rolling average, meaning that no matter what time it is you want to know the average for the last hour then you need to keep some kind of data. An example of a rolling one hour average would be that if I looked at the average at 10:23 then I would see the average from 9:23 to 10:23. If I looked again a little bit later, say 10:51, I would see the average from 9:51 to 10:51.
 
Tconnoly thanks for all the responses! I was going to do a average for every hour then use that average to a 24 hour period. I don't need to look back at any point to see when and what temp it was. Just a accurate average hourly and daily. In your post #7 saying that an average without stored data was not accurate. So I want the best way do this. I just don't get how the FIFO method uses the stored data to be more accurate.

Thanks,
Matt
http://www.plctalk.net/qanda/member.php?u=10513
 
You don't need stored data to be accurate for an average. Save the summation or the average and the number of samples and following my process in post #10, you'll be accurate.
 
In post 7 I was referring to a rolling average. For some reason I thought that might be what you were attempting.

Since it sounds like you just want discrete one hour averages and not a rolling average then you can sum the samples and once an hour compute the average by dividing by your sample count.

From post 11 I assume you had already coded a rolling average with a FIFO. Are you recoding or just trying to understand the difference between a rolling average and a batch average?


Here's another one. If you start the hour at 30C and you end the hour at 32C then your average for the hour is 31C. But if you spent most of the hour at 35C then is the average still 31C? A physicist might say yes and an engineer might say no. So it's always best to understand exactly what is needed and that average means different things in different contexts and code accordingly.
 

Similar Topics

Does this instruction calculate values during a single scan, or does it require number of scans based on element count in the array? For Example...
Replies
3
Views
119
I am working on a program in Cscape to take a pulse input from a flow meter and output an average of the last 5 minutes of flow and total flow to...
Replies
1
Views
577
I am storing values into an array. I am wanting to find an average of the highs and an average of the lows. Something like this... After 50...
Replies
36
Views
8,896
I need to keep a running pass/fail yield of the previous 5,000 parts produced. I have used this formula before to calculate average: AvgValue =...
Replies
6
Views
2,163
is it possible to capture a value while its constantly changing then taking its highest reading? I have a nozzle that sprays for 10 seconds. I...
Replies
14
Views
3,447
Back
Top Bottom