03-03-2023 06:11 PM
I need to take an array and find the index values of where it changes from positive to negative. There will be two instances this happens, and I need to find the indices of these two events.
For context, the arrays will always follow the same behavior: starting positive, then dropping to negatives, returning to positive, and once again dropping to below zero where it will remain negative.
I'm thinking I need to nest a For Loop inside a While Loop conditioned to stop when the array value is less than or equal to 0 to get the first value, but finding the second index is where I am having trouble.
My only thought is to make a subarray with the index found in that nested loop (index1), then make a nested loop to see when it goes positive to find an index2-> subarray that, then nested loop to find the second time it drops below zero for index3.
My total number of values in the range I need would then be index2 + index3?
Please let me know if my description isn't clear and I can add a picture for more context.
I am wondering if I am complicating this process and would like to avoid loops with tens of thousands of iterations.
Even letting me know whether I am on the right track would be great - thanks in advance for the help!
Solved! Go to Solution.
03-03-2023 06:21 PM
You can get it done a for loop pretty easily. Hopefully this will suffice until altenbach comes by with his zero loop solution 😄
The logic is that the conditional output terminal only outputs a value when the current value is negative, and the previous value is positive or zero.
03-03-2023 06:33 PM
Is this real-world data or is this algorithmically generated or otherwise 100% guaranteed to be clean data?
I'm asking because while the algorithm to detect sign changes isn't super hard, you may have edge in real-world cases where the data is close to zero, but then random noise makes it cross the zero line twice in quick succession, giving you bad calculations.
If this is real world data you might want to also consider adding a Hysteresis check. This page has some diagrams showing what I mean:
https://www.ni.com/docs/en-US/bundle/labview-myrio-toolkit/page/myriohelp/myrio_hysteresis.html
03-04-2023 11:14 PM
@Kyle97330 wrote:
Is this real-world data or is this algorithmically generated or otherwise 100% guaranteed to be clean data?
I'm asking because while the algorithm to detect sign changes isn't super hard, you may have edge in real-world cases where the data is close to zero, but then random noise makes it cross the zero line twice in quick succession, giving you bad calculations.
If this is real world data you might want to also consider adding a Hysteresis check. This page has some diagrams showing what I mean:
https://www.ni.com/docs/en-US/bundle/labview-myrio-toolkit/page/myriohelp/myrio_hysteresis.html
I recently ran into this issue where high frequency noise was causing exactly the hysteresis issue you are talking about. We weren't expecting that much noise, so we had to increase the hysteresis value.
03-05-2023 10:34 AM - edited 03-05-2023 11:05 AM
@UMBLaserLab wrote:
Please let me know if my description isn't clear and I can add a picture for more context.
No! pictures are pointless. If you like, attach your VI attempt and a typical datafile. What is the datatype (integer? DBL? Waveform? dynamic? etc.). How much noise is there?
Maybe all you need is something like zero crossing ptbypt.
Most likely, the real zero crossing is at a non-integer position. I would probably try to find the exact position using threshold array (example that you can modify to suit your needs).
03-05-2023 11:08 AM
@altenbach wrote:
Maybe all you need is something like zero crossing ptbypt.
Here's how that could look like:
03-06-2023 04:08 PM
Wow, I knew there would be something simple I was missing, and the PtByPt VIs were the key. Thanks for the replies, this was my first forum post and am impressed by the responsiveness of the LabView community. This is exactly what I needed, and thank you for pointing out the noise problem - something I didn't really think about. There is some noise in the minus-plus crossing, but luckily after review the indices I need will ALWAYS be the first and last plus-minus crossings - when before I assumed it would always be the first and second plus-minus crossing. I can just build an array of indices and use the first and final entries. The difference multiplied by my timestep is my total delta t.
Much appreciated!!