数组切片无法正常工作

时间:2017-09-05 17:19:08

标签: python-3.x

我定义了以下函数,该函数查找数组值超过某个级别的索引。我只是想看看它的一部分而不是整个数组。但是,当我运行我的程序时似乎有问题。非常感谢任何帮助。

def crossingTime2(self, level, channel_data, times, min0):
    ndata = len(channel_data[min0 :min0 + int(30)])
    for i in range(ndata-1):
        thisADC = channel_data[i] 
        nextADC = channel_data[i+1]
        if thisADC >= level and nextADC < level:
            return times[i] 
    raise RuntimeError("This is a bug! Investigate!")

1 个答案:

答案 0 :(得分:0)

有两个主要问题。首先,您始终将i0循环到29(或最终取决于channel_data长度)。这意味着,无论您设置min0的是什么,您都会始终检查channel_data0的{​​{1}}值,而不是Nmin0 }。

第二个(可能的)问题是N声明。如果您要查找if中的峰值而不是第二个条件channel_data,则只有当nextADC < level值超过{{1}时,该语句才会为True价值比再次下降;如,

channel_data
确实

level,但channel_data = [1,2,3,4,5,6,5,4,3,2,1] min0 = 2 # Let's consider this slice: [3,4,5,6] level = 5 小于slice_of_channel_data[2] >= level,所以这会给你一个slice_of_channel_data[2+1]

这种行为是否正确?这取决于你想要完成什么,所以考虑编辑if语句。

代码解决方案:

level

更新

OP 所说的内容中,我正在编辑答案。

目标是找到以下信号的下降和上升时间。

Signal

RuntimeError

关于def crossingTime2(self, level, channel_data, times, min0): ndata = len(channel_data[min0 : min0 + 30]) # Slice of maximum 30 values for i in range(min0, min0 + ndata - 1): # Fixed range # Make sure this condition behaves as you expect. # Otherwise change it to # for i in range(min0, min0 + ndata): # if channel_data[i] >= level: # return times[i] if channel_data[i] >= level and channel_data[i+1] < level: return times[i] raise RuntimeError("This is a bug! Investigate!")

想象一下# Function crossing_time() returns a tuple of # 3 elements: (t_before, t_min, t_after) # where t_before is the time before channel_data # starts falling, t_min is the time when # channel_data reaches it's minimum and # t_after is the time after channel_data # has risen again. # # It raises RuntimeError if doesn't find # t_before or t_after. # # Parameters: # channel_data: values of the signal # time: time axis for channel_data # base_level: the base level of channel_dat; # i.e., the average value of # channel_data before and after # the drop. # tolerance: sensitivity to apply when searching # for t_before and t_after. # Set as default to 10**-3. def crossing_time(channel_data, time, base_level, tolerance = 10**-3): # Find where the minimum of the signal is: pos_min = channel_data.index(min(channel_data)) # t_min is the time when the signal reaches the minimum t_min = time[pos_min] # finding the time right before the drop i = pos_min while abs(channel_data[i] - base_level) > tolerance: if i == 0: # Time before the drop not found => Error raise RuntimeError("This is a bug! Investigate!") i -= 1 t_before = time[i] # time right before the signal drops # finding the time right after the drop i = pos_min while abs(channel_data[i] - base_level) > tolerance: if i == 0: # Time after the drop not found => Error raise RuntimeError("This is a bug! Investigate!") i += 1 t_after = time[i] # time right after the signal rises return (t_before, t_min, t_after) tolerance

channel_data

您不能使用base_level之类的条件,因为您无法保证channel_data = [0,1,-1,0,1,-999,1,0,0,1,0] base_level = 0.5 值实际位于while channel_data[i] != base_level。 与此相反,我们使用参数base_level,因此我们可以检查channel_datatolerance是否足够接近被认为是等于,实际检查它们是否相等的插入。

设置channel_data[i]以满足您的需求。它会根据您正在使用的信号值的大小来计算。