私が望むように動作するピーク検出ルーチンがあります。しかし、私はより柔軟にしたいです。可変幅のサイズを持つPythonのピーク検出
def peak2(x,y,dp,dv):
# Define two arrays: one for the peaks and one
# for the valleys
peaks=[]
valleys=[]
# Create two arrays, one for x, and one for y, where each
# element of the new array # consists of three adjacent
# elements of the old array.
xt=zip(x,x[1:],x[2:])
yt=zip(y,y[1:],y[2:])
# Walk through these arrays, checking to see if the middle
# value of the three old elements exceeds its neighbors by
# d or more.
idx=1
for i,j in zip(xt,yt):
if(j[1]-j[0]>dp and j[1]-j[2]>dp):
peaks.append((x[idx],y[idx]))
elif (j[0]-j[1]>dv and j[2]-j[1]>dv):
valleys.append((x[idx],y[idx]))
idx+=1
return array(peaks),array(valleys)
ご覧のとおり、右側と左側のネイバーとの値を比較してピークを検出します。そして、中心値がその直近の両方の隣接値よりもある閾値だけ大きい場合には、ピークとみなされる。谷を見つけるための同様の論理。
センター値を各側のn個のネイバーと比較するように展開します。私は、関数(それw
を呼び出す)にパラメータを渡します、とw=3
なら、私はこのような何か:
xt=zip(x,x[1:],x[2:])
yt=zip(y,y[1:],y[2:])
ルーチンで、現在は何かということです。しかしw=5
なら、私はこれ欲しい:
xt=zip(x,x[1:],x[2:],x[3:],x[4:])
yt=zip(y,y[1:],y[2:],y[3:],y[4:])
そしてn
が奇数のw=n
、なら、私はこれ欲しい:
n
の要素が含まれ、これらの配列を構築することができますどのように
xt=zip(x,x[1:],x[2:],...,x[n:])
yt=zip(y,y[1:],y[2:],...,y[n:])
他の配列の?
あなたは、引数のリストを作成し、その後zip
に(
*
で)を開梱
を使用してそれらを渡すためにslice
でrange
を使用することができます