小编mon*_*n79的帖子

numpy:累积“更大”运算

我正在尝试编写一个函数来检测所有上升沿 - 向量中值超过特定阈值的索引。这里描述了类似的内容:Python上升/下降沿示波器式触发器,但我想添加滞后,这样触发器就不会触发,除非该值低于另一个限制。

我想出了以下代码:

import numpy as np

arr = np.linspace(-10, 10, 60)
sample_values = np.sin(arr) + 0.6 * np.sin(arr*3)

above_trigger = sample_values > 0.6
below_deadband = sample_values < 0.0
combined = 1 * above_trigger - 1 * below_deadband
Run Code Online (Sandbox Code Playgroud)

现在,在combined数组中1,原始值高于上限,-1原始值低于下限,0原始值介于两者之间:

>>> combined
array([ 1,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  0,  1,  1,  1,  0,  0,
        1,  1,  1,  0, -1, -1, -1, -1, -1, -1, …
Run Code Online (Sandbox Code Playgroud)

python numpy numpy-ufunc

5
推荐指数
1
解决办法
260
查看次数

标签 统计

numpy ×1

numpy-ufunc ×1

python ×1