我正在使用scipy.singal.find_peaks
使用以下方法查找峰值和最小值:
import numpy as np
from scipy.signal import find_peaks
x=np.array([9.8,57.,53.,37.,24.,19.,16.,15.,13.,13.,12.,12.,11.,11.,11.,11.,11.,11.,10.,13.,13.,13.,15.,13.,12.,14.,15.,14.,51.,34.,24.,20.,24.,22.,18.,57.,63.,38.,27.,28.,31.,33.,94.,71.,48.,40.,43.,39.,31.,27.,22.,21.,20.,19.,18.,18.,19.,20.,20.,49.,62.,48.,43.,34.,33.,28.,26.,26.,24.,23.,23.,26.,27.,70.,97.,57.,46.,68.,82.,59.,49.,37.,40.,45.,36.,33.,28.,22.,23.,284.,524.,169.,111.,148.,98.,68.,50.,38.,30.,28.])
peaks, _ = find_peaks(x)
mins, _ =find_peaks(x*-1)
外观如下:
现在,我有兴趣找到与每个峰最接近的最小值。所以我可以区别它们。
浏览find_peaks
文档后,自变量peak_prominece
似乎是我想要的。
prominences = peak_prominences(x, peaks)[0]
contour_heights = x[peaks] - prominences
然后看起来像这样:
检查后,peak_prominences
已找到峰值之前的最小值。对于我的应用程序,我想要最接近的峰,无论它在峰的前面还是后面。
如何使用mins
来为wlen
计算定义参数peak_prominence
??
由于mins
由极小值的索引组成,如何使用它来定义wlen
?我基本上必须找到以min为单位限制每个峰的索引(即peaks[i]
)。
仅使用mins
和peaks
有更好的方法吗?
答案 0 :(得分:1)
现在,我有兴趣找到与每个峰最接近的最小值。所以我可以区别它们。
以下是您在寻找什么?
closest_mins = [mins[np.argmin(np.abs(x-mins))] for x in peaks]
difference = x[peaks]-x[closest_mins]
print(difference)
[ 47. 3. 1. 37. 4. 45. 54. 3. 44. 51. 36. 8. 413. 37.]
下面是用虚线表示的peaks
,mins
和最接近peaks
对的mins
对的绘图。请注意,有mins
个最接近多个peaks
。
plt.plot(x)
plt.plot(peaks, x[peaks],'o', label = 'peaks')
plt.plot(mins, x[mins],'s', label = 'mins')
plt.plot(closest_mins, x[closest_mins],'*', label = 'closest mins')
for p, m in zip(peaks, closest_mins):
plt.plot([p,m], [x[p], x[m]], 'k', dashes = (4,1))
plt.legend();