Syntax Supported by the IML Procedure and the iml Action
PEAKLOC Function
PEAKLOC (series <, filter> <, param1> <, param2> ) ;
This function is supported by the IML procedure and the iml action.
A peak in a series is defined as the index of a local maximum. The PEAKLOC function returns the peaks of an input series. The PEAKLOC function can find peaks by using the following criteria: minimum height, minimum width, maximum width, minimum prominence height, threshold, minimum distance, and maximum number of peaks. These criteria are defined in the next section.
The Geometry of Peaks
A plateau is a set of consecutive local maxima.
You can associate two bases with each peak: one to the left of the peak and one to the right. The first base is found by locating the smallest value between a peak and its two neighboring peaks. If the first base point is on the left side of the peak, the second base is the nearest local minimum on the right side of the peak. Conversely, if the first base point is on the right side of the peak, the second base is the nearest local minimum on the left side.
Figure 272 shows an example of a peak and its associated bases. In the figure, is the left-neighbor peak,
is the right-neighbor peak,
is the minimum point between the peak and its left-neighbor peak, and
is the minimum point between the peak and its right-neighbor peak. The value of the series at
is less than the value of the series at
. Thus, for the peak p,
is the first base, and
is the second base.
Figure 272: The Bases of a Peak

The width of a peak is the distance, measured in points, from the right base to the left base of the peak.
Associated with each peak is an absolute height, which is the difference between the peak value and the value at the first base. In the PEAKLOC function, you can specify a fraction of height that is used to determine the width of the peak according to the following criteria:
If the fraction is 1, the width of the peak is maximal.
For a positive fraction less than 1, the width is found by multiplying the fraction and the absolute height.
Figure 273 shows the width (w_max) of peak p when the fraction is 1. In the figure, the absolute height of peak p is h, which is the difference between the peak value and the value of its first base. Figure 274 demonstrates the width of the peak when the fraction is 0.7 and 0.9. Note that the base points of the peak are adjusted according to the specified fraction of height and calculated using linear interpolation. In this figure, w_0.7 and w_0.9 are the width values when the specified fraction of height is 0.7 and 0.9, respectively. In this figure, and
are the bases of peak p when the fraction of height is 1,
and
are the bases of peak p when the fraction of height is 0.9, and
and
are the bases of peak p when the fraction of height is 0.7.
Figure 273: Height and Width of a Peak

Figure 274: Width of a Peak for Different Fraction of Height Values

Prominence is a quantitative measure of how much a peak stands out among a cluster group of peaks. Associated with each peak are a prominence height (usually called the prominence) and a prominence width. To define these values, first define the right horizontal crossing and the left horizontal crossing for each peak.
The horizontal crossings are the closest points to the left and to the right of the peaks that have greater values than the peak. If no greater values are present, the left horizontal crossing is the beginning of the series, and the right horizontal crossing is the end of the series. Next, the left minimum point is calculated as the point with the minimum value between the peak and the left horizontal crossing. Similarly, the right minimum point is calculated as the point with the minimum value between the peak and the right horizontal crossing. Between the left minimum point and the right minimum point, the point with a higher value would be the first prominence base of the peak. The second base prominence of a peak is the first point on the opposite side of the peak that has the same height as the first prominence base. The second prominence base is calculated using linear interpolation.
Figure 275 demonstrates the definition of the prominence base points. The prominence of a peak is the value of the peak minus the value of the first prominence base, as shown in Figure 275. The maximum prominence width of a peak is the distance, measured in points, from the right prominence base to the left prominence base of the peak. In this figure, is the left horizontal crossing point,
is the right horizontal crossing point,
is the point that has the minimum value between p and
, and
is the point that has the minimum value between p and
. The value of the series at
is greater than the value of the series at
. As a result,
is the first (in this case, right) prominence base, and
is the second (in this case, left) prominence base. In this figure, W is the maximum width of the prominence, and L is the prominence of the peak p. Like the peak width, the prominence width is affected by the fraction of height parameter.
Figure 275: Prominence, Prominence Width, and Prominence Bases

A prominence group is defined as a single peak or a group of peaks. Peak A is in the same prominence group as peak B if the points between the left and right prominence bases of A overlap the points between the left and right prominence bases of B. A peak cannot belong to more than one prominence group. Figure 276 demonstrates the prominence groups for a series.
Peak A is a parent of peak B if (1) A and B belong to the same prominence group, (2) A has higher prominence than B, and (3) points between the left and right prominence bases of A overlap the points between the left and right prominence bases of B. If there is more than one peak that has all three of these conditions, then A is the peak with the lowest prominence. Note that if there are no peaks with higher prominence within the prominence group of peak B, then B is its own parent peak.
Figure 276 shows a series that has four prominence groups. The value of peak is the same as the value of peak
. In this series,
is in the first prominence group; peaks
,
, and
are in the second prominence group; peak
is in the third prominence group; and peak
is in the fourth prominence group. This figure also shows the prominence and width of each peak. In this figure, peaks
,
,
, and
are their own parents. Peak
is the parent of peak
, and peak
is the parent of peak
.
Figure 276: Prominence Groups

