%% CLF diode swap

%% 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 spec data

CLF_QMON_before = load('CLF_QMON_beforediodeswap.txt');
CLF_QMON_before_x = CLF_QMON_before(:,1);
CLF_QMON_before_y = CLF_QMON_before(:,2);

CLF_QMON_after = load('CLF_QMON_afterdiodeswap.txt');
CLF_QMON_after_x = CLF_QMON_after(:,1);
CLF_QMON_after_y = CLF_QMON_after(:,2);


CLF_QMON_DNafter = load('SCRN0023.txt');
CLF_QMON_DNafter_x = CLF_QMON_DNafter(:,1);
CLF_QMON_DNafter_y = CLF_QMON_DNafter(:,2);


CLF_QMON_SNafter = load('newCLF_SN_March4.txt'); %3.72V read out at REFL_DC
CLF_QMON_SNafter_x = CLF_QMON_SNafter(:,1);
CLF_QMON_SNafter_y = CLF_QMON_SNafter(:,2);

CLFTF_before_amp = load('SCRN0011.TXT'); %CLF actually
CLFTF_before_phase = load('SCRN0012.TXT');
CLFTF_before_ampx = CLFTF_before_amp(:,1); %freq
CLFTF_before_ampy = CLFTF_before_amp(:,2); %amp
CLFTF_before_phy = CLFTF_before_phase(:,2); %phase

CLFTF_after_amp = load('SCRN0016.TXT');
CLFTF_after_phase = load('SCRN0017.TXT');
CLFTF_after_ampx = CLFTF_after_amp(:,1); %freq
CLFTF_after_ampy = CLFTF_after_amp(:,2); %amp
CLFTF_after_phy = CLFTF_after_phase(:,2); %phase


%% CLF shit
QMON_cal_before = 640e-3; %Vpp
QMON_cal_after = 1.71e-3; %Vpp
%gain = 1.2e6; %transimpedance at 6MHz?

CLF_QMON_DN_before = 9.147e-6; %got from GIF file. 
CLF_QMON_DN_after = 0.007069e-3;

%oldDN = 13.97e-6;
%newDN = 4.1e-6;
%newDNSN = 5*4.1e-6;
%newSN = sqrt(newDNSN^2-newDN^2);
%newSNDN = sqrt(CLF_QMON_DN_after^2+3.724^2);
newDNSN = 0.037e-3; %SNDNcombined lightbulb measurement
newSN = sqrt(newDNSN^2-CLF_QMON_DN_after^2);
%newSN = sqrt((newDNSN/11.3)^2-CLF_QMON_DN_after^2);

CLFREFLDC = 3.724; %lightbulb mea, V

newSNmodel = sqrt(2*1.6e-19*CLFREFLDC/10e3)*3.3091e+06; %this is used to figure out RF transimpedance
%to get the actual transimpedance in RF you need to...
%/(25(RF amp)*sqrt(2)(SN folded in IQdemod)*5(IQdemod board gain)*10(diode gain))

newRFgain = 3.3091e6;
oldRFgain = 1.2e6;
%(25(RF amp)*sqrt(2)(SN folded in IQdemod)*5(IQdemod board gain)*10(diode gain))
othergainfactor = 25*sqrt(2)*5*10;
newSNlim = sqrt(2*1.6e-19*0.14/10e3)*newRFgain; %0.16/10e3 is darknoise limited snotnoise V
oldSNlim = sqrt(2*1.6e-19*1.8/10e3)*oldRFgain;

newSNcal = sqrt(2*1.6e-19*0.329/10e3)*newRFgain;

%newSNcal_DNsub = sqrt(newSNcal^2-CLF_QMON_DN_after^2);
Sens_after = sqrt(newSNcal.^2+CLF_QMON_DN_after.^2);


%%%
% DN = 8.9e-6;
% 
% %OP = sqrt(2*1.6e-19*3*0.0058e-3)*gain; %factor of 3 in sqrt comes from wrong transimpedance at DC (was 30000, now 10000)
% DNSN = 1.024e-5;
% SN = sqrt(DNSN^2-DN^2);
% gain = 1.2e6; 
% %to get the actual transimpedance in RF you need to...
% %/(25(RF amp)*sqrt(2)(SN folded in IQdemod)*5(IQdemod board gain)*10(diode gain))
% 
% SN1 = sqrt(2*1.6e-19*3*54e-6)*gain; %.54V, 10e3 Ohms
% SNDN = sqrt(2*1.6e-19*3*0.0186e-3)*gain; 
%%%


