%% SQZ Noise Budget

%% constants
c= 299792458; %speed of light
h = 6.626e-34; %J.s
%l = 532e-9; %laser wavelength
l = 1064e-9; %laser wavelength
fl = c/l; %laser frequency

n_ktp = 1.8868; % crystal index of refraction for green. 1.8296 for 1064. 
d_crys = 10e-3; %crystal length
L = 2*0.175; %OPO cavity length 
FSR = c/(L); %Hz c/round trip
%Fin = pi*(.875*.999*.999*.994)^(1/5)/( 1-sqrt(.875*.999*.999*.994) ); %Chua's thesis eq3.53
Fin = pi*(.98*.999*.999*.994)^(1/5)/( 1-sqrt(.98*.999*.999*.994) ); %green Fin
%Fin = 44
FWHM = FSR / Fin;
V2Hz = 40/FSR;
cav_pole = (FSR/Fin)/2;

%% Free-running NPRO model
f = logspace(-1,5,1e3);
fc = 1;
w = 2*pi*f;
wc = 2*pi*fc; %pole frequency [rad]
noise = 2*1e4./(1+(i*w/wc)); %[Hz/sqrt(Hz) -- estimate frequency noise of Mephisto laser -- basically a LP

%% Load data

OPO_inj = load('OPO_DARM_Inj_March27.TXT');
OPO_inj_x = OPO_inj(:,1);
OPO_inj_y1 = OPO_inj(:,2);
OPO_inj_y2 = OPO_inj(:,3);
OPO_inj_y3 = OPO_inj(:,4);
OPO_inj_y4 = OPO_inj(:,5);


CLF_inj = load('CLF_DARM_Inj_March27.TXT');
CLF_inj_x = CLF_inj(:,1);
CLF_inj_y1 = CLF_inj(:,2);
CLF_inj_y2 = CLF_inj(:,3);
CLF_inj_y3 = CLF_inj(:,4);
CLF_inj_y4 = CLF_inj(:,5);

CLF_inj_nosat = load('CLF_DARM_Inj_March27_notsaturated.TXT');
CLF_inj_x_nosat = CLF_inj_nosat(:,1);
CLF_inj_y2_nosat = CLF_inj_nosat(:,2);
CLF_inj_DARM_nosat = CLF_inj_nosat(:,3);


ref_new = load('CLF_LO_ref_April2.txt');
ref_new_x = ref_new(:,1);
ref_new_CLF = ref_new(:,2);
%LO_inj_y1 = ref_new(:,3);


LO_inj = load('LO_DARM_Inj_March29.TXT');
LO_inj_x = LO_inj(:,1);
LO_inj_y3 = LO_inj(:,4); %DARM ref
LO_inj_y4 = LO_inj(:,5); %DARM injected

LO_inj = load('LOIMON_DARM_Inj_March29.TXT');
%LO_inj_x = LO_inj(:,1);
LO_inj_y2 = LO_inj(:,2); %LO ref
LO_inj_y1 = LO_inj(:,3); %LO inj

LO_duringCLFinj = load('LOCTRL_during_CLFinj_March27.txt');
LO_duringCLFinj_x = LO_duringCLFinj(:,1);
LO_duringCLFinj_y1 = LO_duringCLFinj(:,2);


%% calibration
CLF_cal = 0.68; %before CM gain 5dB
%CLF_cal_aftergain = 1.2;
%measured after common mode board gain
%for -13dBm, new diode 1.71V pp at 0.04 refl, -6dBm 6MHz (1MOhms)
%1.77 is factor of 5dM at the input
%LO_cal = 3.32/2; %V/2rad before CM gain
LO_cal = .200/2; %V/2rad before CM gain

%LO_cal_aftergain = (1.99*3.32)/2; %CM gain of 6dB
OPO_cal = (1.2*0.14)/FWHM; %Vpp/FWHM * CM gain(-17dB)



%% Shotnoise, Darknoise, combined noise
% or google shotnoise on photodetector
% shotnoise (ASD) = sqrt(2*e*I)
%dLsn = 2310*sqrt(2*1.6e-19*0.6e-3)*5*sqrt(2);
%DN = 2310*sqrt(2*1.6e-19*0.29e-3)*5*sqrt(2);
%DNSN = sqrt(dLsn^2+DN^2);


%% DARM whitening
%DARM_whitening = zpk(-2.*pi.*[0.3 0.3 0.3 0.3 0.3 0.3  ], -2*pi.*[30 30 30 30 30 30 ], 1);
DARM_antiwhitening = zpk(2 * pi * [30,30,30,30,30,30], 2 * pi * [0.3,0.3,0.3,0.3,0.3,0.3], 1/(100^6) );

[DARMamp, DARMphase] = bode(DARM_antiwhitening, 2*pi*OPO_inj_y1); 
DARMamp = squeeze(DARMamp);
DARMphase = squeeze(DARMphase);
DARMfilter = DARMamp.*exp(i.*deg2rad(DARMphase));
DARMmagzpk = abs(DARMfilter);
DARMphasezpk = angle(DARMfilter);
%% CLF TF model

%CLF_CMgain = 1.995; %6dB
CLF_CMgain_before = 3.98; %12dB
CLF_CMgain_after = 1.26; %2dB
CLF_fastgain = 1; %0dB


%IQdemod_cal = QMON_cal_before; %Vpp/rad

VCXOV2Hz = 925; %Hz/V



%% LO TF model
%CLF_CMgain = 2.2387; %7dB
 

CM_comp = zpk(-2.*pi.*[4e3  ], -2*pi.*[40 ], 1);
CM_boost1 = zpk(-2.*pi.*[10  ], -2*pi.*[0 ], 1);
CM_boost2 = zpk(-2.*pi.*[2e3  ], -2*pi.*[20 ], 1);
SLOW_boost = zpk(-2.*pi.*[400  ], -2*pi.*[4 ], 1);
SLOW_filters = zpk(-2.*pi.*[  ], -2*pi.*[100e3 ], 100e3*2*pi);
PZTdriver = zpk(-2.*pi.*[  10 ],...
            -2*pi.*[ 1 400], (1/10)*400*2*pi*20); %PZTdriver gain = 20 
%PFD = zpk(-2.*pi*[],-2.*pi*[0], 1.6*2.*pi); %1.6V/rad, 32deg/V, has to be measured in the lab
PZT = zpk([],[], 10.7e6); %OPO PZT calibration Hz/V for red, 21.4MHz/V for green
%PZT = zpk([],[], 21e6); %OPO PZT calibration Hz/V for red, 21.4MHz/V for green

PM = zpk(-2*pi*[60],-2*pi*[10], (1/60)*10*.0084); 
%flat = zpk([],[], 3.54*72e3/10); %flat from fast path. 11dB fast gain, 1/10 from AO
%flat = zpk([],[], 20.9e3); %Hz/V calibration fast path, use OPO refl Vpp/Hz
flat = zpk([],[], 20.9e3/2); %Hz/V calibration fast path, use OPO refl Vpp/Hz

oneonf = zpk([],-2*pi*[0], 2*pi ); %phase detection frequency actuation
laserPZTpole = zpk([],-2*pi*[0], 2*pi );
%randompole = zpk([500],-2*pi*[], 1/(2*pi*200) );

VCO = zpk(-2*pi*[ 40],-2*pi*[1.6], 281479.*(1/40).*1.6); %Hz/V 80MHz VCO calibration at DC

%% 
LO_CMgain = 1.99; %6dB
LO_fastgain = 1; %0dB

common_path = LO_CMgain.*CM_boost1.*CM_boost2;
%slow_path = SLOW_boost.*SLOW_filters.*PM.*PZTdriver.*PZT;
fast_path = LO_fastgain.*VCO; 

%gain = 1./(0.233.*FSR); %LO rad per Hz


%LO_IMON_afterboosts = PM*PZTdriver;
%LO_IMON_beforeboosts = flat;
fudgefactor = 1;
LO_TF = common_path.* ( (fast_path) ).*oneonf;
LO_TF_ctrl = VCO.*oneonf; %Hz/V * 1/Hz = 1/V

LOTF_ampx_lf = logspace(0,6,1e5);


[LOctrlamp, LOctrlphase] = bode(LO_TF_ctrl, 2*pi*LO_duringCLFinj_x); 
LOctrlamp = squeeze(LOctrlamp);
LOctrlphase = squeeze(LOctrlphase);
LOctrlfilter = LOctrlamp.*exp(i.*deg2rad(LOctrlphase));
LOctrlmagzpk = abs(LOctrlfilter);
LOctrlphasezpk = angle(LOctrlfilter);

[LOamp, LOphase] = bode(LO_TF, 2*pi*LO_duringCLFinj_x); 
LOamp = squeeze(LOamp);
LOphase = squeeze(LOphase);
LOfilter = LOamp.*exp(i.*deg2rad(LOphase));
LOOLmagzpk = abs(LOfilter);
LOOLphasezpk = angle(LOfilter);

LOCLgain = abs(1./(1+LOOLmagzpk));

% [LOamp, LOphase] = bode(LO_TF, 2*pi*LO_IMON_x); 
% LOamp = squeeze(LOamp);
% LOphase = squeeze(LOphase);
% LOfilter = LOamp.*exp(i.*deg2rad(LOphase));
% LOOLmagzpk = abs(LOfilter);
% LOOLphasezpk = angle(LOfilter);
% 
% LOCLgain = abs(1./(1+LOOLmagzpk));


%% TTFSS TF model
%commongain = 2.8/2; %9dB /2 bc of the chip (100Ohms input)
%commongain = 5/2; %13.9dB /2 bc of the chip (100Ohms input)
commongain = 5.62/2; %15dB /2 bc of the chip (100Ohms input)

PZTcalib = 1.7e6; %laser PZT Hz/V, red
%PZTcalib = 2*1.7e6; %laser PZT Hz/V, green

%PZTcalib = 11.8967e6; %laser PZT Hz/V, green, calibrated Jan 11
%PZTcalib = 0.202e6; %laser PZT Hz/V, green, calibrated Jan 11

FSScom = zpk(-2*pi*[1.6e6  ],-2*pi*[ 75.8e3 ], 0.952);
commboost = zpk(-2*pi*[ 480 ],-2*pi*[ 0 ], 1); 

temp = zpk(-2*pi*[ ],-2*pi*[40 ], 1*40*2*pi*0.2);
%fast path
%When EOM not on
%23e3*2*pi fudge factor to make low f gain correct
fastonlyboost1 = zpk(-2*pi*[300 ],-2*pi*[30 23e3], 4.5e5); 
fastonlyboost2 = zpk(-2*pi*[500 ],-2*pi*[5], 0.99);
fastboost2 = zpk(-2*pi*[ 150],-2*pi*[0 ], 1); %calculated gain with C was 3.16e7

fastonly = fastonlyboost1*fastonlyboost2*fastboost2;

fastinterface = zpk(-2*pi*[100 ],-2*pi*[10 ], 1);
fastinterfacepole = zpk(-2*pi*[23e3],-2*pi*[100], (1/23e3)*100); 
PZTpole = zpk(-2*pi*[ ],-2*pi*[100e3], 1*100e3*2*pi); 

%EOM boosts
EOMboost1 = zpk(-2*pi*[0],-2*pi*[1.6e3 1.7e3], 1*3e5); 
EOMboost2 = zpk(-2*pi*[0 32e3],-2*pi*[1.2e3 1e3], 1*0.9667);
%EOMboost3 = zpk(-2*pi*[3.2e3],-2*pi*[0], 20);
EOMboost3 = zpk(-2*pi*[0],-2*pi*[3.2e3], 20);

%When EOM on
fastgain = 15/2; %22dB
%fastgain = 12.6/2; %22dB
%fastgain = 20/2; %26dB

%fastgain = 15.8/2; %22dB

%fastgain = 1*0.5; % /2 at the input
fastboost1 = zpk(-2*pi*[ ],-2*pi*[2.3e3 ], 9e5);  
fastLP = zpk(-2*pi*[ ],-2*pi*[130e3], 5.128e6);
%fast = fastboost1*fastLP*fastboost2*notch130k;

%fastinterface is the same
EOM = EOMboost1*EOMboost2*EOMboost3;
EOM_par = zpk(-2*pi*[0 340e3 ],-2*pi*[50e3 760e3], 8); 
EOMinterface = zpk(-2*pi*[0 3e6 ],-2*pi*[240 33e3], 0.33);
EOMinterfacepole = zpk(-2*pi*[ ],-2*pi*[2.8e6], 1*2.8e6*2*pi); 
EOMcalib = 15e-3; %15mrad/V for red
%EOMcalib = 30e-3; %15mrad/V for red 30mrad for green

phasetofreq = zpk(-2*pi*[0 ],-2*pi*[], 1/(2*pi)); %have to multiply EOMcalib rad/V 
%with freq to get Hz/V that agrees with PZT actuator Unit and cancels out
%PD detector V/Hz

%% notches

% what's in the [] is already including 2pi (w)
notch130k = zpk(1e5*[ -0.351063829787234+(1j*8.290539855508047) -0.351063829787234-(1j*8.290539855508047)],...
            1e5*[ -5.659574468085103+(1j*6.068402899326267) -5.659574468085103-(1j*6.068402899326267)], 1);
notch210k = zpk(1e6*[ -0.035106382978723+(1j*1.377847448198228) -0.035106382978723-(1j*1.377847448198228)],...
            1e6*[ -0.565957446808511+(1j*1.256737131238753) -0.565957446808511-(1j*1.256737131238753)], 1);
notch280k = zpk(1e6*[ -0.075000000000000+(1j*1.753864047927675) -0.075000000000000-(1j*1.753864047927675)],...
            1e6*[ -1.209090909090909+(1j*1.272699207262647) -1.209090909090909-(1j*1.272699207262647)], 1);
%notch73k = zpk(1e5*[ 0+(1j*4.612656040144425) 0-(1j*4.612656040144425)],...
%            1e5*[ -2.648936170212766+(1j*3.776206153114442) -2.648936170212766-(1j*3.776206153114442)], 1);
%5 Ohm fudge factor
notch73k = zpk(1e5*[ -0.212765957446809+(1j*4.588245809226557) -0.212765957446809-(1j*4.588245809226557)],...
            1e5*[ -2.861702127659575+(1j*3.592760761737690) -2.861702127659575-(1j*3.592760761737690)], 1);
%1 Ohm fudge factor
%notch73k = zpk(1e5*[ -0.010638297872340+(1j*4.593164027677620) -0.010638297872340-(1j*4.593164027677620)],...
%            1e5*[ -5.681818181818181+(1j*3.576084503804999) -5.681818181818181-(1j*3.576084503804999)], 1*1.5);

IQdmod_diffin = zpk(-2*pi*[ ],-2*pi*[1.6e6 723e3], 4.5e15);       
        
fast = fastboost1*fastLP*fastboost2*notch73k*notch130k*notch210k*notch280k;

%% CLF VCO TF
VCO = zpk(-2*pi*[ 40],-2*pi*[1], (1/40).*869.5111); %Hz/V 615Hz/V at DC

%% making transfer function

FSSTF_ampx_lf = logspace(0,7,1e5);

P = (1.84./FWHM); %optical gain (plant, input), PDH Vpk


FASTPATH = PZTcalib.*fastgain.*fast.*0.5.*fastinterface.*fastinterfacepole*PZTpole;
EOMPATH = ((EOM+EOM_par).*EOMinterface.*EOMinterfacepole.*EOMcalib.*phasetofreq);
COMMONPATH = FSScom.*commongain.*commboost;

% All that's needed for the control signal 
filter_FSS_EOM = EOMinterface.*EOMinterfacepole.*EOMcalib.*phasetofreq; %Hz/V
filter_FSS_FAST = 0.5.*fastinterface.*fastinterfacepole.*PZTpole.*PZTcalib; %Hz/V factor of 1/2 on interface board

[TSEOMamp, TSEOMphase] = bode(filter_FSS_EOM, 2*pi*FSSTF_ampx_lf);
TSEOMamp = squeeze(TSEOMamp);
TSEOMphase = squeeze(TSEOMphase);
TSEOMfilter = TSEOMamp.*exp(i.*deg2rad(TSEOMphase));
TSEOMOLmagzpk = abs(TSEOMfilter);
TSEOMOLphasezpk = angle(TSEOMfilter);

[TSFASTamp, TSFASTphase] = bode(filter_FSS_FAST, 2*pi*FSSTF_ampx_lf);
TSFASTamp = squeeze(TSFASTamp);
TSFASTphase = squeeze(TSFASTphase);
TSFASTfilter = TSFASTamp.*exp(i.*deg2rad(TSFASTphase));
TSFASTOLmagzpk = abs(TSFASTfilter);
TSFASTOLphasezpk = angle(TSFASTfilter);

[VCOamp, VCOphase] = bode(VCO, 2*pi*FSSTF_ampx_lf);
VCOamp = squeeze(VCOamp);
VCOphase = squeeze(VCOphase);
VCOfilter = VCOamp.*exp(i.*deg2rad(VCOphase));
VCOOLmagzpk = abs(VCOfilter);
VCOOLphasezpk = angle(VCOfilter);

%% plot TTFSS FAST TF
FSSTF_ampx_lf = logspace(0,7,1e5);
% 
% figure(27);
% subplot(2,1,1);
% semilogx(CLF_inj_x, 20*log10(LOOLmagzpk),'LineWidth', 1.5);
% xlabel('Frequency, Hz'), ylabel('Gain, dB');
% %xlim([1e2,1e6])
% %legend('PD amp board (PDOLmagzpk)')
% title('LO TF');
% grid on;
% subplot(2,1,2);
% semilogx(CLF_inj_x, rad2deg(LOOLphasezpk), 'LineWidth', 1.5);
% xlabel('Frequency, Hz'), ylabel('Phase, deg.');
% grid on;


%% Calculate ambient 

LO_ambient = LO_inj_y1.* ( sqrt(LO_inj_y4.^2-LO_inj_y3.^2)./ sqrt(LO_inj_y2.^2-LO_inj_y1.^2) );
CLF_ambient = ref_new_CLF.* ( sqrt(CLF_inj_DARM_nosat.^2-CLF_inj_y3.^2)./sqrt(CLF_inj_y2_nosat.^2-ref_new_CLF.^2) );
OPO_ambient = OPO_inj_y1.* ( sqrt(OPO_inj_y4.^2-OPO_inj_y3.^2)./ sqrt(OPO_inj_y2.^2 - OPO_inj_y1.^2) );

% cut out points where witness is not a factor of 1.7 above reference, this is
% where we weren't really injecting much and can't make a projection
witness_cut_thresh = 1.5;
LO_ambient_cut = find( (LO_inj_y2 ./ LO_inj_y1) < witness_cut_thresh );
CLF_ambient_cut = find( (CLF_inj_y2_nosat ./ ref_new_CLF) < witness_cut_thresh );
OPO_ambient_cut = find( (OPO_inj_y2 ./ OPO_inj_y1) < witness_cut_thresh );

% find points where the projection is really an upper limit
darm_cut_thresh = 1.7;
darm_cut_LO = find( (LO_inj_y4 ./ LO_inj_y3) < darm_cut_thresh );
darm_cut_CLF = find( (CLF_inj_DARM_nosat ./ CLF_inj_y3) < darm_cut_thresh );
darm_cut_OPO = find( (OPO_inj_y4 ./ OPO_inj_y3) < darm_cut_thresh );

% now do a cut
%NB.data.(noise_name).projection_calibrated([wit_cut darm_cut]) = 0;
LO_ambient([LO_ambient_cut; darm_cut_LO]) = 0;
CLF_ambient([CLF_ambient_cut; darm_cut_CLF]) = 0;
OPO_ambient([OPO_ambient_cut; darm_cut_OPO]) = 0;



figure(121)
set(gcf,'color',[0.5, 0.5, 0.5]) 
fig = gcf;
fig.InvertHardcopy = 'off';

loglog(LO_inj_x, LO_inj_y3.*DARMmagzpk, 'LineWidth', 1.5, 'Color', [1, 0, 0]);
xlabel('Frequency, Hz'), ylabel('m/\surd Hz');
grid on;
hold on
loglog(LO_inj_x, LO_ambient.*DARMmagzpk, '.',  'LineWidth', 1, 'Color', [0.4940, 0.1840, 0.5560]	);
loglog(CLF_inj_x, CLF_ambient.*DARMmagzpk, '.',  'LineWidth', 1, 'Color', [0.8500, 0.3250, 0.0980]	);
loglog(OPO_inj_x, OPO_ambient.*DARMmagzpk, '.',  'LineWidth', 1, 'Color', [0, 0.4470, 0.7410]	);


set(gca,'FontSize',18, 'Color', 'k');
hTitle = title('SQZ Noise Projection');
%xlim([10,1e5])
%ylim([1e-5,1e-1])
ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} DARM', '\color{white} LO', '\color{white} CLF', '\color{white} OPO'},'FontSize',14, 'Location', 'Northeast')
hold off



