rectangular pulse train in python

前端 未结 3 2194
后悔当初
后悔当初 2021-01-06 22:02

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/sign

3条回答
  •  庸人自扰
    2021-01-06 22:35

    You could use the square function from scipy.signal:

    Verbatim from 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)
    

    enter image description here

    Therefore, for your example, do this:

    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)
    

    enter image description here

提交回复
热议问题