DetectPeaksOrValleys Method

NI Vision for Visual Basic

DetectPeaksOrValleys Method

Syntax

CWIMAQVision.DetectPeaksOrValleys DataArray, FindPeaks, PeakValleyReport [, Threshold = 0] [, Width = 3]

Return Type

Long

On success, this method returns 0. On failure, this method returns a negative number.

Purpose

Finds the location, amplitude, and second derivative of peaks or valleys in the input array.

Remarks

The method is based on an algorithm that fits a quadratic polynomial to a sequential groups of data points. Set the Width parameter to specify the number of data points to use in the fit. For each peak or valley, the method tests the quadratic fit against the Threshold value. The method ignores peaks with values lower than the threshold and valleys with troughs higher than threshold.

Parameters

DataArray As Variant

The input array.

FindPeaks As Boolean

Set this parameter to True to detect peaks or False to detect valleys.

PeakValleyReport As CWIMAQPeakValleyReport

On return, a report containing information about each peak or valley detected.

Threshold As Variant

[Optional] Rejects peaks or valleys that are too small. Any peak found with a fitted amplitude that is less than Threshold is ignored. Valleys are ignored if the fitted trough is greater than Threshold.

This parameter has a default value of 0.

Width As Variant

[Optional] The number of consecutive data points to use in the quadratic least-squares fit. The value must be greater than or equal to 3 but should be no larger than one-quarter of the approximate width of the peaks or valleys. Large widths may reduce the apparent amplitude and shift the apparent location of peaks.

This parameter has a default value of 3.

Example

Private Sub Run_Click()
    ' This example requires you to draw a region on Viewer1
    ' before you execute this piece of code.
    Dim InputArray
    Dim ProfileReport As New CWIMAQProfileReport
    Dim PeakValleyReport As New CWIMAQPeakValleyReport
    Dim I As Integer
    Dim Index As Integer
    
    ' Find the profile of the regions on Viewer1.
    CWIMAQVision1.RegionsProfile CWIMAQViewer1.Image, CWIMAQViewer1.Regions, ProfileReport
    
    ' Extract the profile array
    InputArray = ProfileReport(1).ProfileArray
    
    ' Find the location, amplitude and, derivatives of the peaks
    ' along this profile
    CWIMAQVision1.DetectPeaksOrValleys InputArray, True, PeakValleyReport, 100, 3
    
    'Overlay the points where peaks were found
    For I = 1 To PeakValleyReport.Count
        'Find the index into the InputArray where the peak was found. This index corresponds
        'To the index of the points in ProfileReport(1).Coordinates
        Index = Round(PeakValleyReport(I).Position)
        
        'Draw a circular point at ProfileReport(1).Coordinates(Index) -- the point nearest
        'to where the peak was found.
        CWMachineVision1.DrawCircularPoint CWIMAQViewer1.Image.Overlays(1), _
                                           ProfileReport(1).Coordinates(Index), _
                                           4, _
                                           cwimaqOverlayModePaint
    Next I
End Sub

Gauging Example