我从这个网站找到了一个解决方案:https://www.embeddedrelated.com/showarticle/197.php
所以这是我使用的代码:
import matplotlib.pyplot as plt
import numpy
import scipy.integrate
from scipy import signal
t = numpy.arange(0,4,0.001)
# duty cycle on phase A and B
Da = 0.70
Db = 0.40
def extendrange(ra,rb):
if ra is None:
return rb
elif rb is None:
return ra
else:
return (min(ra[0],rb[0]),max(ra[1],rb[1]))
def createLimits(margin, *args):
r = None
for x in args:
r = extendrange(r, (numpy.min(x),numpy.max(x)))
rmargin = (r[1]-r[0])*margin/2.0
return (r[0]-rmargin,r[1]+rmargin)
def showripple(centeralign=False):
# voltage waveforms on phases A and B
if centeralign:
sawtooth = abs(2*(t % 1) - 1)
Va = sawtooth < Da
Vb = sawtooth < Db
else:
ramp = t % 1
Va = ramp < Da
Vb = ramp < Db
# plot results
margin = 0.1
fig = plt.figure(figsize=(8, 6), dpi=80)
ax = fig.add_subplot(2,1,1)
VA=Va*0.8
VB=Vb*0.8+1
y = [VA, VB]
ax.plot(t,y[0],t,y[1])
ax.set_yticks([0.4,1.4])
ax.set_yticklabels(['A','B'])
ax.set_ylim(createLimits(margin,y[0],y[1]))
ax.set_ylabel('Phase duty cycles')
#generating noise
noise = numpy.random.normal(0,0.009,len(VA))
ax = fig.add_subplot(2,1,2)
y = [VA + noise, VB]
ax.plot(t,y[0],t,y[1])
ax.set_yticks([0.4,1.4])
ax.set_yticklabels(['A','B'])
ax.set_ylim(createLimits(margin,y[0],y[1]))
ax.set_ylabel(' Noisy Phase duty cycles')
showripple(centeralign=True)
plt.show()
Plot