% DFT computes the DFT of the input time-series at the requested frequencies.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: DFT computes the DFT of the input time-series at the requested
%              frequencies.
%
% CALL:        b = dft(a, pl)
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'dft')">Parameters Description</a>
%
% VERSION:     $Id: dft.m,v 1.29 2011/04/08 08:56:13 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = dft(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);
  [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names);

  % Make copies or handles to inputs
  bs = copy(as, nargout);

  % Combine plists
  pl = parse(pl, getDefaultPlist);

  % Loop over input AOs
  for j=1:numel(bs)
    if ~isa(bs(j).data, 'tsdata')
      warning('!!! The DFT can only be computed on input time-series. Skipping AO %s', ao_invars{j});
    else

      % Extract necessary parameters
      f = find(pl, 'f');
      if isa(f, 'ao') && (isa(f.data, 'fsdata') || isa(f.data, 'xydata'))
        f = f.data.getX;
      end

      % Compute f if necessary
      if f == -1
        f = linspace(0, bs(j).data.fs/2, length(bs(j).data.getY)/2+1);
      end

      % Compute DFT
      fs  = bs(j).data.fs;
      N   = length(bs(j).data.getY);
      J   = -2*pi*1i.*[0:N-1]/fs;
      dft = zeros(size(f));
      for kk = 1:length(f)
        dft(kk) = exp(f(kk)*J)*bs(j).data.getY;
      end

      % Make output fsdata AO
      yunits = bs(j).data.yunits;
      % Keep the data shape of the input AO
      if size(bs(j).data.y,1) ~= 1
        f   = f.';
        dft = dft.';
      end
      bs(j).data = fsdata(f, dft, bs(j).data.fs);
      bs(j).data.setXunits('Hz');
      bs(j).data.setYunits(yunits./unit('Hz'));
      % Set name
      bs(j).name = sprintf('dft(%s)', ao_invars{j});
      % Add history
      bs(j).addHistory(getInfo('None'), pl, ao_invars(j), bs(j).hist);
      % Clear the errors since they don't make sense anymore
      clearErrors(bs(j));
    end
  end
  

  % Set output
  if nargout == numel(bs)
    % List of outputs
    for ii = 1:numel(bs)
      varargout{ii} = bs(ii);
    end
  else
    % Single output
    varargout{1} = bs;
  end
end

%--------------------------------------------------------------------------
% 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, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: dft.m,v 1.29 2011/04/08 08:56:13 hewitson Exp $', sets, pls);
end

%--------------------------------------------------------------------------
% 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({'f', ['The vector of frequencies at which to compute the DFT <br>or an AO ' ...
                    'where the x-axis is taken for the frequency values.']}, paramValue.DOUBLE_VALUE(-1));
end


