changelog shortlog tags changeset files revisions annotate raw

scripts/signal/fftshift.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (59 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1997, 1998, 2000, 2002, 2004, 2005, 2006, 2007, 2009
2## Vincent Cautaerts
3##
4## This file is part of Octave.
5##
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {} fftshift (@var{v})
22## @deftypefnx {Function File} {} fftshift (@var{v}, @var{dim})
23## Perform a shift of the vector @var{v}, for use with the @code{fft}
24## and @code{ifft} functions, in order the move the frequency 0 to the
25## center of the vector or matrix.
26##
27## If @var{v} is a vector of @math{N} elements corresponding to @math{N}
28## time samples spaced of @math{Dt} each, then @code{fftshift (fft
29## (@var{v}))} corresponds to frequencies
30##
31## @example
32## f = ((1:N) - ceil(N/2)) / N / Dt
33## @end example
34##
35## If @var{v} is a matrix, the same holds for rows and columns. If
36## @var{v} is an array, then the same holds along each dimension.
37##
38## The optional @var{dim} argument can be used to limit the dimension
39## along which the permutation occurs.
40## @end deftypefn
41
42## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp>
43## Created: July 1997
44## Adapted-By: jwe
45
46function retval = fftshift (V, dim)
47
48 retval = 0;
49
50 if (nargin != 1 && nargin != 2)
51 print_usage ();
52 endif
53
54 if (nargin == 2)
55 if (!isscalar (dim))
56 error ("fftshift: dimension must be an integer scalar");
57 endif
58 nd = ndims (V);
59 sz = size (V);
60 sz2 = ceil (sz(dim) / 2);
61 idx = cell ();
62 for i = 1:nd
63 idx{i} = 1:sz(i);
64 endfor
65 idx{dim} = [sz2+1:sz(dim), 1:sz2];
66 retval = V (idx{:});
67 else
68 if (isvector (V))
69 x = length (V);
70 xx = ceil (x/2);
71 retval = V([xx+1:x, 1:xx]);
72 elseif (ismatrix (V))
73 nd = ndims (V);
74 sz = size (V);
75 sz2 = ceil (sz ./ 2);
76 idx = cell ();
77 for i = 1:nd
78 idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)];
79 endfor
80 retval = V (idx{:});
81 else
82 error ("fftshift: expecting vector or matrix argument");
83 endif
84 endif
85
86endfunction