青春有我
对于没有太多噪音的曲线,我建议使用以下小代码片段:from numpy import *# example data with some peaks:x = linspace(0,4,1e3)data = .2*sin(10*x)+ exp(-abs(2-x)**2)# that's the line, you need:a = diff(sign(diff(data))).nonzero()[0] + 1 # local min+maxb = (diff(sign(diff(data))) > 0).nonzero()[0] + 1 # local minc = (diff(sign(diff(data))) < 0).nonzero()[0] + 1 # local max# graphical output...from pylab import *plot(x,data)plot(x[b], data[b], "o", label="min")plot(x[c], data[c], "o", label="max")legend()show()这+1很重要,因为diff减少了原始索引号。