%% Calc_Shot_Noise.m
% a script to estimate the shot noise in the REFL and AS WFS

%% DC Chain
 
% laser power counts on the WFS in the DC chain on May 4 2021
REFL_A_DC = 20200 ; % cts
REFL_B_DC = 20400 ; % cts
AS_A_DC = 60500 ; % cts
AS_B_DC = 64500 ; % cts

% calibration value
% convert from counts to to Watts (multiply following values)
% ADC conversion [V/ct] = 6.1035e-4 from foton based on +-20V 16 bit ADC
% transimpedance [A/V] = 0.001 (1/1kOhm) from foton
% PD responsivity [W/A] = 1.25 from foton
cal_DC = 7.625e-7 ; % W/ct

% power on PD
P_REFL_A = REFL_A_DC * cal_DC ;
P_REFL_B = REFL_B_DC * cal_DC ;
P_AS_A = AS_A_DC * cal_DC ;
P_AS_B = AS_B_DC * cal_DC ;

% to get Shot noise in W/rtHz
% sqrt(S) = sqrt(2*h*f*P), h = Planck constant, f = laser frequency (c/1064
% nm), P is incident power
% k = 2*h*f for simplicity
k = 3.734e-19 ; % J

S_REFL_A = sqrt(k*P_REFL_A) ; % W/rtHz
S_REFL_B = sqrt(k*P_REFL_B) ;% W/rtHz
S_AS_A = sqrt(k*P_AS_A) ; % W/rtHz
S_AS_B = sqrt(k*P_AS_B) ; % W/rtHz

%% RF Chain

% load ref (dark = johnson + adc) and meas (locked measurement)
% then use calibration to convert from ct/rtHz to W/rtHz

% file format: col 1 is freq in Hz, cols 2-5 are I_P I_Y Q_P Q_Y locked
% meas, cols 6-9 are I_P I_Y Q_P Q_Y ref
fileID = fopen('AS_A_RF45.txt', 'r') ;
AS_A_45 = textscan(fileID, ['%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f']) ;
fclose(fileID) ;

fileID = fopen('AS_B_RF45.txt', 'r') ;
AS_B_45 = textscan(fileID, ['%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f']) ;
fclose(fileID) ;

freqA_AS45 = AS_A_45{1} ;
freqB_AS45 = AS_B_45{1} ;

meas_A_AS45 = [] ;
meas_B_AS45 = [] ;
for i=2:5
    meas_A_AS45 = [meas_A_AS45 AS_A_45{i}] ;
    meas_B_AS45 = [meas_B_AS45 AS_B_45{i}] ;
end
% cols 1 and 2 are PIT/YAW I, 3 and 4 are PIT/YAW Q

ref_A_AS45 = [] ;
ref_B_AS45 = [] ;
for i=6:9
    ref_A_AS45 = [ref_A_AS45 AS_A_45{i}] ;
    ref_B_AS45 = [ref_B_AS45 AS_B_45{i}] ;
end
% cols 1 and 2 are PIT/YAW I, 3 and 4 are PIT/YAW Q

fileID = fopen('AS_A_RF36.txt', 'r') ;
AS_A_36 = textscan(fileID, ['%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f']) ;
fclose(fileID) ;

fileID = fopen('AS_B_RF36.txt', 'r') ;
AS_B_36 = textscan(fileID, ['%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f']) ;
fclose(fileID) ;

freqA_AS36 = AS_A_36{1} ;
freqB_AS36 = AS_B_36{1} ;

meas_A_AS36 = [] ;
meas_B_AS36 = [] ;
for i=2:5
    meas_A_AS36 = [meas_A_AS36 AS_A_36{i}] ;
    meas_B_AS36 = [meas_B_AS36 AS_B_36{i}] ;
end
% cols 1 and 2 are PIT/YAW I, 3 and 4 are PIT/YAW Q

ref_A_AS36 = [] ;
ref_B_AS36 = [] ;
for i=6:9
    ref_A_AS36 = [ref_A_AS36 AS_A_36{i}] ;
    ref_B_AS36 = [ref_B_AS36 AS_B_36{i}] ;
end
% cols 1 and 2 are PIT/YAW I, 3 and 4 are PIT/YAW Q

fileID = fopen('REFL_A_RF45.txt', 'r') ;
REFL_A_45 = textscan(fileID, ['%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f']) ;
fclose(fileID) ;

