【问题标题】:rectangular pulse train in pythonpython中的矩形脉冲序列
【发布时间】:2015-06-24 05:54:54
【问题描述】:

我正在尝试在 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 中实现这个信号?

谢谢。

【问题讨论】:

  • 你能展示一个 matlab 的输出示例吗?可能有也可能没有特定的内置函数,但我们肯定可以通过编程实现这种行为
  • 添加链接,我们可以为您转换成内嵌图片
  • 谢谢,我放图片

标签: python matlab numpy scipy


【解决方案1】:

如果您只是在寻找周期性脉冲序列,就像您给出的示例一样 - 这是一个开启 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不提供它的原因。

【讨论】:

  • 这就是我想要的。非常感谢你们的帮助。 :)
  • 指定活动周期(类似 PWM)的可能变化是:sig = np.arange(N) % ncycles &lt; ncycles * pwm_ratio0 &lt; pwm_ratio &lt; 1
【解决方案2】:

您可以使用scipy.signal 中的square 函数:

here逐字逐句:

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)

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

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)

【讨论】:

    【解决方案3】:

    所有答案都很好,但我发现scipy.integrate 存在一些问题,所以我创建了 3 种类型,特别牢记scipy.integrate

    • 统一的脉冲宽度和统一的时间段(每个参数必须是一个数字)。

    def uniform_pulse_function(self, t, start, stop, pulsewidth, period, amplitude):
            func = amplitude * np.where((t > start and t < stop and (t % period <(pulsewidth))),
                                        1, 0)
    • 脉冲宽度一致,但幅度不同(每个参数必须是单个数字,幅度除外,幅度应该是与启动和停止中可以容纳的脉冲数相同长度的元组):

    func = (amplitude[int(t//period)])*np.where((t>start and t<stop and (t%period<(pulsewidth))), 1, 0)
            return func
    • 不均匀的脉冲宽度和不均匀的幅度,但周期应该保持不变(幅度和脉冲宽度应该是一个元组,长度与开始和停止中可以容纳的脉冲数相同,而周期应该是一个数字):

    def custom_pulse_function(self, t, start, stop, pulsewidth, period, amplitude):
            func = (amplitude[int(t//period)]) * np.where((t > start and t < stop and (t % period < (pulsewidth[int(t//period)]))), 1, 0)
            return func

    【讨论】:

      猜你喜欢
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2013-10-12
      • 2019-04-04
      相关资源
      最近更新 更多