%% plot stuff

figure(118)
subplot(2,1,1);

set(gcf,'color',[0.5, 0.5, 0.5]) 
fig = gcf;
fig.InvertHardcopy = 'off';

loglog(OPO_inj_x, OPO_inj_y1./(OPO_cal.*OPO_inj_x),  'LineWidth', 1.5, 'Color', [0.3010, 0.7450, 0.9330]	);
xlabel('Frequency, Hz'), ylabel('rad/\surd Hz');
grid on;
hold on
loglog(OPO_inj_x, OPO_inj_y2./(OPO_cal.*OPO_inj_x), 'LineWidth', 1.5, 'Color', [0, 0.4470, 0.7410]);

set(gca,'FontSize',18, 'Color', 'k');
hTitle = title('OPO to DARM Coupling (March 27, 2019)');
%xlim([10,1e5])
ylim([1e-4,1e2])
ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} OPO error signal (Reference)', '\color{white} OPO error signal (Excited)'},'FontSize',14, 'Location', 'Northeast')
hold off

subplot(2,1,2);;
p1 = loglog(OPO_inj_x, OPO_inj_y3.*DARMmagzpk, 'LineWidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250]);
p1.Color(4) = 0.5
xlabel('Frequency, Hz'), ylabel('m/\surd Hz');
grid on;
hold on
loglog(OPO_inj_x, OPO_inj_y4.*DARMmagzpk, 'LineWidth', 1.5, 'Color', [0.6350, 0.0780, 0.1840]);

set(gca,'FontSize',18, 'Color', 'k');
%hTitle = title('LHO SQZ Noise Budget (Jan 23)');
%xlim([10,1e5])
ylim([1e-18,1e-15])
ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} DARM (Reference)', '\color{white} DARM (Excited)'},'FontSize',14, 'Location', 'Northeast')
hold off


