Home > FMAToolbox > General > Detrend.m

Detrend

PURPOSE ^

Detrend - Detrend signal.

SYNOPSIS ^

function [detrended,model] = Detrend(samples,varargin)

DESCRIPTION ^

Detrend - Detrend signal.

  USAGE

    [detrended,model] = Detrend(samples,<options>)

    samples        <a href="matlab:help samples">samples</a> to detrend
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'model'       use a predefined model (e.g. from a previous iteration)
     'window'      size of the regression window (in number of samples)
     'common'      'on' to use a single model for all windows (default), 'off'
                   to compute a different model for each window
     'order'       AR model order (default = 1)
    =========================================================================

  OUTPUT

    detrended      detrended signal
    model          AR model (can be reused for other signals)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 %Detrend - Detrend signal.
0002 %
0003 %  USAGE
0004 %
0005 %    [detrended,model] = Detrend(samples,<options>)
0006 %
0007 %    samples        <a href="matlab:help samples">samples</a> to detrend
0008 %    <options>      optional list of property-value pairs (see table below)
0009 %
0010 %    =========================================================================
0011 %     Properties    Values
0012 %    -------------------------------------------------------------------------
0013 %     'model'       use a predefined model (e.g. from a previous iteration)
0014 %     'window'      size of the regression window (in number of samples)
0015 %     'common'      'on' to use a single model for all windows (default), 'off'
0016 %                   to compute a different model for each window
0017 %     'order'       AR model order (default = 1)
0018 %    =========================================================================
0019 %
0020 %  OUTPUT
0021 %
0022 %    detrended      detrended signal
0023 %    model          AR model (can be reused for other signals)
0024 
0025 % Copyright (C) 2012 by Anton Sirota, adapted by Michaël Zugaro
0026 %
0027 % This program is free software; you can redistribute it and/or modify
0028 % it under the terms of the GNU General Public License as published by
0029 % the Free Software Foundation; either version 3 of the License, or
0030 % (at your option) any later version.
0031 
0032 function [detrended,model] = Detrend(samples,varargin)
0033 
0034 % Defaults
0035 window = [];
0036 model = [];
0037 order = 1;
0038 common = 'on';
0039 
0040 % Check number of parameters
0041 if nargin < 1,
0042   error('Incorrect number of parameters (type ''help <a href="matlab:help Detrend">Detrend</a>'' for details).');
0043 end
0044 
0045 % Check parameter sizes
0046 if ~isdmatrix(samples),
0047     error('Samples should be a MxN matrix (type ''help <a href="matlab:help Detrend">Detrend</a>'' for details).');
0048 end
0049 
0050 % Parse parameter list
0051 for i = 1:2:length(varargin),
0052     if ~ischar(varargin{i}),
0053         error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help Detrend">Detrend</a>'' for details).']);
0054     end
0055     switch(lower(varargin{i})),
0056         case 'window',
0057             window = varargin{i+1};
0058             if ~isdscalar(window,'>0'),
0059                 error('Incorrect value for property ''window'' (type ''help <a href="matlab:help Detrend">Detrend</a>'' for details).');
0060             end
0061         case 'model',
0062             model = varargin{i+1};
0063             if ~isdvector(model),
0064                 error('Incorrect value for property ''model'' (type ''help <a href="matlab:help Detrend">Detrend</a>'' for details).');
0065             end
0066         case 'order',
0067             order = varargin{i+1};
0068             if ~isiscalar(order,'>0'),
0069                 error('Incorrect value for property ''order'' (type ''help <a href="matlab:help Detrend">Detrend</a>'' for details).');
0070             end
0071         case 'common',
0072             common = varargin{i+1};
0073             if ~isastring(common,'on','off'),
0074                 error('Incorrect value for property ''common'' (type ''help <a href="matlab:help Detrend">Detrend</a>'' for details).');
0075             end
0076         otherwise,
0077             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help Detrend">Detrend</a>'' for details).']);
0078     end
0079 end
0080 %  order = order + 1;
0081 
0082 x = samples(:,2:end);
0083 [nSamples,nChannels] = size(x);
0084 
0085 y = zeros(nSamples,nChannels);
0086 % List data windows
0087 if isempty(window),
0088     windows = [1 nSamples];
0089     nWindows = 1;
0090 else
0091     nWindows = floor(nSamples/window)+1;
0092     windows = repmat([1 window],nWindows,1)+repmat([0:nWindows-1]'*window,1,2);
0093     if nWindows*window > nSamples,
0094         windows(end,2) = nSamples;
0095     end
0096 end
0097 
0098 % Process
0099 if ~isempty(model),
0100     % Model provided as input parameter
0101     for j = 1:nWindows,
0102         for i = 1:nChannels,
0103             y(windows(j,1):windows(j,2),i) = Filter0(model,x(windows(j,1):windows(j,2),i));
0104         end
0105     end
0106 elseif strcmp(common,'on'),
0107     % Use the same model for all windows
0108     model = arburg(x(windows(1,1):windows(1,2),1),order);
0109     for j = 1:nWindows,
0110         for i = 1:nChannels,
0111             y(windows(j,1):windows(j,2),i) = Filter0(model,x(windows(j,1):windows(j,2),i));
0112         end
0113     end
0114 else
0115     % Determine model for each window
0116     for j = 1:nWindows,
0117         for i=1:nChannels,
0118             model = arburg(x(windows(j,1):windows(j,2),i),order);
0119             y(windows(j,1):windows(j,2),i) = Filter0(model,x(windows(j,1):windows(j,2),i));
0120         end
0121     end
0122 end
0123 
0124 detrended = [samples(:,1) y];
0125 
0126 % ------------------------------- Helper function -------------------------------
0127 
0128 function y = Filter0(b,x)
0129 
0130 if mod(length(b),2) ~= 1,
0131     shift = length(b)/2;
0132 else
0133     shift = (length(b)-1)/2;
0134 end
0135 
0136 [y0,z] = filter(b,1,x);
0137 
0138 y = [y0(shift+1:end,:) ; z(1:shift,:)];
0139

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005