python - 使用 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 上)
解决方案
就像评论说的那样,返回的值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)
推荐阅读
- firebase - 我想在特定用户向数据库添加任务时为他安排 fcm 通知,颤动
- vue.js - 如何在vue中用URL替换一些文本?
- c# - 如何在没有循环依赖的情况下在单独的项目访问和 .Show() 中制作 2 个表单
- javascript - 我在我的 React 应用程序中得到 Not a Number 值
- c++ - 如何获取窗口光标位置
- python - 加入多个数据帧,同时保留主数据帧的行数
- oauth-2.0 - 如何在 Jmeter 中从 Azure 获取 RefreshToken
- c# - Azure 服务总线 - 将参数传递给消息处理程序
- html - 使 CSS 交叉淡入淡出动画/绝对元素响应
- java - 当我尝试使用来自 getStringArrayListExtra() 的变量时遇到问题