%%

figure(119)
subplot(3,1,1);

set(gcf,'color',[0.5, 0.5, 0.5]) 
fig = gcf;
fig.InvertHardcopy = 'off';

loglog(CLF_inj_x, 2.*ref_new_CLF./CLF_cal, 'LineWidth', 1.5, 'Color', [0.75, 0.75, 0]);
xlabel('Frequency, Hz'), ylabel('rad/\surd Hz');
grid on;
hold on
loglog(CLF_inj_x, 2.*CLF_inj_y2_nosat./CLF_cal, 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]);
loglog(LO_duringCLFinj_x, LO_duringCLFinj_y1.*LOctrlmagzpk, '--',  'LineWidth', 1.5, 'Color', [0.4940, 0.1840, 0.5560]	);
%loglog(LO_duringCLFinj_x, LO_duringCLFinj_y1./(LOOLmagzpk/1+LOOLmagzpk), '--',  'LineWidth', 1.5, 'Color', [0.4940, 0.1840, 0.5560]	);


set(gca,'FontSize',18, 'Color', 'k');
hTitle = title('CLF to DARM Coupling (March 27, 2019)');
xlim([10,1e4])
ylim([1e-7,1e0])
ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} CLF error signal (Reference)', '\color{white} CLF error signal (Excited)', '\color{white} LO ctrl during CLF exc'},'FontSize',14, 'Location', 'Northeast')
hold off