%SN_after = sqrt(2*1.6e-19*(0.329e-3/gain_new))*gain_new; 
%Sens_after = sqrt(SN_after^2+CLF_QMON_DN_after^2); 



VCXOV2Hz = 925; %Hz/V

%% CLF TF model for 10uW transmission

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

QMON_cal_before = 640e-3; %Vpp
QMON_cal_after = 1.71; 

IQdemod_cal = QMON_cal_before; %Vpp/rad

VCXOV2Hz = 925; %Hz/V

% Generic common mode board components
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*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.6e6); %PZT calibration Hz/V
PM = zpk(-2*pi*[60],-2*pi*[10], (1/60)*10*.00136); 
flat = zpk([],[], VCXOV2Hz); %Hz/V
oneonf = zpk([],-2*pi*[0], 2*pi ); %phase detection frequency actuation
laserPZTpole = zpk([],-2*pi*[0], 2*pi );
mysteriouspole = zpk([],-2*pi*[5e3 30e3], (2*pi)^2*5e3*20e3 ); %prob VCXO

common_path = CM_boost1;
fast_path = flat*oneonf*mysteriouspole; 

%%
CLF_TF = common_path.*fast_path;

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

[CLFbeforeamp, CLFbeforephase] = bode(CLF_CMgain_before.*QMON_cal_before*CLF_TF, 2*pi*CLF_QMON_before_x); 
CLFbeforeamp = squeeze(CLFbeforeamp);
CLFbeforephase = squeeze(CLFbeforephase);
CLFbeforefilter = CLFbeforeamp.*exp(i.*deg2rad(CLFbeforephase));
CLFOLbeforemagzpk = abs(CLFbeforefilter);
CLFOLbeforephasezpk = angle(CLFbeforefilter);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CLFbeforeCLgain = abs(1./(1+CLFOLbeforemagzpk));

%%

[CLFafteramp, CLFafterphase] = bode(CLF_CMgain_after.*QMON_cal_after*CLF_TF, 2*pi*CLF_QMON_after_x); 
CLFafteramp = squeeze(CLFafteramp);
CLFafterphase = squeeze(CLFafterphase);
CLFafterfilter = CLFafteramp.*exp(i.*deg2rad(CLFafterphase));
CLFOLaftermagzpk = abs(CLFafterfilter);
CLFOLafterphasezpk = angle(CLFafterfilter);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CLFafterCLgain = abs(1./(1+CLFOLaftermagzpk));



%%
CLFbeforeTFamp = 10.^(CLFTF_before_ampy/20); %dB to power ratio
CLFbeforecmplx = CLFbeforeTFamp.*exp(i.*deg2rad(CLFTF_before_phy)); 
CLFOLbeforemag = abs(CLFbeforecmplx);
CLFOLbeforephase = angle(CLFbeforecmplx);

CLFafterTFamp = 10.^(CLFTF_after_ampy/20); %dB to power ratio
CLFaftercmplx = CLFafterTFamp.*exp(i.*deg2rad(CLFTF_after_phy)); 
CLFOLaftermag = abs(CLFaftercmplx);
CLFOLafterphase = angle(CLFaftercmplx);

%% OMC sensing noise
OMCDN = 1.45e-6; %V
OMCSN = 1.75e-6;

OMCSens = sqrt(OMCSN^2+OMCDN^2);

%% 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);

%% LO TF model
%CLF_CMgain = 2.2387; %7dB
 
LO1Xcal = 45.2e-3./2; %V/rad
%LO10Xcal = 121.6e-3./2; %V/rad
LO10Xcal = 2./2; %V/rad This fits the model

LO04Xcal = 172e-3; %note says 218e-3
%LOcal = 280e-3./2; %V/rad

%LOcal = 2./2; %V/rad


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.12; %1dB
% 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 = LO10Xcal.*common_path.* ( (fast_path) ).*oneonf;
% 
% %LOTF_ampx_lf = logspace(0,6,1e5);
% 
% 
% [LOamp, LOphase] = bode(LO_TF, 2*pi*LO_IMON_10XCLFpower_xall); 
% LOamp = squeeze(LOamp);
% LOphase = squeeze(LOphase);
% LOfilter = LOamp.*exp(i.*deg2rad(LOphase));
% LOOLmagzpk = abs(LOfilter);
% LOOLphasezpk = angle(LOfilter);
% 
% LOCLgain = abs(1./(1+LOOLmagzpk));

