comparison scripts/signal/fftshift.m @ 4844:9f7ef92b50b0

[project @ 2004-04-02 17:26:53 by jwe]
author jwe
date Fri, 02 Apr 2004 17:26:54 +0000
parents b5267e631ba8
children 4c8a2e4e0717
comparison
equal deleted inserted replaced
4843:7b4e76100964 4844:9f7ef92b50b0
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA 17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
18 ## 02111-1307, USA. 18 ## 02111-1307, USA.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} fftshift (@var{v}) 21 ## @deftypefn {Function File} {} fftshift (@var{v})
22 ## @deftypefnx {Function File} {} fftshift (@var{v}, @var{dim})
22 ## Perform a shift of the vector @var{v}, for use with the @code{fft} 23 ## Perform a shift of the vector @var{v}, for use with the @code{fft}
23 ## and @code{ifft} functions, in order the move the frequency 0 to the 24 ## and @code{ifft} functions, in order the move the frequency 0 to the
24 ## center of the vector or matrix. 25 ## center of the vector or matrix.
25 ## 26 ##
26 ## If @var{v} is a vector of @math{N} elements corresponding to @math{N} 27 ## If @var{v} is a vector of @math{N} elements corresponding to @math{N}
29 ## 30 ##
30 ## @example 31 ## @example
31 ## f = ((1:N) - ceil(N/2)) / N / Dt 32 ## f = ((1:N) - ceil(N/2)) / N / Dt
32 ## @end example 33 ## @end example
33 ## 34 ##
34 ## If @var{v} is a matrix, the same holds for rows and columns. 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.
35 ## @end deftypefn 40 ## @end deftypefn
36 41
37 ## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp> 42 ## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp>
38 ## Created: July 1997 43 ## Created: July 1997
39 ## Adapted-By: jwe 44 ## Adapted-By: jwe
40 45
41 function retval = fftshift (V) 46 function retval = fftshift (V, dim)
42 47
43 retval = 0; 48 retval = 0;
44 49
45 if (nargin != 1) 50 if (nargin != 1 && nargin != 2)
46 usage ("usage: fftshift (X)"); 51 usage ("usage: fftshift (X, dim)");
47 endif 52 endif
48 53
49 if (isvector (V)) 54 if (nargin == 2)
50 x = length (V); 55 if (!isscalar (dim))
51 xx = ceil (x/2); 56 error ("fftshift: dimension must be an integer scalar");
52 retval = V([xx+1:x, 1:xx]); 57 endif
53 elseif (ismatrix (V)) 58 nd = ndims (V);
54 [x, y] = size (V); 59 sz = size (V);
55 xx = ceil (x/2); 60 sz2 = ceil (sz(dim) / 2);
56 yy = ceil (y/2); 61 idx = cell ();
57 retval = V([xx+1:x, 1:xx], [yy+1:y, 1:yy]); 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{:});
58 else 67 else
59 error ("fftshift: expecting vector or matrix argument"); 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
60 endif 84 endif
61 85
62 endfunction 86 endfunction