Mercurial > hg > octave-nkf
annotate scripts/signal/detrend.m @ 19840:c5270263d466 gui-release
close gui-release branch
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 30 Jan 2015 17:41:50 -0500 |
parents | d63878346099 |
children | 4197fc428c7d |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17281
diff
changeset
|
1 ## Copyright (C) 1995-2013 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) 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 |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2539 | 18 |
3381 | 19 ## -*- texinfo -*- |
3367 | 20 ## @deftypefn {Function File} {} detrend (@var{x}, @var{p}) |
21 ## If @var{x} is a vector, @code{detrend (@var{x}, @var{p})} removes the | |
22 ## best fit of a polynomial of order @var{p} from the data @var{x}. | |
3426 | 23 ## |
3367 | 24 ## If @var{x} is a matrix, @code{detrend (@var{x}, @var{p})} does the same |
25 ## for each column in @var{x}. | |
3426 | 26 ## |
3367 | 27 ## The second argument is optional. If it is not specified, a value of 1 |
28 ## 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 |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
31 ## @var{p} must be either @qcode{"constant"} (corresponds to |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
32 ## @code{@var{p}=0}) or |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
33 ## @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
|
34 ## @seealso{polyfit} |
3367 | 35 ## @end deftypefn |
2539 | 36 |
5428 | 37 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539 | 38 ## Created: 11 October 1994 |
39 ## Adapted-By: jwe | |
3426 | 40 |
11409
dd539a976451
signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents:
7279
diff
changeset
|
41 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
|
42 ## Check input |
dd539a976451
signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents:
7279
diff
changeset
|
43 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
|
44 ## Check p |
dd539a976451
signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents:
7279
diff
changeset
|
45 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
|
46 p = 0; |
dd539a976451
signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents:
7279
diff
changeset
|
47 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
|
48 p = 1; |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
49 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
|
50 error ("detrend: second input argument must be 'constant', 'linear' or a positive integer"); |
2539 | 51 endif |
52 else | |
11409
dd539a976451
signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents:
7279
diff
changeset
|
53 error ("detrend: first input argument must be a real vector or matrix"); |
2539 | 54 endif |
3426 | 55 |
2539 | 56 [m, n] = size (x); |
57 if (m == 1) | |
58 x = x'; | |
59 endif | |
3426 | 60 |
2539 | 61 r = rows (x); |
62 b = ((1 : r)' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p)); | |
63 y = x - b * (b \ x); | |
3426 | 64 |
2539 | 65 if (m == 1) |
66 y = y'; | |
67 endif | |
3426 | 68 |
2539 | 69 endfunction |
7279 | 70 |
71 | |
72 %!test | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 %! N = 32; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %! 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
|
75 %! y = detrend (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
76 %! 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
|
77 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 %!test |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
79 %! N = 32; |
7279 | 80 %! t = (0:1:N-1)/N; |
81 %! 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
|
82 %! y = detrend (x,2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 %! assert (abs (y(:)) < 30*eps); |
7279 | 84 |
85 %!test | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
86 %! N = 32; |
7279 | 87 %! t = (0:1:N-1)/N; |
88 %! 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
|
89 %! y = detrend (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %! assert (abs (y(:)) < 20*eps); |
7279 | 91 |