%% Darknoise Shotnoise

%% load data

%% OPO S1200234

OPO_darknoise = load('SCRN0002.TXT');
OPO_darknoise_x = OPO_darknoise(:,1);
OPO_darknoise_y = OPO_darknoise(:,2);

%7V DVM readout at OPO refl PD, S1200234
OPO_shotnoise = load('SCRN0003.TXT'); 
OPO_shotnoise_x = OPO_shotnoise(:,1);
OPO_shotnoise_y = OPO_shotnoise(:,2);

OPO_x = OPO_shotnoise(:,1);

ROPODC = 2e3; %transimpedance V/A
ROPORF = 1.55e3;
OPODN = ROPORF*sqrt(2*1.6e-19*0.65e-3)*sqrt(2)*5.06; 
OPOSN = ROPORF*sqrt(2*1.6e-19*7/ROPODC)*sqrt(2)*5.06; 
OPOsens = sqrt(OPODN^2+OPOSN^2);

%% CLF S1900052

CLF_darknoise = load('SCRN0004.TXT'); %taken at QMON
CLF_darknoise_x = CLF_darknoise(:,1);
CLF_darknoise_y = CLF_darknoise(:,2);

%8.05V DVM readout at CLF refl PD, S1900052
CLF_shotnoise = load('SCRN0006.TXT');
CLF_shotnoise_x = CLF_shotnoise(:,1);
CLF_shotnoise_y = CLF_shotnoise(:,2);

RCLFDC = 10e3;
RCLFRF = 25e3;
CLFDN = RCLFRF*sqrt(2*1.6e-19*0.02e-3)*5.06*22.4; %sqrt(2) not needed, small idler sb
CLFSN = RCLFRF*sqrt(2*1.6e-19*8.05/RCLFDC)*5.06*22.4; 
CLFsens = sqrt(CLFDN^2+CLFSN^2);


%% SHG S1200236

SHG_darknoise = load('SCRN0008.TXT'); %taken at QMON
SHG_darknoise_x = SHG_darknoise(:,1);
SHG_darknoise_y = SHG_darknoise(:,2);

%4.1V DVM readout at SHG trans PD, S1200236
SHG_shotnoise = load('SCRN0009.TXT');
SHG_shotnoise_x = SHG_shotnoise(:,1);
SHG_shotnoise_y = SHG_shotnoise(:,2);

RSHGDC = 20e3;
RSHGRF = 4.8e3;
SHGDN = RSHGRF*sqrt(2*1.6e-19*0.03e-3)*5.06*22.4; %sqrt(2) not needed, small sb
SHGSN = RSHGRF*sqrt(2*1.6e-19*4.1/RSHGDC)*5.06*22.4; 
SHGsens = sqrt(SHGDN^2+SHGSN^2);

%% TTFSS New Focus 1611

TTFSS_darknoise = load('TTFSSDN.TXT'); %taken at pre-amp output -131dBm/Hz
TTFSS_darknoise_x = TTFSS_darknoise(:,1);
TTFSS_darknoise_y = TTFSS_darknoise(:,2);

%-3.459V DVM readout at madbox monitor Aux. Conc. 7 CH1
TTFSS_shotnoise = load('TTFSSSN.TXT'); %taken at pre-amp output -128dBm/Hz
TTFSS_shotnoise_x = TTFSS_shotnoise(:,1);
TTFSS_shotnoise_y = TTFSS_shotnoise(:,2);

% convert dBm/Hz to V/sqrt(Hz) http://referencedesigner.com/rfcal/cal_02.php
% P(dBm) = 10log10(v^2/(R*po)); R = 50Ohms, p0 = 1mW
% Get to V^2/Hz then sqrt()
R = 50;
p0 = 1e-3;
TTFSSDN = sqrt(R.*10.^(TTFSS_darknoise_y./10).*p0);
TTFSSSN = sqrt(R.*10.^(TTFSS_shotnoise_y./10).*p0);

R1611DC = 10e3; %New Focus 1611 https://www.artisantg.com/info/ATGnkfvp.pdf
preampgain = 8; %preamp output gain
R1611RF = 720; 
TTFSSDNcal = R1611RF.*sqrt(2*1.6e-19*0.37e-3)*preampgain; 
TTFSSSNcal = R1611RF.*sqrt(2*1.6e-19*3.459/R1611DC)*preampgain; 
TTFSSsens = sqrt(TTFSSDNcal.^2+TTFSSSNcal.^2);


%% color code
% https://htmlcolorcodes.com
% Convert color code to 1-by-3 RGB array (0~1 each)
str = '#276507';
darkgreen = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
str = '#943810';
darkorange = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
str = '#FA224B';
brightred = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;

%% plot shit

figure(1000)
subplot(3,1,1)
plotOPODN = loglog(OPO_darknoise_x, OPO_darknoise_y.*1e6, 'LineWidth', 2, 'Color', [0.3010 0.7450 0.9330]);
plotOPODN.Color(4) = 0.5;

ylabel('\muV/\surd Hz');
grid on;
hold on

