% Nutsinee Kijbunchoo
% April 24, 2018
% This code calculates OPO refl shotnoise and compare it to measured data.
% The calculation is to be used to estimate shotnoise given any operating
% power.
% OPO refl BBPD serial#S1200234
% demod box #S1000783
% DC Transimpedance 1400Ohms, Responsivity 0.3A/W

%% Load data
OPOrefl_SN = load('OPOrefl_SN_I.TXT');
OPOrefl_DN = load('OPOrefl_DN_I.TXT');

OPOrefl_SN_x = OPOrefl_SN(:,1);
OPOrefl_SN_y = OPOrefl_SN(:,2);

OPOrefl_DN_x = OPOrefl_DN(:,1);
OPOrefl_DN_y = OPOrefl_DN(:,2);


%% Shotnoise calculation
OPOSN = 2310*sqrt(2*1.6e-19*0.5e-3)*5.06*sqrt(2); 
% Would be missing a factor of 1.5 if letting RF transimpedance be 1400 Ohms.
% Transimpedance*sqrt(2*e*I)*demod board gain*demod factor of sqrt(2)
% demod board gain came from S1000783. Look at IQ Resp page.
% the gain came from IF beatnote (mixer output, I or Q mon) of 3.2Vpp
% devided by 0dB input (0.632Vpp)
OPOSN_7mW = 2310*sqrt(2*1.6e-19*0.84e-3)*5.06*sqrt(2); 
OPOSN_3mW = 2310*sqrt(2*1.6e-19*0.42e-3)*5.06*sqrt(2); 
OPOSN_lim = 2310*sqrt(2*1.6e-19*0.29e-3)*5.06*sqrt(2); 


%% Plot stuff
figure(1)
loglog(OPOrefl_SN_x, OPOrefl_SN_y, 'LineWidth' ,2 ); %shotnoise
xlim([1e2,1e4])
ylim([1.5e-7, 7e-7])
hold on
loglog(OPOrefl_SN_x, ones(size(OPOrefl_SN_x))*(OPOSN), '--','LineWidth', 2); %Calculated shotnoise
loglog(OPOrefl_SN_x, ones(size(OPOrefl_SN_x))*(OPOSN_7mW), '--','LineWidth', 2); %Calculated shotnoise
loglog(OPOrefl_SN_x, ones(size(OPOrefl_SN_x))*(OPOSN_3mW), '--','LineWidth', 2); %Calculated shotnoise
loglog(OPOrefl_DN_x, OPOrefl_DN_y, 'LineWidth' ,2 ); %darknoise
loglog(OPOrefl_SN_x, ones(size(OPOrefl_SN_x))*(OPOSN_lim), '--','LineWidth', 2); %Calculated shotnoise


xlabel('Frequency (Hz)','FontSize', 18)
ylabel('V_{rms}/\surdHz','FontSize', 18)
legend({'OPO Refl Shotnoise (measured, input current = 0.5mA)', 'OPO refl shotnoise (calculated)', 'Predicted OPO refl shotnoise at 7mW input to OPO', 'Predicted OPO refl shotnoise at 3mW input to OPO', 'Dark nosie (measured)', 'Shotnoise limited power (0.96mW)'},'FontSize',18)
hold off