如何找到最大峰值位置,用scipy索引?

xj3cbfub  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(234)

我想找到最大峰值的位置,我该怎么做呢?我正在使用scipy.signal来寻找峰值。我想让代码返回峰值的位置(单位:ums)。

y3bcpkx1

y3bcpkx11#

如果要查找由scipy.signal.find_peaks标识的最高峰,则可以执行以下操作:

  1. import numpy as np
  2. from scipy.signal import find_peaks
  3. import matplotlib.pyplot as plt
  4. # Example data
  5. x = np.linspace(-4000, 4000) # equal spacing needed for find_peaks
  6. y = np.sin(x / 1000) + 0.1 * np.random.rand(*x.shape)
  7. # Find peaks
  8. i_peaks, _ = find_peaks(y)
  9. # Find the index from the maximum peak
  10. i_max_peak = i_peaks[np.argmax(y[i_peaks])]
  11. # Find the x value from that index
  12. x_max = x[i_max_peak]
  13. # Plot the figure
  14. plt.plot(x, y)
  15. plt.plot(x[i_peaks], y[i_peaks], 'x')
  16. plt.axvline(x=x_max, ls='--', color="k")
  17. plt.show()

如果你只想得到最高点,那么就像Semei建议的那样使用argmax。

展开查看全部

相关问题