2539
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
2539
|
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. |
3426
|
7 ## |
2539
|
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 |
3426
|
11 ## General Public License for more details. |
|
12 ## |
2539
|
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 |
3381
|
17 ## -*- texinfo -*- |
3367
|
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}. |
3426
|
21 ## |
3367
|
22 ## If @var{x} is a matrix, @code{detrend (@var{x}, @var{p})} does the same |
|
23 ## for each column in @var{x}. |
3426
|
24 ## |
3367
|
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 |
2539
|
28 |
|
29 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
30 ## Created: 11 October 1994 |
|
31 ## Adapted-By: jwe |
3426
|
32 |
2539
|
33 function y = detrend (x, p) |
3426
|
34 |
2539
|
35 if (nargin == 1) |
|
36 p = 1; |
|
37 elseif (nargin == 2) |
|
38 if (! (is_scalar (p) && p == round (p) && p >= 0)) |
3457
|
39 error ("detrend: p must be a nonnegative integer"); |
2539
|
40 endif |
|
41 else |
3449
|
42 usage ("detrend (x, p)"); |
2539
|
43 endif |
3426
|
44 |
2539
|
45 [m, n] = size (x); |
|
46 if (m == 1) |
|
47 x = x'; |
|
48 endif |
3426
|
49 |
2539
|
50 r = rows (x); |
|
51 b = ((1 : r)' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p)); |
|
52 y = x - b * (b \ x); |
3426
|
53 |
2539
|
54 if (m == 1) |
|
55 y = y'; |
|
56 endif |
3426
|
57 |
2539
|
58 endfunction |