Finding local maxima/minima with Numpy in a 1D numpy array

后端 未结 12 1817
迷失自我
迷失自我 2020-11-22 15:13

Can you suggest a module function from numpy/scipy that can find local maxima/minima in a 1D numpy array? Obviously the simplest approach ever is to have a look at the neare

12条回答
  •  眼角桃花
    2020-11-22 15:47

    Why not use Scipy built-in function signal.find_peaks_cwt to do the job ?

    from scipy import signal
    import numpy as np
    
    #generate junk data (numpy 1D arr)
    xs = np.arange(0, np.pi, 0.05)
    data = np.sin(xs)
    
    # maxima : use builtin function to find (max) peaks
    max_peakind = signal.find_peaks_cwt(data, np.arange(1,10))
    
    # inverse  (in order to find minima)
    inv_data = 1/data
    # minima : use builtin function fo find (min) peaks (use inversed data)
    min_peakind = signal.find_peaks_cwt(inv_data, np.arange(1,10))
    
    #show results
    print "maxima",  data[max_peakind]
    print "minima",  data[min_peakind]
    

    results:

    maxima [ 0.9995736]
    minima [ 0.09146464]
    

    Regards

提交回复
热议问题