I have tried using masks and making lists like x = [0,0.5,0.51,1,1.01], y = [1,1,-1,-1,1] works ofcourse, but is quite tedious and not as nice as i want to make a square wave from x = 0 to 5.
Advertisement
Answer
You could write a little function like this to populate x and y
import matplotlib.pyplot as plt
import math
def SquareWave(high, low, start, end, width):
print("Square Wave")
##Calculate how many cycles
ncycles = int(math.ceil((end - start)/width)) #Round up
print(ncycles)
x=[]
y=[]
x.append(start)
y.append(high)
xstep = width / 2
for n in range(ncycles):
start += xstep #Increment by the step width
x.append(start)
y.append(high)
x.append(start)
y.append(low)
start += xstep #Increment by the step width
x.append(start)
y.append(low)
x.append(start)
y.append(high)
return(x,y)
x,y = SquareWave(1,-1, 0, 5, 1)
plt.plot(x,y)
