Non periodic square function train in python

Question:

How can I plot a square-like (non periodic) signal like the one in the image using python? with0 "n" the number of pulses, "p" the time between them, pulse duration, amplitude and "offset" as variables.

Pulse train over time

Already tried scipy.signal.square but im looking for a non periodic solution.

Asked By: oskrjsosa

||

Answers:

The thing to do is to just write some custom code, instead of built-in modules like to tried. For instance:

from matplotlib import pyplot as plt

def plot_square_pulses(n, p, dur, amp, offset):
    plt.figure()
    for i in range(n):
        x1 = i * (p + dur) + offset
        x2 = x1 + dur
        plt.hlines(amp, x1, x2, linewidth=2)
        plt.vlines(x1, 0, amp, linewidth=2)
        plt.vlines(x2, 0, amp, linewidth=2)
    plt.axis('equal')

# Example usage:
plot_square_pulses(n=5, p=0.3, dur=1, amp=1, offset=0.2)

The resulting plot can probably be improved, but should put you in the right direction:

enter image description here

Answered By: ShlomiF
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.