如果您只是在寻找周期性脉冲序列,例如您所给的示例,那么这是一个脉冲序列,该脉冲序列打开5个周期,然后关闭5个周期:
N = 100 # sample count
P = 10 # period
D = 5 # width of pulse
sig = np.arange(N) % P < D
给予
plot(sig)
您可以np.arange(N)
在linspace
这里替换。请注意,这不 等于 您的代码,因为脉冲未居中。
这是一个完全可配置的脉冲序列:
def rect(T):
"""create a centered rectangular pulse of width $T"""
return lambda t: (-T/2 <= t) & (t < T/2)
def pulse_train(t, at, shape):
"""create a train of pulses over $t at times $at and shape $shape"""
return np.sum(shape(t - at[:,np.newaxis]), axis=0)
sig = pulse_train(
t=np.arange(100), # time domain
at=np.array([0, 10, 40, 80]), # times of pulses
shape=rect(10) # shape of pulse
)
给予:
我认为这是matlabpulsetran
函数比用python单行实现更混乱的情况之一,这可能就是scipy不提供它的原因。