%% Trace plotter
% Plots TRACEXX.csv files generated by Keysight E4991B impedance analyser.

% IEEE Transactions font styles

%set(0,'defaulttextinterpreter','latex')
set(0,'DefaultTextFontName','Times',...
'DefaultAxesFontName','Times')


%% Q vals read in

files = dir('*.csv');

temp = importfile(files(1).name);
Q = zeros(length(temp),length(files));
freq = temp(:,1);

for i=1:length(files)
    temp = importfile(files(i).name);
    Q(:,i) = temp(:,2);
end

%% Permittivity plots

% Fairly chunky graphs
set(0,'defaultlinelinewidth',2);
set(0,'defaultlinemarkersize',8);
set(0,'DefaultAxesFontSize',10)
set(0,'DefaultAxesLineWidth',1);

colourspace = [215,48,39;
244,109,67;
253,174,97;
254,224,144;
171,217,233;
116,173,209;
69,117,180]./255;

figure;

line_fewer_markers(freq./1e6,Q(:,1),6,'LockOnMax',1,'Color',colourspace(7,:),'LineStyle',':','Marker','+');
hold on
line_fewer_markers(freq./1e6,Q(:,2),6,'LockOnMax',1,'Color',colourspace(6,:),'LineStyle','-.','Marker','o');
line_fewer_markers(freq./1e6,Q(:,3),6,'LockOnMax',1,'Color',colourspace(5,:),'LineStyle','--','Marker','*');
line_fewer_markers(freq./1e6,Q(:,4),6,'LockOnMax',1,'Color',colourspace(4,:),'LineStyle','-','Marker','x');
line_fewer_markers(freq./1e6,Q(:,5),6,'LockOnMax',1,'Color',colourspace(3,:),'LineStyle',':','Marker','s');
line_fewer_markers(freq./1e6,Q(:,6),6,'LockOnMax',1,'Color',colourspace(2,:),'LineStyle','-.','Marker','d');
line_fewer_markers(freq./1e6,Q(:,7),6,'LockOnMax',1,'Color',colourspace(1,:),'LineStyle','--','Marker','^');

 xlabel('Frequency (MHz)');
 ylabel('Quality factor, Q');
 xlim([50 3000]);
 ylim([0 200]);
 
 S = cell(4,1);
S(1) = cellstr('1 V');
S(2) = cellstr('4 V');
S(3) = cellstr('8 V');
S(4) = cellstr('12 V');
S(5) = cellstr('16 V');
S(6) = cellstr('20 V');
S(7) = cellstr('30 V');

legend(S,'Location','northeast','Interpreter','latex');
box on;
 hold off
 
% Print the figure to a PDF.

set(gcf, 'PaperPosition', [0 0 12 9]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [12 9]); %Set the paper to have width 5 and height 5.

filename = 'Varactor_Q.pdf';
saveas(gcf, filename); %Save figure

%% Smoothened data with moving average to suppress pointless measurement noise
% Valid approach since we're only looking for general trend of Q between
% capacitor types. Not looking to build a frequency-dependent EM simulation
% model for example.
%
% Cleanse Q, by setting anything negative to NaN.

Q(Q<0) = NaN;

L2 = find(freq<650e6);
L1 = find(freq>300e6);

figure;
s = 50;
line_fewer_markers(smooth(freq(L1(1):L2(end))./1e6,s),smooth(Q(L1(1):L2(end),1),s),6,'LockOnMax',1,'Color',colourspace(7,:),'LineStyle',':','Marker','+');
hold on
line_fewer_markers(smooth(freq(L1(1):L2(end))./1e6,s),smooth(Q(L1(1):L2(end),2),s),6,'LockOnMax',1,'Color',colourspace(6,:),'LineStyle','-.','Marker','o');
line_fewer_markers(smooth(freq(L1(1):L2(end))./1e6,s),smooth(Q(L1(1):L2(end),3),s),6,'LockOnMax',1,'Color',colourspace(5,:),'LineStyle','--','Marker','*');
line_fewer_markers(smooth(freq(L1(1):L2(end))./1e6,s),smooth(Q(L1(1):L2(end),4),s),6,'LockOnMax',1,'Color',colourspace(4,:),'LineStyle','-','Marker','x');
line_fewer_markers(smooth(freq(L1(1):L2(end))./1e6,s),smooth(Q(L1(1):L2(end),5),s),6,'LockOnMax',1,'Color',colourspace(3,:),'LineStyle',':','Marker','s');
line_fewer_markers(smooth(freq(L1(1):L2(end))./1e6,s),smooth(Q(L1(1):L2(end),6),s),6,'LockOnMax',1,'Color',colourspace(2,:),'LineStyle','-.','Marker','d');
line_fewer_markers(smooth(freq(L1(1):L2(end))./1e6,s),smooth(Q(L1(1):L2(end),7),s),6,'LockOnMax',1,'Color',colourspace(1,:),'LineStyle','--','Marker','^');


 xlabel('Frequency (MHz)');
 ylabel('Quality factor, Q_{cap}');
 xlim([300 650]);
 ylim([0 200]);
 
 S = cell(4,1);
S(1) = cellstr('1 V');
S(2) = cellstr('4 V');
S(3) = cellstr('8 V');
S(4) = cellstr('12 V');
S(5) = cellstr('16 V');
S(6) = cellstr('20 V');
S(7) = cellstr('30 V');

legend(S,'Location','eastoutside');
box on;
 hold off
 
set(gcf, 'PaperPosition', [0 0 12 9]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [12 9]); %Set the paper to have width 5 and height 5.

filename = 'Varactor_Q_smooth.pdf';
saveas(gcf, filename); %Save figure

%% Save narrow version for paper

set(gcf, 'PaperPosition', [0 0 12 5]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [12 5]); %Set the paper to have width 5 and height 5.

filename = 'Varactor_Q_smooth_narrow.pdf';
saveas(gcf, filename); %Save figure

