问题描述
我正在Python中实现峰值检测算法,该算法仅检测那些超过阈值幅度的峰值。我不想使用内置函数,因为我也必须将此模拟扩展到硬件实现。
I'm implementing a peak detection algorithm in Python that detects only those peaks that are above a threshold magnitude. I don't want to use the inbuilt function as I have to extend this simulation to Hardware implementation also.
from math import sin,isnan
from pylab import *
def peakdet(v, delta,thresh,x):
delta=abs(delta)
maxtab = []
mintab = []
v = asarray(v)
mn, mx = v[0], v[0]
mnpos, mxpos = NaN, NaN
lookformax = True
for i in arange(len(v)):
this = v[i]
if abs(this)>thresh:
if this > mx:
mx = this
mxpos = x[i]
if this < mn:
mn = this
mnpos = x[i]
if lookformax:
if (this < mx-delta):
if (mx>abs(thresh)) and not isnan(mxpos):
maxtab.append((mxpos, mx))
mn = this
mnpos = x[i]
lookformax = False
else:
if (this > mn+delta):
if (mn<-abs(thresh)) and not isnan(mnpos):
mintab.append((mnpos, mn))
mx = this
mxpos = x[i]
lookformax = True
return array(maxtab), array(mintab)
#Input Signal
t=array(range(100))
series=0.3*sin(t)+0.7*cos(2*t)-0.5*sin(1.2*t)
thresh=0.95 #Threshold value
delta=0.0 #
a=zeros(len(t)) #
a[:]=thresh #
maxtab, mintab = peakdet(series,delta,thresh,t)
#Plotting output
scatter(array(maxtab)[:,0], array(maxtab)[:,1], color='red')
scatter(array(mintab)[:,0], array(mintab)[:,1], color='blue')
xlim([0,t[-1]])
title('Peak Detector')
grid(True)
plot(t,a,color='green',linestyle='--',dashes=(5,3))
plot(t,-a,color='green',linestyle='--',dashes=(5,3))
annotate('Threshold',xy=(t[-1],thresh),fontsize=9)
plot(t,series,'k')
show()
此程序的问题在于,即使它们超过阈值,也无法检测到某些峰。
这是我得到的输出:
The problem with this program is that it is unable to detect some peaks even though they are above the threshold.This is the output I got:
我看到了其他带有峰值检测问题的帖子,但找不到任何解决方案。请帮助并提出更正建议。
I saw other posts with peak detection problems but couldn't find any solution. Please help and suggest corrections.
推荐答案
这些代码
if lookformax:
if (this < mx-delta):
if (mx>abs(thresh)) and not isnan(mxpos):
maxtab.append((mxpos, mx))
mn = this
mnpos = x[i]
lookformax = False
else:
if (this > mn+delta):
if (mn<-abs(thresh)) and not isnan(mnpos):
mintab.append((mnpos, mn))
mx = this
mxpos = x[i]
lookformax = True
仅在以下条件下运行
if abs(this)>thresh:
因此,只有在脱粒上方的下一个点小于该阈值时,您才能找到一个峰值。
so your can only find a peak when the next point above the thresh is smaller than it.
给出条件
这篇关于Python中的峰值检测算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!