subplot(3,1,2);
loglog(CLF_inj_x, CLF_inj_y3.*DARMmagzpk,'LineWidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250]);
xlabel('Frequency, Hz'), ylabel('m/\surd Hz');
grid on;
hold on
loglog(CLF_inj_x, CLF_inj_DARM_nosat.*DARMmagzpk, 'LineWidth', 1.5, 'Color', [0.6350, 0.0780, 0.1840]);

set(gca,'FontSize',18, 'Color', 'k');
%hTitle = title('LHO SQZ Noise Budget (Jan 23)');
%xlim([10,1e5])
ylim([1e-18,1e-15])

ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} DARM (Reference)', '\color{white} DARM (Excited)'},'FontSize',14, 'Location', 'Northeast')
hold off

% 
CLF_inj_DARM_nosat([darm_cut_CLF]) = 0;

subplot(3,1,3);
loglog(CLF_inj_x, (CLF_inj_DARM_nosat.*DARMmagzpk)./(2.*CLF_inj_y2_nosat./CLF_cal), '.', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]);
%loglog(CLF_inj_x, (CLF_inj_y4.*DARMmagzpk)./(LO_duringCLFinj_y1/LO_cal), '.', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]);

xlabel('Frequency, Hz'), ylabel('m/rad');
grid on;
hold on
%loglog(CLF_inj_x, (LO_duringCLFinj_y1/LO_cal)./(CLF_inj_y4./DARMmagzpk), '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]);

set(gca,'FontSize',18, 'Color', 'k');
ylim([5e-16,1e-10])
ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} DARM/CLF\_err ASD ratio'},'FontSize',14, 'Location', 'Northeast')
hold off

