% beam width fitting


close all
clear classes

zScan = [685 793 835] * 1e-3; % hole spacing is 2 inches

%beam measurements after ZM2 for various ZM2 strain gauge settings

xWidth_1p3 = [4578.81 3716.64 3321.49] * 1e-6/2;
yWidth_1p3 = [4682.61 3777.92 3362.31] * 1e-6/2;

xWidth_3p15 = [4229.8 3364.7 2989.98] * 1e-6/2;
yWidth_3p15 = [4280.52 3416.62 3031.88] * 1e-6/2;

xWidth_4p5 = [3977.13 3115.57 2741.13] * 1e-6/2;
yWidth_4p5 = [3970.8 3162.24 2778.08] * 1e-6/2;

xWidth_6 = [3681.22 2876.69 2520.46] * 1e-6/2;
yWidth_6 = [3728.91 2920.75 2554.22] * 1e-6/2;


goo = beamPath;

goo.seedWaist(100e-6,1.2);

goox_1p3 = goo.fitBeamWidth(zScan,xWidth_1p3);
gooy_1p3 = goo.fitBeamWidth(zScan,yWidth_1p3);

q1 = goox_1p3.seedq.q;
q2 = gooy_1p3.seedq.q;

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

zdomain = 0.5:.01:2;%linspace(min(zScan),max(zScan),100);
figure(1)
clf
hold on
plot(zScan,xWidth_1p3,'bo')
goox_1p3.plotBeamWidth(zdomain,'b')
goox_1p3.plotBeams(zdomain,.5e-3,'b')
plot(zScan,yWidth_1p3,'ro')
gooy_1p3.plotBeamWidth(zdomain,'r')
gooy_1p3.plotBeams(zdomain,-.5e-3,'r')
hold off



goo2 = beamPath;
goo2.seedWaist(100e-6,1.18);


goox_3p15 = goo2.fitBeamWidth(zScan,xWidth_3p15);
gooy_3p15 = goo2.fitBeamWidth(zScan,yWidth_3p15);

q1 = goox_3p15.seedq.q;
q2 = gooy_3p15.seedq.q;

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

zdomain = 0.5:.01:2;%linspace(min(zScan),max(zScan),100);
figure(2)
clf
hold on
plot(zScan,xWidth_3p15,'bo')
goox_3p15.plotBeamWidth(zdomain,'b')
goox_3p15.plotBeams(zdomain,.5e-3,'b')
plot(zScan,yWidth_3p15,'ro')
gooy_3p15.plotBeamWidth(zdomain,'r')
gooy_3p15.plotBeams(zdomain,-.5e-3,'r')
hold off

goo3 = beamPath;
goo3.seedWaist(100e-6,1.18)

goox_4p5 = goo3.fitBeamWidth(zScan,xWidth_4p5);
gooy_4p5 = goo3.fitBeamWidth(zScan,yWidth_4p5);

q1 = goox_4p5.seedq.q;
q2 = gooy_4p5.seedq.q;

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

zdomain = 0.5:.01:2;%linspace(min(zScan),max(zScan),100);
figure(3)
clf
hold on
plot(zScan,xWidth_4p5,'bo')
goox_4p5.plotBeamWidth(zdomain,'b')
goox_4p5.plotBeams(zdomain,.5e-3,'b')
plot(zScan,yWidth_4p5,'ro')
gooy_4p5.plotBeamWidth(zdomain,'r')
gooy_4p5.plotBeams(zdomain,-.5e-3,'r')
hold off

goo4 = beamPath;
goo4.seedWaist(100e-6,1.15)

goox_6 = goo4.fitBeamWidth(zScan,xWidth_6);
gooy_6 = goo4.fitBeamWidth(zScan,yWidth_6);

q1 = goox_6.seedq.q;
q2 = gooy_6.seedq.q;

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

zdomain = 0.5:.01:2;%linspace(min(zScan),max(zScan),100);
figure(3)
clf
hold on
plot(zScan,xWidth_6,'bo')
goox_6.plotBeamWidth(zdomain,'b')
goox_6.plotBeams(zdomain,.5e-3,'b')
plot(zScan,yWidth_6,'ro')
gooy_6.plotBeamWidth(zdomain,'r')
gooy_4p5.plotBeams(zdomain,-.5e-3,'r')
hold off