annotate extra/tsa/inst/detrend.m @ 12580:b6eace8bc216 octave-forge

[tsa] update contact email address
author schloegl
date Thu, 02 Apr 2015 10:00:34 +0000
parents 430712382527
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
1 function [X,T]=detrend(t,X,p)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
2 % DETREND removes the trend from data, NaN's are considered as missing values
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
3 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
4 % DETREND is fully compatible to previous Matlab and Octave DETREND with the following features added:
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
5 % - handles NaN's by assuming that these are missing values
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
6 % - handles unequally spaced data
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
7 % - second output parameter gives the trend of the data
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
8 % - compatible to Matlab and Octave
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
9 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
10 % [...]=detrend([t,] X [,p])
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
11 % removes trend for unequally spaced data
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
12 % t represents the time points
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
13 % X(i) is the value at time t(i)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
14 % p must be a scalar
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
15 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
16 % [...]=detrend(X,0)
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
17 % [...]=detrend(X,'constant')
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
18 % removes the mean
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
19 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
20 % [...]=detrend(X,p)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
21 % removes polynomial of order p (default p=1)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
22 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
23 % [...]=detrend(X,1) - default
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
24 % [...]=detrend(X,'linear')
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
25 % removes linear trend
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
26 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
27 % [X,T]=detrend(...)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
28 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
29 % X is the detrended data
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
30 % T is the removed trend
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
31 %
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
32 % see also: SUMSKIPNAN, ZSCORE
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
33
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
34 % Copyright (C) 1995, 1996 Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
35 % $Id$
12580
b6eace8bc216 [tsa] update contact email address
schloegl
parents: 11507
diff changeset
36 % Copyright (C) 2001,2007 by Alois Schloegl <alois.schloegl@gmail.com>
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
37 % This function is part of the TSA-toolbox
11507
430712382527 TSA: update WWW addresses to new location
schloegl
parents: 4907
diff changeset
38 % http://pub.ist.ac.at/~schloegl/matlab/tsa/
12580
b6eace8bc216 [tsa] update contact email address
schloegl
parents: 11507
diff changeset
39 % Copyright (C) 1997, 1998, 2008 by Alois Schloegl <alois.schloegl@gmail.com>
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
40 %
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
41 % This program is free software: you can redistribute it and/or modify
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
42 % it under the terms of the GNU General Public License as published by
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
43 % the Free Software Foundation, either version 3 of the License, or
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
44 % (at your option) any later version.
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
45 %
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
46 % This program is distributed in the hope that it will be useful,
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
47 % but WITHOUT ANY WARRANTY; without even the implied warranty of
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
48 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
49 % GNU General Public License for more details.
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
50 %
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
51 % You should have received a copy of the GNU General Public License
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
52 % along with this program. If not, see <http://www.gnu.org/licenses/>.
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
53
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
54
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
55 if (nargin == 1)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
56 p = 1;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
57 X = t;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
58 t = [];
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
59 elseif (nargin == 2)
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
60 if strcmpi(X,'constant'),
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
61 p = 0;
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
62 X = t;
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
63 t = [];
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
64 elseif strcmpi(X,'linear'),
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
65 p = 1;
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
66 X = t;
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
67 t = [];
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
68 elseif ischar(X)
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
69 error('unknown 2nd input argument');
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
70 elseif all(size(X)==1),
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
71 p = X;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
72 X = t;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
73 t = [];
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
74 else
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
75 p = 1;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
76 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
77 elseif (nargin == 3)
2881
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
78 if ischar(X),
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
79 warning('input arguments are not supported');
a3a59d4015ab enable LINEAR and CONSTANT as 2nd argument
schloegl
parents: 2414
diff changeset
80 end;
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
81
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
82 elseif (nargin > 3)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
83 fprintf (1,'usage: detrend (x [, p])\n');
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
84 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
85
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
86 % check data, must be in culomn order
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
87 [m, n] = size (X);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
88 if (m == 1)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
89 X = X';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
90 r=n;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
91 else
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
92 r=m;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
93 end
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
94 % check time scale
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
95 if isempty(t),
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
96 t = (1:r).'; % make time scale
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
97 elseif ~all(size(t)==size(X))
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
98 t = t(:);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
99 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
100 % check dimension of t and X
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
101 if ~all(size(X,1)==size(t,1))
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
102 fprintf (2,'detrend: size(t,1) must same as size(x,1) \n');
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
103 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
104 % check the order of the polynomial
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
105 if (~(all(size(p)==1) & (p == round (p)) & (p >= 0)))
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
106 fprintf (2,'detrend: p must be a nonnegative integer\n');
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
107 end
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
108
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
109 if (nargout>1) , % needs more memory
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
110 T = zeros(size(X))+nan;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
111 %T=repmat(nan,size(X)); % not supported by Octave 2.0.16
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
112
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
113
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
114 if (size(t,2)>1), % for multiple time scales
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
115 for k=1:size(X,2),
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
116 idx=find(~isnan(X(:,k)));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
117 b = (t(idx,k) * ones (1, p + 1)) .^ (ones (length(idx),1) * (0 : p));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
118 T(idx,k) = b * (b \ X(idx,k));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
119 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
120
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
121 else % if only one time scale is used
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
122 b = (t * ones (1, p + 1)) .^ (ones (length(t),1) * (0 : p));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
123 for k=1:size(X,2),
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
124 idx=find(~isnan(X(:,k)));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
125 T(idx,k) = b(idx,:) * (b(idx,:) \ X(idx,k));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
126 %X(idx,k) = X(idx,k) - T(idx,k); % 1st alternative implementation
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
127 %X(:,k) = X(:,k) - T(:,k); % 2nd alternative
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
128 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
129 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
130 X = X-T; % 3nd alternative
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
131
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
132 if (m == 1)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
133 X = X';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
134 T = T';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
135 end
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
136 else % needs less memory
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
137 if (size(t,2)>1), % for multiple time scales
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
138 for k = 1:size(X,2),
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
139 idx = find(~isnan(X(:,k)));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
140 b = (t(idx,k) * ones (1, p + 1)) .^ (ones (length(idx),1) * (0 : p));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
141 X(idx,k) = X(idx,k) - b * (b \ X(idx,k));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
142 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
143 else % if only one time scale is used
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
144 b = (t * ones (1, p + 1)) .^ (ones (length(t),1) * (0 : p));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
145 for k = 1:size(X,2),
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
146 idx = find(~isnan(X(:,k)));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
147 X(idx,k) = X(idx,k) - b(idx,:) * (b(idx,:) \ X(idx,k));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
148 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
149 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
150
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
151 if (m == 1)
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
152 X = X';
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
153 end
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
154 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
155
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
156
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
157