%1611 photodiode with 20dB amplifier at AS_REFL on ISCT6
%signal read into spectrum analyzer with michelwson locked to the bright fringe. 
%10kHz resolution bandwidth, 801 points
%measurement.  Noise floor at -70dBm.

%we measured with auxiliary laser offset on one side of the carrier, then
%moved to the other side. when measuring side 2 maybe michelson was not 
%aligned as well because all of the measured values were lower than for
%side_1.  We are planning to go back and repeat this side.

%auxiliary laser offset controlled on H1:LSC_EXTRA_AO_2 about 4V, amplified
%by SN560.  We needed roughly 4V to see a beat note, and moved around by
%0.05 V divisions
%beat note from ISCT2R table 1611PD rerouted to ISCT6 on rack feedthroughs
%(ISCR2 to ISCR4 or something, with something in the middle?)

%% Import data from text file.
filename = 'C:\Users\Eleanor\OneDrive\lho\negDetuning.txt';
startRow = 2;
formatSpec = '%25f%26f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
freq_NEG = -dataArray{:, 1};
ReflPDpow_NEG = 1e-3*10.^(dataArray{:, 2}./10); %dBm
ASPDpow_NEG = 1e-3*10.^(dataArray{:, 3}./10);  %dBm
clearvars filename startRow formatSpec fileID dataArray ans;
%% Import data from text file.
filename = 'C:\Users\Eleanor\OneDrive\lho\posDetuning.txt';
startRow = 2;
formatSpec = '%25f%26f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
freq_POS = dataArray{:, 1};
ReflPDpow_POS = 1e-3*10.^(dataArray{:, 2}./10); %dBm
ASPDpow_POS = 1e-3*10.^(dataArray{:, 3}./10);  %dBm

clearvars filename startRow formatSpec fileID dataArray ans;

%convert from dBm to nW...

offset=0;
%define some extra variables to use in cftool.
freq_side1=freq_NEG-offset;
amp_side1=ASPDpow_NEG;
freq_side2=freq_POS-offset;
amp_side2=ASPDpow_POS;

FIT1=fitlm(freq_side1,amp_side1,'purequadratic') %p1*x^2+p2*x+p3, least squares linear regression
FIT2=fitlm(freq_side2,amp_side2,'purequadratic')

minimum1= -FIT1.Coefficients{2,1}/2/FIT1.Coefficients{3,1} +offset %-p2/2/p1 + offset
stdErr_min1=minimum1*sqrt((FIT1.Coefficients{2,2}/FIT1.Coefficients{2,1})^2+ ...
    (FIT1.Coefficients{3,2}/FIT1.Coefficients{3,1})^2 ...
    -2*FIT1.CoefficientCovariance(3,2)/FIT1.Coefficients{3,1}/FIT1.Coefficients{2,1})

minimum2= -FIT2.Coefficients{2,1}/2/FIT2.Coefficients{3,1} +offset %-p2/2/p1 + offset
stdErr_min2=minimum1*sqrt((FIT2.Coefficients{2,2}/FIT2.Coefficients{2,1})^2+ ...
    (FIT2.Coefficients{3,2}/FIT2.Coefficients{3,1})^2 ...
    -2*FIT2.CoefficientCovariance(3,2)/FIT2.Coefficients{3,1}/FIT2.Coefficients{2,1})

ls1=acos(0)*3e8/2/pi/minimum1/1e6
d_ls1=acos(0)*3e8/2/pi/1e6*stdErr_min1/1e6
ls2=acos(0)*3e8/2/pi/minimum2/1e6
d_ls2=acos(0)*3e8/2/pi/1e6*stdErr_min2/1e6

schnupp_asy=ls1/2+ls2/2
err_schnupp_asy=(d_ls1+d_ls2)/2



figure(1301);
plot(freq_POS,ASPDpow_POS,'r+')
hold on
%plot(freq_amp_side2(:,1),freq_amp_side2(:,2),'bo')
plot(freq_NEG,ASPDpow_NEG,'bo')
hold off
title('Carrier/AUX beat-note at OMC refl with bright Michelson ')
xlabel('Frequency (MHz)')
ylabel('Photodiode power (OMC REFL beat-note) (nW)')
legend('positive side of carrier frequency','negative side of carrier frequecy')
    