LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Rolling Average

I want to take a rolling average of values that are output from a scale.  I want the data being indexed into an array as such that it only remembers the last 50 values.  I then want to take the mean of the 50 values to get my output.

 

The way I have it designed now is that it does average the last 50 values, but instead of just filling in a new value in the next index slot, it sets all of the values back to zero when it gets 50 iterations.  This causes my scale to increase the average for 50 values, and then reset to zero and do it all over again.  The way it is wired up now, this is what's happening. 

 
I want it to remember the values that were there, but only the last 50.  So what I mean by this is index zero will get the first value, index one will get the second value... all the way to index 49 gets the 50th value, and then index zero gets the 51st value and then ONLY the 1st value is deleted.  The second value will be replaced by the 52nd value with ONLY the second value getting deleted, and so on.  I don't want it to reset every time it goes back to the index zero.
 
The first picture is of my program that the section of the code is in.  I have circled it with a big yellow circle.  It is also repeated slightly above and to the right of the circle.  I have a yellow arrow pointing to the subVI.  The second picture shows what the code is in that sub VI.Big Program.pngSmall Program.png
0 Kudos
Message 1 of 4
(2,698 Views)

I highly recommend using the Mean PtByPt VI.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 4
(2,688 Views)

Example_VI_BD.png

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 3 of 4
(2,671 Views)

First of all, you should attach your VI instead of cropped pictures.

 

As others have said, using mean ptbypt is the correct tool here, if you want to create your own, have a look here (same result, but maybe you can learn something ;)).

(compare ptbypt mean and simple code equivalent)

 

Some general code review comments:

 

  • Looking at your images, there are quite a few very questionable and highly inefficient code constructs. For example you have glaring race conditions! There is no guarantee that the "product in" control in the inner loop is read before its local variable to the left of it receives the updated value for the current iteration of the outer loop. Because there is no data depdendency, both processes occur in parallel and there is no way to tell execution order. Remove the local variable and use a wire. Change the control to an indicator (or simply delete it if it is hidden anyway).
  • Same problem with the "in" control. Big mistake making the result completely unpredictable.
  • "Product in" and "in" should be indicators, not controls.  They receive values from the code, not from the operator. (Your abuse of local varaibles blurs the important distinction betweeen controls and indicators.
  • You can replace your formula nodes with expression nodes or graphical code, less clutter.
  • Since you work exclusively with numeric data, keeping them as formatted strings in the shift register is just insane! There is no justification for that.
  • Why are there time delays in the inner FOR loops?
  • In any case, the shift register belongs on the outer while loop, the inner FOR loops are not suitable for that.
0 Kudos
Message 4 of 4
(2,656 Views)