%% OMC DCPD projection
% OMC design document T1000276
% OMC DCPD responsivity 0.858A/W (alog 45736)

%% OMC parameters
Fin = 390;
FSR = 264.8e6; 
OMC_cav_pole = (FSR/Fin)/2;
DCPDresp = 0.858; %A/W

%% Calculate OMC shotnoise
I_DCPD = 20e-3;
OMCSN = sqrt(2*1.6e-19*I_DCPD);

%% plot OMC cavity pole
OMC_pole = zpk(-2*pi*[ ],-2*pi*[ OMC_cav_pole], (2*pi*OMC_cav_pole));

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

[OMCpoleamp, OMCpolephase] = bode(OMC_pole, FSSTF_ampx_lf);
OMCpoleamp = squeeze(OMCpoleamp);
OMCpolephase = squeeze(OMCpolephase);
OMCpolefilter = OMCpoleamp.*exp(i.*deg2rad(OMCpolephase));
OMCpolemagzpk = abs(OMCpolefilter);
OMCpolephasezpk = angle(OMCpolefilter);


%% load spec data

CLF_nominal = load('CLF_before_halfwaveplatechange_OMCpower.TXT');
CLF_nominal_x = CLF_nominal(:,1);
CLF_nominal_y = CLF_nominal(:,2);

CLF_fullpwr = load('CLF_fullpower.TXT');
CLF_fullpwr_x = CLF_fullpwr(:,1);
CLF_fullpwr_y = CLF_fullpwr(:,2);

%% shitt diode 8kHz zero
PD_zero = zpk(-2*pi*[ 8e3],-2*pi*[ ], 1./(2*pi*8e3));

[PDamp, PDphase] = bode(PD_zero, CLF_fullpwr_x);
PDamp = squeeze(PDamp);
PDphase = squeeze(PDphase);
PDfilter = PDamp.*exp(i.*deg2rad(PDphase));
PDOLmagzpk = abs(PDfilter);
PDOLphasezpk = angle(PDfilter);

%% plot spec

figure(54);
% CLF launch RIN * W * PD 8kHz zero * DCPD responsivity * QE / (factor of 10 down from OMC cav pole)
p1 = loglog(CLF_nominal_x, (10^3.*CLF_nominal_y.*5e-6.*PDOLmagzpk.*DCPDresp.*0.98./(0.89*10)), 'LineWidth', 1.5); %100/5.5 is 5.5% BS
%p1.Color(4) = 0.5;
xlabel('Frequency, Hz'), ylabel('mA/\surd Hz');
xlim([10,1e5])
ylim([5e-11,1e-6])
%ylim([1e-7,2e-4])
%title('LASER\_IR\_DC Calibrated, Dec 6, 2018');
grid on;

hold on
%loglog(CLF_fullpwr_x, ( 10^3.*CLF_fullpwr_y.*5e-6.*PDOLmagzpk.*DCPDresp.*0.98./(1.6762*10) ),'LineWidth', 1.5); % *mW/V pretending this is the best we would be able to do
p2 = loglog(CLF_fullpwr_x, ones(size(CLF_fullpwr_x)).*(10^3.*OMCSN), 'LineWidth', 2); %Calculated shotnoise
p2.Color(4) = 0.5;

set(gca,'FontSize',18);


hTitle = title('CLF intensity noise projection to OMC DCPD (Dec 11, 2018)');
set(hTitle,'FontSize',16)

legend({'CLF intensity noise seen by DCPD (5uW to OMC)', 'OMC shotnoise (projected given 20mA input to DCPD)'},'FontSize',14, 'Location', 'Northeast')

hold off

%% plot OMC cavity pole
figure(55);
subplot(2,1,1);
semilogx(FSSTF_ampx_lf./(2*pi), 20*log10(OMCpolemagzpk),'LineWidth', 1.5);
xlabel('Frequency, Hz'), ylabel('Gain, dB');
xlim([1,3e7])
legend('raw','Fitted')
title('Bode Plot Shitty PD zero');
grid on;
subplot(2,1,2);
semilogx(FSSTF_ampx_lf./(2*pi), rad2deg(OMCpolephasezpk),'LineWidth', 1.5);
xlabel('Frequency, Hz'), ylabel('Phase, deg.');
xlim([1,3e7])
grid on;


