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