]> Creatis software - CreaPhase.git/blob - octave_packages/m/signal/ifftshift.m
update packages
[CreaPhase.git] / octave_packages / m / signal / ifftshift.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} {} ifftshift (@var{x})
21 ## @deftypefnx {Function File} {} ifftshift (@var{x}, @var{dim})
22 ## Undo the action of the @code{fftshift} function.  For even length
23 ## @var{x}, @code{fftshift} is its own inverse, but odd lengths differ
24 ## slightly.
25 ## @end deftypefn
26
27 ## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp>
28 ## Created: July 1997
29 ## Adapted-By: jwe
30 ## Modified-By: Paul Kienzle, converted from fftshift
31 ## Modified-By: David Bateman, add NDArray capability and option dim arg
32
33 function retval = ifftshift (x, dim)
34
35   retval = 0;
36
37   if (nargin != 1 && nargin != 2)
38     print_usage ();
39   endif
40
41   if (nargin == 2)
42     if (! isscalar (dim))
43       error ("ifftshift: dimension must be an integer scalar");
44     endif
45     nd = ndims (x);
46     sz = size (x);
47     sz2 = floor (sz(dim) / 2);
48     idx = repmat ({':'}, nd, 1);
49     idx{dim} = [sz2+1:sz(dim), 1:sz2];
50     retval = x(idx{:});
51   else
52     if (isvector (x))
53       xl = length (x);
54       xx = floor (xl/2);
55       retval = x([xx+1:xl, 1:xx]);
56     elseif (ismatrix (x))
57       nd = ndims (x);
58       sz = size (x);
59       sz2 = floor (sz ./ 2);
60       idx = cell ();
61       for i = 1:nd
62         idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)];
63       endfor
64       retval = x(idx{:});
65     else
66       error ("ifftshift: expecting vector or matrix argument");
67     endif
68   endif
69
70 endfunction
71
72 %!test
73 %!  x = [0:7];
74 %!  y = ifftshift (x);
75 %!  assert(y, [4 5 6 7 0 1 2 3]);
76 %!  assert(ifftshift (y), x);
77
78 %!test
79 %!  x = [0:6];
80 %!  y = ifftshift (x);
81 %!  assert(y, [3 4 5 6 0 1 2]);
82 %!  assert(ifftshift (y), [6 0 1 2 3 4 5]);
83
84 %!test
85 %!  x = [0:7]';
86 %!  y = ifftshift (x);
87 %!  assert(y, [4;5;6;7;0;1;2;3]);
88 %!  assert(ifftshift (y), x);
89
90 %!test
91 %!  x = [0:6]';
92 %!  y = ifftshift (x);
93 %!  assert(y, [3;4;5;6;0;1;2]);
94 %!  assert(ifftshift (y), [6;0;1;2;3;4;5]);
95
96 %!test
97 %!  x = [0:3];
98 %!  x = [x;2*x;3*x+1;4*x+1];
99 %!  y = ifftshift (x);
100 %!  assert(y, [[7 10 1 4];[9 13 1 5];[2 3 0 1];[4 6 0 2]]);
101 %!  assert(ifftshift (y), x);
102
103 %!test
104 %!  x = [0:3];
105 %!  x = [x;2*x;3*x+1;4*x+1];
106 %!  y = ifftshift (x,1);
107 %!  assert(y, [[1 4 7 10];[1 5 9 13];[0 1 2 3];[0 2 4 6]]);
108 %!  assert(ifftshift (y,1), x);
109
110 %!test
111 %!  x = [0:3];
112 %!  x = [x;2*x;3*x+1;4*x+1];
113 %!  y = ifftshift (x,2);
114 %!  assert(y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]);
115 %!  assert(ifftshift (y,2), x);
116