% beam width fitting


close all
clear classes

% A:L2 = 0 mm

zScan_0 = [202 320 400 458 555] * 1e-3; % hole spacing is 2 inches

%beam measurements after ZM2 for various ZM2 strain gauge settings

xWidth_0 = [729.029 1015.436 1239.723 1406.296 1689.945] * 1e-6/2;
yWidth_0 = [728.033 1031.594 1259.941 1441.320 1749.560] * 1e-6/2;


goo = beamPath;

goo.seedWaist(200e-6,0);

goox_0 = goo.fitBeamWidth(zScan_0,xWidth_0);
gooy_0 = goo.fitBeamWidth(zScan_0,yWidth_0);

q1 = goox_0.seedq.q;
q2 = gooy_0.seedq.q;

w1 = goox_0.seedq.waistSize;
w2 = gooy_0.seedq.waistSize;
            
lambda = goox_0.seedq.lambda;
fraction_0 = (2*pi/lambda*w1*w2 * 1/abs(conj(q2)-q1))^2; %square for 2D modematching

zdomain = -0.5:.01:1;%linspace(min(zScan),max(zScan),100);
figure(1)
clf
hold on
plot(zScan_0,xWidth_0,'bo')
goox_0.plotBeamWidth(zdomain,'b')
goox_0.plotBeams(zdomain,.5e-3,'b')
plot(zScan_0,yWidth_0,'ro')
gooy_0.plotBeamWidth(zdomain,'r')
gooy_0.plotBeams(zdomain,-.5e-3,'r')
hold off

% A:L2 = -13 mm

zScan_13 = [308 340 416 485 555] * 1e-3; % hole spacing is 2 inches

%beam measurements after ZM2 for various ZM2 strain gauge settings

xWidth_13 = [946.038 1037.039 1260.202 1455.835 1661.984] * 1e-6/2;
yWidth_13 = [959.841 1054.128 1292.843 1486.505 1709.732] * 1e-6/2;


goo = beamPath;

goo.seedWaist(200e-6,0);

goox_13 = goo.fitBeamWidth(zScan_13,xWidth_13);
gooy_13 = goo.fitBeamWidth(zScan_13,yWidth_13);

q1 = goox_13.seedq.q;
q2 = gooy_13.seedq.q;

w1 = goox_13.seedq.waistSize;
w2 = gooy_13.seedq.waistSize;
            
lambda = goox_13.seedq.lambda;
fraction_13 = (2*pi/lambda*w1*w2 * 1/abs(conj(q2)-q1))^2; %square for 2D modematching

zdomain = -0.5:.01:1;%linspace(min(zScan),max(zScan),100);
figure(2)
clf
hold on
plot(zScan_13,xWidth_13,'bo')
goox_13.plotBeamWidth(zdomain,'b')
goox_13.plotBeams(zdomain,.5e-3,'b')
plot(zScan_13,yWidth_13,'ro')
gooy_13.plotBeamWidth(zdomain,'r')
gooy_13.plotBeams(zdomain,-.5e-3,'r')
hold off

% A:L2 = +17 mm

zScan_17 = [308 340 416 485 555] * 1e-3; % hole spacing is 2 inches

%beam measurements after ZM2 for various ZM2 strain gauge settings

xWidth_17 = [1035.985 1127.876 1338.529 1543.718 1737.392] * 1e-6/2;
yWidth_17 = [1056.579 1152.449 1362.110 1558.867 1783.611] * 1e-6/2;


goo = beamPath;

goo.seedWaist(200e-6,0);

goox_17 = goo.fitBeamWidth(zScan_17,xWidth_17);
gooy_17 = goo.fitBeamWidth(zScan_17,yWidth_17);

q1 = goox_17.seedq.q;
q2 = gooy_17.seedq.q;

w1 = goox_17.seedq.waistSize;
w2 = gooy_17.seedq.waistSize;
            
lambda = goox_17.seedq.lambda;
fraction_17 = (2*pi/lambda*w1*w2 * 1/abs(conj(q2)-q1))^2; %square for 2D modematching

zdomain = -0.5:.01:1;%linspace(min(zScan),max(zScan),100);
figure(3)
clf
hold on
plot(zScan_17,xWidth_17,'bo')
goox_17.plotBeamWidth(zdomain,'b')
goox_17.plotBeams(zdomain,.5e-3,'b')
plot(zScan_17,yWidth_17,'ro')
gooy_17.plotBeamWidth(zdomain,'r')
gooy_17.plotBeams(zdomain,-.5e-3,'r')
hold off