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