%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Network analyzer data analysis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear, close all;

%% Data taken with network analyzer
path1  = '~/matlab_workspace/freq_data/';
file1  = 'trace01.csv';% s11 Psec3 magni board with res with chip power off
file2  = 'trace02.csv';% s11 Psec3 board magni with res with chip power on
file3  = 'trace03.csv';% s11 Psec2 board magni with res with chip power
file4  = 'trace04.csv';% s12 113cm cable magni
file5  = 'trace05.csv';% s12 Psec2 board magni without res with chip
file6  = 'trace06.csv';% s12 Psec3 board magni without res with chip
file7  = 'trace07.csv';% s12 Psec3 board phase without res with chip
file8  = 'trace08.csv';% s11 Psec3 board phase without res with chip
file9  = 'trace09.csv';% s11 Psec3 board phase with    res with chip
file10 = 'trace10.csv';% s11 Psec3 board magni without res with chip
file11 = 'trace11.csv';% s11 Psec3 board smith with    res with chip
file12 = 'trace12.csv';% s12 Psec3 board smith without res with chip
file13 = 'trace13.csv';% s12 37cm  cable magni
file14 = 'trace14.csv';% s12 37cm  cable phase
file15 = 'trace15.csv';% s11 Psec2 board magni without res without chip
file16 = 'trace16.csv';% s12 Psec2 board magni without res without chip
file17 = 'trace17.csv';% s12 Psec2 board phase without res without chip

freq1  = csvread([path1  file1 ],3);
freq2  = csvread([path1  file2 ],3);
freq3  = csvread([path1  file3 ],3);
freq4  = csvread([path1  file4 ],3);
freq5  = csvread([path1  file5 ],3);
freq6  = csvread([path1  file6 ],3);
freq7  = csvread([path1  file7 ],3);
freq8  = csvread([path1  file8 ],3);
freq9  = csvread([path1  file9 ],3);
freq10 = csvread([path1 file10],3);
freq11 = csvread([path1 file11],3);
freq12 = csvread([path1 file12],3);
freq13 = csvread([path1 file13],3);
freq14 = csvread([path1 file14],3);
freq15 = csvread([path1 file15],3);
freq16 = csvread([path1 file16],3);
freq17 = csvread([path1 file17],3);

%% Data taken with scope + fast pulser
path2 = '~/matlab_workspace/scope_data/';
file18 ='trace01.csv';%freq sweep with all   cap 50 Ohms
file19 ='trace02.csv';%freq sweep with input cap
file20 ='trace03.csv';%freq sweep with DAC   cap
file21 ='trace04.csv';%freq sweep with all   cap
file22 ='trace05.csv';%freq sweep with all   cap board on
file23 ='trace06.csv';%freq sweep cable data industry
file24 ='trace07.csv';%freq sweep cable Tecktro 
file25 ='trace08.csv';%freq sweep cable Cadence
file26 ='trace09.csv';%freq sweep board Cadence
file27 ='trace10.csv';%freq sweep inside chip
file28 ='trace11.csv';%freq sweep board Cadence sparam
file29 ='trace12.csv';%freq sweep board Cadence sparam
file30 ='trace14.csv';%freq sweep board Cadence sparam
file31 ='trace15.csv';%freq sweep board Cadence sparam

data1 = csvread([path2 file18 ]);
data2 = csvread([path2 file19 ]);
data3 = csvread([path2 file20 ]);
data4 = csvread([path2 file21 ]);
data5 = csvread([path2 file22 ]);
data6 = csvread([path2 file23 ]);
data7 = csvread([path2 file24 ]);
data8 = csvread([path2 file25 ]);
data9 = csvread([path2 file26 ]);
data10= csvread([path2 file27 ]);
data11= csvread([path2 file28 ],2);
data12= csvread([path2 file29 ],2);
data13= csvread([path2 file30 ],2);
data14= csvread([path2 file31 ],2);

path3 = '~/matlab_workspace/Network2/';
netw1 = 'TRACE01.CSV';% S12 Board and Chip
netw2 = 'TRACE02.CSV';% S12 Board
netw3 = 'TRACE03.CSV';% S11 Real
netw4 = 'TRACE04.CSV';% S11 Imag
netw5 = 'TRACE05.CSV';% S12 Black Cable
netw6 = 'TRACE06.CSV';% S12 Cable 
netw7 = 'TRACE07.CSV';% S12 Rigid Cable
netw8 = 'TRACE08.CSV';% S11 Real Psec2 50Ohms
netw9 = 'TRACE09.CSV';% S11 Imag Psec2 50Ohms

