LabVIEW

cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β 

How to calculate the center of gravity of 1D data

Solved!
Go to solution

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.

 

calculate.png

0 Kudos
Message 1 of 9
(2,141 Views)
Solution
Accepted by topic author chen3477

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

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 2 of 9
(2,131 Views)

Thanks a lot! Problem solved!

0 Kudos
Message 3 of 9
(2,124 Views)

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. πŸ˜‰

 

0 Kudos
Message 4 of 9
(2,092 Views)
Solution
Accepted by topic author chen3477

@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:

 

CenterofG.png

0 Kudos
Message 5 of 9
(2,085 Views)

That's it. Thank you.

0 Kudos
Message 6 of 9
(2,041 Views)

@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:

 

CenterofG.png


But Christian (Ben wonders) doesn't that require twice as much memory?

 

Smiley Surprised

 

Ben

 

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 7 of 9
(2,033 Views)

A smart compiler can do it in place and even use sse. πŸ™‚

Message 8 of 9
(2,027 Views)

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. :))

0 Kudos
Message 9 of 9
(2,009 Views)