LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Does anyone know a better/simple way to find the average (see attached code)?

Solved!
Go to solution

So I have data coming from +1 and the +2. I need to find the average of each, then put the results in a 1D array. The code is doing exactly what I want (see attached VI). I feel like there is a better way of this doing. If how I am doing it is how you would do it as well, let me know too.

 

0 Kudos
Message 1 of 5
(1,017 Views)

You're doing cumulative averages over a growing number of samples.   There's definitely a better way to do that without needing to grow any arrays.   The new average is a weighted average of the old average and the new value.   Basically, where N is the # values used used to determine the old average:

 

new_avg =  (N*old_average + new_value) / (N + 1)

 

Attached is a code mod to include this method next to yours to show that they agree.  Below is a snippet with just the new method.

 

 

-Kevin P

 

 

 

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 2 of 5
(979 Views)
Solution
Accepted by topic author GRCK5000

Hmmm, not sure where that snippet disappeared to.  Guess I'll try again.

 

Note: I edited the code down quick for the snippet without really thinking.  There should be a time delay in the loop.

 

FindingAverage - modKP LV2020.png

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 3 of 5
(906 Views)
Solution
Accepted by topic author GRCK5000

Here's what I might do. Same result!

 

altenbach_0-1696955704823.png

 

 

... and if you don't like programming it yourself, use Mean ptbypt with sample lenght =0.

 

altenbach_0-1696956307136.png

 

 

Of course your example is a bit random, because you can calculate these specific averages from first principles from the value of [i] alone.

 

 

altenbach_1-1696960379080.png

 

 

 

Maybe you want to make an example that is more realistic, for example by generating new random values for each iteration.

 

Message 4 of 5
(881 Views)

Thanks Mr. Altenbach! This is awesome!!!

Thank you, KP! That definitely works.

0 Kudos
Message 5 of 5
(845 Views)