在python中的矩形脉冲序列

Jua*_*nma 2 python matlab numpy scipy

我正在尝试在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]);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 我怎么能在python中实现这个信号?

谢谢.

Eri*_*ric 6

如果您正在寻找周期性的脉冲序列,例如您给出的示例 - 这是一个脉冲序列,它持续5个周期然后关闭五个周期:

N = 100 # sample count
P = 10  # period
D = 5   # width of pulse
sig = np.arange(N) % P < D
Run Code Online (Sandbox Code Playgroud)

给予

plot(sig)
Run Code Online (Sandbox Code Playgroud)

图(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
)
Run Code Online (Sandbox Code Playgroud)

赠送:

可配置的脉冲序列


我认为这是其中一个例子,其中matlab的pulsetran函数比python中的单行实现更令人困惑,这可能是scipy没有提供它的原因.


ato*_*3ls 6

您可以使用以下square功能scipy.signal:

这里逐字:

from scipy import signal
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 500, endpoint=False)
plt.plot(t, signal.square(2 * np.pi * 5 * t))
plt.ylim(-2, 2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

因此,对于您的示例,请执行以下操作:

T=10
D=5
N=10
shift = 1/4   # number of cycles to shift (1/4 cycle in your example)
x = np.linspace(0, T*N, 10000, endpoint=False)
y=signal.square(2 * np.pi * (1/T) * x + 2*shift*np.pi)
plt.plot(x,y)
plt.ylim(-2, 2)
plt.xlim(0, T*N)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述