netw1 = csvread([path3 netw1 ],3);
netw2 = csvread([path3 netw2 ],3);
netw3 = csvread([path3 netw3 ],3);
netw4 = csvread([path3 netw4 ],3);
netw5 = csvread([path3 netw5 ],3);
netw6 = csvread([path3 netw6 ],3);
netw7 = csvread([path3 netw7 ],3);
netw8 = csvread([path3 netw8 ],3);
netw9 = csvread([path3 netw9 ],3);


%% Cable freq
% Plot the measured  bode plot of a 113cm and 37cm cable small  line
% Plot the predicted bode plot of a 113cm and 37cm cable big    line
% Plot the theoretical response for 133cm and 37cm cable dashed line

theo = logspace(log10(3e5),log10(200e9),100);
ampl1 = -(1.43/50*25.4*(1/1.4+1/0.3).*sqrt(theo/1e6)+9.15*sqrt(2.1)*1e-4.*theo/1e6)/100*1.14;
ampl2 = ampl1/1.14*0.37;

pred1 = data6(:,2)*1.13/100;
pred2 = data6(:,2)*0.37/100;

figure(1)
hold on
 plot(freq4(:,1) , freq4(:,2) ,'b','LineWidth',1);  %cable meas 113cm
 plot(freq13(:,1), freq13(:,2),'r','LineWidth',1);  %cable meas 37 cm
 plot(data6(:,1) , pred1      ,'b','LineWidth',2);  %cable vend 113cm
 plot(data6(:,1) , pred2      ,'r','LineWidth',2);  %cable vend 37 cm
 plot(theo      , ampl1       ,'b--','LineWidth',2);%cable theo 113cm
 plot(theo      , ampl2       ,'r--','LineWidth',2);%cable theo 37 cm
 grid on
 axis([5e5 200e9 -3 .5])
 xlabel('Frequency (Hz)')
 ylabel('20*log(in/out)')
 title('Cable bode plots');
hold off

clear theo ampl1 ampl2;
%% Cable phase
phase1      = zeros(length(freq14(:,2)),1);
phase1(1,1) = freq14(1,2);
delta      = 0;

for i = 2:length(freq14(:,2))
    if((freq14(i,2)-freq14(i-1,2)>180))
        delta = delta-360;
    end
    phase1(i,1) = freq14(i,2)+delta;
end

phase2      = zeros(length(freq17(:,2)),1);
phase2(1,1) = freq17(1,2);
delta2      = 0;

for i = 2:length(freq17(:,2))
    if((freq17(i,2)-freq17(i-1,2)>180))
        delta2 = delta2-360;
    end
    phase2(i,1) = freq17(i,2)+delta2;
end

phase3      = zeros(length(freq7(:,2)),1);
phase3(1,1) = freq7(1,2);
delta3      = 0;

for i = 2:length(freq7(:,2))
    if((freq7(i,2)-freq7(i-1,2)>180))
        delta3 = delta3-360;
    end
    phase3(i,1) = freq7(i,2)+delta3;
end

phasec = phase1*6.5/37;
phaseb = phase2-phasec;
phasef = phase3-phasec;
phasep = phasef-phaseb;

figure(2)
hold on
plot(freq14(:,1),phase1  ,'r'  ,'LineWidth',2); %cable phase 37 cm
plot(freq14(:,1),phasec  ,'g'  ,'LineWidth',2); %cable phase 6.5cm
plot(freq17(:,1),phase2  ,'b'  ,'LineWidth',2); %board+cable 6.5cm phase
plot(freq17(:,1),phaseb  ,'m'  ,'LineWidth',2); %board only
plot(freq7(:,1) ,phasef  ,'k'  ,'LineWidth',2); %board+chip
plot(freq7(:,1) ,phasep  ,'c'  ,'LineWidth',2); %chip  only

grid on
hold off

%% Pulser freq

% Plot the mesured bode plot of the pulser trough a cable
% Plot the theoret bode plot of the pulser trough a cable

omega = 5.8e9;
theo  = logspace(6,9,100);
ampl  = 20*log10(abs(1./(1+1i.*theo/omega)));
ampl1 = -(1.43/50*25.4*(1/1.4+1/0.3).*sqrt(theo/1e6)+9.15*sqrt(2.1)*1e-4.*theo/1e6)/100*1.15;
ampl2 = ampl+ampl1;

