与其在列表或数组中查找大于特定threshold的所有样本/数据点,不如只查找signal大于threshold的第一个样本。信号可能多次越过阈值。例如,如果我有一个示例信号:signal = [1, 2, 3, 4, 4, 3, 2, 1, 0, 3, 2, 1, 0, 0, 1, 1, 4, 8, 7, 6, 5, 0]
以及threshold = 2,然后signal = numpy.array(signal)
is_bigger_than_threshold = signal > threshold
会给出signal中所有大于threshold的值。
然而,我只想在信号变得大于阈值时获得第一个样本。因此,我将遍历整个列表并进行布尔比较,如first_bigger_than_threshold = list()
first_bigger_than_threshold.append(False)
for i in xrange(1, len(is_bigger_than_threshold)):
if(is_bigger_than_threshold[i] == False):
val = False
elif(is_bigger_than_threshold[i]):
if(is_bigger_than_threshold[i - 1] == False):
val = True
elif(is_bigger_than_threshold[i - 1] == True):
val = False
first_bigger_than_threshold.append(val)
这给了我我想要的结果,即