% deslib demo: 4.5 and 4.6

clear all
close all
clc

co = [ 0    0.4470    0.7410;
    0.8500    0.3250    0.0980;
    0.9290    0.6940    0.1250;
    0.4940    0.1840    0.5560;
    0.4660    0.6740    0.1880;
    0.3010    0.7450    0.9330;
    0.6350    0.0780    0.1840];


%% define elements given in the circuit 
f=1e9;
ZL=25;
C1=4.7e-12;
Z01=50;
l1=0.127;
Z02=100;
l2=0.171;
C2=10e-12;

%% 
% transform ZL to Reflection factor
GL=[z2g(ZL,Z01),f];
%connect capacitance in series
G1=parc(GL,C1,Z01);
%calculate transmission line in series. The characteristic impedance is the
%same as in the reflection coefficient!
G2=serline(G1,l1,f);
%calculate the reflection of the open parallel stub
Gopen=[1,f]; %the reflection at open circuit is 1 
Gstub=serline(Gopen,l2,f);
%renormalize the reflection coefficient of the stub to 50 Ohm
Gstub50=renorm(Gstub,Z02,Z01);
%connect the stub and the other calculated parts in parallel
G3=parg(G2,Gstub50);
%add series capactitance
Gin=serc(G3,C2,Z01);
%convert the result to an impedance
Zin=g2z(Gin,Z01);

%%
figure
smtool
plotc(GL(:,1),1,'.',co(1,:),15)
drawgarc(real(1./g2z(GL(:,1),Z01)*Z01),0,0,1,':')
drawgarc(real(1./g2z(GL(:,1),Z01)*Z01),imag(1./g2z(GL(:,1),Z01)*Z01),imag(1./g2z(G1(:,1),Z01)*Z01),1,'-',co(1,:))
plotc(G1(:,1),1,'.',co(1,:),15)

drawarc([0,abs(G1(1,1)),angle(G2(1,1))*180/pi,angle(G1(1,1))*180/pi],1,'-',co(2,:))
drawarc([0,abs(G1(1,1)),0,360],1,':')
plotc(G2(:,1),1,'.',co(2,:),15)

drawarc([0,1,angle(Gstub(1,1))*180/pi,0],1,'-',co(3,:))
plotc(Gstub(:,1),1,'.',co(3,:),15)

drawgarc(real(1./g2z(G3(:,1),Z01)*Z01),0,0,1,':')
drawgarc(real(1./g2z(G3(:,1),Z01)*Z01),imag(1./g2z(G3(:,1),Z01)*Z01),imag(1./g2z(G2(:,1),Z01)*Z01),1,'-',co(4,:))
plotc(G3(:,1),1,'.',co(4,:),15)

drawrarc(real(g2z(G3(:,1),Z01)/Z01),imag(g2z(G3(:,1),Z01)/Z01),imag(g2z(Gin(:,1),Z01)/Z01),1,'-',co(6,:))
plotc(Gin(:,1),1,'.',co(6,:),15)




