1######################################################################## 2## 3## Copyright (C) 1997-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {} ifftshift (@var{x}) 28## @deftypefnx {} {} ifftshift (@var{x}, @var{dim}) 29## Undo the action of the @code{fftshift} function. 30## 31## For even length @var{x}, @code{fftshift} is its own inverse, but odd lengths 32## differ slightly. 33## @seealso{fftshift} 34## @end deftypefn 35 36function retval = ifftshift (x, dim) 37 38 if (nargin != 1 && nargin != 2) 39 print_usage (); 40 endif 41 42 if (! (isnumeric (x) || islogical (x) || ischar (x))) 43 error ("ifftshift: X must be a vector or matrix"); 44 endif 45 46 if (nargin == 2) 47 if (! (isscalar (dim) && dim > 0 && dim == fix (dim))) 48 error ("ifftshift: dimension DIM must be a positive integer"); 49 endif 50 nd = ndims (x); 51 sz = size (x); 52 sz2 = floor (sz(dim) / 2); 53 idx = repmat ({':'}, nd, 1); 54 idx{dim} = [sz2+1:sz(dim), 1:sz2]; 55 retval = x(idx{:}); 56 else 57 if (isvector (x)) 58 xl = length (x); 59 xx = floor (xl/2); 60 retval = x([xx+1:xl, 1:xx]); 61 else 62 nd = ndims (x); 63 sz = size (x); 64 sz2 = floor (sz ./ 2); 65 idx = cell (); 66 for i = 1:nd 67 idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)]; 68 endfor 69 retval = x(idx{:}); 70 endif 71 endif 72 73endfunction 74 75 76%!test 77%! x = [0:7]; 78%! y = ifftshift (x); 79%! assert (y, [4 5 6 7 0 1 2 3]); 80%! assert (ifftshift (y), x); 81 82%!test 83%! x = [0:6]; 84%! y = ifftshift (x); 85%! assert (y, [3 4 5 6 0 1 2]); 86%! assert (ifftshift (y), [6 0 1 2 3 4 5]); 87 88%!test 89%! x = [0:7]'; 90%! y = ifftshift (x); 91%! assert (y, [4;5;6;7;0;1;2;3]); 92%! assert (ifftshift (y), x); 93 94%!test 95%! x = [0:6]'; 96%! y = ifftshift (x); 97%! assert (y, [3;4;5;6;0;1;2]); 98%! assert (ifftshift (y), [6;0;1;2;3;4;5]); 99 100%!test 101%! x = [0:3]; 102%! x = [x;2*x;3*x+1;4*x+1]; 103%! y = ifftshift (x); 104%! assert (y, [[7 10 1 4];[9 13 1 5];[2 3 0 1];[4 6 0 2]]); 105%! assert (ifftshift (y), x); 106 107%!test 108%! x = [0:3]; 109%! x = [x;2*x;3*x+1;4*x+1]; 110%! y = ifftshift (x,1); 111%! assert (y, [[1 4 7 10];[1 5 9 13];[0 1 2 3];[0 2 4 6]]); 112%! assert (ifftshift (y,1), x); 113 114%!test 115%! x = [0:3]; 116%! x = [x;2*x;3*x+1;4*x+1]; 117%! y = ifftshift (x,2); 118%! assert (y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]); 119%! assert (ifftshift (y,2), x); 120 121%!test 122%! x = "efgabcd"; 123%! y = ifftshift (x); 124%! assert (y, "abcdefg"); 125%! assert (ifftshift (y), "defgabc"); 126 127## Test N-dimensional input 128%!test <*45207> 129%! x = [0:3]; 130%! x = x + x' + reshape (x, [1 1 4]); 131%! y1 = [4 5 2 3; 5 6 3 4; 2 3 0 1; 3 4 1 2]; 132%! y = ifftshift (x); 133%! assert (y, reshape ([y1 + 2, y1 + 3, y1, y1 + 1], [4 4 4])); 134%! assert (ifftshift (y), x); 135 136## Test input validation 137%!error ifftshift () 138%!error ifftshift (1, 2, 3) 139%!error ifftshift (0:3, -1) 140%!error ifftshift (0:3, 0:3) 141