Mercurial > jwe > octave
changeset 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 | 6ae2d885626c |
children | 2703e1407887 |
files | scripts/signal/detrend.m |
diffstat | 1 files changed, 34 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/signal/detrend.m Wed Feb 27 10:00:31 2019 -0800 +++ b/scripts/signal/detrend.m Wed Feb 27 11:49:36 2019 -0800 @@ -39,30 +39,33 @@ function y = detrend (x, p = 1) - if (nargin > 0 && isreal (x) && ndims (x) <= 2) - ## Check p - if (ischar (p) && strcmpi (p, "constant")) - p = 0; - elseif (ischar (p) && strcmpi (p, "linear")) - p = 1; - elseif (! isscalar (p) || p < 0 || p != fix (p)) - error ("detrend: second input argument must be 'constant', 'linear' or a positive integer"); - endif - else - error ("detrend: first input argument must be a real vector or matrix"); + if (nargin < 1 || nargin > 2) + print_usage (); + endif + + if (! isnumeric (x) || ndims (x) > 2) + error ("detrend: X must be a numeric vector or matrix"); + endif + + if (ischar (p) && strcmpi (p, "constant")) + p = 0; + elseif (ischar (p) && strcmpi (p, "linear")) + p = 1; + elseif (! isscalar (p) || p < 0 || p != fix (p)) + error ("detrend: P must be \"constant\", \"linear\", or a positive integer"); endif [m, n] = size (x); if (m == 1) - x = x'; + x = x.'; endif r = rows (x); - b = ((1 : r)' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p)); + b = ((1 : r).' * ones (1, p + 1)) .^ (ones (r, 1) * (0 : p)); y = x - b * (b \ x); if (m == 1) - y = y'; + y = y.'; endif endfunction @@ -84,6 +87,22 @@ %!test %! N = 32; %! t = (0:1:N-1)/N; -%! x = [t;4*t-3]'; +%! x = [t;4*t-3].'; +%! y = detrend (x); +%! assert (abs (y(:)) < 20*eps); + +%!test +%! N = 32; +%! x = ((0:1:N-1)/N + 2) * 1i; %! y = detrend (x); %! assert (abs (y(:)) < 20*eps); + +## Test input validation +%!error detrend () +%!error detrend (1, 2, 3) +%!error detrend ("a") +%!error detrend (true) +%!error detrend (1, "invalid") +%!error detrend (1, -1) +%!error detrend (1, 1.25) +