Example Code

Count Array Elements Above Threshold in LabVIEW (Simple Peak Measurement)

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Download All

Overview
This VI counts the number of elements that are above a given threshold.

 

Description
This is a simple VI that shows how you can count the number of elements in an array that are above a given threshold. For simple peaks, you can take the number of samples and multiply it by the Delta t (time between samples) and calculate the pulse time.

 

Requirements
LabVIEW Base Development System (or compatible)

 

Steps to Implement or Execute Code
1. Add the values to the array
2. Specify the threshold
3. Run the VI


Additional Information or References
Snippet.png
**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
F1_Fan
Member
Member
on

Thanks for posting this vi.  Works exactly as you described on the phone.

-Chris

Chris.Osiecki
Member
Member
on

Perhaps a simpler method would be to use vector operations. (Sorry, I don't have the snippet tool)

counts above threshold.bmp

This method does have a couple of drawbacks, however. The first is the count is capped at I16 limits (~32k). You can eliminate that issue by inserting a To I32 after the Boolean To (0,1), but that allocates more memory and actually makes the function slower than the original post. The second is there are additional buffer allocations, so in a memory limited application, it might cause problems. Aside from the array building in the original post, there are no buffer allocations for the thresholding operation, and the count limit is much higher.

You could use a type cast instead of the Boolean To (0,1), and eliminate the I16 limit, like this:

counts above threshold 2.bmp

Its faster than my first example, but it still adds about 25% more memory than the original post. For a 1M point data set, it was 38% faster. Just make sure the Type Cast is to an I32 array.

Eleftherios
Member
Member
on

what if you wanted to know the position of the elements inside the array that were above threshold?

Chris.Osiecki
Member
Member
on

You would have to scan for values at that point. Here's how I would do it:

temp.bmp

1. Threshold your array

2. Set your starting index

3. Search for a TRUE (TRUE values will be where InputArray was greater than your threshold)

4. Add 1 to the found index and use that for the starting index for your next search.

5. If the search finds no more TRUEs, it will return -1. If the return is < 0, stop the search.

6. Use the last iteration counter value to give you a count of elements found.

Auto-indexing of the found TRUE indices will produce an array that contains the index of all values in InputArray above your threshold.

Note: This method will always have a -1 as the last value of the output array. However, since the iteration counter starts at 0, the Count will be correct without the need for adjustment.

Eleftherios
Member
Member
on

@ COsiecki : Thank you very much for your help!

What if i have an 16x16 array? should i break it down to 16 different arrays and then continue doing what you do for 16 times? or is there another way?

Also i don't want it to stop searching because my array is going to change every second, just to give a message that, a or these elements have more than 10 errors.

If anyone has any idea that would be really helpfull.

thank you!