fileID = fopen('REFL_B_RF45.txt', 'r') ;
REFL_B_45 = textscan(fileID, ['%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f']) ;
fclose(fileID) ;

freqA_REFL45 = REFL_A_45{1} ;
freqB_REFL45 = REFL_B_45{1} ;

meas_A_REFL45 = [] ;
meas_B_REFL45 = [] ;
for i=2:5
    meas_A_REFL45 = [meas_A_REFL45 REFL_A_45{i}] ;
    meas_B_REFL45 = [meas_B_REFL45 REFL_B_45{i}] ;
end
% cols 1 and 2 are PIT/YAW I, 3 and 4 are PIT/YAW Q

ref_A_REFL45 = [] ;
ref_B_REFL45 = [] ;
for i=6:9
    ref_A_REFL45 = [ref_A_REFL45 REFL_A_45{i}] ;
    ref_B_REFL45 = [ref_B_REFL45 REFL_B_45{i}] ;
end
% cols 1 and 2 are PIT/YAW I, 3 and 4 are PIT/YAW Q

fileID = fopen('REFL_A_RF9.txt', 'r') ;
REFL_A_9 = textscan(fileID, ['%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f']) ;
fclose(fileID) ;

fileID = fopen('REFL_B_RF9.txt', 'r') ;
REFL_B_9 = textscan(fileID, ['%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f']) ;
fclose(fileID) ;

freqA_REFL9 = REFL_A_9{1} ;
freqB_REFL9 = REFL_B_9{1} ;

meas_A_REFL9 = [] ;
meas_B_REFL9 = [] ;
for i=2:5
    meas_A_REFL9 = [meas_A_REFL9 REFL_A_9{i}] ;
    meas_B_REFL9 = [meas_B_REFL9 REFL_B_9{i}] ;
end
% cols 1 and 2 are PIT/YAW I, 3 and 4 are PIT/YAW Q

ref_A_REFL9 = [] ;
ref_B_REFL9 = [] ;
for i=6:9
    ref_A_REFL9 = [ref_A_REFL9 REFL_A_9{i}] ;
    ref_B_REFL9 = [ref_B_REFL9 REFL_B_9{i}] ;
end
% cols 1 and 2 are PIT/YAW I, 3 and 4 are PIT/YAW Q

% gain factors applied to the WFS during lock, cts/cts units
gain_REFL9 = 1 ;
gain_REFL45 = 1.41 ;
gain_AS45 = 1.41 ;
gain_AS36 = 3.95 ;

% whitening dB applied to WFS
dB_REFL9 = 21 ;
dB_REFL45 = 15 ;
dB_AS45 = 3 ;
dB_AS36 = 15 ;

% transimpedance for each WFS signal chain, in Ohms
trans_REFL9 = 900 ;
trans_REFL45 = 650 ;
trans_AS45 = 800 ;
trans_AS36 = 900 ;

% photodiode responsivity in W/A
pd_res = 1.25 ;

% ADC conversion in V/ct, 16 bit +-20V
adc = 6.1035e-4 ;

% conversion for AS RF45 A and B locked and dark, results in W/rtHz
lock_AS45_A = meas_A_AS45 * 1/gain_AS45 * adc * 1/(10^(dB_AS45/20)) * 1/trans_AS45 * pd_res ;
lock_AS45_B = meas_B_AS45 * 1/gain_AS45 * adc * 1/(10^(dB_AS45/20)) * 1/trans_AS45 * pd_res ;
ref_AS45_A = ref_A_AS45 * 1/gain_AS45 * adc * 1/(10^(dB_AS45/20)) * 1/trans_AS45 * pd_res ;
ref_AS45_B = ref_B_AS45 * 1/gain_AS45 * adc * 1/(10^(dB_AS45/20)) * 1/trans_AS45 * pd_res ;

% conversion for AS RF36 A and B locked and dark, results in W/rtHz
lock_AS36_A = meas_A_AS36 * 1/gain_AS36 * adc * 1/(10^(dB_AS36/20)) * 1/trans_AS36 * pd_res ;
lock_AS36_B = meas_B_AS36 * 1/gain_AS36 * adc * 1/(10^(dB_AS36/20)) * 1/trans_AS36 * pd_res ;
ref_AS36_A = ref_A_AS36 * 1/gain_AS36 * adc * 1/(10^(dB_AS36/20)) * 1/trans_AS36 * pd_res ;
ref_AS36_B = ref_B_AS36 * 1/gain_AS36 * adc * 1/(10^(dB_AS36/20)) * 1/trans_AS36 * pd_res ;

