home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / signal / detrend.m < prev    next >
Text File  |  1999-12-15  |  2KB  |  59 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## -*- texinfo -*-
  18. ## @deftypefn {Function File} {} detrend (@var{x}, @var{p})
  19. ## If @var{x} is a vector, @code{detrend (@var{x}, @var{p})} removes the
  20. ## best fit of a polynomial of order @var{p} from the data @var{x}.
  21. ## 
  22. ## If @var{x} is a matrix, @code{detrend (@var{x}, @var{p})} does the same
  23. ## for each column in @var{x}.
  24. ## 
  25. ## The second argument is optional.  If it is not specified, a value of 1
  26. ## is assumed.  This corresponds to removing a linear trend.
  27. ## @end deftypefn
  28.  
  29. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  30. ## Created: 11 October 1994
  31. ## Adapted-By: jwe
  32.   
  33. function y = detrend (x, p)
  34.   
  35.   if (nargin == 1)
  36.     p = 1;
  37.   elseif (nargin == 2)
  38.     if (! (is_scalar (p) && p == round (p) && p >= 0))
  39.       error ("detrend:  p must be a nonnegative integer");
  40.     endif
  41.   else
  42.     usage ("detrend (x [, p])");
  43.   endif
  44.   
  45.   [m, n] = size (x);
  46.   if (m == 1)
  47.     x = x';
  48.   endif
  49.   
  50.   r = rows (x);
  51.   b = ((1 : r)' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p));
  52.   y = x - b * (b \ x);
  53.   
  54.   if (m == 1)
  55.     y = y';
  56.   endif
  57.   
  58. endfunction
  59.