%
%        C1lentickle1.m
%
%     * OLT
%     * PM coupling
%     * AM coupling
%     * Seismic noise
%                            Feb 2012 KK
%
%


setupLentickle; 

%% Run the lentickle engine

% interferometer model
opt = optC1(paramC1DRMI); % create the opt object
opt = probesC1_00(opt, paramC1DRMI);

% control model
cucumber = C1cucumber(opt); % create the cucumber structure
                                     % look at exampleMICHcucumber.m for
                                     % more info about the cucumber.
% frequency vector
fHigh = 10000; fLow = 0.5;
f = logspace(log10(fLow),log10(fHigh),500).'; % choose the frequency array we will use

% get the closed loop results

results = lentickleEngine(cucumber,[],f); % calculate all results

%% Transfer functions
% All the transfer functions (which include closed loops and all cross
% coupling effects) can be extracted from the results structure, and the
% pickleTF function makes that easy.

%% Open Loop Gains
% We will calculate the open loop gains of our two degrees of freedom, the
% differential and common modes of the arms, 'DIFF' and 'COMM'. pickleTF
% will easily give us the closed loop gain, and calulating the open loop
% gain from that is fairly easy. (OLG = 1 - 1./CLG)

MICHOLG = 1 - 1./pickleTF(results,'MICH','MICH');
PRCLOLG = 1 - 1./pickleTF(results,'PRCL','PRCL');
SRCLOLG = 1 - 1./pickleTF(results,'SRCL','SRCL');

% make a plot of them

figure(1)
subplot(2,1,1)
loglog(f,abs(MICHOLG),'r',f,abs(PRCLOLG),'b',f,abs(SRCLOLG),'--g','Linewidth',2);
title('Open Loop Gain')
ylabel('Magnitude')
legend('MICH','PRCL','SRCL')
xlim([fLow fHigh])
grid on

subplot(2,1,2)
semilogx(f,180/pi*angle(MICHOLG),'r',f,180/pi*angle(PRCLOLG),'b',f,180/pi*angle(SRCLOLG),'--g','Linewidth',2);
ylabel('Phase (degrees)')
xlabel('Frequency (Hz)')
xlim([fLow fHigh])
grid on

%% Noise Transfer Functions
% Here we will calculate the transfer function of laser noises to our
% length sensors, we will calibrate the sensor in terms of meters of the
% relevant degree of freedom.



%% Calibrations
% First we will calculate the calibration of AS_Q in DIFF meters, and
% REFL_I in COMM meters.

% ASQ2 MICH
ASQ2_MICH_calmeters = pickleTF(results,'IX','MICH') - pickleTF(results,'IY','MICH'); % units of [AS_Q counts]/m
% REFL_3I1 PARCL
REFL3I1_P_calmeters = pickleTF(results,'PR','PRCL') ;              % units of [REFL_I counts]/m
% AS3Q2 SRCL
AS3Q2_S_calmeters = pickleTF(results, 'SR', 'SRCL'); 



%% PM noise coupling
% Now we will calculate the coulping of laser frequency noise to AS_Q and
% REFL_I. The PM 'mirror' is the phase modulator actuator, we divide by
% i*f to get frequency.

FMtoASQ2 = pickleTF(results,'PM','AS_Q2') ./ ( 1i * f ); % units of [AS_Q counts]/Hz
FMtoREFL3I1 = pickleTF(results,'PM','REFL_3I1') ./ ( 1i * f ); % units of [REFL_I counts]/Hz
FMtoAS3Q2 = pickleTF(results,'PM','AS_3Q2') ./ (1i *f);

% Now we calibrate in terms of meters.

FMtoMICH = FMtoASQ2 ./ ASQ2_MICH_calmeters; % units of m/Hz
FMtoPRCL = FMtoREFL3I1 ./ REFL3I1_P_calmeters; % units of m/Hz
FMtoSRCL = FMtoAS3Q2 ./ AS3Q2_S_calmeters;

% plots

figure(2)
subplot(2,1,1)
loglog(f,abs(FMtoMICH),'r',f,abs(FMtoPRCL),'b',f,abs(FMtoSRCL),'g','Linewidth',2);
title('Frequency Noise Coupling')
ylabel('Magnitude (m/Hz)')
legend('MICH (ASQ2)','PRCL(REFL3I1)','SRCL(AS3Q2)')
xlim([fLow fHigh])
grid on
subplot(2,1,2)
semilogx(f,180/pi*angle(FMtoMICH),'r',f,180/pi*angle(FMtoPRCL),'b',f,180/pi*angle(FMtoSRCL),'g','Linewidth',2);
ylabel('Phase (degrees)')
xlabel('Frequency (Hz)')
xlim([fLow fHigh])
grid on