% conversion for REFL RF45 A and B locked and dark, results in W/rtHz
lock_REFL45_A = meas_A_REFL45 * 1/gain_REFL45 * adc * 1/(10^(dB_REFL45/20)) * 1/trans_REFL45 * pd_res ;
lock_REFL45_B = meas_B_REFL45 * 1/gain_REFL45 * adc * 1/(10^(dB_REFL45/20)) * 1/trans_REFL45 * pd_res ;
ref_REFL45_A = ref_A_REFL45 * 1/gain_REFL45 * adc * 1/(10^(dB_REFL45/20)) * 1/trans_REFL45 * pd_res ;
ref_REFL45_B = ref_B_REFL45 * 1/gain_REFL45 * adc * 1/(10^(dB_REFL45/20)) * 1/trans_REFL45 * pd_res ;

% conversion for REFL RF9 A and B locked and dark, results in W/rtHz
lock_REFL9_A = meas_A_REFL9 * 1/gain_REFL9 * adc * 1/(10^(dB_REFL9/20)) * 1/trans_REFL9 * pd_res ;
lock_REFL9_B = meas_B_REFL9 * 1/gain_REFL9 * adc * 1/(10^(dB_REFL9/20)) * 1/trans_REFL9 * pd_res ;
ref_REFL9_A = ref_A_REFL9 * 1/gain_REFL9 * adc * 1/(10^(dB_REFL9/20)) * 1/trans_REFL9 * pd_res ;
ref_REFL9_B = ref_B_REFL9 * 1/gain_REFL9 * adc * 1/(10^(dB_REFL9/20)) * 1/trans_REFL9 * pd_res ;

%% Make plots of locked. reference and shot noise spectra

% Plot REFL RF45 A
figure
loglog(freqA_REFL45, lock_REFL45_A(:,1))
hold on
loglog(freqA_REFL45, lock_REFL45_A(:,2))
hold on
loglog(freqA_REFL45, lock_REFL45_A(:,3))
hold on
loglog(freqA_REFL45, lock_REFL45_A(:,4))
hold on
loglog(freqA_REFL45, ref_REFL45_A(:,1), '--', 'LineWidth', 2)
hold on
loglog(freqA_REFL45, ref_REFL45_A(:,2), '--', 'LineWidth', 2)
hold on
loglog(freqA_REFL45, ref_REFL45_A(:,3), '--', 'LineWidth', 2)
hold on
loglog(freqA_REFL45, ref_REFL45_A(:,4), '--', 'LineWidth', 2)
hold on
yline(S_REFL_A, '-.', 'Shot Noise', 'LineWidth',2)
ylim([1e-11 1e-3])
grid on
title('REFL A RF45')
xlabel('Freq [Hz]')
ylabel('W/rtHz')
legend('Locked I PIT', 'Locked I YAW', 'Locked Q PIT', 'Locked Q YAW', 'Dark I PIT', 'Dark I YAW', 'Dark Q PIT', 'Dark Q YAW', 'NumColumns', 2)
saveas(gcf, 'REFL_A_45.fig')
saveas(gcf, 'REFL_A_45.jpg')

% Plot REFL RF45 B
figure
loglog(freqB_REFL45, lock_REFL45_B(:,1))
hold on
loglog(freqB_REFL45, lock_REFL45_B(:,2))
hold on
loglog(freqB_REFL45, lock_REFL45_B(:,3))
hold on
loglog(freqB_REFL45, lock_REFL45_B(:,4))
hold on
loglog(freqB_REFL45, ref_REFL45_B(:,1), '--', 'LineWidth', 2)
hold on
loglog(freqB_REFL45, ref_REFL45_B(:,2), '--', 'LineWidth', 2)
hold on
loglog(freqB_REFL45, ref_REFL45_B(:,3), '--', 'LineWidth', 2)
hold on
loglog(freqB_REFL45, ref_REFL45_B(:,4), '--', 'LineWidth', 2)
hold on
yline(S_REFL_B, '-.', 'Shot Noise', 'LineWidth', 2)
title('REFL B RF45')
grid on
ylim([1e-11 1e-3])
xlabel('Freq [Hz]')
ylabel('W/rtHz')
legend('Locked I PIT', 'Locked I YAW', 'Locked Q PIT', 'Locked Q YAW', 'Dark I PIT', 'Dark I YAW', 'Dark Q PIT', 'Dark Q YAW', 'NumColumns', 2)
saveas(gcf, 'REFL_B_45.fig')
saveas(gcf, 'REFL_B_45.jpg')



