Mercurial > jwe > octave
annotate scripts/signal/detrend.m @ 26814:be5b43255a38 jwe-cdef-package
detrend.m: support complex arrays, overhaul input validation (bug #53211)
* detrend.m: Allow input array to be complex or real. Overhaul input
validation and error reporting. Add BIST tests for complex input and input
validation.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Wed, 27 Feb 2019 11:49:36 -0800 |
parents | 00f796120a6d |
children | b442ec6dda5c |
rev | line source |
---|---|
26379
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
1 ## Copyright (C) 1995-2019 Kurt Hornik |
3426 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
3922 | 6 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
7 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## (at 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 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
3426 | 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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
17 ## <https://www.gnu.org/licenses/>. |
2539 | 18 |
3381 | 19 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
20 ## @deftypefn {} {} detrend (@var{x}, @var{p}) |
3367 | 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 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
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:
19833
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 |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
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:
19833
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 | 34 ## @end deftypefn |
2539 | 35 |
5428 | 36 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539 | 37 ## Created: 11 October 1994 |
38 ## Adapted-By: jwe | |
3426 | 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) |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
41 |
26814
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
42 if (nargin < 1 || nargin > 2) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
43 print_usage (); |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
44 endif |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
45 |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
46 if (! isnumeric (x) || ndims (x) > 2) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
47 error ("detrend: X must be a numeric vector or matrix"); |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
48 endif |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
49 |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
50 if (ischar (p) && strcmpi (p, "constant")) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
51 p = 0; |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
52 elseif (ischar (p) && strcmpi (p, "linear")) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
53 p = 1; |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
54 elseif (! isscalar (p) || p < 0 || p != fix (p)) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
55 error ("detrend: P must be \"constant\", \"linear\", or a positive integer"); |
2539 | 56 endif |
3426 | 57 |
2539 | 58 [m, n] = size (x); |
59 if (m == 1) | |
26814
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
60 x = x.'; |
2539 | 61 endif |
3426 | 62 |
2539 | 63 r = rows (x); |
26814
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
64 b = ((1 : r).' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p)); |
2539 | 65 y = x - b * (b \ x); |
3426 | 66 |
2539 | 67 if (m == 1) |
26814
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
68 y = y.'; |
2539 | 69 endif |
3426 | 70 |
2539 | 71 endfunction |
7279 | 72 |
73 | |
74 %!test | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 %! N = 32; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
76 %! 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
|
77 %! y = detrend (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 %! 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
|
79 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
80 %!test |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
81 %! N = 32; |
7279 | 82 %! t = (0:1:N-1)/N; |
83 %! 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
|
84 %! y = detrend (x,2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
85 %! assert (abs (y(:)) < 30*eps); |
7279 | 86 |
87 %!test | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %! N = 32; |
7279 | 89 %! t = (0:1:N-1)/N; |
26814
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
90 %! x = [t;4*t-3].'; |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
91 %! y = detrend (x); |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
92 %! assert (abs (y(:)) < 20*eps); |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
93 |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
94 %!test |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
95 %! N = 32; |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
96 %! x = ((0:1:N-1)/N + 2) * 1i; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
97 %! y = detrend (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %! assert (abs (y(:)) < 20*eps); |
26814
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
99 |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
100 ## Test input validation |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
101 %!error detrend () |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
102 %!error detrend (1, 2, 3) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
103 %!error detrend ("a") |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
104 %!error detrend (true) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
105 %!error detrend (1, "invalid") |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
106 %!error detrend (1, -1) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
107 %!error detrend (1, 1.25) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26379
diff
changeset
|
108 |