% Sweep CARM  (common arm length) in DRFPMI and generate plots.
%
% [xPos, sigDC, fDC] = sweepCARM(Nsweep)
% see sweepDC for information about the parameters
%
% This displays some DC signals, and some demod signals, during
% a CARM sweep.  The phase of the REFL_I1 signal is set to maximize
% its response to this sweep.

function [xPos, sigDC, fDC] = sweepCARM(Nsweep)


  % Check number of input arguments
  if nargin < 1
    Nsweep = 1000;
  elseif nargin > 1
    error('Too Many Arguments!')
  end

  % prepare some variables
  sweepSetup

  % adding this to sweepSetup causes matlab to crash, so do it here
  nDC = [nREFL(1) nAS(1) nPOX(1) nPOY(1) nTRX(1) nTRY(1)];

  

  % SET SOME DETUNINGS TO MIMIC LOCK AQ STATUS REACHED BY RUNNING
  % smoo_prfpmi (T_SRM = 1)
  
  
  %%%%%%%%%%%%%%%%%%%%%%%%%ARMS AT FULL RESONANCE
      % XARM DeToon in degrees
  dTunePhase_XARM = 0; %.07; % XARM micro detuning (see note) (.07 = 1/2 fringe )
  % set position offsets for XARM
  dTune = dTunePhase_XARM * (opt.lambda / 360);
  opt = setPosOffset(opt, nEX,  dTune); %opt.lambda / 4 +

     % YARM DeToon in degrees
  dTunePhase_YARM = 0; %.07; % YARM micro detuning (see note)
  % set position offsets for YARM
  dTune = dTunePhase_YARM * (opt.lambda / 360);
  opt = setPosOffset(opt, nEY,  dTune); %opt.lambda / 4 +
  %%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%MICH AT 0.8 of Dark fringe
  
      % MICH DeToon in degrees
  dTunePhase_MICH = 45; % MICH micro detuning (see note)
  % set position offsets for MICH
  dTune = dTunePhase_MICH * (opt.lambda / 360);
  opt = setPosOffset(opt, nBS,  dTune); %opt.lambda / 4 +
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%PRC ON RESONANCE FOR CARRIER (CARRIER POWER BUILDS UP) 
         % PRC DeToon in degrees
   dTunePhase_PRC = -32; % PRC micro detuning (see note)
   % set position offsets for PRC
   dTune = dTunePhase_PRC * (opt.lambda / 360);
   opt = setPosOffset(opt, nPR,  dTune); %opt.lambda / 4 +
   
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
  pos = zeros(opt.Noptic, 1);
  pos(nEX) = 3e-9;
  pos(nEY) = 3e-9;
  
  [xPos, sigDC, fDC] = sweepDC(opt, -pos, pos, Nsweep);
  
  %%%%%%%%%%%%%%FIGURES
  figure(3)
  subplot(2, 1, 1)
  semilogy(xPos(nEX, :), sigDC(nDC, :))
  legend(prbNames(nDC))
  title('CARM sweep: DC signals')
  xlabel('CARM DETUNING [m]')
  grid on

  subplot(2, 1, 2)
  plot(xPos(nEX, :), sigDC(nREFL(2:end), :))
  legend(prbNames(nREFL(2:end)))
  title('CARM sweep: REFL demod signals')
  xlabel('CARM DETUNING [m]')
  grid on
  
  figure(4)
  subplot(2, 1, 1)
  semilogy(xPos(nEX, :), sigDC(nDC(5:6), :))
  legend(prbNames(nDC(5:6)))
  title('CARM sweep: DC signals')
  xlabel('CARM DETUNING [m]')
  grid on

%   subplot(2, 1, 2)
%   plot(xPos(nEX, :), sigDC(nPOX(2), :)./sigDC(nDC(5), :),...
%       xPos(nEX, :), sigDC(nPOX(3), :)./sigDC(nDC(5), :))
% 
%       legend('POXI/TRX','POXQ/TRX')
%   title('CARM sweep: POX demod signals')
%   grid on

   subplot(2, 1, 2)
  plot(xPos(nEX, :), sigDC(nPOX(2), :),...
      xPos(nEX, :), sigDC(nPOX(3), :))

      legend('POXI','POXQ')
  title('CARM sweep: POX demod signals')
  xlabel('CARM DETUNING [m]')
  grid on
