home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 8.ddi / SIGNAL.DI$ / DETREND.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  854 b   |  31 lines

  1. function y = detrend(x,o)
  2. %DETREND Remove a linear trend from a vector, usually for FFT processing.
  3. %    Y = DETREND(X) removes the best straight-line fit from the data in
  4. %    vector X and returns it in vector Y.  If X is a matrix, DETREND
  5. %    removes the trend from each column of the matrix.
  6. %    Y = DETREND(X,0) removes just the mean value from vector X, or
  7. %    the mean value from each column, if X is a matrix.
  8.  
  9. %    J.N. Little 6-08-86
  10. %    Revised 2-29-88 JNL
  11. %    Copyright (c) 1986-88 by the MathWorks, Inc.
  12.  
  13. if nargin == 1
  14.     o = 1;
  15. end
  16. [m,n] = size(x);
  17. if m == 1    % If a row, turn into column vector
  18.     x = x(:);
  19. end
  20. [mp,np] = size(x);
  21. if o == 0    % Remove just mean from each column
  22.     y = x - ones(mp,1)*mean(x);
  23. else        % Remove straight-line fit from each column
  24.     a = [(1:mp)'/mp ones(mp,1)];
  25.     y = x - a*(a\x);
  26. end
  27. if m == 1
  28.     y = y.';
  29. end
  30.  
  31.