1 ## Copyright (C) 1997-2012 Vincent Cautaerts
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} fftshift (@var{x})
21 ## @deftypefnx {Function File} {} fftshift (@var{x}, @var{dim})
22 ## Perform a shift of the vector @var{x}, for use with the @code{fft}
23 ## and @code{ifft} functions, in order the move the frequency 0 to the
24 ## center of the vector or matrix.
26 ## If @var{x} is a vector of @math{N} elements corresponding to @math{N}
27 ## time samples spaced by @math{dt}, then
28 ## @code{fftshift (fft (@var{x}))} corresponds to frequencies
31 ## f = [ -(ceil((N-1)/2):-1:1)*df 0 (1:floor((N-1)/2))*df ]
35 ## where @nospell{@math{df}} = 1 / @math{dt}.
37 ## If @var{x} is a matrix, the same holds for rows and columns. If
38 ## @var{x} is an array, then the same holds along each dimension.
40 ## The optional @var{dim} argument can be used to limit the dimension
41 ## along which the permutation occurs.
44 ## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp>
48 function retval = fftshift (x, dim)
50 if (nargin != 1 && nargin != 2)
55 if (! (isscalar (dim) && dim > 0 && dim == fix (dim)))
56 error ("fftshift: dimension DIM must be a positive integer");
60 sz2 = ceil (sz(dim) / 2);
62 idx = repmat ({':'}, nd, 1);
63 idx{dim} = [sz2+1:sz(dim), 1:sz2];
69 retval = x([xx+1:xl, 1:xx]);
76 idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)];
80 error ("fftshift: expecting vector or matrix argument");
90 %! assert(y, [4 5 6 7 0 1 2 3]);
91 %! assert(fftshift (y), x);
96 %! assert(y, [4 5 6 0 1 2 3]);
97 %! assert(fftshift (y), [1 2 3 4 5 6 0]);
102 %! assert(y, [4;5;6;7;0;1;2;3]);
103 %! assert(fftshift (y), x);
108 %! assert(y, [4;5;6;0;1;2;3]);
109 %! assert(fftshift (y), [1;2;3;4;5;6;0]);
113 %! x = [x;2*x;3*x+1;4*x+1];
115 %! assert(y, [[7 10 1 4];[9 13 1 5];[2 3 0 1];[4 6 0 2]]);
116 %! assert(fftshift (y), x);
120 %! x = [x;2*x;3*x+1;4*x+1];
121 %! y = fftshift (x,1);
122 %! assert(y, [[1 4 7 10];[1 5 9 13];[0 1 2 3];[0 2 4 6]]);
123 %! assert(fftshift (y,1), x);
127 %! x = [x;2*x;3*x+1;4*x+1];
128 %! y = fftshift (x,2);
129 %! assert(y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]);
130 %! assert(fftshift (y,2), x);