Global Index (short | long) | Local contents | Local Index (short | long)
y = detrend(x,o,bp)
DETREND Remove a linear trend from a vector, usually for FFT processing. Y = DETREND(X) removes the best straight-line fit linear trend from the data in vector X and returns it in vector Y. If X is a matrix, DETREND removes the trend from each column of the matrix. Y = DETREND(X,'constant') removes just the mean value from the vector X, or the mean value from each column, if X is a matrix. Y = DETREND(X,'linear',BP) removes a continuous, piecewise linear trend. Breakpoint indices for the linear trend are contained in the vector BP. The default is no breakpoints, such that one single straight line is removed from each column of X. See also MEAN
function y = detrend(x,o,bp) % Author(s): J.N. Little, 6-08-86 % J.N. Little, 2-29-88, revised % G. Wolodkin, 2-02-98, added piecewise linear fit of L. Ljung % Copyright (c) 1984-98 by The MathWorks, Inc. % $Revision: 1.4 $ $Date: 1998/06/08 21:34:05 $ if nargin < 2, o = 1; end if nargin < 3, bp = 0; end % Reshape x if necessary, assuming the dimension to be % detrended is the first szx = size(x); ndimx = length(szx); if ndimx > 2; x = reshape(x, szx(1), prod(szx(2:ndimx))); end n = size(x,1); if n == 1, x = x(:); % If a row, turn into column vector end N = size(x,1); switch o case {0,'c','constant'} y = x - ones(N,1)*mean(x); % Remove just mean from each column case {1,'l','linear'} bp = unique([0;bp(:);N-1]); % Include both endpoints lb = length(bp)-1; a = [zeros(N,lb) ones(N,1)]; % Build regressor with linear pieces + DC for kb = 1:lb M = N - bp(kb); a([1:M]+bp(kb),kb) = [1:M]'/M; end y = x - a*(a\x); % Remove best fit otherwise % This should eventually become an error. warning(['Invalid trend type ''' num2str(o) '''.. assuming ''linear''.']); y = detrend(x,1,bp); end if n == 1 y = y.'; end % Reshape output so it is the same dimension as input if ndimx > 2; y = reshape(y, szx); end