%%
prism_folder = fullfile('Full Dataset','BK7 Prism');
run_name = 'Run13_TM';

file_location = fullfile(prism_folder,run_name);
file_location_proc = fullfile(file_location,'processed');

datafile = fullfile(file_location_proc,strcat(run_name,'_processed.mat'));

load(datafile)
piezofig = figure(1);

box on
piezofig.Position(3:4) =[600,400];
plot(ks/10,dist,'.','MarkerSize',12)

xlabel('Piezo voltage (V)');ylabel('Distance (µm)','Interpreter','tex');title('Piezo displacement calibration')
hold on

plot(ks/10,polyval(piezoresponse,ks),'-')
ylim([-0.2,1])
ax=gca;
ax.FontSize = 18;
ax.FontName = 'Serif';
yyaxis right;
ylabel('Distance/\lambda_f','Interpreter', 'tex')
inviss=plot(ks/10,dist/filw,'.','MarkerSize',12);
invisss=plot(ks/10,polyval(piezoresponse,ks)/filw,'LineStyle','none');
inviss.Visible = 'off';
invisss.Visible = 'off';
ylim([-0.2,1].*(1/filw))
ax.YAxis(2).Color = 'k';
ax.YTick = 0:filw:2;
ax.YTickLabel = {'0','0.5','1','1.5'};
ax = gca();
ax.XGrid = 'on';
ax.YGrid = 'off';
yyaxis right
ylm = ylim();
legend('Distance','Quadratic fit','','Location','southeast')
hold off



figure(2)
expfit = @(x) coupling_lw*exp(-2*kappa*x) + intrinsic_lw;
hold on
errorbar(dist(errexclude),FWHM(errexclude)-intrinsic_lw,FWHMerrNeg(errexclude),FWHMerrPos(errexclude),'.','CapSize',1.5,'LineWidth',0.25)  
plot(dist(errexclude),expfit(dist(errexclude))-intrinsic_lw,'LineWidth',3);
ax = gca;
ax.YScale='log';              
xlabel('Distance (\mum)','fontsize', 18);ylabel('log[Linewidth - intrinsic (MHz)]','fontsize', 18);
title(strcat('[',run_name,'] Prism-resonator log(coupling linewidth) by gap'),'fontsize', 18, 'Interpreter','none');
text(0.10,0.2,{strcat('$A_{experimental}\, = \; $',num2str(coupling_lw))},'Units', 'Normalized','Interpreter','latex','fontsize', 16)
text(0.10,0.10,{strcat('$A_{theoretical}\quad = \; $',num2str(coupling_lw_theoretical))},'Units', 'Normalized','Interpreter','latex','fontsize', 16)
legend({strcat('fit$\: $',num2str(coupling_lw),'$e^{-2','\times',num2str(kappa),'(x)} +$',num2str(intrinsic_lw)),'piezo response data'},'Interpreter','latex','fontsize', 16)
fig = gcf;
fig.Position(3:4) =[700,400];

grid on
hold off


