% common emitter amplifier, inductor applied to remove dc, sinusoidal output voltage
% all voltages normalized by Vcc
% all currents normalized by Icq
% resistor normalized to r=(Icq/Vcc)R

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;
pattern=sin(wt);

nr=40; % number of points over r, reduce if the simulation is too slow
rstop=4; % the final value for r

for k=1:nr
    r(k)=rstop*k/nr; % modulation index
    rr=r(k);
    if rr < 1
        jc=1+pattern;
        mce=1+rr-rr*jc;
    else
        mce=1-pattern;
        jc=1+1/rr-1/rr*mce;
    end
    mout=1-mce;
    jout=jc-1;
    pcc=1*jc; % 1* is for educational purposes, a symbol for Vcc*
    pd=mce.*jc;
    pout=mout.*jout;
    pl=1*mout; % 1* is for educational purposes, a symbol for Icq*
    Pcc(k)=mean(pcc);
    Pd(k)=mean(pd);
    Pout(k)=mean(pout);
    Pl(k)=mean(pl);
    efficiency(k)=Pout(k)/Pcc(k)*100;
    figure(1)
    subplot(4,2,1)
    plot(deg,mce)
    xlabel('wt [deg]')
    ylabel('v_{CE}/V_{CC}')
    axis([0 360 0 2])
    set(gca,'xtick',[0 30 60 90 120 150 180 210 240 270 300 330 360])
    subplot(4,2,3)
    plot(deg,jc)
    xlabel('wt [deg]')
    ylabel('i_C/I_{CQ}')
    axis([0 360 0 2])
    set(gca,'xtick',[0 30 60 90 120 150 180 210 240 270 300 330 360])
    subplot(4,2,5)
    plot(deg,mout)
    xlabel('wt [deg]')
    ylabel('v_{OUT}/V_{CC}')
    axis([0 360 -1 1])
    set(gca,'xtick',[0 30 60 90 120 150 180 210 240 270 300 330 360])
    subplot(4,2,2)
    plot(deg,pcc)
    xlabel('wt [deg]')
    ylabel('p_{CC}/(V_{CC} I_{CQ})')
    axis([0 360 0 2])
    set(gca,'xtick',[0 30 60 90 120 150 180 210 240 270 300 330 360])
    subplot(4,2,4)
    plot(deg,pd)
    xlabel('wt [deg]')
    ylabel('p_D/(V_{CC} I_{CQ})')
    axis([0 360 0 2])
    set(gca,'xtick',[0 30 60 90 120 150 180 210 240 270 300 330 360])
    subplot(4,2,6)
    plot(deg,pout)
    xlabel('wt [deg]')
    ylabel('p_{OUT}/(V_{CC} I_{CQ})')
    axis([0 360 0 2])
    set(gca,'xtick',[0 30 60 90 120 150 180 210 240 270 300 330 360])
    subplot(4,2,7)
    plot(mce,jc,'.')
    xlabel('v_{CE}/V_{CC}')
    ylabel('i_C/I_{CQ}')
    axis([0 2 0 2])
    subplot(4,2,8)
    plot(deg,pl)
    xlabel('wt [deg]')
    ylabel('p_L/(V_{CC} I_{CQ})')
    axis([0 360 -1 1])
    set(gca,'xtick',[0 30 60 90 120 150 180 210 240 270 300 330 360])
    pause(1)
end

figure(2)
hold on
plot(r,Pcc,'g')
plot(r,Pd,'b')
plot(r,Pout,'r')
plot(r,Pl,'y')
xlabel('R*Icq/Vcc')
ylabel('P_{CC} [green], P_D [blue], P_{OUT} [red], P_L [yellow], all /(V_{CC} I_{CQ})')
axis([0 rstop -0.1 1.1])

figure(3)
plot(r,efficiency)
xlabel('R I_{CQ}/V_{CC}')
ylabel('efficiency [%]')