%%


figure(120)
subplot(3,1,1);

set(gcf,'color',[0.5, 0.5, 0.5]) 
fig = gcf;
fig.InvertHardcopy = 'off';

loglog(LO_inj_x, LO_inj_y1/LO_cal, 'LineWidth', 1.5, 'Color', [0.75, 0, 0.75]);
xlabel('Frequency, Hz'), ylabel('rad/\surd Hz');
grid on;
hold on
loglog(LO_inj_x, LO_inj_y2/LO_cal, 'LineWidth', 1.5, 'Color', [0.4940, 0.1840, 0.5560]);
%loglog(LO_inj_x, LO_inj_y1./LOCLgain, 'LineWidth', 1.5, 'Color', [0.75, 0, 0.75]);



set(gca,'FontSize',18, 'Color', 'k');
hTitle = title('LO to DARM Coupling (March 29, 2019)');
xlim([10,1e4])
ylim([1e-7,1e0])
ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} LO error signal (Reference)', '\color{white} LO error signal (Excited)', '\color{white} LO during CLF exc'},'FontSize',14, 'Location', 'Northeast')
hold off

subplot(3,1,2);
loglog(LO_inj_x, LO_inj_y3.*DARMmagzpk, 'LineWidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250]);
xlabel('Frequency, Hz'), ylabel('m/\surd Hz');
grid on;
hold on
loglog(LO_inj_x, LO_inj_y4.*DARMmagzpk, 'LineWidth', 1.5, 'Color', [0.6350, 0.0780, 0.1840]);

set(gca,'FontSize',18, 'Color', 'k');
%hTitle = title('LHO SQZ Noise Budget (Jan 23)');
%xlim([10,1e5])
ylim([1e-18,1e-15])

ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} DARM (Reference)', '\color{white} DARM (Excited)'},'FontSize',14, 'Location', 'Northeast')
hold off

% 

%LO_inj_y4([LO_ambient_cut darm_cut_LO]) = 0;
LO_inj_y4([darm_cut_LO]) = 0;

subplot(3,1,3);
loglog(LO_inj_x, (LO_inj_y4.*DARMmagzpk)./(LO_inj_y2./LO_cal), '.', 'LineWidth', 1.5, 'Color', [0.4940, 0.1840, 0.5560]);
xlabel('Frequency, Hz'), ylabel('m/rad');
grid on;
hold on
%loglog(CLF_inj_x, (LO_duringCLFinj_y1/LO_cal)./(CLF_inj_y4./DARMmagzpk), '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]);

set(gca,'FontSize',18, 'Color', 'k');
ylim([5e-16,1e-10])
ax = gca
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} DARM/LO\_err ASD ratio'},'FontSize',14, 'Location', 'Northeast')
hold off