figure(3)
hold on
 plot(data3(:,1),20*log10(data3(:,2)),'b','LineWidth',1);%pulser cable
 plot(freq4(:,1), freq4(:,2)         ,'r','LineWidth',1);%networ cable
 plot(theo      ,ampl2               ,'g','LineWidth',1);%theore cable
 grid on
 axis([5e5 1e9 -3 .5])
 xlabel('Frequency (Hz)')
 ylabel('20*log(in/out)')
 title('Cable bode plots');
hold off

clear omega theo ampl ampl1 ampl2;
%% Bare board freq

% Plot the mesured bode plot of the bare board with the pulser
% Plot the mesured bode plot of the bare board with the network a.

omega = 2e9;
theo  = logspace(6,log10(3e9),100);

ampl  = 20*log10(abs(1./(1+1i.*theo/omega)));
bode  = 20*log10(data2(:,3)./data2(:,2));

figure(4)
hold on 
 plot(freq16(:,1),freq16(:,2),'b'  ,'LineWidth',2);%network ana
 plot(data2(:,1) ,bode       ,'b--','LineWidth',2);%pulser
 plot(theo       ,ampl       ,'r'  ,'LineWidth',2);%low pass
 grid on
 axis([5e5 3e9 -5 .5])
 xlabel('Frequency (Hz)')
 ylabel('20*log(in/out)')
 title('Bare board bode plots');
hold off

clear omega theo ampl bode;
%% S12 & S11 plots

% Plot of bare  board S12
% Plot of whole board s12

omega1 = 400e6;
omega2 = 2e9;
theo  = logspace(6,log10(3e9),100);
ampl1  = 20*log10(abs(1./(1+1i.*theo/omega1)));
ampl2  = 20*log10(abs(1./(1+1i.*theo/omega2)));
bode1  = 20*log10(data2(:,3)./data2(:,2));
bode2  = 20*log10(data4(:,3)./data4(:,2));

figure(5)
hold on 
 plot(freq16(:,1),freq16(:,2) ,'b'  ,'LineWidth',2);%network ana bare board
 plot(data2(:,1) ,bode1       ,'b'  ,'LineWidth',1);%pulser      bare board
 plot(freq6(:,1) ,freq6(:,2)  ,'b'  ,'LineWidth',2);%network ana full board
 plot(data4(:,1) ,bode2       ,'b'  ,'LineWidth',1);%pulser      bare board
 plot(theo       ,ampl1       ,'r'  ,'LineWidth',2);%low pass
 plot(theo       ,ampl2       ,'r'  ,'LineWidth',2);%low pass
 %plot(data11(:,1) ,data11(:,2),'r'  ,'LineWidth',2);%Cadence
 %plot(data12(:,1) ,data12(:,2),'r--','LineWidth',2);%Cadence
 plot(data13(:,1) ,data13(:,2),'g--','LineWidth',2);%Cadence
 plot(data14(:,1) ,data14(:,2),'c--','LineWidth',2);%Cadence
 grid on
 axis([5e5 3e9 -12 .5])
 xlabel('Frequency (Hz)')
 ylabel('20*log(in/out)')
 title('S12 bode plots');
hold off

%% Network analysis number 2 plots

figure(6)
hold on
 plot(netw1(:,1),netw1(:,2),'r'  ,'LineWidth',2);
 plot(netw2(:,1),netw2(:,2),'b'  ,'LineWidth',2);
 plot(netw2(:,1),netw1(:,2)-netw2(:,2),'g'  ,'LineWidth',2);
 grid on;
 axis([5e5 3e9 -15 .5])
 xlabel('Frequency (Hz)')
 ylabel('20*log(in/out)')
 title('Bode plots');
hold off

figure(7)
hold on
plot(netw3(:,1),netw3(:,2),'b','LineWidth',2);
plot(netw4(:,1),netw4(:,2),'r','LineWidth',2);
grid on;
hold off

%% Analysis Eric idea

ttt1 = csvread('BW_pulser_amp.txt',2);
theo = data3(:,1);
omega = 2e9;
ampl  = 20*log10(abs(1./(1+1i.*theo/omega)));
ampl1 = -(1.43/50*25.4*(1/1.4+1/0.3).*sqrt(theo/1e6)+9.15*sqrt(2.1)*1e-4.*theo/1e6)/100*1.15;
ampl2 = ampl+ampl1;
figure(8)
hold on
plot(data3(:,1),20*log10(data3(:,2)),'b','LineWidth',1);%pulser cable
plot(freq4(:,1), freq4(:,2)         ,'r','LineWidth',1);%networ cable
plot(ttt1(:,1)*1e6,20*log10(ttt1(:,2)/.249));
hold off

