09-01-2020 05:04 PM - edited 09-01-2020 05:10 PM
Hi NI Community,
I'm trying to write a simple code to detect a transient duration. I figured out the logic but trying to work with array in LabVIEW.
For example:
We have a DAQ collecting voltage and here is the array of voltage measurements: 00002220000 where the numbers of 1 second apart from each other (1 sample per second) so that is a total of 11 seconds because there are 11 measurements made in the DAQ. We want to know how long the signal is above 0, so we set a trigger level if the measure goes above 1 and when it comes back down to 1. Then timestamp those 2 points and subtract them from each other to get the total duration of how long the signal is above 0V. In this case, how do we utilize array index to do this in LabVIEW? I'm trying to do this real time as the DAQ is collecting data.
Here is the logic
if array[i] >= 1 and array[i -1] <1:
timestamp1
if array[i] <=1 and array[i-1] > 1:
timestamp2
I think a way I want to do it is below but I'm not sure how to implement it
while DAQ collecting data real time:
- create a temporary array of 10 samples from the data DAQ is acquiring
- compare the data inside that temporary array with the logic above
Thank you
09-01-2020 06:22 PM
No need to detect transitions. Just count how many points are above the threshold.
09-02-2020 06:46 AM
If you can get away with simply counting the values, use that. The problem is ill-defined (for us).
What if there are multiple sections above the threshold?
@victor55 wrote:while DAQ collecting data real time:
- create a temporary array of 10 samples from the data DAQ is acquiring
- compare the data inside that temporary array with the logic above
That would work. There's a caveat.
You need to persist state over iterations. If the latest iteration stopped above the threshold, the next iteration you need the previous start time (or index). And you need to look for a value going down. This is pretty straightforward with a shift register.
The index has to be kept over iterations as well. So the first iteration, the first index of the new array is 0, the next iteration it would be 0+size of the previous array.
09-02-2020 10:40 AM - edited 09-02-2020 11:14 AM
I would use FGV to keep locally the history and look for two conditions:
1. signal from negative goes positive - start the time measurement (store the current time as timestamp)
2. signal from positive goes negative - measure the elapsed time (current time minus timestamp) and report the event
See attached a quick code for this idea. You just need to call the VI with different signal value, there is no limit of how many samples the signal can stay high or low.
09-02-2020 11:53 AM
@peter111 wrote:
See attached a quick code for this idea.
I strongly recommend not to use a shift register with expanded history. It is simply not scalable! (Next time you need the same solution for 500 points! :o). There are always better ways!
09-02-2020 11:59 AM - edited 09-02-2020 12:04 PM
@peter111 wrote:
See attached a quick code for this idea.
FGV are still Globals.
Introducing them without too much reason will get you into problems.
If this code works, and you copy or clone it (for other channels), the FGV turns into a communication device.
For this situation, you can make the FGV reentrant. It won't be a FGV anymore, but it'd be more like a PtByPt VI.
I'd also strongly recommend against that (PtByPt VIs). If the PtByPt VI ends up in (the hierarchy of) a class, the class won't work properly when used in an array. Of course that could be a corner case for some, but quite normal for me... I'd put the state data (shift registers) in the private data of a class. That makes it work completely by wire\by value. Just how I like it, but YMMV.
09-02-2020 12:14 PM
Here's a code skeleton for the "counting" suggestion. You could easily interpolate between adjacent points to get a resolution below dt if you want.
How much do you know about the signal? How much noise do you have?
09-02-2020 12:37 PM
Thanks Altenbach,
Good point - using shift register will make the VI non-scalable.
09-02-2020 12:41 PM
@peter111 wrote:
Thanks Altenbach,
Good point - using shift register will make the VI non-scalable.
A shift register is OK, just not one with a resized history. 😉
You only need a single plain shift register (or feedback node) containing an array of N elements where you e.g. replace the oldest with every iteration (and rotate if order matters).