clc; clear all; close all;

% 31.01.2021
% Michael Koefinger

% Approx. by Pozar
% checked for diff pair:
% https://www.eeweb.com/tools/edge-coupled-microstrip-impedance/

l = 1.8e-2;
d = 0.1702e-3;
eps_0 = 8.85e-12;
eps_r = 3.72;
Z0 = 45;
c0 = 3e8;


A = Z0/60*sqrt((eps_r+1)/2)+(eps_r-1)/(eps_r+1)*(0.23+0.11/eps_r);
B = 377*pi/(2*Z0*sqrt(eps_r));

Wperd_1 = 8*exp(A)/(exp(2*A)-2);
Wperd_2 = 2/pi*(B-1-log(2*B-1)+(eps_r-1)/(2*eps_r)*(log(B-1)+0.39-0.61/eps_r));  

if Wperd_1 < 2
    W = Wperd_1*d;
else
    W = Wperd_2*d;
end

Wmm = W/1e-3;
Wmils = W/0.0000254;

Wreal = W;
eps_e = (eps_r+1)/2+(eps_r-1)/2*1/(sqrt(1+12*d/Wreal));
vp = c0/sqrt(eps_e);
tdelay = l/vp;

if Wreal/d <= 1
    Z0real = 60/sqrt(eps_e)*log(8*d/Wreal+Wreal/(4*d));
else
    Z0real = 120*pi/(sqrt(eps_e)*(Wreal/d+1.393+0.667*log(Wreal/d+1.444)));
end


% measurement with rigol DS1054Z
trise = 8e-9;
tcrit = trise/4;

% stub
lstub_p = 0.5e-3;
lstub_n = 0;
tdstub_p = lstub_p/vp;
tdstub_n = lstub_n/vp;
Wstub = 0.254e-3;

if Wstub/d <= 1
    Z0stub = 60/sqrt(eps_e)*log(8*d/Wstub+Wstub/(4*d));
else
    Z0stub = 120*pi/(sqrt(eps_e)*(Wstub/d+1.393+0.667*log(Wstub/d+1.444)));
end


ZL = Z0stub+Z0real;
Z0 = 45;
gamma = (ZL - Z0)/(ZL+Z0);

ZX = ZL*Z0/(ZL-Z0);
ZLx = ZL*ZX/(ZL+ZX);
gammax = (ZLx-Z0)/(ZLx+Z0);
