% symmetrical class A amplifier with BJTs
% Vcc=10V, Icq=0.5A, 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*0.025; % amplitude of the input voltage
    vin=Vinm(k)*s; % amplitude * pattern
    vout=200*vin; % A*vin, A=200 for this amplifier
    ic1=0.5+20*vin; % Icq+gm*m*vin
    ic2=0.5-20*vin; % Icq-gm*m*vin
    icc=ic1+ic2;
    vce1=10-2*vout; % Vcc-vout/n
    vce2=10+2*vout; % Vcc+vout/n
    pd1=ic1.*vce1;
    pd2=ic2.*vce2;
    pcc=10*icc; % Vcc*icc
    pout=vout.*vout/2.5;
    Pcc(k)=mean(pcc);
    Pd1(k)=mean(pd1);
    Pd2(k)=mean(pd2);
    Pout(k)=mean(pout);
    efficiency(k)=Pout(k)/Pcc(k)*100;
    
    figure(1)
    
    subplot(4,3,1)
    plot(deg,ic1)
    xlabel('w_0t [deg]')
    ylabel('i_{C1} [A]')
    axis([0 360 0 1])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,2)
    plot(deg,vce1)
    xlabel('w_0t [deg]')
    ylabel('v_{CE1} [V]')
    axis([0 360 0 20])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,3)
    plot(vce1,ic1,'.')
    xlabel('v_{CE1} [V]')
    ylabel('i_{C1} [A]')
    axis([0 20 0 1])
    set(gca,'xtick',[0 5 10 15 20])
    
    subplot(4,3,4)
    plot(deg,ic2)
    xlabel('w_0t [deg]')
    ylabel('i_{C2} [A]')
    axis([0 360 0 1])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,5)
    plot(deg,vce2)
    xlabel('w_0t [deg]')
    ylabel('v_{CE2} [V]')
    axis([0 360 0 20])
    set(gca,'xtick',[0 60 120 180 240 300 360])
    subplot(4,3,6)
    plot(vce2,ic2,'.')
    xlabel('v_{CE2} [V]')
    ylabel('i_{C2} [A]')
    axis([0 20 0 1])
    set(gca,'xtick',[0 5 10 15 20])
    
    subplot(4,3,7)
    plot(deg,vin)
    xlabel('w_0t [deg]')
    ylabel('v_{IN} [V]')
    axis([0 360 -0.025 0.025])
    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,pcc)
    xlabel('w_0t [deg]')
    ylabel('p_{CC} [W]')
    axis([0 360 0 12])
    set(gca,'xtick',[0 60 120 180 240 300 360])
        
    pause(1)
    
end

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

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