我想找到最大峰值的位置,我该怎么做呢?我正在使用scipy.signal来寻找峰值。我想让代码返回峰值的位置(单位:ums)。
y3bcpkx11#
如果要查找由scipy.signal.find_peaks标识的最高峰,则可以执行以下操作:
scipy.signal.find_peaks
import numpy as npfrom scipy.signal import find_peaksimport matplotlib.pyplot as plt# Example datax = np.linspace(-4000, 4000) # equal spacing needed for find_peaksy = np.sin(x / 1000) + 0.1 * np.random.rand(*x.shape)# Find peaksi_peaks, _ = find_peaks(y)# Find the index from the maximum peaki_max_peak = i_peaks[np.argmax(y[i_peaks])]# Find the x value from that indexx_max = x[i_max_peak]# Plot the figureplt.plot(x, y)plt.plot(x[i_peaks], y[i_peaks], 'x')plt.axvline(x=x_max, ls='--', color="k")plt.show()
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()
如果你只想得到最高点,那么就像Semei建议的那样使用argmax。
1条答案
按热度按时间y3bcpkx11#
如果要查找由
scipy.signal.find_peaks
标识的最高峰,则可以执行以下操作:如果你只想得到最高点,那么就像Semei建议的那样使用argmax。