annotate scripts/signal/detrend.m @ 20354:227d582fa300 stable

build: Sort generated PKG_ADD contents consistently * libinterp/mk-pkg-add: Set LC_COLLATE=C when sorting for consistent output.
author Mike Miller <mtmiller@octave.org>
date Fri, 10 Jul 2015 01:10:30 -0400
parents f1d0f506ee78
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 1995-2015 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
14 ##
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
18
3381
69b167451491 [project @ 1999-12-15 20:48:10 by jwe]
jwe
parents: 3367
diff changeset
19 ## -*- texinfo -*-
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2539
diff changeset
20 ## @deftypefn {Function File} {} detrend (@var{x}, @var{p})
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2539
diff changeset
21 ## If @var{x} is a vector, @code{detrend (@var{x}, @var{p})} removes the
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2539
diff changeset
22 ## best fit of a polynomial of order @var{p} from the data @var{x}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
23 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2539
diff changeset
24 ## If @var{x} is a matrix, @code{detrend (@var{x}, @var{p})} does the same
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2539
diff changeset
25 ## for each column in @var{x}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
26 ##
20200
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
27 ## The second argument @var{p} is optional. If it is not specified, a value of
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
28 ## 1 is assumed. This corresponds to removing a linear trend.
11409
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
29 ##
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
30 ## The order of the polynomial can also be given as a string, in which case
20200
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
31 ## @var{p} must be either @qcode{"constant"} (corresponds to @code{@var{p}=0})
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
32 ## or @qcode{"linear"} (corresponds to @code{@var{p}=1}).
11409
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
33 ## @seealso{polyfit}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 2539
diff changeset
34 ## @end deftypefn
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
35
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
36 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
37 ## Created: 11 October 1994
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
38 ## Adapted-By: jwe
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
39
11409
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
40 function y = detrend (x, p = 1)
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
41 ## Check input
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
42 if (nargin > 0 && isreal (x) && ndims (x) <= 2)
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
43 ## Check p
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
44 if (ischar (p) && strcmpi (p, "constant"))
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
45 p = 0;
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
46 elseif (ischar (p) && strcmpi (p, "linear"))
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
47 p = 1;
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
48 elseif (! isscalar (p) || p < 0 || p != fix (p))
11409
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
49 error ("detrend: second input argument must be 'constant', 'linear' or a positive integer");
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
50 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
51 else
11409
dd539a976451 signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents: 7279
diff changeset
52 error ("detrend: first input argument must be a real vector or matrix");
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
53 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
54
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
55 [m, n] = size (x);
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
56 if (m == 1)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
57 x = x';
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
58 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
59
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
60 r = rows (x);
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
61 b = ((1 : r)' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p));
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
62 y = x - b * (b \ x);
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
63
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
64 if (m == 1)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
65 y = y';
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
66 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3381
diff changeset
67
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
68 endfunction
7279
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
69
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
70
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
71 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
72 %! N = 32;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
73 %! x = (0:1:N-1)/N + 2;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
74 %! y = detrend (x);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
75 %! assert (abs (y(:)) < 20*eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
76
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
77 %!test
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
78 %! N = 32;
7279
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
79 %! t = (0:1:N-1)/N;
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
80 %! x = t .* t + 2;
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
81 %! y = detrend (x,2);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
82 %! assert (abs (y(:)) < 30*eps);
7279
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
83
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
84 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
85 %! N = 32;
7279
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
86 %! t = (0:1:N-1)/N;
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
87 %! x = [t;4*t-3]';
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
88 %! y = detrend (x);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
89 %! assert (abs (y(:)) < 20*eps);
7279
72b5e1701da2 [project @ 2007-12-10 21:01:48 by jwe]
jwe
parents: 7017
diff changeset
90