% Plot REFL RF9 A
figure
loglog(freqA_REFL9, lock_REFL9_A(:,1))
hold on
loglog(freqA_REFL9, lock_REFL9_A(:,2))
hold on
loglog(freqA_REFL9, lock_REFL9_A(:,3))
hold on
loglog(freqA_REFL9, lock_REFL9_A(:,4))
hold on
loglog(freqA_REFL9, ref_REFL9_A(:,1), '--', 'LineWidth', 2)
hold on
loglog(freqA_REFL9, ref_REFL9_A(:,2), '--', 'LineWidth', 2)
hold on
loglog(freqA_REFL9, ref_REFL9_A(:,3), '--', 'LineWidth', 2)
hold on
loglog(freqA_REFL9, ref_REFL9_A(:,4), '--', 'LineWidth', 2)
hold on
yline(S_REFL_A, '-.', 'Shot Noise', 'LineWidth',2)
ylim([1e-11 1e-3])
grid on
title('REFL A RF9')
xlabel('Freq [Hz]')
ylabel('W/rtHz')
legend('Locked I PIT', 'Locked I YAW', 'Locked Q PIT', 'Locked Q YAW', 'Dark I PIT', 'Dark I YAW', 'Dark Q PIT', 'Dark Q YAW', 'NumColumns', 2)
saveas(gcf, 'REFL_A_9.fig')
saveas(gcf, 'REFL_A_9.jpg')



% Plot REFL RF9 B
figure
loglog(freqB_REFL9, lock_REFL9_B(:,1))
hold on
loglog(freqB_REFL9, lock_REFL9_B(:,2))
hold on
loglog(freqB_REFL9, lock_REFL9_B(:,3))
hold on
loglog(freqB_REFL9, lock_REFL9_B(:,4))
hold on
loglog(freqB_REFL9, ref_REFL9_B(:,1), '--', 'LineWidth', 2)
hold on
loglog(freqB_REFL9, ref_REFL9_B(:,2), '--', 'LineWidth', 2)
hold on
loglog(freqB_REFL9, ref_REFL9_B(:,3), '--', 'LineWidth', 2)
hold on
loglog(freqB_REFL9, ref_REFL9_B(:,4), '--', 'LineWidth', 2)
hold on
yline(S_REFL_B, '-.', 'Shot Noise', 'LineWidth', 2)
title('REFL B RF9')
grid on
ylim([1e-11 1e-3])
xlabel('Freq [Hz]')
ylabel('W/rtHz')
legend('Locked I PIT', 'Locked I YAW', 'Locked Q PIT', 'Locked Q YAW', 'Dark I PIT', 'Dark I YAW', 'Dark Q PIT', 'Dark Q YAW', 'NumColumns', 2)
saveas(gcf, 'REFL_B_9.fig')
saveas(gcf, 'REFL_B_9.jpg')



% Plot AS RF45 A
figure
loglog(freqA_AS45, lock_AS45_A(:,1))
hold on
loglog(freqA_AS45, lock_AS45_A(:,2))
hold on
loglog(freqA_AS45, lock_AS45_A(:,3))
hold on
loglog(freqA_AS45, lock_AS45_A(:,4))
hold on
loglog(freqA_AS45, ref_AS45_A(:,1), '--', 'LineWidth', 2)
hold on
loglog(freqA_AS45, ref_AS45_A(:,2), '--', 'LineWidth', 2)
hold on
loglog(freqA_AS45, ref_AS45_A(:,3), '--', 'LineWidth', 2)
hold on
loglog(freqA_AS45, ref_AS45_A(:,4), '--', 'LineWidth', 2)
hold on
yline(S_AS_A, '-.', 'Shot Noise', 'LineWidth',2)
ylim([1e-11 1e-3])
grid on
title('AS A RF45')
xlabel('Freq [Hz]')
ylabel('W/rtHz')
legend('Locked I PIT', 'Locked I YAW', 'Locked Q PIT', 'Locked Q YAW', 'Dark I PIT', 'Dark I YAW', 'Dark Q PIT', 'Dark Q YAW', 'NumColumns', 2)
saveas(gcf, 'AS_A_45.fig')
saveas(gcf, 'AS_A_45.jpg')



