Home > classes > @ao > detrend.m

detrend

PURPOSE ^

DETREND detrends the input analysis object using a polynomial of degree N.

SYNOPSIS ^

function varargout = detrend(varargin)

DESCRIPTION ^

 DETREND detrends the input analysis object using a polynomial of degree N.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: DETREND detrends the input analysis object using a
              polynomial of degree N.

 CALL:        b = detrend(a1,a2,a3,..., pl)

 INPUTS:      aN  - a list of analysis objects
              pl  - a parameter list

 OUTPUTS:     b - array of analysis objects

              If the last input argument is a parameter list (plist) it is used.
              The following parameters are recognised.

 PARAMETERS:  'N' - degree of polynomial [default: 0]

 M-FILE INFO: Get information about this methods by calling
              >> ao.getInfo('detrend')

              Get information about a specified set-plist by calling:
              >> ao.getInfo('detrend', 'None')

 VERSION:     $Id: detrend.m,v 1.10 2008/09/05 11:05:29 ingo Exp $

 HISTORY:     01-03-2007 M Hewitson
                 Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % DETREND detrends the input analysis object using a polynomial of degree N.
0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %
0004 % DESCRIPTION: DETREND detrends the input analysis object using a
0005 %              polynomial of degree N.
0006 %
0007 % CALL:        b = detrend(a1,a2,a3,..., pl)
0008 %
0009 % INPUTS:      aN  - a list of analysis objects
0010 %              pl  - a parameter list
0011 %
0012 % OUTPUTS:     b - array of analysis objects
0013 %
0014 %              If the last input argument is a parameter list (plist) it is used.
0015 %              The following parameters are recognised.
0016 %
0017 % PARAMETERS:  'N' - degree of polynomial [default: 0]
0018 %
0019 % M-FILE INFO: Get information about this methods by calling
0020 %              >> ao.getInfo('detrend')
0021 %
0022 %              Get information about a specified set-plist by calling:
0023 %              >> ao.getInfo('detrend', 'None')
0024 %
0025 % VERSION:     $Id: detrend.m,v 1.10 2008/09/05 11:05:29 ingo Exp $
0026 %
0027 % HISTORY:     01-03-2007 M Hewitson
0028 %                 Creation
0029 %
0030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0031 
0032 function varargout = detrend(varargin)
0033 
0034   % Check if this is a call for parameters
0035   if utils.helper.isinfocall(varargin{:})
0036     varargout{1} = getInfo(varargin{3});
0037     return
0038   end
0039 
0040   import utils.const.*
0041   utils.helper.msg(msg.MNAME, 'running %s/%s', mfilename('class'), mfilename);
0042 
0043   % Collect input variable names
0044   in_names = cell(size(varargin));
0045   for ii = 1:nargin,in_names{ii} = inputname(ii);end
0046 
0047   % Collect all AOs
0048   [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
0049   pl              = utils.helper.collect_objects(varargin(:), 'plist', in_names);
0050 
0051   % Decide on a deep copy or a modify
0052   bs = copy(as, nargout);
0053 
0054   % Combine plists
0055   pl = combine(pl, getDefaultPlist);
0056 
0057   % Unpack parameter list
0058   N = find(pl, 'N');
0059 
0060   % Loop over analysis objects
0061   for jj=1:numel(bs)
0062     % check data
0063     if ~isa(bs(jj).data, 'tsdata')
0064       warning('!!! This method only works with time-series at the moment. Skipping %s', ao_invars{jj});
0065     else
0066       % detrend with polynomial
0067       try
0068         if find(pl, 'M-FILE ONLY')
0069           bs(jj).data.setY(polydetrend(bs(jj).data.y.', N));
0070         else
0071           bs(jj).data.setY(ltpda_polyreg(bs(jj).data.y, N).');
0072         end
0073       catch
0074         warning('!!! failed to execture ltpda_polyreg.mex. Using m-file call.');
0075         bs(jj).data.setY(polydetrend(bs(jj).data.y.', N));
0076       end
0077       % add history
0078       bs(jj).addHistory(getInfo, pl, ao_invars(jj), bs(jj).hist);
0079       % add name
0080       bs(jj).setName(sprintf('detrend(%s)', bs(jj).name), 'internal');
0081     end
0082   end
0083 
0084   if nargout > 0
0085     varargout{1} = bs;
0086   end
0087 end
0088 
0089 %--------------------------------------------------------------------------
0090 % Get Info Object
0091 %--------------------------------------------------------------------------
0092 function ii = getInfo(varargin)
0093 
0094   if nargin == 1 && strcmpi(varargin{1}, 'None')
0095     sets = {};
0096     pl   = [];
0097   else
0098     sets = {'Default'};
0099     pl   = getDefaultPlist;
0100   end
0101   % Build info object
0102   ii = minfo(mfilename, 'ao', '', utils.const.categories.sigproc, '$Id: detrend.m,v 1.10 2008/09/05 11:05:29 ingo Exp $', sets, pl);
0103 end
0104 
0105 %--------------------------------------------------------------------------
0106 % Get Default Plist
0107 %--------------------------------------------------------------------------
0108 function pl = getDefaultPlist()
0109   pl = plist('N', 0);
0110 end
0111 
0112 function y = polydetrend(varargin)
0113   % POLYDETREND detrends the input data vector with a polynomial.
0114   %
0115   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0116   %
0117   % DESCRIPTION: POLYDETREND detrends the input data vector with a polynomial.
0118   %              This function is meant to be used by other ltpda_ functions.
0119   %              To work with Analysis Objects, use ltpda_polydetrend which calls
0120   %              this function.
0121   %
0122   % CALL:        y = polydetrend(x,order);
0123   %
0124   % INPUTS:      x     - vector of x values
0125   %              order - order of polynomial to fit and subtract
0126   %
0127   % OUTPUTS:     y   - detrended data
0128   %
0129   % VERSION:     $Id: detrend.m,v 1.10 2008/09/05 11:05:29 ingo Exp $
0130   %
0131   % HISTORY:     30-05-07 M Hewitson
0132   %                 Creation
0133   %
0134   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0135 
0136   if nargin == 2
0137     x     = varargin{1};
0138     order = varargin{2};
0139     t     = [1:length(x)]';
0140   elseif nargin == 3
0141     t     = varargin{1};
0142     x     = varargin{2};
0143     order = varargin{3};
0144   else
0145     error('### incorrect inputs.');
0146   end
0147 
0148   % fit polynomial
0149   p = polyfit(t, x, order);
0150 
0151   % make polynomial series
0152   py = polyval(p, t);
0153 
0154   % detrend
0155   y = x - py;
0156 end
0157 % END
0158

Generated on Mon 08-Sep-2008 13:18:47 by m2html © 2003