如何使用Python在这个实时图形中进行峰值检测

2024-09-28 10:12:23 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个简单的代码matplotlib。(用于实时绘图)(python 3.x)

在这个例子中,如何应用峰值检测?在

我想得到最大峰值点索引和绘制点。实时

下面的图片是这样的

原来是实时图形

original

我要画个顶点

enter image description here

我怎么能?在

这是示例代码 但peakutils方法有误。在

http://matplotlib.org/examples/animation/animate_decay.html

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import peakutils 
from peakutils.plot import plot as pplot


def data_gen(t=0):
    cnt = 0
    while cnt < 1000:
        cnt += 1
        t += 0.1
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)


def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 10)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []


def run(data):
    # update the data
    t, y = data
    xdata.append(t)
    ydata.append(y)
    indexes = peakutils.indexes(ydata, thres=0.5, min_dist=5)  #error line
    print(indexes) 
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
                              repeat=False, init_func=init)
plt.show()

Tags: importdataplotmatplotlibdefasnpline

热门问题