clear
Zo = 50;
Yo = 1/Zo;
Ro= 105;

[s_pa(:,:,:,1),f,~,~] = readSnP('18_06_08_105Ohm_kleiner.s2p',2);
[s_pa(:,:,:,2),f,~,~] = readSnP('105 Ohm_freq_simu_teilw.s2p',2);
n = 1;
%  for i = 1:length(f)
%      Zr(i) =( 2*(Zo/Ro)*(s_pa(1,1,i,1)) ) / (1 - (s_pa(1,1,i,1)))+47;
%  end

% plot(f,real(Zr));
% hold on

figure
% Y-Parameter über S-Parameter bestimmen siehe M.Pozar S.192
 for i = 1:length(f)
     Yr(i) = Yo*((((1-(s_pa(1,1,i,n)))*(1+s_pa(2,2,i,n)))+(s_pa(1,2,i,n)*s_pa(2,1,i,n))) / (((1+s_pa(1,1,i,n))*(1+s_pa(2,2,i,n)))-(s_pa(1,2,i,n)*s_pa(2,1,i,n))));
 end
 
for i = 1:length(f)
Zr(i) = 1 / (Yr(i));
end; 
%R aus Reflexionsdaten-Messung
plot(f,real(Zr/Ro));
hold on 
plot(f,imag(Zr/Ro));
plot(f,abs(Zr/Ro));

figure
 for i = 1:length(f)
     Yt(i) = Yo*(-2*(s_pa(1,2,i,n)) / (((1+s_pa(1,1,i,n))*(1+s_pa(2,2,i,n)))-(s_pa(1,2,i,n)*s_pa(2,1,i,n))));
 end
for i = 1:length(f)
Zt(i) = -1 / (Yt(i));
end; 
%R aus Transmissionsdaten-Messung
plot(f,real(Zt/Ro));
hold on
plot(f,imag(Zt/Ro));
plot(f,abs(Zt/Ro));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Simulation %%%%%%%%%%%%%%%%%%%%%%%%%%
n=2;
figure

 for i = 1:length(f)
     Yr(i) = Yo*((((1-(s_pa(1,1,i,n)))*(1+s_pa(2,2,i,n)))+(s_pa(1,2,i,n)*s_pa(2,1,i,n))) / (((1+s_pa(1,1,i,n))*(1+s_pa(2,2,i,n)))-(s_pa(1,2,i,n)*s_pa(2,1,i,n))));
 end
 
for i = 1:length(f)
Zr(i) = 1 / (Yr(i));
end; 
%R aus Reflexionsdaten-Simulation
plot(f,real(Zr/Ro));
hold on 
plot(f,imag(Zr/Ro));
plot(f,abs(Zr/Ro));

figure
 for i = 1:length(f)
     Yt(i) = Yo*(-2*(s_pa(1,2,i,n)) / (((1+s_pa(1,1,i,n))*(1+s_pa(2,2,i,n)))-(s_pa(1,2,i,n)*s_pa(2,1,i,n))));
 end
for i = 1:length(f)
Zt(i) = -1 / (Yt(i));
end; 
%R aus Transmissionsdaten-Simulation
plot(f,real(Zt/Ro));
hold on
plot(f,imag(Zt/Ro));
plot(f,abs(Zt/Ro));