# HG changeset patch # User Mike Miller # Date 1432865784 14400 # Node ID abbe33bf0c74d43144b8c8f483d04e8cd73ab0e6 # Parent 3ccc2d02e64b61c6827009d42e5b12274e7ba5fc fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207) * fftshift.m, ifftshift.m: Improve input argument validation instead of calling ismatrix to restore support for N-dimensional arrays. Rephrase error messages. Add %!tests for N-dimensional inputs and %!error input validation tests. diff -r 3ccc2d02e64b -r abbe33bf0c74 scripts/signal/fftshift.m --- a/scripts/signal/fftshift.m Sat May 23 10:35:40 2015 -0400 +++ b/scripts/signal/fftshift.m Thu May 28 22:16:24 2015 -0400 @@ -52,6 +52,10 @@ print_usage (); endif + if (! (isnumeric (x) || islogical (x) || ischar (x))) + error ("fftshift: X must be a vector or matrix"); + endif + if (nargin == 2) if (! (isscalar (dim) && dim > 0 && dim == fix (dim))) error ("fftshift: dimension DIM must be a positive integer"); @@ -68,7 +72,7 @@ xl = length (x); xx = ceil (xl/2); retval = x([xx+1:xl, 1:xx]); - elseif (ismatrix (x)) + else nd = ndims (x); sz = size (x); sz2 = ceil (sz ./ 2); @@ -77,8 +81,6 @@ idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)]; endfor retval = x(idx{:}); - else - error ("fftshift: expecting vector or matrix argument"); endif endif @@ -130,3 +132,24 @@ %! assert (y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]); %! assert (fftshift (y,2), x); +%!test +%! x = "abcdefg"; +%! y = fftshift (x); +%! assert (y, "efgabcd"); +%! assert (fftshift (y), "bcdefga"); + +## Test N-dimensional input (bug #45207) +%!test +%! x = [0:3]; +%! x = x + x' + reshape (x, [1 1 4]); +%! y1 = [4 5 2 3; 5 6 3 4; 2 3 0 1; 3 4 1 2]; +%! y = fftshift (x); +%! assert (y, reshape ([y1 + 2, y1 + 3, y1, y1 + 1], [4 4 4])); +%! assert (fftshift (y), x); + +%% Test input validation +%!error fftshift () +%!error fftshift (1, 2, 3) +%!error fftshift (0:3, -1) +%!error fftshift (0:3, 0:3) + diff -r 3ccc2d02e64b -r abbe33bf0c74 scripts/signal/ifftshift.m --- a/scripts/signal/ifftshift.m Sat May 23 10:35:40 2015 -0400 +++ b/scripts/signal/ifftshift.m Thu May 28 22:16:24 2015 -0400 @@ -34,15 +34,17 @@ function retval = ifftshift (x, dim) - retval = 0; - if (nargin != 1 && nargin != 2) print_usage (); endif + if (! (isnumeric (x) || islogical (x) || ischar (x))) + error ("ifftshift: X must be a vector or matrix"); + endif + if (nargin == 2) - if (! isscalar (dim)) - error ("ifftshift: dimension must be an integer scalar"); + if (! (isscalar (dim) && dim > 0 && dim == fix (dim))) + error ("ifftshift: dimension DIM must be a positive integer"); endif nd = ndims (x); sz = size (x); @@ -55,7 +57,7 @@ xl = length (x); xx = floor (xl/2); retval = x([xx+1:xl, 1:xx]); - elseif (ismatrix (x)) + else nd = ndims (x); sz = size (x); sz2 = floor (sz ./ 2); @@ -64,8 +66,6 @@ idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)]; endfor retval = x(idx{:}); - else - error ("ifftshift: expecting vector or matrix argument"); endif endif @@ -117,3 +117,24 @@ %! assert (y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]); %! assert (ifftshift (y,2), x); +%!test +%! x = "efgabcd"; +%! y = ifftshift (x); +%! assert (y, "abcdefg"); +%! assert (ifftshift (y), "defgabc"); + +## Test N-dimensional input (bug #45207) +%!test +%! x = [0:3]; +%! x = x + x' + reshape (x, [1 1 4]); +%! y1 = [4 5 2 3; 5 6 3 4; 2 3 0 1; 3 4 1 2]; +%! y = ifftshift (x); +%! assert (y, reshape ([y1 + 2, y1 + 3, y1, y1 + 1], [4 4 4])); +%! assert (ifftshift (y), x); + +%% Test input validation +%!error ifftshift () +%!error ifftshift (1, 2, 3) +%!error ifftshift (0:3, -1) +%!error ifftshift (0:3, 0:3) +