如何在Python中检测函数的最末端的峰?

问题描述 投票:1回答:1

我正在尝试使用scipy查找函数的峰,但是我希望它也检测边界处的峰。这是这种情况的快照。正如我们看到的那样,该方法已检测到10个峰,但我希望包括边界处的峰在内的总峰为12个。有什么办法吗?我也不想像那样包括开始和结束索引。我将在大型数据集上运行此方法,以便寻找通用解决方案。

coordinates = df.loc[:, columns].sum(axis=1)
peaks_max_y = argrelextrema(coordinates.to_numpy(), np.greater)[0]

index = np.arange(df.shape[0])
_ = plt.plot(index, coordinates, 'b-', linewidth = 2)
_ = plt.plot(index[peaks_max_y], coordinates[peaks_max_y], 'ro', label = 'minima peaks')
plt.title(pid)

enter image description here

python matplotlib plot scipy
1个回答
0
投票

由于find_peaks将处理其余的点,如何仅在端点处检查斜率?如果左侧的斜率为负,则在开始处会有一个峰。同样,如果最后结果为正,则您有一个峰值。

类似这样的东西:

if np.sign(np.diff(y))[0] < 0:
    # accept y[0] as peak
if np.sign(np.diff(y))[-1] > 0:
    # accept y[-1] as peak
© www.soinside.com 2019 - 2024. All rights reserved.