%% Analysis of data taken at Fermilab the 3/17/2011

clear, close all;

path   = '~/matlab_workspace/Data_Fermilab/';

data1 = csvread([path 'FILE00.F1'],9,0,[9,0,409,0]);
data2 = csvread([path 'FILE01.F1'],9,0,[9,0,409,0]);
data3 = csvread([path 'FILE02.F1'],9,0,[9,0,409,0]);
data4 = csvread([path 'FILE03.F1'],9,0,[9,0,409,0]);
data5 = csvread([path 'FILE04.F1'],9,0,[9,0,409,0]);
data6 = csvread([path 'FILE05.F1'],9,0,[9,0,409,0]);
data7 = csvread([path 'FILE06.F1'],9,0,[9,0,409,0]);
data8 = csvread([path 'FILE07.F1'],9,0,[9,0,409,0]);

time=linspace(0,5e-9,401)';

figure('Name','TDR of Psec3 chip','NumberTitle','off')
subplot(2,2,1)
plot(time,data1,'-x','Color',[1,0,0]         ,'LineWidth',2);
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR of tester board with the chip');

subplot(2,2,2)
plot(time,data2,'-x','Color',[0.21,0.78,0.21],'LineWidth',2);
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR of sampling chip + 50ohms');

subplot(2,2,3)
plot(time,data5,'-x','Color',[0,0.4,1]       ,'LineWidth',2);
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR of launcher');

subplot(2,2,4)
plot(time,data7,'-x','Color',[.44,0.21,0.78] ,'LineWidth',2);
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR of tester board without chip');


freq = linspace(50e6,20e9,401)';

figure('Name','S11 of Psec3 chip','NumberTitle','off')
subplot(2,2,1)
plot(freq,data3,'-x','Color',[1,0,0]         ,'LineWidth',2);
grid on
axis([50e6 20e9 -62 2])
xlabel('Frequency (Hz)')
ylabel('Gain (dB)')
title('S11 of tester board with the chip');

subplot(2,2,2)
plot(freq,data4,'-x','Color',[0.21,0.78,0.21],'LineWidth',2);
grid on
axis([50e6 20e9 -62 2])
xlabel('Frequency (Hz)')
ylabel('Gain (dB)')
title('S11 of gated chip');

subplot(2,2,3)
plot(freq,data6,'-x','Color',[0,0.4,1]       ,'LineWidth',2);
grid on
axis([50e6 20e9 -62 2])
xlabel('Frequency (Hz)')
ylabel('Gain (dB)')
title('S11 of gated launcher');

subplot(2,2,4)
plot(freq,data8,'-x','Color',[.44,0.21,0.78] ,'LineWidth',2);
grid on
axis([50e6 20e9 -62 2])
xlabel('Frequency (Hz)')
ylabel('Gain (dB)')
title('S11 of tester board without the chip');

figure('Name','Comparison of TDRs','NumberTitle', 'off')
subplot(2,1,1)
hold on
plot(time,data1,'-x','Color',[1,0,0]         ,'LineWidth',2);
plot(time,data2,'-x','Color',[0.21,0.78,0.21],'LineWidth',2);
hold off
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR comparison of the chip with and without the board');

subplot(2,1,2)
hold on
plot(time,data1,'-x','Color',[1,0,0]         ,'LineWidth',2);
plot(time,data7,'-x','Color',[.44,0.21,0.78] ,'LineWidth',2);
hold off
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR comparison of the board with and without the chip');

figure('Name', 'Comparison of S11s', 'NumberTitle','off')
subplot(2,1,1)
hold on
plot(freq,data3,'-x','Color',[1,0,0]         ,'LineWidth',2);
plot(freq,data4,'-x','Color',[0.21,0.78,0.21],'LineWidth',2);
hold off
grid on
axis([50e6 20e9 -62 2])
xlabel('Frequency (Hz)')
ylabel('Gain (dB)')
title('S11 comparison of the chip with and without the board');

subplot(2,1,2)
hold on
plot(freq,data3,'-x','Color',[1,0,0]         ,'LineWidth',2);
plot(freq,data8,'-x','Color',[.44,0.21,0.78] ,'LineWidth',2);
hold off
grid on
axis([50e6 20e9 -62 2])
xlabel('Frequency (Hz)')
ylabel('Gain (dB)')
title('S11 comparison of the board with and without the chip');


c= 11e-12;
delay = 80;
fit = zeros(1,401);
fit(delay:401) = .27*(-1+(1-exp(-time(1:401-delay+1)/(50*c))));

figure('Name', 'TDR chip theo', 'NumberTitle','off')
hold on
plot(time,data2-7.5e-3,'-x','Color',[0.21,0.78,0.21],'LineWidth',2);
plot(time,fit,'-x','Color',[0,0.4,1],'LineWidth',2);
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR of sampling chip + 50ohms');

figure('Name', 'TDR launcher', 'NumberTitle','off')
plot(time,data5,'-x','Color',[0,0.4,1]       ,'LineWidth',2);
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR of launcher');

figure(10)
plot(time,data1,'-x','Color',[1,0,0]         ,'LineWidth',2);
grid on
axis([0 5e-9 -.3 .2])
xlabel('Time (S)')
ylabel('Amplitude (V)')
title('TDR comparison of the board with and without the chip');