3191
|
1 ## Copyright (C) 1997 by Vincent Cautaerts |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
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 |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3191
|
19 |
3449
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} fftshift (@var{v}) |
4844
|
22 ## @deftypefnx {Function File} {} fftshift (@var{v}, @var{dim}) |
3449
|
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 |
3499
|
25 ## center of the vector or matrix. |
3191
|
26 ## |
4077
|
27 ## If @var{v} is a vector of @math{N} elements corresponding to @math{N} |
3499
|
28 ## time samples spaced of @math{Dt} each, then @code{fftshift (fft |
3449
|
29 ## (@var{v}))} corresponds to frequencies |
3191
|
30 ## |
3449
|
31 ## @example |
4077
|
32 ## f = ((1:N) - ceil(N/2)) / N / Dt |
3449
|
33 ## @end example |
3191
|
34 ## |
4844
|
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. |
3449
|
40 ## @end deftypefn |
3191
|
41 |
|
42 ## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp> |
|
43 ## Created: July 1997 |
|
44 ## Adapted-By: jwe |
|
45 |
4844
|
46 function retval = fftshift (V, dim) |
3191
|
47 |
|
48 retval = 0; |
|
49 |
4844
|
50 if (nargin != 1 && nargin != 2) |
6046
|
51 print_usage (); |
3191
|
52 endif |
|
53 |
4844
|
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{:}); |
3191
|
67 else |
4844
|
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 |
3191
|
84 endif |
|
85 |
|
86 endfunction |