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  {} {} fftshift (@var{x})
28## @deftypefnx {} {} fftshift (@var{x}, @var{dim})
29## Perform a shift of the vector @var{x}, for use with the @code{fft} and
30## @code{ifft} functions, in order to move the frequency 0 to the center of
31## the vector or matrix.
32##
33## If @var{x} is a vector of @math{N} elements corresponding to @math{N} time
34## samples spaced by @nospell{@math{dt}}, then
35## @code{fftshift (fft (@var{x}))} corresponds to frequencies
36##
37## @example
38## f = [ -(ceil((N-1)/2):-1:1), 0, (1:floor((N-1)/2)) ] * df
39## @end example
40##
41## @noindent
42## where @nospell{@math{df = 1 / (N * dt)}}.
43##
44## If @var{x} is a matrix, the same holds for rows and columns.  If @var{x}
45## is an array, then the same holds along each dimension.
46##
47## The optional @var{dim} argument can be used to limit the dimension along
48## which the permutation occurs.
49## @seealso{ifftshift}
50## @end deftypefn
51
52function retval = fftshift (x, dim)
53
54  if (nargin != 1 && nargin != 2)
55    print_usage ();
56  endif
57
58  if (! (isnumeric (x) || islogical (x) || ischar (x)))
59    error ("fftshift: X must be a vector or matrix");
60  endif
61
62  if (nargin == 2)
63    if (! (isscalar (dim) && dim > 0 && dim == fix (dim)))
64      error ("fftshift: dimension DIM must be a positive integer");
65    endif
66    nd = ndims (x);
67    sz = size (x);
68    sz2 = ceil (sz(dim) / 2);
69    idx = cell ();
70    idx = repmat ({':'}, nd, 1);
71    idx{dim} = [sz2+1:sz(dim), 1:sz2];
72    retval = x(idx{:});
73  else
74    if (isvector (x))
75      xl = length (x);
76      xx = ceil (xl/2);
77      retval = x([xx+1:xl, 1:xx]);
78    else
79      nd = ndims (x);
80      sz = size (x);
81      sz2 = ceil (sz ./ 2);
82      idx = cell ();
83      for i = 1:nd
84        idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)];
85      endfor
86      retval = x(idx{:});
87    endif
88  endif
89
90endfunction
91
92
93%!test
94%! x = [0:7];
95%! y = fftshift (x);
96%! assert (y, [4 5 6 7 0 1 2 3]);
97%! assert (fftshift (y), x);
98
99%!test
100%! x = [0:6];
101%! y = fftshift (x);
102%! assert (y, [4 5 6 0 1 2 3]);
103%! assert (fftshift (y), [1 2 3 4 5 6 0]);
104
105%!test
106%! x = [0:7]';
107%! y = fftshift (x);
108%! assert (y, [4;5;6;7;0;1;2;3]);
109%! assert (fftshift (y), x);
110
111%!test
112%! x = [0:6]';
113%! y = fftshift (x);
114%! assert (y, [4;5;6;0;1;2;3]);
115%! assert (fftshift (y), [1;2;3;4;5;6;0]);
116
117%!test
118%! x = [0:3];
119%! x = [x;2*x;3*x+1;4*x+1];
120%! y = fftshift (x);
121%! assert (y, [[7 10 1 4];[9 13 1 5];[2 3 0 1];[4 6 0 2]]);
122%! assert (fftshift (y), x);
123
124%!test
125%! x = [0:3];
126%! x = [x;2*x;3*x+1;4*x+1];
127%! y = fftshift (x,1);
128%! assert (y, [[1 4 7 10];[1 5 9 13];[0 1 2 3];[0 2 4 6]]);
129%! assert (fftshift (y,1), x);
130
131%!test
132%! x = [0:3];
133%! x = [x;2*x;3*x+1;4*x+1];
134%! y = fftshift (x,2);
135%! assert (y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]);
136%! assert (fftshift (y,2), x);
137
138%!test
139%! x = "abcdefg";
140%! y = fftshift (x);
141%! assert (y, "efgabcd");
142%! assert (fftshift (y), "bcdefga");
143
144## Test N-dimensional input
145%!test <*45207>
146%! x = [0:3];
147%! x = x + x' + reshape (x, [1 1 4]);
148%! y1 = [4 5 2 3; 5 6 3 4; 2 3 0 1; 3 4 1 2];
149%! y = fftshift (x);
150%! assert (y, reshape ([y1 + 2, y1 + 3, y1, y1 + 1], [4 4 4]));
151%! assert (fftshift (y), x);
152
153## Test input validation
154%!error fftshift ()
155%!error fftshift (1, 2, 3)
156%!error fftshift (0:3, -1)
157%!error fftshift (0:3, 0:3)
158