%% Analysis of the MockTile

clear, close all;

path   = '~/matlab_workspace/DataMockTile/';
data1  = reshape(csvread([path 'DATA00.S1'],5,0,[5,0,201*9+4,0]),[],201); %line 10
data2  = reshape(csvread([path 'DATA01.S1'],5,0,[5,0,201*9+4,0]),[],201); %line9
data3  = reshape(csvread([path 'DATA02.S1'],5,0,[5,0,201*9+4,0]),[],201); %line8
data4  = reshape(csvread([path 'DATA03.S1'],5,0,[5,0,201*9+4,0]),[],201); %line7
data5  = reshape(csvread([path 'DATA04.S1'],5,0,[5,0,201*9+4,0]),[],201); %line6
data6  = reshape(csvread([path 'DATA05.S1'],5,0,[5,0,201*9+4,0]),[],201); %line5
data7  = reshape(csvread([path 'DATA06.S1'],5,0,[5,0,201*9+4,0]),[],201); %line4
data8  = reshape(csvread([path 'DATA07.S1'],5,0,[5,0,201*9+4,0]),[],201); %line3
data9  = reshape(csvread([path 'DATA08.S1'],5,0,[5,0,201*9+4,0]),[],201); %line2
data10 = reshape(csvread([path 'DATA09.S1'],5,0,[5,0,201*9+4,0]),[],201); %line1
ideal1  = reshape(csvread([path 'DATA10.S1'],5,0,[5,0,201*9+4,0]),[],201);%testbench silver
ideal2  = reshape(csvread([path 'DATA11.S1'],5,0,[5,0,1601*9+4,0]),[],1601);%testbench single silver
ideal3  = reshape(csvread([path 'DATA12.S1'],5,0,[5,0,1601*9+4,0]),[],1601);%testbench silver launcher
ideal4  = reshape(csvread([path 'DATA13.s1'],5,0,[5,0,1601*9+4,0]),[],1601);%testbench single copper
ideal5  = reshape(csvread([path 'DATA14.s1'],5,0,[5,0,1601*9+4,0]),[],1601);%testbench  silver terminated

%% PLot of everything
figure(1)
hold on
plot(data2(1,:),data2(4,:));
plot(data3(1,:),data3(4,:));
plot(data4(1,:),data4(4,:));
plot(data5(1,:),data5(4,:));
plot(data6(1,:),data6(4,:),'k');
plot(data7(1,:),data7(4,:));
plot(data8(1,:),data8(4,:));
plot(data9(1,:),data9(4,:));
plot(data10(1,:),data10(4,:));
plot(ideal1(1,:),ideal1(4,:), 'r');
plot(ideal2(1,:),ideal2(4,:), 'g');
plot(ideal3(1,:),ideal3(4,:), 'k');
plot(ideal4(1,:),ideal4(4,:), 'm');
plot(ideal5(1,:),ideal5(4,:), 'c');
plot((data2(1,:)+data3(1,:)+data4(1,:)+data5(1,:)+data7(1,:)+data8(1,:)+data9(1,:)+data10(1,:))/8,data2(4,:),'y');
grid on
hold off
axis([0 6e9 -30 1])
xlabel('Frequencies (Hz)')
ylabel('Amplitude (dB)')
title('S12 responses of all lines');

%% Some processing
db = zeros(1,10);
for k= 2:10
    
for i = 1:200
    data = eval(['data' num2str(k)]);
    if ((data(4,i)+3 > 0) && (data(4,i+1) < 0));
        eval(['db(1,' num2str(k) ') = data(1,i+1);']);
    end
end

end

for i = 1:200
    if ((ideal1(4,i)+3 > 0) && (ideal1(4,i+1) < 0));
        id = data(1,i+1);
    end
end

db = fliplr(db);
maxdb = max(db);
mindb = min(db(find(db)));

base = ones(886,866)*mindb;
inter = zeros(886,5);
for j = 1:10
    eval(['s' num2str(j) '= ones(886,15)*db(' num2str(j) ');']);
end

plate1 = [s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter];
plate2 = [s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter,s1,inter];
plate = [plate1,s1,inter,s2,inter,s3,inter,s4,inter,s5,inter,plate2,s6,inter,s7,inter,s8,inter,s9,inter,s10,inter,plate1];

%% Visualisation of bandwidth
figure(2)
surf(plate,'EdgeColor','none')
hold off
cmap = colormap(jet);
ratio = 56-(db-mindb)/(maxdb-mindb)*(56-8);

%% Pulse in time domain
figure(3)
plot(linspace(-1e-9,20e-9,201)',data1(4,:));
grid on
axis([-1e-9 20e-9 -.05 .5])
xlabel('Time (s)')
ylabel('Amplitude (V)')
title('Time response of the strip');

%% 3dB Cutoff frequency
figure(4)
plot(db)

%% More plot of single and multiple strips

figure('Name','Single copper strip','NumberTitle','off')
semilogx(ideal4(1,:),ideal4(4,:),'Color',[1,0,0]         ,'LineWidth',2);
grid on
axis([0,6e9,-15,5])
legend('S12 single copper stripline')
xlabel('Frequency in Hz')
ylabel('Gain in dB')
title('S12 of single copper strip')


figure('Name','Single silver strip','NumberTitle','off')
semilogx(ideal2(1,:),ideal2(4,:),'Color',[0.21,0.78,0.21],'LineWidth',2);
grid on
axis([0,6e9,-15,5])
legend('S12 single silver stripline')
xlabel('Frequency in Hz')
ylabel('Gain in dB')
title('S12 of single silver strip')

figure('Name','Multiple silver strip','NumberTitle','off')
semilogx(ideal5(1,:),ideal5(4,:),'Color',[0,0.4,1]       ,'LineWidth',2);
grid on
axis([0,6e9,-15,5])
legend('S12 multiple silver stripline')
xlabel('Frequency in Hz')
ylabel('Gain in dB')
title('S12 of multiple silver strip')

figure('Name','Everyone','NumberTitle','off')
hold on
semilogx(ideal4(1,:),ideal4(4,:),'Color',[1,0,0]         ,'LineWidth',2);
semilogx(ideal2(1,:),ideal2(4,:),'Color',[0.21,0.78,0.21],'LineWidth',2);
semilogx(ideal5(1,:),ideal5(4,:),'Color',[0,0.4,1]       ,'LineWidth',2);
hold off
grid on
axis([0,6e9,-15,5])
legend('S12 of single copper strip','S12 of single silver strip','S12 multiple silver stripline')
xlabel('Frequency in Hz')
ylabel('Gain in dB')
title('S12 of multiple silver strip')
set(gca,'xscale','log');
