python - 从数据中查找峰值

标签 python numpy matplotlib

我通过 matplotlib 在 x 轴上绘制频率,在 y 轴上绘制强度。

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import argrelmax, argrelmin

data = np.loadtxt('pr736024.dat')
z=np.array(data[:,1])   
y = (z-z.min())/(z.max()-z.min()) #for normalization

x = np.loadtxt('frq.txt')

plt.plot(x,y, linewidth = 0.2, color='black')
plt.plot(x,indexes, "o")
plt.show()

enter image description here

我想获得峰值处的强度值(图中可以看到 6 个峰值)。我怎样才能实现这个目标?

最佳答案

这个答案改编 self 在 here 中的答案。 .

这里有一个 numpythonic 解决方案(这比显式执行循环要好得多)。

您必须定义一个阈值,在该阈值之上检测到最大值,在您的情况下可以是 0.2。

我使用roll函数将数字+1或-1移动到该位置。此外,“峰值”被定义为局部最大值,其中前一个和后一个数字都小于中心值。

完整代码如下:

import numpy as np
import matplotlib.pyplot as plt

# input signal
x = np.arange(1,100,1)
y = 0.3 * np.sin(t) + 0.7 * np.cos(2 * t) - 0.5 * np.sin(1.2 * t)
threshold = 0.2

# max
maxi = np.where(np.where([(y - np.roll(y,1) > 0) & (y - np.roll(y,-1) > 0)],y, 0)> threshold, y,np.nan)

关于python - 从数据中查找峰值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51282964/

相关文章:

python - 在满足某些条件的 numpy 数组中查找长度 N 的范围

python - Flask-SocketIO 的 emit 函数线程安全吗?

python - 如何使用大阵列来防止大量使用 RAM?

python - 如何获得具有边界条件的图像面积?

python - 放大数据、重新调整 y 轴刻度的最有效方法

python - 如果附加到 python 中的数组,如何更优雅地执行 elif statments

python - postgres - 无法使用 psycopg2 删除数据库

python - numpy.zeros(n) 和 numpy.zeros(n,1) 之间的区别

python - 自定义matplotlib图像显示添加copy/paste

python - 如何从 Matplotlib 格式化等高线