回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我在Python中实现了一个峰值检测算法,它只检测那些超过阈值大小的峰值。我也不想用硬件来扩展这个功能。在</p>
<pre><code>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.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>((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()
</code></pre>
<p>这个程序的问题是,它无法检测到一些峰值,即使它们高于阈值。
这是我得到的输出:</p>
<p><img src="https://i.stack.imgur.com/WKEch.png" alt="Peak Detection Output"/></p>
<p>我看到了其他有峰值检测问题的帖子,但找不到任何解决方案。请帮助并建议更正。在</p>