% CONV vector convolution.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: CONV vector convolution.
%
% CALL:        >> c = conv(a,b)
%
% INPUTS:      pl   - a parameter list
%              a,b  - input analysis object
%
% OUTPUTS:
%              c    - output analysis object containing the filtered data.
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'conv')">Parameters Description</a>
%
% VERSION:     $Id: conv.m,v 1.18 2011/04/08 08:56:16 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = conv(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 and plists
  [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);

  if nargout == 0
    error('### conv cannot be used as a modifier. Please give an output variable.');
  end

  if numel(as) < 2
    error('### conv requires at least two input AOs to work.');
  end

  % Convolute the data
  name = ao_invars{1};
  desc = as(1).description;
  res = as(1).y;
  plotinfo = as(1).plotinfo;
  for j=2:numel(as)
    res = conv(res, as(j).y);
    name = sprintf('%s,%s', name, ao_invars{j});
    desc = strcat(desc, [', ' as(j).description]);
    if ~isempty(as(j).plotinfo)
      plotinfo = combine(plotinfo, as(j).plotinfo);
    end
  end

  % Make new AO
  bs = ao(res);
  % name for this object
  bs.name = sprintf('conv(%s)', name);
  % set all descriptions
  bs.description = desc;
  % keep 'plotinfo'
  bs.plotinfo = plotinfo;
  % add history
  bs.addHistory(getInfo('None'), getDefaultPlist, ao_invars, [as(:).hist]);

  % 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: conv.m,v 1.18 2011/04/08 08:56:16 hewitson Exp $', sets, pls);
  ii.setModifier(false);
  ii.setArgsmin(2);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------

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

function pl_default = buildplist()
  pl_default = plist.EMPTY_PLIST;
end

