% FFT implements the fft operator for matrix objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: FFT implements the fft operator for matrix objects.
%
% CALL:        out = fft(in,pl);
%
% INPUTS:      in      -  input matrix objects
%              pl      -  parameter list
%
% OUTPUTS:     out     -  output matrix objects
%
% <a href="matlab:utils.helper.displayMethodInfo('matrix', 'fft')">Parameters Description</a>
%
% VERSION:     $Id: fft.m,v 1.6 2011/04/08 08:56:31 hewitson 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
  
  if nargout == 0
    error('### Matrix fft operator can not be used as a modifier.');
  end
  
  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin
    in_names{ii} = inputname(ii);
  end
  
  % Collect all smodels and plists
  [ms, matrix_invars, rest] = utils.helper.collect_objects(varargin(:), 'matrix', in_names);
  [pl, pl_invars, rest]     = utils.helper.collect_objects(varargin(:), 'plist', in_names);
  
  % Merge with default plist
  pl = parse(pl, getDefaultPlist);
  
  % deep copy
  mat = copy(ms,1);
  
  n = numel(mat);
  
  % loop over number of matrices
  for ii = 1:n
    % get size of object
    [rw,cl] = size(mat(ii).objs);
    
    % loop over raws and columns
    for kk = 1:rw
      for jj = 1:cl
        mat(ii).objs(kk,jj) = fft(mat(ii).objs(kk,jj),pl);
      end
    end
    mat(ii).addHistory(getInfo('None'), [], {inputname(1)}, [mat(ii).hist]);
  end
  
  varargout{1} = mat;
  
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Local Functions                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getInfo
%
% DESCRIPTION: Get Info Object
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function ii = getInfo(varargin)
  
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pls  = [];
  else
    sets = {'Default'};
    pls  = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'matrix', 'ltpda', utils.const.categories.aop, '$Id: fft.m,v 1.6 2011/04/08 08:56:31 hewitson Exp $', sets, pls);
  ii.setArgsmin(2);
  ii.setModifier(false);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getDefaultPlist
%
% DESCRIPTION: Get Default Plist
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = 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});
end
