python - python中的矩形脉冲序列

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

enter image description here 我如何在 python 中实现这个信号?

谢谢。

最佳答案

如果您只是在寻找周期性脉冲序列,就像您给出的示例一样 - 这是一个开启 5 个周期然后关闭五个周期的脉冲序列:

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

给予

plot(sig)

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
)

给予:

configurable pulse train


我认为这是 matlab 的 pulsetran 函数比它在 python 中的单行实现更令人困惑的情况之一,这可能是 scipy 不提供它的原因。

关于python - python中的矩形脉冲序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29698213/

相关文章:

matlab - 在 Matlab 中检查给定原始矩阵和更改矩阵的行交换

matlab - MATLAB 教程中的 SIFT 实现

python - 通过应用掩码提取部分卷积神经网络特征

64 位窗口上的 Python 32 位内存限制

python - 使用 eq_ 和 Nose 测试,有没有办法知道两个字符串的哪一行和哪一行不同?

python - 在标识映射之外强制使用 sqlalchemy ORM get()

python - 列表中每个元组的第一个元素与字母字符之间的高效映射

python - 为什么在Python的交互式解释器中有时会输出转义字符?

r - 使用 kde2d (R) 和 ksdensity2d (Matlab) 生成的 2D KDE 的差异

python - 查找 numpy 数组中两个数字的指定匹配的所有出现次数