% UPSAMPLE overloads upsample function for AOs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: UPSAMPLE AOs containing time-series data. All other AOs with
%              no time-series data are skipped but appear in the output.
%
%              A signal at sample rate fs is upsampled by inserting N-1 zeros between the
%              input samples.
%
% CALL:        b = upsample(a, pl)
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'upsample')">Parameters Description</a>
%
% VERSION:     $Id: upsample.m,v 1.29 2011/07/14 05:33:18 mauro Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

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

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


  % Get output sample rate
  Nup = find(pl, 'N');
  if isempty(Nup)
    error('### Please give a plist with a parameter ''N''.');
  end
  % Get initial phase
  phase = find(pl, 'phase');
  if isempty(phase)
    phase = 0;
  end

  % Loop over AOs
  for jj = 1:numel(bs)
    if ~isa(bs(jj).data, 'tsdata')
      warning('!!! Upsample only works on tsdata objects. Skipping AO %s', ao_invars{jj});
    else
      % upsample y
      bs(jj).data.setY(upsample(bs(jj).data.y, floor(Nup), phase));
      % Correct fs
      bs(jj).data.setFs(floor(Nup)*bs(jj).data.fs);
      % Set name
      bs(jj).name = sprintf('upsample(%s)', ao_invars{jj});
      % Add history
      bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist);
      % clear errors
      bs(jj).clearErrors;
    end
  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: upsample.m,v 1.29 2011/07/14 05:33:18 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 pl = buildplist()
  
  pl = plist();
  
  % N
  p = param({'N', 'The upsample factor.'}, paramValue.DOUBLE_VALUE(1));
  pl.append(p);
  
  % phase
  p = param({'phase', 'The initial phase [0, N-1].'}, paramValue.DOUBLE_VALUE(0));
  pl.append(p);
  
end
