comparison scripts/signal/fftshift.m @ 20272:abbe33bf0c74 stable

fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207) * fftshift.m, ifftshift.m: Improve input argument validation instead of calling ismatrix to restore support for N-dimensional arrays. Rephrase error messages. Add %!tests for N-dimensional inputs and %!error input validation tests.
author Mike Miller <mtmiller@octave.org>
date Thu, 28 May 2015 22:16:24 -0400
parents f1d0f506ee78
children
comparison
equal deleted inserted replaced
20269:3ccc2d02e64b 20272:abbe33bf0c74
50 50
51 if (nargin != 1 && nargin != 2) 51 if (nargin != 1 && nargin != 2)
52 print_usage (); 52 print_usage ();
53 endif 53 endif
54 54
55 if (! (isnumeric (x) || islogical (x) || ischar (x)))
56 error ("fftshift: X must be a vector or matrix");
57 endif
58
55 if (nargin == 2) 59 if (nargin == 2)
56 if (! (isscalar (dim) && dim > 0 && dim == fix (dim))) 60 if (! (isscalar (dim) && dim > 0 && dim == fix (dim)))
57 error ("fftshift: dimension DIM must be a positive integer"); 61 error ("fftshift: dimension DIM must be a positive integer");
58 endif 62 endif
59 nd = ndims (x); 63 nd = ndims (x);
66 else 70 else
67 if (isvector (x)) 71 if (isvector (x))
68 xl = length (x); 72 xl = length (x);
69 xx = ceil (xl/2); 73 xx = ceil (xl/2);
70 retval = x([xx+1:xl, 1:xx]); 74 retval = x([xx+1:xl, 1:xx]);
71 elseif (ismatrix (x)) 75 else
72 nd = ndims (x); 76 nd = ndims (x);
73 sz = size (x); 77 sz = size (x);
74 sz2 = ceil (sz ./ 2); 78 sz2 = ceil (sz ./ 2);
75 idx = cell (); 79 idx = cell ();
76 for i = 1:nd 80 for i = 1:nd
77 idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)]; 81 idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)];
78 endfor 82 endfor
79 retval = x(idx{:}); 83 retval = x(idx{:});
80 else
81 error ("fftshift: expecting vector or matrix argument");
82 endif 84 endif
83 endif 85 endif
84 86
85 endfunction 87 endfunction
86 88
128 %! x = [x;2*x;3*x+1;4*x+1]; 130 %! x = [x;2*x;3*x+1;4*x+1];
129 %! y = fftshift (x,2); 131 %! y = fftshift (x,2);
130 %! assert (y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]); 132 %! assert (y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]);
131 %! assert (fftshift (y,2), x); 133 %! assert (fftshift (y,2), x);
132 134
135 %!test
136 %! x = "abcdefg";
137 %! y = fftshift (x);
138 %! assert (y, "efgabcd");
139 %! assert (fftshift (y), "bcdefga");
140
141 ## Test N-dimensional input (bug #45207)
142 %!test
143 %! x = [0:3];
144 %! x = x + x' + reshape (x, [1 1 4]);
145 %! y1 = [4 5 2 3; 5 6 3 4; 2 3 0 1; 3 4 1 2];
146 %! y = fftshift (x);
147 %! assert (y, reshape ([y1 + 2, y1 + 3, y1, y1 + 1], [4 4 4]));
148 %! assert (fftshift (y), x);
149
150 %% Test input validation
151 %!error fftshift ()
152 %!error fftshift (1, 2, 3)
153 %!error fftshift (0:3, -1)
154 %!error fftshift (0:3, 0:3)
155