% Plot AS RF45 B
figure
loglog(freqB_AS45, lock_AS45_B(:,1))
hold on
loglog(freqB_AS45, lock_AS45_B(:,2))
hold on
loglog(freqB_AS45, lock_AS45_B(:,3))
hold on
loglog(freqB_AS45, lock_AS45_B(:,4))
hold on
loglog(freqB_AS45, ref_AS45_B(:,1), '--', 'LineWidth', 2)
hold on
loglog(freqB_AS45, ref_AS45_B(:,2), '--', 'LineWidth', 2)
hold on
loglog(freqB_AS45, ref_AS45_B(:,3), '--', 'LineWidth', 2)
hold on
loglog(freqB_AS45, ref_AS45_B(:,4), '--', 'LineWidth', 2)
hold on
yline(S_AS_B, '-.', 'Shot Noise', 'LineWidth', 2)
title('AS B RF45')
grid on
ylim([1e-11 1e-3])
xlabel('Freq [Hz]')
ylabel('W/rtHz')
legend('Locked I PIT', 'Locked I YAW', 'Locked Q PIT', 'Locked Q YAW', 'Dark I PIT', 'Dark I YAW', 'Dark Q PIT', 'Dark Q YAW', 'NumColumns', 2)
saveas(gcf, 'AS_B_45.fig')
saveas(gcf, 'AS_B_45.jpg')


% Plot AS RF36 A
figure
loglog(freqA_AS36, lock_AS36_A(:,1))
hold on
loglog(freqA_AS36, lock_AS36_A(:,2))
hold on
loglog(freqA_AS36, lock_AS36_A(:,3))
hold on
loglog(freqA_AS36, lock_AS36_A(:,4))
hold on
loglog(freqA_AS36, ref_AS36_A(:,1), '--', 'LineWidth', 2)
hold on
loglog(freqA_AS36, ref_AS36_A(:,2), '--', 'LineWidth', 2)
hold on
loglog(freqA_AS36, ref_AS36_A(:,3), '--', 'LineWidth', 2)
hold on
loglog(freqA_AS36, ref_AS36_A(:,4), '--', 'LineWidth', 2)
hold on
yline(S_AS_A, '-.', 'Shot Noise', 'LineWidth',2)
ylim([1e-11 1e-3])
grid on
title('AS A RF36')
xlabel('Freq [Hz]')
ylabel('W/rtHz')
legend('Locked I PIT', 'Locked I YAW', 'Locked Q PIT', 'Locked Q YAW', 'Dark I PIT', 'Dark I YAW', 'Dark Q PIT', 'Dark Q YAW', 'NumColumns', 2)
saveas(gcf, 'AS_A_36.fig')
saveas(gcf, 'AS_A_36.jpg')


% Plot AS RF36 B
figure
loglog(freqB_AS36, lock_AS36_B(:,1))
hold on
loglog(freqB_AS36, lock_AS36_B(:,2))
hold on
loglog(freqB_AS36, lock_AS36_B(:,3))
hold on
loglog(freqB_AS36, lock_AS36_B(:,4))
hold on
loglog(freqB_AS36, ref_AS36_B(:,1), '--', 'LineWidth', 2)
hold on
loglog(freqB_AS36, ref_AS36_B(:,2), '--', 'LineWidth', 2)
hold on
loglog(freqB_AS36, ref_AS36_B(:,3), '--', 'LineWidth', 2)
hold on
loglog(freqB_AS36, ref_AS36_B(:,4), '--', 'LineWidth', 2)
hold on
yline(S_AS_B, '-.', 'Shot Noise', 'LineWidth', 2)
title('AS B RF36')
grid on
ylim([1e-11 1e-3])
xlabel('Freq [Hz]')
ylabel('W/rtHz')
legend('Locked I PIT', 'Locked I YAW', 'Locked Q PIT', 'Locked Q YAW', 'Dark I PIT', 'Dark I YAW', 'Dark Q PIT', 'Dark Q YAW', 'NumColumns', 2)
saveas(gcf, 'AS_B_36.fig')
saveas(gcf, 'AS_B_36.jpg')






