Python中的矩形脉冲序列 [英] rectangular pulse train in python
问题描述
我正在尝试在python中实现矩形脉冲序列.
I'm trying to implement a rectangular pulse train in python.
我搜索了scipy,没有实现的信号. http://docs.scipy.org/doc/scipy/reference/signal. html
I searched scipy and there is no signal that implements. http://docs.scipy.org/doc/scipy/reference/signal.html
在matlab中有一个名为pulstran的信号: http://es.mathworks.com/help/signal/ref/pulstran. html
In matlab there is a signal named pulstran: http://es.mathworks.com/help/signal/ref/pulstran.html
matlab中的代码示例如下:
An example of code in matlab would be like this:
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中实现此信号?
How i could implement this signal in python?
谢谢.
推荐答案
如果您只是在寻找周期性脉冲序列,例如您所给的示例,那么这是一个脉冲序列,该脉冲序列打开5个周期,然后关闭5个周期:
If you're looking for just periodic pulse trains, like the example you gave - here's a pulse train that is on for 5 cycles then off for five cycles:
N = 100 # sample count
P = 10 # period
D = 5 # width of pulse
sig = np.arange(N) % P < D
给予
plot(sig)
您可以在此处用linspace
替换np.arange(N)
.请注意,这与您的代码等效不同,因为脉冲未居中.
You can replace np.arange(N)
with your linspace
here. Note this is not equivalent to your code, as the pulses are not centered.
这是一个完全可配置的脉冲序列:
And here's a fully configurable pulse train:
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不提供它的原因.
I think this is one of those cases where matlab's pulsetran
function is more confusing than the one-line implementation of it in python, which is possibly why scipy does not provide it.
这篇关于Python中的矩形脉冲序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!