LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to store data points into array

Solved!
Go to solution

I am new to labview and I am trying to store the frequency output of the "Pulse measurements VI" into an array, add all the elements together and divide by the length of the array. I do not want an array of numbers to be shown on the indicator. Just the average of the 25 numbers.

 

basically, I want to do this, but in labview. 

 

arrayIndex = 0;

for(int i = 0; i<=25; i++)

{

newdata = somedata;

myarray[arrayIndex] = newdata;

dataAvg = dataAvg + myArray[arrayIndex];

arrayIndex++;

}

 

dataAvg = dataAvg / 25;

 

 

 

 

I attached my attempt, but its probably a very poor attempt. (The N on my first for loop was 25 when i tried running the VI)

 

 

Thanks in advanced

 

 

0 Kudos
Message 1 of 11
(11,409 Views)

I really can't figure out what you are trying to there, but I do see multiple problems with your VI.

 

1.  Don't start and stop a task in every loop iteration of the while loop.  Start the task before the loop, and end it after.

2.  Your inner while loop will run exactly 1 time, or exactly infinite times.  Your stop terminal is controlled by a boolean value coming from outside the loop.  It will be either true (and thus run once), or false (and thus run forever).  The boolean value won't be read again until the outermost while loop iterates again.

3.  Your first while loop runs exactly 0 times because of the value wired into the N terminal.

4.  Your second For Loop, running 25 times, or the number of rows in your 2-D array, whichever is less.  And I'm guessing it will be 0 since that is how many rows will be in that array since that is how many times the other while loop ran when it auto-indexed the 1-D array into a 2-D array

5.  If 3 and 4 weren't already causing problems, the inside of the 2nd while loop has issues.  You are indexing out the element 25 of the 1-D array.  But that 1-D array will only have 1 element since all you did was turn a scalar into a 1-D array inside the first For Loop.

6.  Your inner For Loop has a 150 msec wait controlling the pace.  But your data acquisition is only collecting 500 samples out of a much faster acquisition rate.

7.  Indicators with no labels on the block diagram, so we can't tell what they are supposed to do.

 

I would recommend looking at the online LabVIEW tutorials
LabVIEW Introduction Course - Three Hours
LabVIEW Introduction Course - Six Hours

Message 2 of 11
(11,372 Views)

shift register may solve your problem, see the attached snippet..

 

0 Kudos
Message 3 of 11
(11,332 Views)
In addition to the courses that were recommended to you, there is one more thing to remember. A common mistake that people make when transitioning to LabVIEW from a primative language like C it to over-complicate the code -- especially when working with arrays.

For example, in your case you have some logic that generates a number -- for now it doesn't matter what the number is. What you want is to run that logic N times and then display an average.

In LabVIEW, you simply take that logic and wrap a for loop around it so it will run N times. Now you wire the number through the wall of the for loop. This will turn it into an array of the numbers. Now wire that array to the mean function. Done.

One of the LabVIEW Champions named Christian Altenbach has a tagline: "Do more, with less code, in less time"

That about sums it up.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
Message 4 of 11
(11,311 Views)

Thank you all for your replies. I was previously taking a "snapshot" of two signals so I only wanted to read N number of data points then stop and clear the task. I overlooked that I was starting and stopping the task every iteration when I switched to a running comparison. My first while loop is the main loop for my state machine so I want that to run indefinitely.

 

 

Mike, your explanation makes sense. I was still thinking in terms of C because that is what I know. I tried to approach it the way you described and I feel like im closer, but my "averaged" and "unaveraged" indicators still read identical values, which tells me that im not really averaging anything. I tried it with the pulse measurement VI both inside and outside the for loop and it didnt seem to make a difference.

 

 

 

0 Kudos
Message 5 of 11
(11,285 Views)
Of course they are the same. Your for loop does nothing but create an array of 500 identical elements. If you want a mean of successive acquisitions, you need to program that. Look at using a shift register with a build array or the point-by-point mean.
0 Kudos
Message 6 of 11
(11,262 Views)

Inner For Loop,  you are taking a single value.  Doing some math on the same number 500 times in a row building it in an array.  So now you have 500-element array numbers with exactly the same elements throughout.  Then you take the Mean of that Array.  Of course the value of the mean is going to be the same as it was before.

 

So what 500 items do you really want to average?   Is it the last 500 items of that while loop where the waveforms are being generated?   If so, then use mean pt. by pt.  Do you want to average the actual waveform?  Right now you are doing some waveform analysis  (Duty Cycle?  I can't tell from the image), which yields just two single scalar values based on your waveform.

0 Kudos
Message 7 of 11
(11,255 Views)

I am wanting to take N number of data points from the output of the pulse measurements VI and average them. I am wanting to smooth out the data being displayed on the front panel. Right now my frequency jumps around a few Hz and my duty cycle fluctucates by a few percent. I am wanting to take the average.

0 Kudos
Message 8 of 11
(11,250 Views)
Solution
Accepted by topic author newEngineer09
You need n number of DAQmx Reads in order to take the mean of n number of pulse measurements. Silly to take 500 of anything when you have a single DAQmx read that returns a single measurement. As I said, you can use a shift register or point-by-point inside the loop. Or, run an acquisition n number of times with a for loop and pass the pulse measurement out into the auto-indexed array and calculate the mean after all acquisitions are complete.
Message 9 of 11
(11,233 Views)

Use the Mean PtByPt VI.  It keeps a history and averages the X measurements you gave it (X being however many samples you tell it).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 10 of 11
(11,229 Views)