# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## How to calculate the center of gravity of 1D data

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.

Message 1 of 9
(2,245 Views)
Solution
Accepted by chen3477

## Re: How to calculate the center of gravity of 1D data

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
Message 2 of 9
(2,235 Views)

## Re: How to calculate the center of gravity of 1D data

Thanks a lot! Problem solved!

Message 3 of 9
(2,228 Views)

## Re: How to calculate the center of gravity of 1D data

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. 😉

Message 4 of 9
(2,196 Views)
Solution
Accepted by chen3477

## Re: How to calculate the center of gravity of 1D data

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

Message 5 of 9
(2,189 Views)

## Re: How to calculate the center of gravity of 1D data

That's it. Thank you.

Message 6 of 9
(2,145 Views)

## Re: How to calculate the center of gravity of 1D data

@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

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

## Re: How to calculate the center of gravity of 1D data

A smart compiler can do it in place and even use sse. 🙂

Message 8 of 9
(2,131 Views)

## Re: How to calculate the center of gravity of 1D data

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

Message 9 of 9
(2,113 Views)