Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I can write something myself by finding zero-crossings of the first derivative or something, but it seems like a common-enough function to be included in standard libraries. Anyone know of one?

My particular application is a 2D array, but usually it would be used for finding peaks in FFTs, etc.

Specifically, in these kinds of problems, there are multiple strong peaks, and then lots of smaller "peaks" that are just caused by noise that should be ignored. These are just examples; not my actual data:

1-dimensional peaks:

FFT output with peaks

2-dimensional peaks:

Radon transform output with circled peak

The peak-finding algorithm would find the location of these peaks (not just their values), and ideally would find the true inter-sample peak, not just the index with maximum value, probably using quadratic interpolation or something.

Typically you only care about a few strong peaks, so they'd either be chosen because they're above a certain threshold, or because they're the first n peaks of an ordered list, ranked by amplitude.

As I said, I know how to write something like this myself. I'm just asking if there's a pre-existing function or package that's known to work well.

Update:

I translated a MATLAB script and it works decently for the 1-D case, but could be better.

Updated update:

sixtenbe created a better version for the 1-D case.

share|improve this question
    
@endolith Do you have the original MATLAB file that you translated to python for this? Thanks! –  Learnaholic Mar 22 '12 at 23:38
    
    
Thanks a million! This was perfect for my application. –  lollygagger Feb 7 '13 at 16:35
1  
What about this: docs.scipy.org/doc/scipy/reference/generated/… –  dashesy Apr 8 '13 at 19:13

5 Answers 5

up vote 8 down vote accepted

I do not think that what you are looking for is provided by SciPy. I would write the code myself, in this situation.

The spline interpolation and smoothing from scipy.interpolate are quite nice and might be quite helpful in fitting peaks and then finding the location of their maximum.

share|improve this answer

I'm looking at a similar problem, and I've found some of the best references come from chemistry (from peaks finding in mass-spec data). For a good thorough review of peaking finding algorithms read this. This is one of the best clearest reviews of peak finding techniques that I've run across. (Wavelets are the best for finding peaks of this sort in noisy data.).

It looks like your peaks are clearly defined and aren't hidden in the noise. That being the case I'd recommend using smooth savtizky-golay derivatives to find the peaks (If you just differentiate the data above you'll have a mess of false positives.). This is a very effective technique and is pretty easy to implemented (you do need a matrix class w/ basic operations). If you simply find the zero crossing of the first S-G derivative I think you'll be happy.

share|improve this answer
1  
I was looking for a general purpose solution, not one that only works on those particular images. I adapted a MATLAB script to Python and it works decently. –  endolith Dec 17 '09 at 18:30
1  
Right on. Matlab is a good source for algorithms. What technique does the script use? (BTW, SG is a very general purpose technique). –  Paul Dec 17 '09 at 21:23
2  
I linked it above. It basically just searches for local maxima that are larger than a certain threshold above their neighbors. There are certainly better methods. –  endolith Dec 18 '09 at 19:35
1  
@Paul I bookmarked that page. IYO and in summary, what specific technique did you think worked the best for this peak picking business? –  Learnaholic Mar 22 '12 at 23:39
    
why are zeros of derivative better than just testing if a middle out of three points is larger or smaller of the other two. i have already applied sg transfor, seems like an extra cost. –  kirill_igum May 3 at 1:16

Detecting peaks in a spectrum in a reliable way has been studied quite a bit, for example all the work on sinusoidal modelling for music/audio signals in the 80ies. Look for "Sinusoidal Modeling" in the literature.

If your signals are as clean as the example, a simple "give me something with an amplitude higher than N neighbours" should work reasonably well. If you have noisy signals, a simple but effective way is to look at your peaks in time, to track them: you then detect spectral lines instead of spectral peaks. IOW, you compute the FFT on a sliding window of your signal, to get a set of spectrum in time (also called spectrogram). You then look at the evolution of the spectral peak in time (i.e. in consecutive windows).

share|improve this answer
    
Look at peaks in time? Detect spectral lines? I'm not sure what this means. Would it work for square waves? –  endolith Nov 27 '09 at 17:10
    
I tried to add some explanation, let me know if this is still unclear. –  David Cournapeau Nov 30 '09 at 12:02
    
Oh, you're talking about using STFT instead of FFT. This question isn't about FFTs specifically; that's just an example. It's about finding the peaks in any general 1D or 2D array. –  endolith Nov 30 '09 at 16:44

There is a function in scipy named scipy.signal.find_peaks_cwt which sounds like is suitable for your needs, however I didn't experience with it so I cannot recommend..

http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks_cwt.html

share|improve this answer
7  
Yeah, that didn't exist when I asked this, and I'm still not sure how to use it –  endolith Sep 17 '13 at 16:04

There are standard statistical functions and methods for finding outliers to data, which is probably what you need in the first case. Using derivatives would solve your second. I'm not sure for a method which solves both continuous functions and sampled data, however.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.