我正在尝试在 python 中实现矩形脉冲序列。
我搜索了 scipy,没有实现的信号。 http://docs.scipy.org/doc/scipy/reference/signal.html
在 matlab 中有一个名为 pulstran 的信号: http://es.mathworks.com/help/signal/ref/pulstran.html
matlab 中的代码示例如下:
T=10; %Period
D=5; %Duration
N=10; %Number of pulses
x=linspace(0,T*N,10000);
d=[0:T:T*N];
y=pulstran(x,d,'rectpuls',D);
plot(x,y);
ylim([-1,2]);
我如何在 python 中实现这个信号?
谢谢。
最佳答案
如果您只是在寻找周期性脉冲序列,就像您给出的示例一样 - 这是一个开启 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
)
给予:
我认为这是 matlab 的 pulsetran
函数比它在 python 中的单行实现更令人困惑的情况之一,这可能是 scipy 不提供它的原因。
关于python - python中的矩形脉冲序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29698213/