Documentation Center

  • Trial Software
  • Product Updates


Find local maxima


pks = findpeaks(data)
[pks,locs] = findpeaks(data)
[...] = findpeaks(data,'Name',value)


pks = findpeaks(data) returns local maxima or peaks, pks, in the input data. data requires a row or column vector with real-valued elements with a minimum length of three. findpeaks compares each element of data to its neighboring values. If an element of data is larger than both of its neighbors or equals Inf, the element is a local peak. If there are no local maxima, pks is an empty vector.

[pks,locs] = findpeaks(data) returns the indices of the local peaks.

[...] = findpeaks(data,'Name',value) accepts one or more comma-separated name/value pairs. Specify 'Name' inside single quotes. 'Name' is not case sensitive.

Input Arguments

Name-Value Pair Arguments


Minimum peak height

Specify the minimum peak height as a real-valued scalar. findpeaks only returns peaks that exceed the MINPEAKHEIGHT. Sometimes, specifying a minimum peak height reduces processing time.

Default: -Inf


Minimum peak separation

Specify the minimum peak distance, or minimum separation between peaks as a positive integer. You can use the 'MINPEAKDISTANCE' option to specify that the algorithm ignore small peaks that occur in the neighborhood of a larger peak. When you specify a value for 'MINPEAKDISTANCE', the algorithm initially identifies all the peaks in the input data and sorts those peaks in descending order. Beginning with the largest peak, the algorithm ignores all identified peaks not separated by more than the value of 'MINPEAKDISTANCE'.

Default: 1


Minimum height difference

Specify the threshold height difference between a peak and its neighboring values as a positive real number. findpeaks only returns peaks that exceed their neighbors by at least the value of 'THRESHOLD'.

Default: 0


Number of peaks

Specify the maximum number of peaks to return as a positive integer. findpeaks operates from the first element of the input data and terminates when the number of peaks reaches the value of 'NPEAKS'.

Default: Returns all peaks that meet the specified criteria


Peak sorting

Specify whether to return the peaks in order. Possible values for 'SORTSTR' are 'ascend', 'descend', and 'none'. 'ascend' returns peaks in ascending, or increasing order from the smallest to largest value. The option 'descend' specifies peaks in descending order, from the largest to smallest value. Using 'none' returns peaks in the order they occur in the input data. Specify the value string in lowercase only.

Default: 'none'


expand all

Find Peaks in Data

Find peaks in a vector.

data = [2 12 4 6 9 4 3 1 19 7];
pks = findpeaks(data)
pks =

    12     9    19

Find peaks separated by more than three elements and return their locations.

data = [2 12 4 6 9 4 3 1 19 7];
[pks,locs] = findpeaks(data,'minpeakdistance',3)
pks =

    12    19

locs =

     2     9

Create a signal with 11 peaks. Find the peaks using default settings and mark them in a plot.

x = linspace(0,1,1024);
Pos = [0.1 0.13 0.15 0.23 0.25 0.40 0.44 0.65 0.76 0.78 0.81];
Hgt = [4 5 3 4 5 4.2 2.1 4.3 3.1 5.1 4.2];
Wdt = [0.005 0.005 0.006 0.01 0.01 0.03 0.01 0.01 0.005 0.008 0.005];
PeakSig = zeros(size(x));
for n = 1:length(Pos)
    PeakSig = PeakSig + Hgt(n)./(1 + abs((x - Pos(n))./Wdt(n))).^4;
[pks,locs] = findpeaks(PeakSig);
plot(x,PeakSig), hold on
%  Offset values of peak heights for plotting
plot(x(locs),pks+0.05,'k^','markerfacecolor',[1 0 0]), hold off

See Also

| | |

Was this topic helpful?