findpeaks() - Signal Processing
examplepks = findpeaks(data) returns
a vector with the local maxima (peaks) of the input signal vector, data.
A local peak is a data sample that is either
larger than its two neighboring samples or is equal to Inf.
Non-Inf signal endpoints are excluded. If a peak
is flat, the function returns only the point with the lowest index.
example[pks,locs] = findpeaks(data) additionally
returns the indices at which the peaks occur.
example[pks,locs,w,p]
= findpeaks(data) additionally returns
the widths of the peaks as the vector w and the
prominences of the peaks as the vector p.
example[___] = findpeaks(data,x) specifies x as
the location vector and returns any of the output arguments from previous
syntaxes. locs and w are
expressed in terms of x.
example[___] = findpeaks(data,Fs) specifies
the sample rate, Fs, of the data. The first sample
of data is assumed to have been taken at time
zero. locs and w are converted
to time units.
example[___] = findpeaks(___,Name,Value) specifies
options using name-value pair arguments in addition to any of the
input arguments in previous syntaxes.
examplefindpeaks(___) without output
arguments plots the signal and overlays the peak values.
Syntax
pks = findpeaks(data) example[pks,locs] = findpeaks(data) example[pks,locs,w,p]
= findpeaks(data) example[___] = findpeaks(data,x) example[___] = findpeaks(data,Fs) example[___] = findpeaks(___,Name,Value) examplefindpeaks(___) example
Example
Find Peaks in a VectorOpen This ExampleDefine a vector with three peaks and plot it.data = [25 8 15 5 6 10 10 3 1 20 7];
plot(data)
Find the local maxima. The peaks are output in order of occurrence. The first sample is not included despite being the maximum. For the flat peak, the function returns only the point with lowest index.pks = findpeaks(data)
pks =
15 10 20
Use findpeaks without output arguments to display the peaks.findpeaks(data)
Find Peaks and Their LocationsOpen This ExampleCreate a signal that consists of a sum of bell curves. Specify the location, height, and width of each curve.x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 4 2 2 3];
Wdt = [2 6 3 3 4 6]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss);
Plot the individual curves and their sum.plot(x,Gauss,'--',x,PeakSig)
Use findpeaks with default settings to find the peaks of the signal and their locations.[pks,locs] = findpeaks(PeakSig,x);
Plot the peaks using findpeaks and label them.findpeaks(PeakSig,x)
text(locs+.02,pks,num2str((1:numel(pks))'))
Sort the peaks from tallest to shortest.[psor,lsor] = findpeaks(PeakSig,x,'SortStr','descend');
findpeaks(PeakSig,x)
text(lsor+.02,psor,num2str((1:numel(psor))'))
Peak ProminencesOpen This ExampleCreate a signal that consists of a sum of bell curves riding on a full period of a cosine. Specify the location, height, and width of each curve.x = linspace(0,1,1000);
base = 4*cos(2*pi*x);
Pos = [1 2 3 5 7 8]/10;
Hgt = [3 7 5 5 4 5];
Wdt = [1 3 3 4 2 3]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss)+base;
Plot the individual curves and their sum.plot(x,Gauss,'--',x,PeakSig,x,base)
Use findpeaks to locate and plot the peaks that have a prominence of at least 4.findpeaks(PeakSig,x,'MinPeakProminence',4,'Annotate','extents')
The highest and lowest peaks are the only ones that satisfy the condition.Display the prominences and the widths at half prominence of all the peaks.[pks,locs,widths,proms] = findpeaks(PeakSig,x);
widths
proms
widths =
0.0154 0.0431 0.0377 0.0625 0.0274 0.0409
proms =
2.6816 5.5773 3.1448 4.4171 2.9191 3.6363
Find Peaks with Minimum SeparationOpen This Example
Sunspots are a cyclic phenomenon. Their number is known to peak roughly every 11 years.
Load the file sunspot.dat, which contains the average number of sunspots observed every year from 1700 to 1987. Find and plot the maxima.load sunspot.dat
year = sunspot(:,1);
avSpots = sunspot(:,2);
findpeaks(avSpots,year)
Improve your estimate of the cycle duration by ignoring peaks that are very close to each other. Find and plot the peaks again, but now restrict the acceptable peak-to-peak separations to values greater than six years.findpeaks(avSpots,year,'MinPeakDistance',6)
Use the peak locations returned by findpeaks to compute the mean interval between maxima.[pks,locs] = findpeaks(avSpots,year,'MinPeakDistance',6);
meanCycle = mean(diff(locs))
meanCycle =
10.9600
Create a datetime array using the year data. Assume the sunspots were counted every year on March 20th, close to the vernal equinox. Find the peak sunspot years. Use the years function to specify the minimum peak separation as a duration.ty = datetime(year,3,20);
[pk,lk] = findpeaks(avSpots,ty,'MinPeakDistance',years(6));
plot(ty,avSpots,lk,pk,'o')
Compute the mean sunspot cycle using datetime functionality.dttmCycle = years(mean(diff(lk)))
dttmCycle =
10.9600
Constrain Peak FeaturesOpen This Example
Load an audio signal sampled at 7418 Hz. Select 200 samples.load mtlb
select = mtlb(1001:1200);
Find the peaks that are separated by at least 5 ms.To apply this constraint, findpeaks chooses the tallest peak in the signal and eliminates all peaks within 5 ms of it. The function then repeats the procedure for the tallest remaining peak and iterates until it runs out of peaks to consider.findpeaks(select,Fs,'MinPeakDistance',0.005)
Find the peaks that have an amplitude of at least 1 V.findpeaks(select,Fs,'MinPeakHeight',1)
Find the peaks that are at least 1 V higher than their neighboring samples.findpeaks(select,Fs,'Threshold',1)
Find the peaks that drop at least 1 V on either side before the signal attains a higher value.findpeaks(select,Fs,'MinPeakProminence',1)
Peaks of Saturated SignalOpen This Example
Sensors can return clipped readings if the data are larger than a given saturation point. You can choose to disregard these peaks as meaningless or incorporate them to your analysis.
Generate a signal that consists of a product of trigonometric functions of frequencies 5 Hz and 3 Hz embedded in white Gaussian noise of variance
. Specify a sample rate of 100 Hz and a sample time of one second. Reset the random number generator for reproducible results.rng default
fs = 1e2;
t = 0:1/fs:1-1/fs;
s = sin(2*pi*5*t).*sin(2*pi*3*t)+randn(size(t))/10;
Simulate a saturated measurement by truncating every reading that is greater than a specified bound of 0.32. Plot the saturated signal.bnd = 0.32;
s(s>bnd) = bnd;
plot(t,s)
xlabel('Time (s)')
Locate the peaks of the signal. findpeaks reports only the rising edge of each flat peak.[pk,lc] = findpeaks(s,t);
hold on
plot(lc,pk,'x')
Use the 'Threshold' name-value pair to exclude the flat peaks. Require a minimum amplitude difference of
between a peak and its neighbors.[pkt,lct] = findpeaks(s,t,'Threshold',1e-4);
plot(lct,pkt,'o','MarkerSize',12)
Determine Peak WidthsOpen This ExampleCreate a signal that consists of a sum of bell curves. Specify the location, height, and width of each curve.x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 2 2 2 3];
Wdt = [3 8 4 3 4 6]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss);
Plot the individual curves and their sum.plot(x,Gauss,'--',x,PeakSig)
Measure the widths of the peaks using the half prominence as reference.findpeaks(PeakSig,x,'Annotate','extents')
Measure the widths again, this time using the half height as reference.findpeaks(PeakSig,x,'Annotate','extents','WidthReference','halfheight')
Related ExamplesPeak AnalysisFind Peaks in Data
Output / Return Value
Limitations
Alternatives / See Also
Reference