17

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?

enter image description here

Community
  • 1
  • 1
Wajih
  • 793
  • 3
  • 14
  • 31
  • I think you cant separate signal processing from programming in this question. If someone comes up with a statistics based answer, what would it be? Signal processing or programming? –  Apr 25 '12 at 01:24

2 Answers2

8

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.

slayton
  • 20,123
  • 10
  • 60
  • 89
  • 2
    +1 if you know that you histogram is 'inherently' bimodal, then I'd smooth it until it has only two maxima. – leonbloy Apr 24 '12 at 21:24
  • @slayton, The histogram has already been passed through a smoothing phase. –  Apr 25 '12 at 01:27
  • 1
    @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. – slayton Apr 25 '12 at 14:29
7

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.

Community
  • 1
  • 1
yuk
  • 19,098
  • 13
  • 68
  • 99
  • 1
    I 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 – abcd Apr 24 '12 at 18:19
  • @Bringbackspy: Yes, you can use `ksdensity` function instead of `hist`. 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. – yuk Apr 24 '12 at 18:33
  • yuk, just great! Thanks looks promising so I am going to have a PEEK into it :) –  Apr 25 '12 at 01:25