10-09-2023 01:38 PM
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.
Solved! Go to Solution.
10-09-2023 03:01 PM - edited 10-09-2023 03:07 PM
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
10-10-2023 07:42 AM - edited 10-10-2023 07:45 AM
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.
-Kevin P
10-10-2023 11:35 AM - edited 10-10-2023 12:53 PM
Here's what I might do. Same result!
... and if you don't like programming it yourself, use Mean ptbypt with sample lenght =0.
Of course your example is a bit random, because you can calculate these specific averages from first principles from the value of [i] alone.
Maybe you want to make an example that is more realistic, for example by generating new random values for each iteration.
10-10-2023 02:58 PM - edited 10-10-2023 03:00 PM
Thanks Mr. Altenbach! This is awesome!!!
Thank you, KP! That definitely works.