%% At 2.01GHz
% K-delta
% Mu
% gamma_S
% gamma_L
% RL
% G_S 
% G_0 
% G_L 
% G_T 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (Anonymous) Functions for phasor/polar notation

% Convert degrees to radians & vice-versa
d2r = @(x) (x*pi/180);
r2d = @(x) (x*180/pi);
% Complex exponential functions with argument in radians and degress
% respectively.
%ie. cis(x) = e^jx = cosx + jsinx
cis = @(x) exp(i*x);
cisd = @(x) cis(d2r(x));
% Returns the polar angle of a complex number in degrees.
angled = @(x) r2d(angle(x));
to_pd = @(x) [abs(x) angled(x)];
conjpol = @(x) [x(1) -x(2)];
to_rd = @(m, d) m .* cisd(d);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% At 2.01GHz %% Riktiga Värden!
S11 = 0.375251936481504 + 0.365415292674947i;
S12 = 0.038307678791770 + 0.057114803592474i;
S21 = -1.992893948652006 + 5.281927336911322i;
S22 = 0.078760254775654 - 0.045706774282787i;

% S11 = .467*cisd(-156.6);
% S12 = 0.05 * cisd(10.83);
% S21 = 8.524 * cisd(56.924);
% S22 = 0.272 * cisd(-59.719);

Z0 = 50; %systemimpedans
% Delta
D = S11*S22 - S12*S21
D_phasor = [abs(D) angled(D)];
% Rollet's Stability Factor
K = (1 - abs(S11)^2 - abs(S22)^2 + abs(D)^2)/(2*abs(S12*S21))
B1 = 1 + abs(S11)^2 - abs(S22)^2 - abs(D^2)
B2 = 1 + abs(S22)^2 - abs(S11)^2 - abs(D^2)
C1 = S11 - D*conj(S22)
C2 = S22 - D*conj(S11)

mu = (1 - abs(S11)^2)/(abs(S22 - D*conj(S11)) + abs(S12*S21));

gammaS = (B1 - sqrt(B1^2 - 4*abs(C1)^2))/(2*C1)
gammaS_phasor = [abs(gammaS) angled(gammaS)]

gammaL = (B2 - sqrt(B2^2 - 4*abs(C2)^2))/(2*C2)
gammaL_phasor = [abs(gammaL) angled(gammaL)]

% %% Only appies to unilateral amplifier! 
% G_S = 1/(1 - abs(gammaS)^2)
% G_0 = abs(S21)^2
% G_L = (1 - abs(gammaL)^2)/(abs(1 - S22*gammaL)^2)
% G_TU = 10*log10(G_S*G_0*G_L)

gammaIN = S11 + (S12*S21*gammaL)/(1 - S22*gammaL); % (6,6a)

% (6,11)
G_T = (abs(S21)^2 * (1 - abs(gammaS)^2) * (1 - abs(gammaL)^2))/(abs(1 - gammaS*gammaIN)^2 * abs(1 - S22*gammaL)^2);
G_T_dB = 10*log10(G_T);

RL_in = -20*log10(abs(S11));

G_T_max = (abs(S21)/abs(S12))*(K - sqrt(K^2 - 1));
G_T_max_dB = 10*log10(G_T_max);

%% Noise Figure (F)
% from datasheet p.7 'Typical Noise Parameters'
Rn_50 = 0.11;
Rn = Rn_50 * 50
Fmin = 0.43 %dB
gammaOPT = 0.54*cisd(51.7)


% %% Ex. 6.5 s.216
% S11 = 0.6*cisd(-60)
% S12 = 0.05 * cisd(26)
% S21 = 1.9 * cisd(81)
% S22 = 0.5 * cisd(-60)
% B1 = 1 + abs(S11)^2 - abs(S22)^2 - abs(D^2);
% B2 = 1 + abs(S22)^2 - abs(S11)^2 - abs(D^2);
% C1 = S11 - D*conj(S22); 
% C2 = S22 - D*conj(S11);
% gammaS = (B1 - sqrt(B1^2 - 4*abs(C1)^2))/(2*C1);
% Rn = 20
% Fmin = 1.6 %dB
% gammaOPT = 0.62*cisd(100)

% (P.6.60)
F = Fmin + (4*Rn/Z0) * ((abs(gammaS - gammaOPT)^2)/((1 - abs(gammaS)^2)*abs(1 + gammaOPT)^2)) 
% Noise figure parameter
N = (db2pow(F)-db2pow(Fmin))/(4*Rn/Z0)*abs(1+gammaOPT)^2
% C_F (Noise figure circle center)
C_F = gammaOPT/(N+1)
% Rf (Nosie figure circle radius)
R_F = sqrt(N*(N+1-abs(gammaOPT)^2))/(N+1)

figure;
smithchart;
hold on;
viscircles([real(C_F) imag(C_F)], [R_F])
plot(real(C_F), imag(C_F),'ro','LineWidth',1.5)