loglog(OPO_darknoise_x, ones(size(OPO_x)).*OPODN.*1e6, '--','LineWidth', 2, 'Color',  [0.3010 0.7450 0.9330]);
plotOPOSN = loglog(OPO_shotnoise_x, OPO_shotnoise_y.*1e6, 'LineWidth', 2, 'Color', [0 0.4470 0.7410]);
plotOPODN.Color(4) = 0.5;
loglog(OPO_shotnoise_x, ones(size(OPO_x)).*OPOsens.*1e6, '--','LineWidth', 2, 'Color', [0 0.4470 0.7410]);

xlim([1e3,1e5])
ylim([1e-7*1e6,1e-4*1e6])
set(gca,'FontSize',18);
hTitle = title('SQZ PDs Darknoise Shotnoise (Oct 8th 2019)');
set(hTitle,'FontSize',16);
legend({'OPO darknoise (measured at IMON, PD S1200234)', 'OPO darknoise (calculated, SN limited current = 0.65mA)',...
    'OPO shotnoise (measured at IMON)', 'OPO shotnoise (calculated, 1.55kOhms RF Transimpedance)'},'FontSize',12, 'Location', 'Northeast');
hold off

subplot(3,1,2)
plotCLFDN = loglog(OPO_x, CLF_darknoise_y.*1e6, 'LineWidth', 2, 'Color', [0.8500 0.3250 0.0980]);
plotCLFDN.Color(4) = 0.5;

ylabel('\muV/\surd Hz');
grid on
hold on

loglog(OPO_x, ones(size(OPO_x)).*CLFDN.*1e6, '--','LineWidth', 2, 'Color', [0.8500 0.3250 0.0980]);
plotCLFSN = loglog(OPO_x, CLF_shotnoise_y.*1e6, 'LineWidth', 2, 'Color', darkorange);
plotCLFSN.Color(4) = 0.5;
loglog(OPO_x, ones(size(OPO_x)).*CLFsens.*1e6, '--','LineWidth', 2, 'Color', darkorange);

xlim([1e3,1e5])
ylim([1e-7.*1e6,1e-4.*1e6])
set(gca,'FontSize',18);
legend({
    'CLF darknoise (measured at QMON, PD S1900052)','CLF darknoise (calculated, SN limited current = 20uA)',...
    'CLF shotnoise (measured at QMON)','CLF shotnoise (calculated, 25kOhms RF Transimpedance)'},'FontSize',12, 'Location', 'Southeast');

hold off


subplot(3,1,3)
plotCLFDN = loglog(OPO_x, SHG_darknoise_y.*1e6, 'LineWidth', 2, 'Color', [0.4660 0.6740 0.1880]);
plotCLFDN.Color(4) = 0.5;

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

loglog(OPO_x, ones(size(OPO_x)).*SHGDN.*1e6, '--','LineWidth', 2, 'Color', [0.4660 0.6740 0.1880]);
plotCLFSN = loglog(OPO_x, SHG_shotnoise_y.*1e6, 'LineWidth', 2, 'Color', darkgreen);
plotCLFSN.Color(4) = 0.5;
loglog(OPO_x, ones(size(OPO_x)).*SHGsens.*1e6, '--','LineWidth', 2, 'Color', darkgreen);

xlim([1e3,1e5])
ylim([1e-7.*1e6,1e-4.*1e6])
set(gca,'FontSize',18);
legend({
    'SHG darknoise (measured at IMON, PD S1200236)','SHG darknoise (calculated, SN limited current = 30uA)',...
    'SHG shotnoise (measured at IMON)','SHG shotnoise (calculated, 4.8kOhms RF Transimpedance)'},'FontSize',12, 'Location', 'Southeast');

hold off


%% Plot 

figure(1001)

plotTTFSSDN = loglog(TTFSS_darknoise_x.*1e-2.*1e-6, TTFSSDN.*1e6, 'LineWidth', 2, 'Color', [0.4940 0.1840 0.5560]);
plotTTFSSDN.Color(4) = 0.5;
xlabel('Frequency, MHz'), ylabel('\muV/\surd Hz');

grid on
hold on

loglog(TTFSS_darknoise_x.*1e-2.*1e-6, ones(size(TTFSS_darknoise_x.*1e-2.*1e-6)).*TTFSSDNcal.*1e6, '--','LineWidth', 2, 'Color', [0.4940 0.1840 0.5560]);

plotTTFSSSN = loglog(TTFSS_darknoise_x.*1e-2.*1e-6, TTFSSSN.*1e6, 'LineWidth', 2, 'Color', [0.6350 0.0780 0.1840]);
plotTTFSSSN.Color(4) = 0.5;

loglog(TTFSS_darknoise_x.*1e-2.*1e-6, ones(size(TTFSS_darknoise_x.*1e-2.*1e-6)).*TTFSSsens.*1e6, '--','LineWidth', 2, 'Color', [0.6350 0.0780 0.1840]);


ylim([5e-8.*1e6,20e-8.*1e6])
set(gca,'FontSize',18);
hTitle = title('SQZ PDs Darknoise Shotnoise (New Focus 1611, Oct 8th 2019)');
legend({'TTFSS darknoise (measured at TTFSS pre-amp output, PD New Focus 1611)','TTFSS darknoise (calculated, SN limited current = 0.37mA)',...
    'TTFSS shotnoise (measured at TTFSS pre-amp output)',...
    'TTFSS shotnoise (calculated, 720Ohms RF Transimpedance)'},'FontSize',12, 'Location', 'Northeast');

hold off