rectangular pulse train in python

前端 未结 3 2174
后悔当初
后悔当初 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:14

    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.

    0 讨论(0)
  • 2021-01-06 22:29

    All the answers are nice but I found they are having some problems with scipy.integrate so I created 3 types specially keeping in mind scipy.integrate:

    • Uniform width of pulses and uniform time period (each argument must be a single number).

    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)

    • Uniform Pulsewidth but different amplitudes (each argument must be a single number except amplitude which should be a tuple of same length as the number of pulses that can be accommodated within start and stop):

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

    • Non-uniform Pulsewidth and non uniform amplitude but period should remain constant (Amplitude and Pulsewidth should be a tuple of same length as the number of pulses that can be accommodated within start and stop while period should be a single number):

    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

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题