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

2024-03-20

我想找到最大峰值的位置我该怎么做? 我正在使用 scipy.signal 来查找峰值。我希望代码返回峰值的位置(以微米为单位)。


如果您想找到由 确定的最高峰值scipy.signal.find_peaks那么你可以执行以下操作:

import numpy as np
from scipy.signal import find_peaks
import matplotlib.pyplot as plt

# Example data
x = np.linspace(-4000, 4000)  # equal spacing needed for find_peaks
y = np.sin(x / 1000) + 0.1 * np.random.rand(*x.shape)

# Find peaks
i_peaks, _ = find_peaks(y)

# Find the index from the maximum peak
i_max_peak = i_peaks[np.argmax(y[i_peaks])]

# Find the x value from that index
x_max = x[i_max_peak]

# Plot the figure
plt.plot(x, y)
plt.plot(x[i_peaks], y[i_peaks], 'x')
plt.axvline(x=x_max, ls='--', color="k")
plt.show()

如果您只想要最高点,那么只需按照 Sembei 的建议使用 argmax 即可。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 scipy 找到最大峰值位置、索引? 的相关文章

随机推荐