Detect the two highest Peaks from Histogram
Solution 1
Return data from hist (or histc) function to a variable (y = hist(x,bin);
) and use PEAKFINDER FileExchange submission to find local maximums.
I have also used PEAKDET function from Eli Billauer. Works great. You can check my answer here with code example.
Solution 2
The simplest way to do this would be to first smooth the data using a gaussian kernel to remove the high frequency variations.
Then use the function localmax
to find the local maximums.
Comments
-
Wajih about 2 years
I was trying to understand on how to detect the two peaks from the histogram. There can be multiple but I need to pick the two highest. Basically what I need to to is that although I will have these peaks shifted left or right, I need to get hold of them. Their spread can vary and their PEAK values might change so I have to find a way to get hold of these two peaks in Matlab.
What I have done so far is to create a 5 value window. This window is populated with values from the histogram and a scan is performed. Each time I move 5-steps ahead to the next value and compare the previous window value with current. Which ever is greater is kept.
Is there a better way of doing this?
-
abcd about 12 yearsI would suggest performing a kernel density estimation before running a peak finder on it. The sample figures on the fileexchange page show several false positives. Also, why was that other question migrated to dsp in the first place? o_O
-
yuk about 12 years@Bringbackspy: Yes, you can use
ksdensity
function instead ofhist
. As for question migration to dsp, I have no idea. It was moved by some moderator's decision while I was working on the answer. -
leonbloy about 12 years+1 if you know that you histogram is 'inherently' bimodal, then I'd smooth it until it has only two maxima.
-
Admin about 12 yearsyuk, just great! Thanks looks promising so I am going to have a PEEK into it :)
-
Admin about 12 years@slayton, The histogram has already been passed through a smoothing phase.
-
slayton about 12 years@Wajih, ok, but if you want to just isolate the peaks then you can smooth it more to filter out the high frequency variations. These variations are what makes peak detection difficult because you have lots of local maximums. If you filter them out then it because quite trivial.