首页 > 解决方案 > 使用 matplotlib 和 numpy 查找一组局部最大值的最大值

问题描述

我想问一个关于使用 matplotlib 和 numpy 找到一组峰的最大值的问题。

我得到了包含峰值的数据,并被要求计算一组峰值的最大值。

下面是山峰的图片。

在此处输入图像描述

我发现了该find_peaks 方法并尝试使用它来解决问题。

我在 Jupyter 中编写了以下代码块:

%pylab inline
from scipy.signal import find_peaks

testdata = loadtxt("testdata.dat", usecols=(0,1))
testdata_x = testdata[100:200,0]
testdata_y = testdata[100:200,1]
plot(testdata_x, testdata_y)
show()

peaks = find_peaks(testdata_y)
peaks

但是,我得到以下输出peaks

(array([ 7, 12, 36, 40, 65, 69, 93, 97]), {})

我无法理解为什么我会得到上述输出并且正在努力寻找解决方案。

我还尝试通过以下内容:

peaks = find_peaks(testdata_y, testdata_x)

但这无济于事。

我该如何解决这个问题?

如有必要,我已在此处附加数据文件作为下载链接(托管在 filehosting.org 上)

标签: pythonnumpymatplotlibjupyter-notebook

解决方案


就像评论说的那样,返回的值find_peaks是峰值的索引(或位置)。

要查找这些峰值的值,请使用峰值索引从 中获取值testdata_y。然后你可以得到最大值。

%pylab inline
from scipy.signal import find_peaks

testdata = loadtxt("testdata.dat", usecols=(0,1))
testdata_x = testdata[100:200,0]
testdata_y = testdata[100:200,1]
plot(testdata_x, testdata_y)
show()

peaks = find_peaks(testdata_y)
peak_values = testdata_y[peaks[0]]
max_peak = max(peak_values)

推荐阅读