function y = fftshift(x)
%FFTSHIFT Shift DC component to center of spectrum.
%   For vectors, FFTSHIFT(X) swaps the left and right halves of
%   X.  For matrices, FFTSHIFT(X) swaps the first and third
%   quadrants and the second and fourth quadrants.  For N-D
%   arrays, FFTSHIFT(X) swaps "half-spaces" of X along each
%   dimension.
%
%   FFTSHIFT is useful for visualizing the Fourier transform with
%   the DC component in the middle of the spectrum.
%
%   See also IFFTSHIFT, FFT, FFT2, FFTN.

%   J.N. Little 6-23-86
%   Revised for N-D by Steve Eddins, February 1997
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.5 $  $Date: 1997/11/21 23:23:44 $

numDims = ndims(x);
idx = cell(1, numDims);
for k = 1:numDims
    m = size(x, k);
    p = ceil(m/2);
    idx{k} = [p+1:m 1:p];
end

% Use comma-separated list syntax for N-D indexing.
y = x(idx{:});
