%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Data import %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

file1 = csvread('freq_sweep.csv'); %frequency sweep done with the fully populated board with 50ohms.
file2 = csvread('freq_sweep_bareboard.csv'); %frequency sweep done with only the input coupling cap.
file3 = csvread('freq_sweep_withcapextd.csv'); %frequency sweep done with the capacitance at the output of the DAC.
file4 = csvread('freq_sweep_fullboard.csv'); %frequency sweep done with the fully populated board no 50ohms.
file5 = csvread('freq_sweep_opamp.csv'); %frequency sweep done with the OpAmp on and all capacitance on the path.
file6 = csvread('freq_sweep_cabletheo.csv'); %bode response of the cable given by the industry and calculated at the length used
file7 = csvread('cable_S21.csv'); %data taken with the Tectronics scope we assume cable length = 1 foot
file8 = csvread('freq_sweep_cablecadence.csv'); %data emulated with cadence and the tline
file9 = csvread('freq_sweep_chipcadence.csv'); %data emulated with cadence using the above tline
file10= csvread('freq_sweep_insidechip.csv'); %data taken inside the chip

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Data processing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

cable1 = 20*log(file1(:,3));
cable2 = 20*log(file2(:,2));
cable3 = 20*log(file3(:,2));
cable4 = 20*log(file4(:,2));
cable5 = 20*log(file5(:,2));
cable6 = file6(:,2)*1.14/100;
cable7 = file7(:,2)*1.14/.3;
cable8 = file8(:,2);

freq1 = file1(:,1);
freq2 = file2(:,1);
freq3 = file3(:,1);
freq4 = file4(:,1);
freq5 = file5(:,1);
freq6 = file6(:,1);
freq7 = file7(:,1);
freq8 = file8(:,1);
freq9 = file9(:,1);
freq10= file10(:,1);

data1 = 20*log(file1(:,2)./file1(:,3));
data2 = 20*log(file2(:,3)./file2(:,2));
data3 = 20*log(file3(:,3)./file3(:,2));
data4 = 20*log(file4(:,3)./file4(:,2));
data5 = 20*log(file5(:,3)./file5(:,2));
data9 = file9(:,2);
data10= file10(:,2)-file10(2,2)+data4(30,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure(1)
hold on
% semilogx(freq1,data1,'-r.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq2,data2,'-b.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq3,data3,'-m.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
semilogx(freq4,data4,'-b.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq5,data5,'-r.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
semilogx(freq9,data9,'-c.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq10,data10,'-y.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);

grid on
axis([1e6 1e9 -22 2])
xlabel('Frequency (Hz)')
ylabel('20*log(in/out)')
title('Data bode plots');
hold off

figure(2)
hold on
% semilogx(freq1,cable1,'-r.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq2,cable2,'-b.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq3,cable3,'-m.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq4,cable4,'-k.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
semilogx(freq5,cable5,'-b.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq6,cable6,'-c.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
% semilogx(freq7,cable7,'-y.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);
semilogx(freq8,cable8,'-r.','LineWidth',2,'MarkerEdgeColor','k','MarkerSize',10);

grid on
axis([1e6 1e9 -22 6])
xlabel('Frequency (Hz)')
ylabel('20*log(in/out)')
title('Cable bode plot');
% hold off
% 
% figure(3)
% c1 = .1e-6;
% c2 = 5e-12;
% c3 = 10.1e-6;
% t = 10e-9;
% x = logspace(6,9);
% y = 20*log10(abs(1./((2./(1i*c1.*x)+1./(1i*c3.*x)).*(exp(1i.*x*t)/50+1i*c2.*x.*exp(-1i.*x*t))+exp(1i.*x*t))));
% semilogx(x,y);