% measure_bunch_tunes - Acquire multiple measurements, average, fit
%
% Copyright (c) 2009 Dimtel, Inc., All Rights Reserved
%
% $Id: proc_bunch_tunes.m,v 1.1.1.1 2010/10/05 16:43:01 dim Exp $
% $Date: 2010/10/05 16:43:01 $
% $Author: dim $
% $Revision: 1.1.1.1 $
% $Log: proc_bunch_tunes.m,v $
% Revision 1.1.1.1  2010/10/05 16:43:01  dim
% Initial import of iGp12
%
%

nb = input('Bunches to process [1:2:89]: ');
if (isempty(nb))
  nb    = 1:2:89;
end
Ib = input('Beam current, mA: ');

% FFT length - very sensitive
Ns    = 2048;

% Perform the averaging
tune_avg;

% Feedback delay in turns - zero seems to work best
delay = 0;

% Median filtering
resp = input('Do you want median filtering [n]: ', 's');
if (~isempty(resp) && lower(resp(1)) == 'y')
  for k=1:n
    S1(:,k) = medianFilter(S(:,k), 3);
  end
  S = S1;
end

while (1)
  % Plot one spectrum, ask for limits
  clf;
  plot(prm.freq/1e3, 10*log10(S(:,1)));
  xlabel('Frequency (kHz)');
  ylabel('Power spectral density (dB)');

  f1 = input('Lower fitting limit, kHz: ')*1e3;
  f2 = input('Upper fitting limit, kHz: ')*1e3;

  fit_notch1(S(:,1), prm, f1, f2, delay);
  resp = input('Is this acceptable [y]: ', 's');
  if (isempty(resp) || lower(resp(1)) == 'y')
    break;
  end
end

% Simple tune plots, without fitting
tune_plots;

resp = input('Perform fitting [y]: ', 's');
if (isempty(resp) || lower(resp(1)) == 'y')
  % Actual fitting
  tune_fit;
end
