%Calculates the S params of bipolar NPN transistor, BF199
%as a function of frequency 

%To calculate the beta versus frequency, let Z0 -> 0 and compute Beta = Ic/Ib = VoVi*Zin/Z0

clear all
close all
clear functions
set(0,'DefaultLineLineWidth',3)
set(0,'DefaultAxesFontSize',20)
set(0,'DefaultTextFontSize',20)

Z0 = 50.;
Nvals = 300;
frequency = logspace(7,8,Nvals);
omega = 2*pi*frequency;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Quiescent parameters BF199

Vce = 10.0;
Beta = 100;
Ic = 7; %mA
%The parasitic inductances and capacitances...
Lb = 1.5e-9;
Le = 1.5e-9;
Lc = 1.5e-9;
Ccb = 0.5e-12; %At Vcb = 4.0 volts
%%%%% LOOK AT THIS!! V BIG CAPACITANCE!!
Cbe = 10.e-11;%At Vbe = 1.0 volts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%The low frequency Re input resistance...
Re = Beta*25/Ic; 

%Some impedances
Zbe = 1./omega/j/Cbe;
Zcb = 1./omega/j/Ccb;

Rbb = 0;
Zb = Rbb + j*omega*Lb; 
Ze = j*omega*Le;
Zc = j*omega*Lc;
Zbe = (Re.*Zbe)./(Zbe + Re);

%After a life-time devoted to Algebra we obtain the following
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The input params S21 and S11...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Zin = (Zcb + Zc + Z0).*(1 + j*omega*Re*Cbe) + Beta*(Zc + Z0);
Zin = Zin.*(Zcb + Zc + Z0)./((Zcb + Zc + Z0 + Zbe + Ze).*(1 + j*omega*Re*Cbe) + Beta*(Zc + Z0 + Ze));
Zin = -Zin + Zb + Zcb + Zc + Z0;

S11 = (Zin - Z0)./(Zin + Z0);

V0Vi = Z0./Zin;
V0Vi = V0Vi.*((Zbe + Ze).*(1 + j*omega*Re*Cbe) + Beta*(Ze - Zcb));
V0Vi = V0Vi./((Zcb + Zc + Z0 + Zbe + Ze).*(1 + j*omega*Re*Cbe) + Beta*(Zc + Z0 + Ze));
S21 = V0Vi.*(1 + S11);

%The following is the ratio of collector RF current (in Z0) to base RF current
%It is an indicator of "RF Beta" and should go thru 1 at the cross over frequency of 550 MHz
IcIb = V0Vi.*Zin/Z0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The output params S12 and S22...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Zout = Beta*(Ze - Zcb) + (Zbe + Ze).*(1 + j*omega*Re*Cbe);
Zout  = Zout.*(Zb + Z0)./(Beta.*(Zb + Z0 + Ze) + (1 + j*omega*Re*Cbe).*(Zbe + Ze + Zb + Z0));
Zout = Zout + Zc + Zcb;

S22 = (Zout - Z0)./(Zout + Z0);
V0Vi = Z0./Zout;
V0Vi = V0Vi.*((Zbe + Ze).*(1 + j*omega*Re*Cbe) + Beta*Ze);
V0Vi = V0Vi./((Zbe + Ze + Zb + Z0).*(1 + j*omega*Re*Cbe) + Beta*(Zb + Z0 + Ze));

S12 = V0Vi.*(1 + S22);

if(1==0)
subplot(2,5,1)
semilogx(frequency,abs(S11))
title('|S11|')
subplot(2,5,6)
semilogx(frequency,angle(S11)*180/pi)
title('<S11 (^{o})')

subplot(2,5,2)
semilogx(frequency,abs(S21))
title('|S21|')
subplot(2,5,7)
semilogx(frequency,angle(S21)*180/pi)
title('<S21 (^{o})')

subplot(2,5,3)
semilogx(frequency,abs(S12))
title('|S12|')
subplot(2,5,8)
semilogx(frequency,angle(S12)*180/pi)
title('<S12 (^{o})')

subplot(2,5,4)
semilogx(frequency,abs(S22))
title('|S22|')
subplot(2,5,9)
semilogx(frequency,angle(S22)*180/pi)
title('<S22 (^{o})')

subplot(2,5,5)
semilogx(frequency,real(Zin),frequency,imag(Zin),'r')
title('Zin')

subplot(2,5,10)
loglog(frequency,abs(IcIb))
title('\beta = |Ic/Ib|')

shg

SPARMS_BF199
end
