2539
|
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 |
3367
|
17 ## @deftypefn {Function File} {} detrend (@var{x}, @var{p}) |
|
18 ## If @var{x} is a vector, @code{detrend (@var{x}, @var{p})} removes the |
|
19 ## best fit of a polynomial of order @var{p} from the data @var{x}. |
|
20 ## |
|
21 ## If @var{x} is a matrix, @code{detrend (@var{x}, @var{p})} does the same |
|
22 ## for each column in @var{x}. |
|
23 ## |
|
24 ## The second argument is optional. If it is not specified, a value of 1 |
|
25 ## is assumed. This corresponds to removing a linear trend. |
|
26 ## @end deftypefn |
2539
|
27 |
|
28 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
29 ## Created: 11 October 1994 |
|
30 ## Adapted-By: jwe |
|
31 |
|
32 function y = detrend (x, p) |
|
33 |
|
34 if (nargin == 1) |
|
35 p = 1; |
|
36 elseif (nargin == 2) |
|
37 if (! (is_scalar (p) && p == round (p) && p >= 0)) |
|
38 error ("detrend: p must be a nonnegative integer"); |
|
39 endif |
|
40 else |
|
41 usage ("detrend (x [, p])"); |
|
42 endif |
|
43 |
|
44 [m, n] = size (x); |
|
45 if (m == 1) |
|
46 x = x'; |
|
47 endif |
|
48 |
|
49 r = rows (x); |
|
50 b = ((1 : r)' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p)); |
|
51 y = x - b * (b \ x); |
|
52 |
|
53 if (m == 1) |
|
54 y = y'; |
|
55 endif |
|
56 |
|
57 endfunction |