[freq11,xx11,yy11]=textread('S11_11GHz.txt','%f\t%f\t%f','headerlines',1);
[freq11r,x11r,yy11r]=reconstruct(freq11,xx11,yy11);

[freq11_sh,xx11_sh,yy11_sh]=textread('S11_11GHz_short_pos.txt','%f\t%f\t%f','headerlines',1);
[freq11r_sh,x11r_sh,yy11r_sh]=reconstruct(freq11_sh,xx11_sh,yy11_sh);

%figure;
%smithchart(xx11+j.*yy11);
%hold on;
%plot(x11r,yy11r,'r')
%hold off;

%figure;
%smithchart(xx11_sh+j.*yy11_sh);
%hold on;
%plot(x11r_sh,yy11r_sh,'r')
%hold off;

%figure;
phase11r_sh=unwrap(atan2(yy11r_sh,x11r_sh));
abs11r_sh=sqrt(yy11r_sh.^2+x11r_sh.^2);
%plot(freq11r_sh,phase11r_sh,'b');

[value,index]=min(sqrt(yy11r_sh.^2+x11r_sh.^2));
offset=phase11r_sh(index);
% Obtained after some trials
const=-0.127;
phase11_r_2=phase11r_sh-const*offset;
%hold on; plot(freq11r_sh,phase11_r_2,'r'); hold off;

xx11_shunt=abs11r_sh.*cos(phase11_r_2);
yy11_shunt=abs11r_sh.*sin(phase11_r_2);
figure;
smithchart(xx11_shunt+j*yy11_shunt);
hold on;
plot(x11r_sh,yy11r_sh,'r')
shift=-0.125;
plot(xx11_shunt+shift,yy11_shunt,'g')
hold off;

[fris,Q0,Ql,Qext,beta]=qfactor(freq11r_sh,xx11_shunt+shift,yy11_shunt);

% Without the reconstruction to remove the effect of the other mode
phase11_sh=unwrap(atan2(yy11_sh,xx11_sh));
abs11_sh=sqrt(yy11_sh.^2+xx11_sh.^2);
%plot(freq11r_sh,phase11r_sh,'b');

[value2,index2]=min(sqrt(yy11_sh.^2+xx11_sh.^2));
offset2=phase11_sh(index2);
% Obtained after some trials
const2=0.1;
phase11_2=phase11_sh-const2*offset2;

xx11_shunt_or=abs11_sh.*cos(phase11_2);
yy11_shunt_or=abs11_sh.*sin(phase11_2);
figure;
smithchart(xx11_shunt_or+j*yy11_shunt_or);
hold on;
%plot(x11r_sh,yy11r_sh,'m')
plot(xx11_sh,yy11_sh,'r')
shift2=-0.18;
plot(xx11_shunt_or+shift2,yy11_shunt_or,'g')
hold off;
[fris2,Q02,Ql2,Qext2,beta2]=qfactor(freq11r_sh,xx11_shunt_or+shift2,yy11_shunt_or);
