% RMS Calculate RMS deviation from spectrum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: RMS Calculate RMS deviation from spectrum
%
% CALL:        b=rms(a)
%
% INPUTS:      a  - input analysis object containing spectrum
%
% OUTPUTS:     b  - analysis object containing RMS deviation
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'rms')">Parameters Description</a>
%
% VERSION:     $Id: rms.m,v 1.22 2011/04/08 08:56:11 hewitson Exp $
%
% NOTE:        Taken from code by: 1998.05.25      Masaki Ando
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = rms(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);

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

  % Loop over input AOs
  for j=1:numel(bs)
    % check input data
    if isa(bs(j).data, 'fsdata')
      % get data
      f = bs(j).data.getX;
      spe = [f bs(j).data.getY];
      % start and end frequencies
      s = f(1);
      e = f(end);
      % compute integrated rms
      l1=spe(:,1)>=s;
      sp=spe(l1,:);
      l2=sp(:,1)<=e;
      sp=sp(l2,:);
      si=size(sp);
      li=si(1,1);
      freq=sp(:,1);
      sp2=sp(:,2).^2;
      ms=sp2;
      for i= li-1 :-1: 1
        ms(i)=ms(i+1)+(sp2(i+1)+sp2(i))*(freq(i+1)-freq(i))/2;
      end
      % set data
      bs(j).data.setXY(freq, sqrt(ms));
      % set name
      bs(j).name = sprintf('RMS(%s)', ao_invars{j});
      % Add history
      bs(j).addHistory(getInfo('None'), plist, ao_invars(j), bs(j).hist);
      % clear errors
      bs(j).clearErrors;
    else
      warning('!!! Skipping AO %s - it''s not an frequency series.', ao_invars{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 = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: rms.m,v 1.22 2011/04/08 08:56:11 hewitson Exp $', sets, pl);
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.EMPTY_PLIST;
end
% END

