05-06-2015 02:47 PM - edited 05-06-2015 02:53 PM
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
Solved! Go to Solution.
05-06-2015 03:45 PM
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
05-06-2015 09:52 PM
shift register may solve your problem, see the attached snippet..
05-07-2015 12:11 AM
05-07-2015 07:10 AM
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.
05-07-2015 07:22 AM
05-07-2015 07:26 AM
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.
05-07-2015 07:31 AM
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.
05-07-2015 07:49 AM
05-07-2015 07:52 AM
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).