【问题标题】:Finding zero crossings from a list in python从python中的列表中查找零交叉
【发布时间】:2013-07-15 18:31:44
【问题描述】:

我确实为此编写了代码:

for i in xrange(len(Derivative)):
    if ((Derivative[i-1] > Derivative[i]) and (Derivative[i+1] < Derivative[i]) and (Derivative[i-1] > 0.0) and (Derivative[i+1] < 0.0) and (Derivative[i] > 0.0)):
        print str(i+1)

这里Derivative 是我必须检测过零的列表,主要是那些在过零之前的值为正而过零之后的值为负的值。

我已附上Derivative的图表以进一步阐明问题!

我想知道在 Python 中是否有更好的方法,我的意思是更短和更精确的代码?

【问题讨论】:

    标签: python-2.7 signal-processing


    【解决方案1】:

    您只需将当前导数与它后面的导数进行比较。因此,您可以删除对 Derivative[i-1] 的任何引用。如果 Derivative[i] 大于零,并且 Derivative[i+1] 小于零,则必然 Derivative[i+1]

    for i in xrange(len(Derivative)-1):
        if Derivative[i] > 0 and Derivative[i+1] < 0:
            print "crossing between indexes {} and {}".format(i, i+1)
    

    另外,我将xrange 的参数缩短了1。否则,你会得到list index out of range

    【讨论】:

    • 我有四个列表来试用该代码。我确实在其中两个中得到了list index out of range 错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2014-04-09
    • 1970-01-01
    相关资源
    最近更新 更多