LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Find average value of a buffer

Solved!
Go to solution

Hi,

In my VI front panel I have a numeric control, a button, and a numeric display.

When user enter a number in the numeric control and press the button, the numeric display should show the average value of all the past 5 values entered in the numeric control, for example:

1st enter: 10 => average = 10 (because sum = 10)

2nd enter: 0 => average = 5   (because sum = 10 + 0 = 10)

3rd enter: 5 => average = 5   (you get the idea..)

4th enter: 1 => average = 4

5th enter: 14 => average = 6

6th enter: 2 => average = 4.4 (sum = 0 + 5 + 1 + 14 + 2 = 22, because the 1st value is thrown out)

How to write this VI?

Thank you!!

0 Kudos
Message 1 of 10
(4,186 Views)

There are about a hundred ways to skin that cat!  basically you need an array that can contain 0-5 elements (Some Kind of rolling buffer).  One of the easiest ways relys on how Charts work with a strip update.

NOTE: history.length = 5, Update mode = Strip (O'scope would work identically for this but sweep would behave much different)

BH.png

 

(Oh silly snippet tool! Man- I wish I had CCT!)

 

A queue could also be used as a rolling buffer by setting max number of elements.  That would avoid the need to run in the UI thread


"Should be" isn't "Is" -Jay
Message 2 of 10
(4,173 Views)
Solution
Accepted by topic author splee

You can also use the arrays to hold the data in a shift register and find the average value of the last five elements.

 

Average Elements.png

 

Good luck

-----

The best solution is the one you find it by yourself
Message 3 of 10
(4,150 Views)

Hi,

Why do you need the "while loop", and the "wait" function?

Is it possible to do without these?

0 Kudos
Message 4 of 10
(4,134 Views)

You can do without that but for using the above example you need to have the shift register in place. If you don't want to run continously keep the while loop and connect the stop terminal to True and remove the initalization part before the loop.

-----

The best solution is the one you find it by yourself
0 Kudos
Message 5 of 10
(4,127 Views)

Hi P Anand,

With regard to your solution, I have a few question:

- what is the size of the array constant? Does it have to be initialized?

- if I keep entering numbers, will there be a limit to the size of the array?

0 Kudos
Message 6 of 10
(4,106 Views)

@splee wrote:

 

- what is the size of the array constant? Does it have to be initialized?

 


No if you want to use this as a sub vi you leave the shift register uninitialized.

 


@splee wrote:

- if I keep entering numbers, will there be a limit to the size of the array?


If you want to limit the array size you need to use Replace array subset or Delete From array functions.

-----

The best solution is the one you find it by yourself
0 Kudos
Message 7 of 10
(4,103 Views)

 

Just curious...

- how to reset the 1st subarray of the "Split 1D array" to all zeros? Just in case I want to clear the buffer elements to all zero

- how to reset the output of the "Build array"?


@P Anand wrote:

You can also use the arrays to hold the data in a shift register and find the average value of the last five elements.

 

Average Elements.png

 

Good luck


 

 

0 Kudos
Message 8 of 10
(4,073 Views)

Anyone has any idea how to reset/clear the array values?

0 Kudos
Message 9 of 10
(4,047 Views)

Why not use the ready-to-use Pt-By-Pt-Mean? Life can be so easy...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 10 of 10
(4,043 Views)