Python 如何找到滞后的过零点?
在numpy中,我想检测信号从(以前)低于某个阈值到高于某个其他阈值的交叉点。这适用于去抖动或存在噪声时的精确过零等情况 像这样:Python 如何找到滞后的过零点?,python,numpy,debouncing,Python,Numpy,Debouncing,在numpy中,我想检测信号从(以前)低于某个阈值到高于某个其他阈值的交叉点。这适用于去抖动或存在噪声时的精确过零等情况 像这样: import numpy # set up little test problem N = 1000 values = numpy.sin(numpy.linspace(0, 20, N)) values += 0.4 * numpy.random.random(N) - 0.2 v_high = 0.3 v_low = -0.3 # find transiti
import numpy
# set up little test problem
N = 1000
values = numpy.sin(numpy.linspace(0, 20, N))
values += 0.4 * numpy.random.random(N) - 0.2
v_high = 0.3
v_low = -0.3
# find transitions from below v_low to above v_high
transitions = numpy.zeros_like(values, dtype=numpy.bool)
state = "high"
for i in range(N):
if values[i] > v_high:
# previous state was low, this is a low-to-high transition
if state == "low":
transitions[i] = True
state = "high"
if values[i] < v_low:
state = "low"
导入numpy
#设置一个小测试问题
N=1000
值=numpy.sin(numpy.linspace(0,20,N))
值+=0.4*numpy.random.random(N)-0.2
v_高=0.3
v_低=-0.3
#查找从低于v_低到高于v_高的转换
transitions=numpy.zeros_like(值,dtype=numpy.bool)
state=“高”
对于范围(N)中的i:
如果值[i]>v_高:
#以前的状态为低,这是从低到高的转换
如果状态==“低”:
转换[i]=True
state=“高”
如果值[i]
我想要一种不显式循环数组的方法:但我想不出任何方法,因为每个状态值都依赖于前一个状态。可以不使用循环吗?可以这样做:
def hyst(x, th_lo, th_hi, initial = False):
hi = x >= th_hi
lo_or_hi = (x <= th_lo) | hi
ind = np.nonzero(lo_or_hi)[0]
if not ind.size: # prevent index error if ind is empty
return np.zeros_like(x, dtype=bool) | initial
cnt = np.cumsum(lo_or_hi) # from 0 to len(x)
return np.where(cnt, hi[ind[cnt-1]], initial)
结果:
我必须对我的工作进行修改,所有修改都基于上面的答案,以允许在使用标准阈值和反向阈值时检测阈值交叉 我不喜欢这个名字,也许现在应该改为
thu-hi2lo
和thu-lo2hi
,而不是thu-lo
和thu-hi
?使用原始值,行为也同样艰难
def hyst(x, th_lo, th_hi, initial = False):
"""
x : Numpy Array
Series to apply hysteresis to.
th_lo : float or int
Below this threshold the value of hyst will be False (0).
th_hi : float or int
Above this threshold the value of hyst will be True (1).
"""
if th_lo > th_hi: # If thresholds are reversed, x must be reversed as well
x = x[::-1]
th_lo, th_hi = th_hi, th_lo
rev = True
else:
rev = False
hi = x >= th_hi
lo_or_hi = (x <= th_lo) | hi
ind = np.nonzero(lo_or_hi)[0] # Index für alle darunter oder darüber
if not ind.size: # prevent index error if ind is empty
x_hyst = np.zeros_like(x, dtype=bool) | initial
else:
cnt = np.cumsum(lo_or_hi) # from 0 to len(x)
x_hyst = np.where(cnt, hi[ind[cnt-1]], initial)
if rev:
x_hyst = x_hyst[::-1]
return x_hyst
@SpaceDog您可以,但如果您使用C语言,则最好编写一个与原始问题类似的简单循环。我的答案中的技巧在Python中更快,因为它使用矢量化的numpy代码,而不是缓慢的Python循环。矢量化代码必须在数据上传递多次,而C中的一个简单循环可以在一次传递中完成所有操作。我问这个问题是因为我试图理解你的函数的功能,所以我可以用C编写代码。。。
def hyst(x, th_lo, th_hi, initial = False):
"""
x : Numpy Array
Series to apply hysteresis to.
th_lo : float or int
Below this threshold the value of hyst will be False (0).
th_hi : float or int
Above this threshold the value of hyst will be True (1).
"""
if th_lo > th_hi: # If thresholds are reversed, x must be reversed as well
x = x[::-1]
th_lo, th_hi = th_hi, th_lo
rev = True
else:
rev = False
hi = x >= th_hi
lo_or_hi = (x <= th_lo) | hi
ind = np.nonzero(lo_or_hi)[0] # Index für alle darunter oder darüber
if not ind.size: # prevent index error if ind is empty
x_hyst = np.zeros_like(x, dtype=bool) | initial
else:
cnt = np.cumsum(lo_or_hi) # from 0 to len(x)
x_hyst = np.where(cnt, hi[ind[cnt-1]], initial)
if rev:
x_hyst = x_hyst[::-1]
return x_hyst
x = np.linspace(0,20, 1000)
y = np.sin(x)
h1 = hyst(y, -0.2, 0.2)
h2 = hyst(y, +0.5, -0.5)
plt.plot(x, y, x, -0.2 + h1*0.4, x, -0.5 + h2)
plt.legend(('input', 'output, classic, hyst(y, -0.2, +0.2)',
'output, reversed, hyst(y, +0.5, -0.5)'))
plt.title('Thresholding with hysteresis')
plt.show()