08-28-2019 10:17 AM
Hi everyone. I'm trying to read a series of data, save it to 1D array and then calculate the center of gravity. I wrote an independent program and it could work out well. But when I copy it to the main program problems came out. I want to calculate the gravity core every time when a series of data is written into the array.When the next 20 number being written into the array, it can automatically calculate the next gravity core. The problem is that the sum keeps adding and can't start from 0 when a new series of data enters.
Solved! Go to Solution.
08-28-2019 10:22 AM
Right-click your feedback node and select "Move Initializer one loop out".
You can then wire a zero to re-init to zero before the for loop runs.
Ben
08-28-2019 10:37 AM
Thanks a lot! Problem solved!
08-28-2019 11:20 AM
The VI you have attached does not resemble the code shown in the image at all.
Can you please attach your "center of gravity" code, because I think you are doing this way too complicated. Done right, it is basically a one-liner. 😉
08-28-2019 11:27 AM
@altenbach wrote:
Can you please attach your "center of gravity" code, because I think you are doing this way too complicated. Done right, it is basically a one-liner. 😉
Here's one possibility:
08-28-2019 02:04 PM
That's it. Thank you.
08-28-2019 02:16 PM
@altenbach wrote:
@altenbach wrote:
Can you please attach your "center of gravity" code, because I think you are doing this way too complicated. Done right, it is basically a one-liner. 😉
Here's one possibility:
But Christian (Ben wonders) doesn't that require twice as much memory?
Ben
08-28-2019 02:22 PM
A smart compiler can do it in place and even use sse. 🙂
08-28-2019 03:04 PM - edited 08-28-2019 03:07 PM
My answer above was written on the bus. I haven't looked at allocation dots and such. Yes, you could accumulate the upper sum in a shift register instead. I doubt it would a measurable difference and chances are the compiler will generate basically the same code under the hood ;). It can probably do the sum first and then re-use the loop input for the output.
The code could be further simplified if the array size (i.e. # of samples/channel) is always the same. Now the [i] ramp can be calculated at program start, giving even simpler (i.e. loop-free) code in the discussed part.
(My main sore point in the original code was a custom counter mimicking [i] directly wired to "index array" and the array size wired to N as well as the explicit counter starting at -1, and immediately increment to give an identical output like the [i] terminal. Then we have these useless indicators inside the fast loop that will only visibly display the final sums anyway. :))