%% AM Noise coupling
%
% Same manner as FM coupling
%

AMtoASQ2 = pickleTF(results,'AM','AS_Q2') ; % units of [AS_Q counts]/Hz
AMtoREFL3I1 = pickleTF(results,'AM','REFL_3I1'); % units of [REFL_I counts]/Hz
AMtoAS3Q2 = pickleTF(results,'AM','AS_3Q2');

AMtoMICH = AMtoASQ2 ./ ASQ2_MICH_calmeters; % units of m/Hz
AMtoPRCL = AMtoREFL3I1 ./ REFL3I1_P_calmeters; % units of m/Hz
AMtoSRCL = AMtoAS3Q2 ./ AS3Q2_S_calmeters;

% plots

figure(3)
subplot(2,1,1)
loglog(f,abs(AMtoMICH),'r',f,abs(AMtoPRCL),'b',f,abs(AMtoSRCL),'g','Linewidth',2);
title('Amplitude Noise Coupling')
ylabel('Magnitude (/Hz)')
legend('MICH (ASQ2)','PRCL(REFL3I1)','SRCL(AS3Q2)')
xlim([fLow fHigh])
grid on
subplot(2,1,2)
semilogx(f,180/pi*angle(AMtoMICH),'r',f,180/pi*angle(AMtoPRCL),'b',f,180/pi*angle(AMtoSRCL),'g','Linewidth',2);
ylabel('Phase (degrees)')
xlabel('Frequency (Hz)')
xlim([fLow fHigh])
grid on


%% Sesimic noise estimation

S = load('seismic_gwinc.mat');    % gwinc data, m/rtHZ, ASD
Sdata0 = interp1(S.seismic.darmseis_f, S.seismic.darmseis_x, f, 'cubic', NaN);
Sdata = Sdata0;

% MICH (ASQ2)
SdataMICH = sqrt( (Sdata.*pickleTF(results, 'IX','MICH')).^2 ...
                + (Sdata.*pickleTF(results, 'IY','MICH')).^2 ...
            + (Sdata.*pickleTF(results, 'PR','MICH')).^2 ...
            + (Sdata.*pickleTF(results, 'SR','MICH')).^2 );  % m/rtHz * cts/m = cts/rtHz

         seis_calM = SdataMICH ./ ASQ2_MICH_calmeters;     %cts/rtHz/(cts/m)=m/rtHz 

% PRCL (REFL3I1)
SdataPRCL = sqrt( (Sdata.*pickleTF(results, 'IX','PRCL')).^2 ...
                + (Sdata.*pickleTF(results, 'IY','PRCL')).^2 ...
            + (Sdata.*pickleTF(results, 'PR','PRCL')).^2 ...
            + (Sdata.*pickleTF(results, 'SR','PRCL')).^2 );  % m/rtHz * cts/m = cts/rtHz

         seis_calP = SdataPRCL ./ REFL3I1_P_calmeters;     %cts/rtHz/(cts/m)=m/rtHz 

% SRCL (AS3Q2)
SdataSRCL = sqrt( (Sdata.*pickleTF(results, 'IX','SRCL')).^2 ...
                + (Sdata.*pickleTF(results, 'IY','SRCL')).^2 ...
            + (Sdata.*pickleTF(results, 'PR','SRCL')).^2 ...
            + (Sdata.*pickleTF(results, 'SR','SRCL')).^2 );  % m/rtHz * cts/m = cts/rtHz

         seis_calS = SdataSRCL ./ AS3Q2_S_calmeters;     %cts/rtHz/(cts/m)=m/rtHz 

         

% plot
figure(4)
loglog(f,sqrt(4*abs(Sdata).^2),'k','Linewidth',2)
hold on
loglog(f,abs(seis_calM),'b',f,abs(seis_calP),'g--',f,abs(seis_calS),'m--','Linewidth',2)grid on
title('Seismic noise level estimate')
ylabel('ASD [m/rtHz]')
xlabel('Frequency (Hz)')
legend('raw data added for 4 optics','MICH calibrated','PRCL calibrated','SRCL calibrated')
xlim([fLow fHigh])


%% Thermal noise budget