% Sweep MICH (signal recycling cavity length) in DRFPMI and generate plots.
%
% [xPos, sigDC, fDC] = sweepMICH(Nsweep)
% see sweepDC for information about the parameters
%
% This displays some DC signals, and some demod signals, during
% a MICH sweep.  From this sweep, one might try to find a good
% signal for MICH.

function [xPos, sigDC, fDC] = sweepMICH(varargin)

  % prepare some variables
  sweepSetup

  if nargin < 1
    Nsweep = 200;
  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)];

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % MICH sweep
  pos = zeros(opt.Noptic, 1);
  pos(nPR) = -2e-8;
  pos(nBS) = -sqrt(2) * pos(nPR);
  
  [xPos, sigDC, fDC] = sweepDC(opt, -pos, pos, Nsweep);

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

  subplot(2, 1, 1)
  semilogy(xPos(nBS, :), abs(fDCp(:, :, n)))
  title('MICH sweep: RF component powers in the PRC')
  legend(num2str(vFrf / 1e6))
  grid on
  axis([-pos(nBS) pos(nBS) .1 100])

  subplot(2, 1, 2)
  plot(xPos(nBS, :), sigDC(nREFL(2:end), :))
  legend(prbNames(nREFL(2:end)))
  title('MICH sweep: REFL demod signals')
  grid on
  axis([-pos(nBS) pos(nBS) -.3 .3])
