% RESAMPLE overloads resample function for AOs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: RESAMPLE overloads resample function for AOs.
%
% CALL:        bs = resample(a1,a2,a3,...,pl)
%              bs = resample(as,pl)
%              bs = as.resample(pl)
%
% INPUTS:      aN   - input analysis objects
%              as   - input analysis objects array
%              pl   - input parameter list
%
% OUTPUTS:     bs   - array of analysis objects, one for each input
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'resample')">Parameters Description</a>
%
% VERSION:    $Id: resample.m,v 1.50 2011/07/14 05:33:18 mauro Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = resample(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);
  
  if isempty(pl)
    error('### Please give a plist with a parameter ''fsout''.');
  end
  
  % Get output sample rate
  fsout = find(pl, 'fsout');
  if isempty(fsout)
    error('### Please give a plist with a parameter ''fsout''.');
  end
  
  % Get a filter if specified
  filt = find(pl, 'filter');
  if ~isempty(filt) && ~isa(filt, 'mfir')
    error('The filter specified must be an mfir filter object (FIR filter).');
  end
  
  % Loop over AOs
  for jj = 1:numel(bs)
    if ~isa(bs(jj).data, 'tsdata')
      warning('!!! Skipping non-tsdata AO: %s', ao_invars{jj});
    else
      % Compute the resampling factors
      [P,Q] = utils.math.intfact(fsout,bs(jj).data.fs);
      utils.helper.msg(msg.PROC1, 'resampling by %g/%g', P, Q);
      
      % Check we have an evenly sampled data series
      if ~bs(jj).data.evenly()
        error('### The AO %s is unevenly sampled. It can not be resampled this way.', ao_invars{jj});
      end
      % resample y
      if isempty(filt)
        bs(jj).data.setY(resample(bs(jj).data.getY, P, Q));
      else
        [p,q] = rat( fsout/bs(jj).fs, 1e-12 );
        b = p*filt.a;
        bs(jj).data.setY(resample(bs(jj).data.getY, P, Q, b));
      end
      % Set new sample rate
      bs(jj).data.setFs(fsout);
      % Set output AO name
      bs(jj).name = sprintf('resample(%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: resample.m,v 1.50 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();
  
  % Type
  p = param({'fsout',['The desired output frequency<br>'...
    '(must be positive and integer).']}, paramValue.EMPTY_DOUBLE);
  pl.append(p);
  
  p = param({'filter', 'The filter to apply in the resampling process.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
end
% END