The Syntax of the PEAKLOC Function
The required input arguments to the PEAKLOC function are as follows:
- Series
specifies the input series vector, which is an
or
vector that contains no missing values.
- Filter
-
An optional vector that contains up to seven criteria that a local maximum must satisfy to be classified as a peak. By default, no filters are applied. If you specify a missing value, that criterion is ignored. You can specify the following criteria for peaks:
- minHeight
(
Filter[1]) specifies a minimum height value. A local maximum must exceed this value to be considered as a peak.- minProminence
(
Filter[2]) specifies a minimum prominence value. The prominence of a local maximum must exceed this value to be considered as a peak. This value must be nonnegative.- threshold
(
Filter[3]) specifies the minimum difference between the value of a peak and the value of the points on its immediate left and right in the series. This value must be nonnegative.- minWidth
(
Filter[4]) specifies the minimum width of a peak. This value must be nonnegative. You can specify whether the base points should be calculated from peak height or peak prominence by specifying the width method. Note that the fractionOfHeight parameter affects the width calculation.- maxWidth
(
Filter[5]) specifies the maximum width of a peak. This value must be nonnegative. You can specify whether the base points should be calculated from peak height or peak prominence by specifying the width method. Note that the fractionOfHeight parameter affects the width calculation.- minDistance
(
Filter[6]) specifies the minimum number of indices in the series between two neighboring peaks. This value must be at least 2.- numPeaks
(
Filter[7]) specifies the maximum number of peaks that can be returned by the function. This value must be a nonnegative integer. Peaks with greater height are prioritized when they are returned by the PEAKLOC function. If the total number of peaks is less than numPeaks, this filter does not exclude any peaks.
The PEAKLOC function also supports two optional vectors of parameters. The param1 argument is a two-element numerical vector. The meaning of each element follows:
- tolerance
(
param1[1]) specifies the maximum difference between the values of two neighboring peaks before they are considered to be a plateau. This value must be nonnegative. The default value is 1E–10.- fractionOfHeight
(
param1[2]) specifies the percentage of the peak value or prominence that needs to be considered for width calculation (depending on the width method). Valid values are between 0 and 1, inclusive. The default value is 0.5.
The param2 argument is a two-element character vector. The case of the values (lowercase or uppercase) does not matter. The meaning of each element follows:
- plateau
-
(
param2[1]) specifies which indices to return if a peak is considered a plateau. The default value is "left". There are five valid values:- "left"
returns the index of the leftmost point in the plateau.
- "right"
returns the index of the rightmost point in the plateau.
- "both"
returns the indices of both the leftmost and rightmost points in the plateau.
- "middle"
returns the index of the middle point in the plateau. If the number of points in a plateau is even, it returns the indices of the two middle points.
- "all"
returns the indices of all points in the plateau.
- widthMethod
-
(
param2[2]) specifies whether to calculate the width from the bases of the peak or from the bases of its prominence. As described earlier, the number of indices in the series between the bases of the peak is the width of that peak, and the number of indices in the series between the bases of the prominence is the prominence width. The default value is "prominence". There are two valid options:- "height"
returns the peak width.
- "prominence"
returns the prominence width.
Examples of the PEAKLOC Function
The following example creates a signal whose peaks are detected by using the PEAKLOC function. Figure 277 plots the series and overlays the peaks.
series = {3 4 6.5 7 6 5 4 5 6 7.5 8 7.5 6 5 4 3
2 3 4 5 5 2 3.5 5.5 6 5.5 4 };
locs = peakloc(series);
You can plot the signal and the detected peaks as follows:
x = 1:ncol(series);
peaks = j(1, ncol(series), .);
peaks[locs] = series[locs];
title "Peakloc Example";
call series(x, series) grid={x y}
scatterX=x scatterY=peaks
scatterOption="markerattrs=(size=12)"
scatterOnTop=0;
Figure 277: Series with All Detected Peaks

The following statements set the filter for the minimum height to the value 7. The PEAKLOC function returns only peaks that are at least 7 units in height. The series and the peaks are shown in Figure 278.
filteredPeaks = peakloc(series, {7});
filteredPeakVals = j(1, ncol(series), .);
filteredPeakVals[filteredPeaks] = series[filteredPeaks];
title "Filtered Peaks";
call series(x, series) grid={x y}
scatterX=x scatterY=filteredPeakVals
scatterOption="markerattrs=(size=12)"
scatterOnTop=0;
Figure 278: Filtered Peaks