%% making transfer function

% TF_ampx_lf = logspace(0,7,1e5);
% 
% [VCOamp, VCOphase] = bode(VCO, 2*pi*LO_IMON_10XCLFpower_x);
% VCOamp = squeeze(VCOamp);
% VCOphase = squeeze(VCOphase);
% VCOfilter = VCOamp.*exp(i.*deg2rad(VCOphase));
% VCOOLmagzpk = abs(VCOfilter);
% VCOOLphasezpk = angle(VCOfilter);

%% plot LOTF
% FSSTF_ampx_lf = logspace(0,7,1e5);
% 
% figure(27);
% subplot(2,1,1);
% semilogx(LOTF_10XCLFpower_ampx, 20*log10(LOOLmag),...
%     LO_IMON_10XCLFpower_xall, 20*log10(LOOLmagzpk), '--', 'LineWidth', 1.5, 'Color', [0.4940, 0.1840, 0.5560]);
% xlabel('Frequency, Hz'), ylabel('Gain, dB');
% xlim([1e2,1e5])
% legend('Measured', 'Calculated')
% title('LO OMC TF 10X CLF power Feb 5, 2019');
% grid on;
% subplot(2,1,2);
% semilogx(LOTF_10XCLFpower_ampx, rad2deg(LOOLphase),...
%     LO_IMON_10XCLFpower_xall, rad2deg(LOOLphasezpk),'--', 'LineWidth', 1.5, 'Color', [0.4940, 0.1840, 0.5560]);
% xlabel('Frequency, Hz'), ylabel('Phase, deg.');
% xlim([1e2,1e5])
% grid on;

%% plot CLF TF

