這裏是一個峯值檢測程序,它可以按我的需要工作。不過,我想讓它更加靈活。可變寬度尺寸的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:])
如果w=n
,其中n
是奇數的話,我想這一點:
xt=zip(x,x[1:],x[2:],...,x[n:])
yt=zip(y,y[1:],y[2:],...,y[n:])
那麼,如何建立這些陣列,其中每個元素包含n
元素其他陣列?