freq : 0.05; /* frequency of the reference signal */
hysteresis : 0.15; /* hysteresis of the delta pwm */
currentStep : 0.5; /* rate of increase (or decrease) in the output current */
RefSinSignal(t) := sin(2*%pi*freq*t);
UpSineSignal(t) := RefSinSignal(t) + hysteresis;
LoSineSignal(t) := RefSinSignal(t) - hysteresis;
stepSize : 1e-2; /* step size for calculations */
stepLim : ceiling(1/(freq*stepSize)); /* final step */
myX : makelist(k*stepSize,k,0,stepLim)$ /* X axis points of evaluation */
increasing : 1; /* flag to identify if the current is increasing or decreasing */
currentY : [0]$ /* current signal (input to modulate) */
deltaSignal : [0]$ /* the delta-PWM signal (modulated) */
crossing : [0]$ /* rising and falling times of the PWM signal */
for j:1 thru stepLim do /* create the current waveform and its delta-PWM */
block(
if(increasing=1)
then if(currentY[j]<=UpSineSignal(j*stepSize)) /* if current is increasing and below upper limit */
then block(
currentY : append(currentY,[currentY[j]+(currentStep*stepSize)]),
deltaSignal : append(deltaSignal,[1]))
else block(
currentY : append(currentY,[currentY[j]-(currentStep*stepSize)]),
deltaSignal : append(deltaSignal,[0]),
crossing : append(crossing,[myX[j]]),
increasing : 0)
else
if(currentY[j]>=LoSineSignal(j*stepSize)) /* if current is decreasing and above lower limit */
then block(
currentY : append(currentY,[currentY[j]-(currentStep*stepSize)]),
deltaSignal : append(deltaSignal,[0]))
else block(
currentY : append(currentY,[currentY[j]+(currentStep*stepSize)]),
deltaSignal : append(deltaSignal,[1]),
crossing : append(crossing,[myX[j]]),
increasing : 1)
);
crossing : setify(crossing)$ /* convert list to set */
/* Plot them */
load(draw);
topPlot : gr2d(points_joined=true,point_type=dot,
line_width=2,ytics={-1,0,1},font="Times",
ylabel="Analog signals",font_size=15,
color=red,key="Reference",explicit(RefSinSignal(x),x,0,20),
color=green,key="Limits",explicit(UpSineSignal(x),x,0,20),
key="",explicit(LoSineSignal(x),x,0,20),
color=blue,key="Output",points(myX,currentY),
xtics=crossing,grid=true,
user_preamble="set format x ""; set tmargin 0;
set bmargin 0; set xlabel "" 0,1.5",
title=""
)$
lowPlot : gr2d(points_joined=true,point_type=dot,
line_width=2,ytics={0,1},font="Times",title="",
ylabel="Delta-PWM signal",xlabel="Time",
color=magenta,points(myX,deltaSignal),
xtics=crossing,grid=true,yrange=[-.25,1.25],
user_preamble="set format x ""; set tmargin 0;
set bmargin 1.5"
)$
draw(terminal=svg,file_name="Delta PWM",
topPlot,lowPlot);