figure(32);
subplot(2,1,1);
semilogx(CLFTF_before_ampx, 20*log10(CLFOLbeforemag),...
    CLF_QMON_before_x, 20*log10(CLFOLbeforemagzpk), '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]	);
xlabel('Frequency, Hz'), ylabel('Gain, dB');
xlim([1e2,1e5])
legend('Measured', 'Calculated')
title('CLF TF before diode swap');
grid on;
subplot(2,1,2);
semilogx(CLFTF_before_ampx, rad2deg(CLFOLbeforephase),...
    CLF_QMON_before_x, rad2deg(CLFOLbeforephasezpk),'--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]);
xlabel('Frequency, Hz'), ylabel('Phase, deg.');
xlim([1e2,1e5])
grid on;

%%

figure(33);
subplot(2,1,1);
semilogx(CLFTF_after_ampx, 20*log10(CLFOLaftermag),...
    CLF_QMON_after_x, 20*log10(CLFOLaftermagzpk), '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]	);
xlabel('Frequency, Hz'), ylabel('Gain, dB');
xlim([1e2,1e5])
legend('Measured', 'Calculated')
title('CLF TF after diode swap');
grid on;
subplot(2,1,2);
semilogx(CLFTF_after_ampx, rad2deg(CLFOLafterphase),...
    CLF_QMON_after_x, rad2deg(CLFOLafterphasezpk),'--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]);
xlabel('Frequency, Hz'), ylabel('Phase, deg.');
xlim([1e2,1e5])
grid on;

%% calibration
%LOcal = 551e-3./2; %V/rad (sqz calibration)


%% RMS

% %LO 1X rms
% flipdiff2 = flip(diff(LO_IMON_1XCLFpower_x));
% flipPSD2 = flip((1e3.*LO_IMON_1XCLFpower_y./LOcal).^2);
% meansquare2 = cumsum(flipPSD2(1:end-1).*flipdiff2);
% LO_IMON_1Xpower_rms = flip(sqrt(meansquare2));
% 
% %LO sensing noise 04X rms
% flipdiff4 = flip(diff(LO_IMON_10XCLFpower_xall));
% flipPSD4 = flip((abs(LOOLmagzpk./(1+LOOLmagzpk)).*1e3.*ones(size(LO_IMON_10XCLFpower_xall)).*OMCSens./LO10Xcal).^2);
% meansquare4 = cumsum(flipPSD4(1:end-1).*flipdiff4);
% LO_sensingnoise_rms = flip(sqrt(meansquare4));
% 
% %CLF QMON lowpower rms
% flipdiff3 = flip(diff(CLF_QMON_10Xpower_xall));
% flipPSD3 = flip((CLFCLgain.*1e3.*CLF_QMON_10Xpower_yall.*VCXOV2Hz./CLF_QMON_10Xpower_xall).^2);
% meansquare3 = cumsum(flipPSD3(1:end-1).*flipdiff3);
% CLF_QMON_10Xpower_rms = flip(sqrt(meansquare3));
% 
% %CLF sensing noise rms
% flipdiff5 = flip(diff(LO_IMON_10XCLFpower_xall));
% flipPSD5 = flip((abs(CLFOLmagzpk./(1+CLFOLmagzpk)).*1e3.*ones(size(CLF_QMON_10Xpower_xall)).*Sens_before/(QMON_cal_after)).^2);
% meansquare5 = cumsum(flipPSD5(1:end-1).*flipdiff5);
% CLF_sensingnoise_rms = flip(sqrt(meansquare5));
% 


%% plot LO, CLF

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

loglog(CLF_QMON_before_x, 1e3.*CLF_QMON_before_y./QMON_cal_before./CLFbeforeCLgain ,'LineWidth', 2, 'Color', [0.8500, 0.3250, 0.0980]);


xlabel('Frequency, Hz'), ylabel('mrad/\surd Hz');
grid on;
hold on

%loglog(CLF_QMON_lowpower_x, 1e3.*CLF_QMON_lowpower_y/(QMON_cal_lowpower),'LineWidth', 2, 'Color',[0.8500, 0.3250, 0.0980]);
%loglog(LO_IMON_10XCLFpower_x, 1e3.*LO_IMON_10XCLFpower_y./LO10Xcal, 'LineWidth', 2, 'Color', [0.75, 0, 0.75]);
%loglog(LO_IMON_04XCLFpower_x, 1e3.*LO_IMON_04XCLFpower_y./LO04Xcal, 'LineWidth', 2, 'Color', [0.75, 0, 0.75]);

loglog(CLF_QMON_after_x, 1e3.*CLF_QMON_after_y./QMON_cal_after./CLFafterCLgain ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);

%loglog(CLF_QMON_DNafter_x, 1e3.*CLF_QMON_DNafter_y./QMON_cal_after ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);

%loglog(CLF_QMON_DNafter_x, 1e3.*CLF_QMON_DNbefore_y./QMON_cal_before./CLFafterCLgain ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);

%p10 = loglog(LO_IMON_10XCLFpower_xall, abs(LOOLmagzpk./(1+LOOLmagzpk)).*1e3.*ones(size(LO_IMON_10XCLFpower_xall)).*OMCSens./LO10Xcal, 'LineWidth', 1.5, 'Color',  [0.75, 0, 0.75]); 

loglog(CLF_QMON_before_x, 1e3.*ones(size(CLF_QMON_before_x)).*CLF_QMON_DN_before./QMON_cal_before, '--','LineWidth', 2, 'Color', [0.8500, 0.3250, 0.0980]);

loglog(CLF_QMON_after_x, 1e3.*ones(size(CLF_QMON_after_x)).*CLF_QMON_DN_after./QMON_cal_after, '--' ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);

%loglog(CLF_QMON_SNafter_x, 1e3.*ones(size(CLF_QMON_SNafter_x)).*CLF_QMON_SNafter_y./QMON_cal_after, '--' ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);

p10 = loglog(CLF_QMON_before_x, 1e3.*ones(size(CLF_QMON_before_x)).*oldSNlim./QMON_cal_before ,'LineWidth', 1.5, 'Color' ,[0.8500, 0.3250, 0.0980]);
p10.Color(4) = 0.5;

p11 = loglog(CLF_QMON_after_x, 1e3.*ones(size(CLF_QMON_after_x)).*newSNlim./QMON_cal_after ,'LineWidth', 1.5, 'Color' ,[0.9290, 0.6940, 0.1250]);
p11.Color(4) = 0.5;

p12 = loglog(CLF_QMON_after_x, 1e3.*ones(size(CLF_QMON_after_x)).*newSNcal./QMON_cal_after , ':', 'LineWidth', 1.5, 'Color' ,[1,1,0]);
%p12.Color(4) = 0.5;
%p12 = loglog(CLF_QMON_after_x, 1e3.*ones(size(CLF_QMON_after_x)).*newSNcal , 'LineWidth', 1.5, 'Color' ,[1,1,0]);
p13 = loglog(CLF_QMON_after_x, 1e3.*ones(size(CLF_QMON_after_x)).*Sens_after./QMON_cal_after , '-.', 'LineWidth', 1.5, 'Color' ,[1,1,0]);
%p14 = loglog(CLF_QMON_after_x, 1e3.*ones(size(CLF_QMON_after_x)).*newSN , 'LineWidth', 1.5, 'Color' ,[1,1,0]);

%loglog(CLF_QMON_before_x, 1e3.*ones(size(CLF_QMON_before_x)).*Sens_before ,'LineWidth', 1.5, 'Color' ,[0.8500, 0.3250, 0.0980]);
%p12 = loglog(CLF_QMON_after_x, 1e3.*ones(size(CLF_QMON_after_x)).*Sens_after ,'LineWidth', 1.5, 'Color' ,[0.9290, 0.6940, 0.1250]);

%p11.Color(4) = 0.5;

% 
% p9 = loglog(CLF_QMON_10Xpower_xall(1:end-1), CLF_QMON_10Xpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]); 
% 
% %p7 = loglog(LO_IMON_1XCLFpower_x(1:end-1), LO_IMON_04XCLFpower_rms, '--', 'LineWidth', 1.5, 'Color',  [0.75, 0, 0.75]); 
% 
% loglog(LO_IMON_10XCLFpower_xall(1:end-1), LO_sensingnoise_rms, '--', 'LineWidth', 1.5, 'Color',  [0.75, 0, 0.75]); 
% 
% p9 = loglog(CLF_QMON_10Xpower_xall(1:end-1), CLF_sensingnoise_rms, '--', 'LineWidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250]	); 
% 
% 
% total_phasenoise = sqrt(CLF_sensingnoise_rms.^2 + LO_sensingnoise_rms.^2 + CLF_QMON_10Xpower_rms.^2);
% 
% loglog(CLF_QMON_10Xpower_xall(1:end-1), total_phasenoise, ':','LineWidth', 2.5, 'Color', [1, 0, 0]); 


set(gca,'FontSize',18, 'Color', 'k');
hTitle = title('CLF Before and After diode swap (40uW CLF refl) March 1, 2019');
xlim([10,1e5])

ylim([1e-3,1e2])
ax = gca;
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} CLF QMON before swap (loop-corrected)','\color{white} CLF QMON after swap (loop corrected)',...
    '\color{white} Darknoise before swap', '\color{white} Darknoise after swap', '\color{white} old diode SN limited current (180uA)',...
    '\color{white} New diode SN limited current (14uA)', '\color{white} New diode Shot Noise', '\color{white} New diode Sensing Noise'},...
    'FontSize',14, 'Location', 'Northeast');
hold off

%% plot SN

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

loglog(CLF_QMON_SNafter_x, CLF_QMON_SNafter_y ,'LineWidth', 2);


xlabel('Frequency, Hz'), ylabel('V/\surd Hz');
grid on;
hold on
loglog(CLF_QMON_DNafter_x, CLF_QMON_DNafter_y ,'LineWidth', 2);

loglog(CLF_QMON_after_x, ones(size(CLF_QMON_after_x)).*newDNSN , 'LineWidth', 1.5);
loglog(CLF_QMON_after_x, ones(size(CLF_QMON_after_x)).*newSN ,  'LineWidth', 1.5);
loglog(CLF_QMON_after_x, ones(size(CLF_QMON_after_x)).*newSNmodel ,  'LineWidth', 1.5);
loglog(CLF_QMON_after_x, ones(size(CLF_QMON_after_x)).*newSNlim ,  'LineWidth', 1.5);
loglog(CLF_QMON_after_x, ones(size(CLF_QMON_after_x)).*CLF_QMON_DN_after ,  'LineWidth', 1.5);



set(gca,'FontSize',18, 'Color', 'k');
hTitle = title('CLF Before and After diode swap (40uW CLF refl) March 1, 2019');
xlim([10,1e5])

ylim([1e-6,1e-3])
ax = gca;
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
legend({'\color{white} CLF QMON SN after swap','\color{white} CLF QMON DN after swap',...
    '\color{white} DN-SN combined', '\color{white} SN', '\color{white} SN model (Transimpedance = Ohms)',...
    '\color{white} New SN lim', '\color{white} New CLF DN', '\color{white} New diode Sensing Noise'},...
    'FontSize',14, 'Location', 'Northeast');
hold off

