% 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 [varargout] = osa(varargin)

  % prepare some variables
  sweepSetup

  if nargin < 1
    Nsweep = 100;
  else
    Nsweep = varargin{1};
  end

  % 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)];

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % CARM sweep
  
  [opt, nPR] = addMirror(opt, 'PR', 0, 1 / 1e9, 0, 100e-6);
  
  pos = zeros(opt.Noptic, 1);
  pos(nEX) = 3e-13;
  pos(nEY) = 3e-13;  
  
  [xPos, sigDC, fDC] = sweepDC(opt, -pos, pos, 3);

  
  % make some plots
  fDCp = permute(fDC, [2, 3, 1]);  
  n = getFieldAt(opt, 'PR', 'bk');		% PR front input field

  freaks = vFrf;
  
  peaks = abs(fDCp(:,n,2));

  subplot(211)
  bar(vFrf/1e6,20*log10(abs(peaks*1e3)))
 
  subplot(212)
  bar(vFrf/1e6,20*log10(abs(peaks/vArf)))
  xlabel('Modulation Frequency [MHz]')
  ylabel('Field Amplitude (dBmW)')
  colormap(autumn)
  shading faceted
  
  
  
  
  
  % Output argument handling  ---------------------
  if nargin > 0
    varargout{1} = fDC;
    if nargout > 1
      varargout{2} = sigDC;
      if nargout > 2
        varargout{3} = xPos;
        if nargout > 3
          error('Too Many output arguments.')
        end
      end
    end
  end
  % ------------------------------------------------
  
