% FFT overloads the fft method for Analysis objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: FFT overloads the fft operator for Analysis objects.
%
% CALL:        b = fft(a, pl)
%
% PARAMETERS:  'type'  - plain, one or two sided fft [default: 'plain']
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'fft')">Parameters Description</a>
%
% VERSION:     $Id: fft.m,v 1.50 2011/04/28 21:31:31 mauro Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = fft(varargin)

  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end

  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);

  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end

  % Collect all AOs
  [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);

  % Decide on a deep copy or a modify
  bs = copy(as, nargout);

  % Apply defaults to plist
  pl = applyDefaults(getDefaultPlist, varargin{:});

  % two-sided or one?
  type = find(pl, 'type');
  
  % scale?
  scale = utils.prog.yes2true(find(pl, 'scale'));
  
  % Check input analysis object
  for jj = 1:numel(bs)
    % call core method of the fft
    bs(jj).fft_core(type);
    % scale if desired
    if scale
        bs(jj) = bs(jj)./ao(plist('vals',as(jj).fs,'yunits','Hz'));
    end
    % set name
    bs(jj).name = sprintf('fft(%s)', ao_invars{jj});
    % Add history
    bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist);
  end

  % Set output
  varargout = utils.helper.setoutputs(nargout, bs);
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist();
  end
  % Build info object
  ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: fft.m,v 1.50 2011/04/28 21:31:31 mauro Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function plout = getDefaultPlist()
  persistent pl;  
  if ~exist('pl', 'var') || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function plo = buildplist()
  plo = plist();

  % FFT Type
  p = plist({'type', 'The fft type. Plain (complete non-symmetric), One-sided (from zero to Nyquist) or two-sided (complete symmetric).'}, {2, {'plain', 'one', 'two'}, paramValue.SINGLE});
  plo.append(p);
  
  % Scale by sample rate?
  p = param({'scale',['set to ''true'' to scale FFT by sampling rate to match '...
      'amplitude in continuous domain. Only applicable to time-series AOs.']},...
      paramValue.FALSE_TRUE);  
  
  plo.append(p);

end
