Mercurial > octave
annotate scripts/signal/detrend.m @ 33658:b1d6e40ac737 stable tip
NEWS.9.md: Fix typo and minor formatting changes.
* etc/NEWS.9.md: Fix typo. Adjust whitespace. Use markdown syntax for code
snippets.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Wed, 05 Jun 2024 11:27:35 +0200 |
parents | 2e484f9f1f18 |
children |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
31706
diff
changeset
|
3 ## Copyright (C) 1995-2024 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26811
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
3426 | 7 ## |
3922 | 8 ## This file is part of Octave. |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
3922 | 11 ## 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
|
12 ## 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
|
13 ## (at your option) any later version. |
3426 | 14 ## |
3922 | 15 ## Octave is distributed in the hope that it will be useful, but |
2539 | 16 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
3426 | 19 ## |
2539 | 20 ## You should have received a copy of the GNU General Public License |
7016 | 21 ## 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
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
2539 | 25 |
3381 | 26 ## -*- texinfo -*- |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
27 ## @deftypefn {} {@var{y} =} detrend (@var{x}, @var{p}) |
3367 | 28 ## If @var{x} is a vector, @code{detrend (@var{x}, @var{p})} removes the |
29 ## best fit of a polynomial of order @var{p} from the data @var{x}. | |
3426 | 30 ## |
3367 | 31 ## If @var{x} is a matrix, @code{detrend (@var{x}, @var{p})} does the same |
32 ## for each column in @var{x}. | |
3426 | 33 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## 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
|
35 ## 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
|
36 ## |
dd539a976451
signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents:
7279
diff
changeset
|
37 ## 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
|
38 ## @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
|
39 ## 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
|
40 ## @seealso{polyfit} |
3367 | 41 ## @end deftypefn |
2539 | 42 |
11409
dd539a976451
signal/detrend.m: Also accept polynomial order as a string for compatibility
Soren Hauberg <hauberg@gmail.com>
parents:
7279
diff
changeset
|
43 function y = detrend (x, p = 1) |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
44 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
45 if (nargin < 1) |
26811
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
46 print_usage (); |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
47 endif |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
48 |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
49 if (! isnumeric (x) || ndims (x) > 2) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
50 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:
26376
diff
changeset
|
51 endif |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
52 |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
53 if (ischar (p) && strcmpi (p, "constant")) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
54 p = 0; |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
55 elseif (ischar (p) && strcmpi (p, "linear")) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
56 p = 1; |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
57 elseif (! isscalar (p) || p < 0 || p != fix (p)) |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
58 error ('detrend: P must be "constant", "linear", or a positive integer'); |
2539 | 59 endif |
3426 | 60 |
2539 | 61 [m, n] = size (x); |
62 if (m == 1) | |
26811
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
63 x = x.'; |
2539 | 64 endif |
3426 | 65 |
2539 | 66 r = rows (x); |
26811
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
67 b = ((1 : r).' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p)); |
2539 | 68 y = x - b * (b \ x); |
3426 | 69 |
2539 | 70 if (m == 1) |
26811
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
71 y = y.'; |
2539 | 72 endif |
3426 | 73 |
2539 | 74 endfunction |
7279 | 75 |
76 | |
77 %!test | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 %! N = 32; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
79 %! 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
|
80 %! y = detrend (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
81 %! 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
|
82 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 %!test |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
84 %! N = 32; |
7279 | 85 %! t = (0:1:N-1)/N; |
86 %! 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
|
87 %! y = detrend (x,2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %! assert (abs (y(:)) < 30*eps); |
7279 | 89 |
90 %!test | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %! N = 32; |
7279 | 92 %! t = (0:1:N-1)/N; |
26811
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
93 %! x = [t;4*t-3].'; |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
94 %! y = detrend (x); |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
95 %! assert (abs (y(:)) < 20*eps); |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
96 |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
97 %!test |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
98 %! N = 32; |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
99 %! 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
|
100 %! y = detrend (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 %! assert (abs (y(:)) < 20*eps); |
26811
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
102 |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
103 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
104 %!error <Invalid call> detrend () |
26811
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
105 %!error detrend ("a") |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
106 %!error detrend (true) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
107 %!error detrend (1, "invalid") |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
108 %!error detrend (1, -1) |
be5b43255a38
detrend.m: support complex arrays, overhaul input validation (bug #53211)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
109 %!error detrend (1, 1.25) |