% symmetrical class A amplifier with MOSFETs
% Vdd=12V, Vgg=4V, Vt=3V, B=0.5A/V^2, m=1, n=1/2, R=2.5ohm

clear all
close all

%initialization
npoint=720; % reduce if the simulation is too slow
i=1:npoint;
wt=2*pi*(i-0.5)/npoint;
deg=wt*180/pi;
s=sin(wt);

np=20; % number of frames, reduce if the simulation is too slow

for k=1:np
    
    % computation of voltages and currents
    Vinm(k)=k/np*1; % amplitude of the input voltage, 1 just stands for 1V
    vin=Vinm(k)*s; % amplitude * pattern
    vout=5*vin; % A*vin, A=5 for this amplifier
    id1=0.25*(1+vin).*(1+vin);
    id2=0.25*(1-vin).*(1-vin);
    idd=id1+id2;
    vds1=12-2*vout;
    vds2=12+2*vout;
    pd1=id1.*vds1;
    pd2=id2.*vds2;
    pdd=12*idd;
    pout=vout.*vout/2.5;
    Pdd(k)=mean(pdd);
    Pd1(k)=mean(pd1);
    Pd2(k)=mean(pd2);
    Pout(k)=mean(pout);
    efficiency(k)=Pout(k)/Pdd(k)*100;
    
    figure(1)
    
    subplot(4,3,1)
    plot(deg,id1)
    xlabel('w_0t [deg]')
    ylabel('i_{D1} [A]')
    axis([0 360 0 1])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,2)
    plot(deg,vds1)
    xlabel('w_0t [deg]')
    ylabel('v_{DS1} [V]')
    axis([0 360 0 25])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,3)
    plot(vds1,id1,'.')
    xlabel('v_{DS1} [V]')
    ylabel('i_{D1} [A]')
    axis([0 25 0 1])
   set(gca,'xtick',[0 5 10 15 20 25])
    
    subplot(4,3,4)
    plot(deg,id2)
    xlabel('w_0t [deg]')
    ylabel('i_{D2} [A]')
    axis([0 360 0 1])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,5)
    plot(deg,vds2)
    xlabel('w_0t [deg]')
    ylabel('v_{DS2} [V]')
    axis([0 360 0 25])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,6)
    plot(vds2,id2,'.')
    xlabel('v_{DS2} [V]')
    ylabel('i_{D2} [A]')
    axis([0 25 0 1])
   set(gca,'xtick',[0 5 10 15 20 25])
    
    subplot(4,3,7)
    plot(deg,vin)
    xlabel('w_0t [deg]')
    ylabel('v_{IN} [V]')
    axis([0 360 -5 5])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,8)
    plot(deg,vout)
    xlabel('w_0t [deg]')
    ylabel('v_{OUT} [V]')
    axis([0 360 -5 5])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,9)
    plot(deg,pout)
    xlabel('w_0t [deg]')
    ylabel('p_{OUT} [W]')
    axis([0 360 0 12])
    set(gca,'xtick',[0 60 120 180 240 300 360])
        
    subplot(4,3,10)
    plot(deg,pd1)
    xlabel('w_0t [deg]')
    ylabel('p_{D1} [W]')
    axis([0 360 0 12])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,11)
    plot(deg,pd2)
    xlabel('w_0t [deg]')
    ylabel('p_{D2} [W]')
    axis([0 360 0 12])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,12)
    plot(deg,pdd)
    xlabel('w_0t [deg]')
    ylabel('p_{DD} [W]')
    axis([0 360 0 12])
    set(gca,'xtick',[0 60 120 180 240 300 360])
        
    pause(0.1)
    
end

figure(2)
hold on
plot(Vinm,Pdd,'g')
plot(Vinm,Pd1,'b')
plot(Vinm,Pd1,'m')
plot(Vinm,Pout,'r')
plot(Vinm,Pd1+Pd2,'y')
xlabel('V_{INm} [V]')
ylabel('P_{DD} [green], P_{D1} [blue], P_{D2} [magenta], P_{OUT} [red], P_{D1}+P_{D2} [yellow], all in [W]')
axis([0 1 0 12])

figure(3)
plot(Vinm,efficiency)
xlabel('V_{INm} [V]')
ylabel('efficiency [%]')
