我有一些流程图,它们代表了寻找时间序列的峰值和谷值的算法。
我试图将它们转换为Python,但我不知道如何修正 "GOTO "的东西。
def minzo(trough,peak, i,x,r,p,sp):
f=-1
i=i+1
if i == (len(ts) - 1):
endzo1 = endzo()
else:
if x[i] < trough:
trough=x[i]
else:
if x[i]-trough>=r:
p=p+1
sp.append(trough)
peak=ts[i]
maxzo1=maxzo()
return trough,peak,i,x,r,p,sp
x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
sp = []
r = 1
trough = x[0]
peak = x[0]
i = -1
while i<len(x):
i = i + 1
if i == (len(x)-1):
GOTO ENDZO???
else:
if x[i]>peak:
peak=x[i]
if peak-trough>=r:
sp.append(trough)
GOTO MAXZO
else:
if x[i]<trough:
trough=x[i]
if peak-trough>=r:
sp.append(peak)
GOTO MINZO
流程图如下。
开始
GOTO
这样做对吗?
def endzo(x, trough, peak, i, p, f, l, r, s, n):
n = p + 1
if abs(f) == 1:
if f == 1:
s[n] = peak
else:
s[n] = trough
else:
s[n] = (trough + peak) / 2
return x, trough, peak, i, p, f, l, r, s, n
def maxzo(x, trough, peak, i, p, f, l, r, s, n):
f = f + 1
while i < len(x):
i = i + 1
if i == (len(x) - 1):
x, trough, peak, i, p, f, l, r, s, n = endzo(x, trough, peak, i, p, f, l, r, s, n)
else:
if x[i] > peak:
peak = x[i]
else:
if peak - x[i] >= r:
p = p + 1
s[p] = peak
trough = x[i]
x, trough, peak, i, p, f, l, r, s, n = minzo(x, trough, peak, i, p, f, l, r, s, n)
return x, trough, peak, i, p, f, l, r, s, n
def minzo(x, trough, peak, i, p, f, l, r, s, n):
f = -1
while i < len(x):
i = i + 1
if i == (len(x) - 1):
x, trough, peak, i, p, f, l, r, s, n = endzo(x, trough, peak, i, p, f, l, r, s, n)
else:
if x[i] < trough:
trough = x[i]
else:
if x[i] - trough >= r:
p = p + 1
s[p] = trough
peak = x[i]
x, trough, peak, i, p, f, l, r, s, n = maxzo(x, trough, peak, i, p, f, l, r, s, n)
return x, trough, peak, i, p, f, l, r, s, n
x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
trough = x[0]
peak = x[0]
i = -1
p = 0
f = 0
l = len(x) - 1
r = 1
s = []
n = 0
while i <= l:
i = i + 1
if i == l:
x, trough, peak, i, p, f, l, r, s, n = endzo(x, trough, peak, i, p, f, l, r, s, n)
else:
if x[i] > peak:
peak = x[i]
if peak - trough >= r:
s[p] = trough
x, trough, peak, i, p, f, l, r, s, n = maxzo(x, trough, peak, i, p, f, l, r, s, n)
else:
if x[i] < trough:
trough = x[i]
if peak - trough >= r:
s[p] = peak
x, trough, peak, i, p, f, l, r, s, n = minzo(x, trough, peak, i, p, f, l, r, s, n)
print(s)