# PeakDetector

Advanced Analysis Library Only

AnalysisLibErrType PeakDetector (double inputArray[], int size, double threshold, int width, int polarity, int initialize, int endOfData, int *count, double **locations, double **amplitudes, double **secondDerivatives);

## Purpose

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

The input data might be a single array or consecutive blocks of data. Consecutive blocks of data are useful when the application involves large data arrays or real-time processing. The initialize and endOfData parameters help you work with consecutive blocks of data. For example, if you have three blocks of data, you can perform peak detection on them according to the following pseudocode:

for i = 1 to 3

Acquire data
if (i == 1)

Initialize = True

else

Initialize = False

if (i == 3)

EndOfData = True

else

EndOfData = False

Set width, threshold, choice
Call PeakDectector function
Copy the calculated locations, amplitudes and second derivatives to different variables so they won't be overwritten during the next iteration of the loop.

continue

PeakDetector is based on an algorithm that fits a quadratic polynomial to sequential groups of data points. The width value specifies the number of data points to use. The best choice for the value of width is 3. Larger widths can reduce the apparent amplitude of peaks and shift the apparent locations.

For each peak or valley, PeakDetector tests the quadratic fit against the threshold level. PeakDetector ignores peaks with heights lower than the threshold or valleys with troughs higher than the threshold.

You must use the initialize and endOfData parameters to notify PeakDetector when you pass the first and last blocks so that PeakDetector can initialize and release data internal to the peak detection algorithm.

Call FreeAnalysisMem on the returned array pointers when you no longer need them.

## Parameters

Input
Name | Type | Description
inputArray | double-precision array | Input array that contains the data to process.
size | integer | Number of elements in inputArray.
threshold | double | Threshold value used to reject peaks and valleys that are too small. threshold eliminates the effect of noise in the input data. PeakDetector ignores any peak with a fitted amplitude that is less than threshold and any valley with a fitted amplitude that is greater than threshold.
width | integer | Span that specifies the number of consecutive data points to use in the quadratic least squares fit. The width value should not exceed approximately half of the half-width of the peaks or valleys. The value can be much smaller for noise-free data. The best choice for the value of width is 3. Larger widths can reduce the apparent amplitude of peaks and shift the apparent locations.
polarity | integer | Pass 0 to detect peaks; pass 1 to detect valleys.
initialize | integer | Specifies whether the input array being processed is the first block of data. Pass a nonzero value if the current input array is the first data block (or the only data block) to process; otherwise, pass 0.
endOfData | integer | Pass a nonzero value if the current input array is the last data block (or the only data block) to process; otherwise, pass 0.
Output
Name | Type | Description
count | integer | The number of peaks or valleys found in the current block of data. count is the size of the three output arrays: locations, amplitudes, and secondDerivatives.
locations | double-precision pointer | A pointer to an array that contains the locations of the peaks or valleys that PeakDetector finds in the current block of data. The elements of the generated locations array represent indices from the beginning of processing, the most recent call to PeakDetector with a nonzero initialize value. Call FreeAnalysisMem on the array pointer when you no longer need it.
amplitudes | double-precision pointer | A pointer to an array that contains the amplitudes of the peaks or valleys that PeakDetector finds in the current block of data. Call FreeAnalysisMem on the array pointer when you no longer need it.
secondDerivatives | double-precision pointer | A pointer to an array that contains the second derivatives of the peaks or valleys that PeakDetector finds in the current block of data. Call FreeAnalysisMem on the array pointer when you no longer need it.

## Return Value

Name | Type | Description
status | AnalysisLibErrType | A value that specifies the type of error that occurred. Refer to analysis.h for definitions of these constants.