MATLAB Answers

is there's a way to automatically find peaks in an histogram?

4 views (last 30 days)
sani
sani on 28 May 2020
Commented: sani on 30 May 2020
Hello,
I have a script to analyze data from a detector (the data is not necessarily repeated). right now I have to find the peaks manually which is time-consuming!
is there's a way I can get the range of the peak base in the x-axis automatically? and is there's a way to neglect small peaks (let's say small in respect to the ratio from the max peak)?
here is an example of histogram I'm working with

  2 Comments

sani
sani on 28 May 2020
yes but I couldn't applied it for histogram, just to my original data (and it dosn't relevant)

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 29 May 2020
You can do this using the islocalmax function.
% Sample data
rng default
x = randn(1, 1e5);
h = histogram(x);
% Retrieve some properties from the histogram
V = h.Values;
E = h.BinEdges;
% Use islocalmax
L = islocalmax(V);
% Find the centers of the bins that islocalmax identified as peaks
left = E(L);
right = E([false L]);
center = (left + right)/2;
% Plot markers on those bins
hold on
plot(center, V(L), 'o')

  4 Comments

Show 1 older comment
Steven Lord
Steven Lord on 29 May 2020
The documentation for islocalmax lists several options that you can use to customize how islocalmax looks for peaks. MinProminence, MinSeparation, and/or MaxNumExtrema may be of use depending on whether you want peaks that are sufficiently higher than their surroundings, sufficiently far apart, or only a limited number of peaks.
Image Analyst
Image Analyst on 29 May 2020
How is islocalmax() different than findpeaks()? Is it just that it's in base MATLAB instead of the Signal Processing Toolbox?

Sign in to comment.

More Answers (0)


Translated by