]> Creatis software - CreaPhase.git/blob - octave_packages/m/signal/fftshift.m
update packages
[CreaPhase.git] / octave_packages / m / signal / fftshift.m
1 ## Copyright (C) 1997-2012 Vincent Cautaerts
2 ##
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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.
25 ##
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
29 ##
30 ## @example
31 ## f = [ -(ceil((N-1)/2):-1:1)*df 0 (1:floor((N-1)/2))*df ]
32 ## @end example
33 ##
34 ## @noindent
35 ## where @nospell{@math{df}} = 1 / @math{dt}.
36 ##
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.
39 ##
40 ## The optional @var{dim} argument can be used to limit the dimension
41 ## along which the permutation occurs.
42 ## @end deftypefn
43
44 ## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp>
45 ## Created: July 1997
46 ## Adapted-By: jwe
47
48 function retval = fftshift (x, dim)
49
50   if (nargin != 1 && nargin != 2)
51     print_usage ();
52   endif
53
54   if (nargin == 2)
55     if (! (isscalar (dim) && dim > 0 && dim == fix (dim)))
56       error ("fftshift: dimension DIM must be a positive integer");
57     endif
58     nd = ndims (x);
59     sz = size (x);
60     sz2 = ceil (sz(dim) / 2);
61     idx = cell ();
62     idx = repmat ({':'}, nd, 1);
63     idx{dim} = [sz2+1:sz(dim), 1:sz2];
64     retval = x(idx{:});
65   else
66     if (isvector (x))
67       xl = length (x);
68       xx = ceil (xl/2);
69       retval = x([xx+1:xl, 1:xx]);
70     elseif (ismatrix (x))
71       nd = ndims (x);
72       sz = size (x);
73       sz2 = ceil (sz ./ 2);
74       idx = cell ();
75       for i = 1:nd
76         idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)];
77       endfor
78       retval = x(idx{:});
79     else
80       error ("fftshift: expecting vector or matrix argument");
81     endif
82   endif
83
84 endfunction
85
86
87 %!test
88 %!  x = [0:7];
89 %!  y = fftshift (x);
90 %!  assert(y, [4 5 6 7 0 1 2 3]);
91 %!  assert(fftshift (y), x);
92
93 %!test
94 %!  x = [0:6];
95 %!  y = fftshift (x);
96 %!  assert(y, [4 5 6 0 1 2 3]);
97 %!  assert(fftshift (y), [1 2 3 4 5 6 0]);
98
99 %!test
100 %!  x = [0:7]';
101 %!  y = fftshift (x);
102 %!  assert(y, [4;5;6;7;0;1;2;3]);
103 %!  assert(fftshift (y), x);
104
105 %!test
106 %!  x = [0:6]';
107 %!  y = fftshift (x);
108 %!  assert(y, [4;5;6;0;1;2;3]);
109 %!  assert(fftshift (y), [1;2;3;4;5;6;0]);
110
111 %!test
112 %!  x = [0:3];
113 %!  x = [x;2*x;3*x+1;4*x+1];
114 %!  y = fftshift (x);
115 %!  assert(y, [[7 10 1 4];[9 13 1 5];[2 3 0 1];[4 6 0 2]]);
116 %!  assert(fftshift (y), x);
117
118 %!test
119 %!  x = [0:3];
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);
124
125 %!test
126 %!  x = [0:3];
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);
131