Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

matlab - rectangular pulse train in python

I'm trying to implement a rectangular pulse train in python.

I searched scipy and there is no signal that implements. http://docs.scipy.org/doc/scipy/reference/signal.html

In matlab there is a signal named pulstran: http://es.mathworks.com/help/signal/ref/pulstran.html

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]);

enter image description here How i could implement this signal in python?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Giving

plot(sig)

plot(sig)

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
)

Giving:

configurable pulse train


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...