Mercurial > hg > octave-nkf
annotate scripts/signal/ifftshift.m @ 20788:7374a3a6d594
use new string_value method to handle value extraction errors
* urlwrite.cc: Use new string_value method.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 08 Oct 2015 17:26:40 -0400 |
parents | abbe33bf0c74 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1997-2015 Vincent Cautaerts |
5820 | 2 ## |
7016 | 3 ## This file is part of Octave. |
4 ## | |
5820 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5820 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5820 | 18 |
19 ## -*- texinfo -*- | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
20 ## @deftypefn {Function File} {} ifftshift (@var{x}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
21 ## @deftypefnx {Function File} {} ifftshift (@var{x}, @var{dim}) |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
22 ## Undo the action of the @code{fftshift} function. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
23 ## |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
24 ## For even length @var{x}, @code{fftshift} is its own inverse, but odd lengths |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
25 ## differ slightly. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
26 ## @seealso{fftshift} |
5820 | 27 ## @end deftypefn |
28 | |
29 ## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp> | |
30 ## Created: July 1997 | |
31 ## Adapted-By: jwe | |
32 ## Modified-By: Paul Kienzle, converted from fftshift | |
33 ## Modified-By: David Bateman, add NDArray capability and option dim arg | |
34 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
35 function retval = ifftshift (x, dim) |
5820 | 36 |
37 if (nargin != 1 && nargin != 2) | |
6046 | 38 print_usage (); |
5820 | 39 endif |
40 | |
20447
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
41 if (! (isnumeric (x) || islogical (x) || ischar (x))) |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
42 error ("ifftshift: X must be a vector or matrix"); |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
43 endif |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
44 |
5820 | 45 if (nargin == 2) |
20447
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
46 if (! (isscalar (dim) && dim > 0 && dim == fix (dim))) |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
47 error ("ifftshift: dimension DIM must be a positive integer"); |
5820 | 48 endif |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
49 nd = ndims (x); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
50 sz = size (x); |
5820 | 51 sz2 = floor (sz(dim) / 2); |
12676
2783fa95cab7
Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents:
12541
diff
changeset
|
52 idx = repmat ({':'}, nd, 1); |
7208 | 53 idx{dim} = [sz2+1:sz(dim), 1:sz2]; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
54 retval = x(idx{:}); |
5820 | 55 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
56 if (isvector (x)) |
12536
7d9dbe4c803b
Fix bug #32873, ifftshift fails.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
57 xl = length (x); |
7d9dbe4c803b
Fix bug #32873, ifftshift fails.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
58 xx = floor (xl/2); |
7d9dbe4c803b
Fix bug #32873, ifftshift fails.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
59 retval = x([xx+1:xl, 1:xx]); |
20447
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
60 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
61 nd = ndims (x); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
62 sz = size (x); |
5820 | 63 sz2 = floor (sz ./ 2); |
64 idx = cell (); | |
8507 | 65 for i = 1:nd |
5820 | 66 idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)]; |
67 endfor | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
68 retval = x(idx{:}); |
5820 | 69 endif |
70 endif | |
71 | |
72 endfunction | |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
73 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
75 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
76 %! x = [0:7]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
77 %! y = ifftshift (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 %! assert (y, [4 5 6 7 0 1 2 3]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
79 %! assert (ifftshift (y), x); |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
80 |
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
81 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
82 %! x = [0:6]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 %! y = ifftshift (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
84 %! assert (y, [3 4 5 6 0 1 2]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
85 %! assert (ifftshift (y), [6 0 1 2 3 4 5]); |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
86 |
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
87 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %! x = [0:7]'; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %! y = ifftshift (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %! assert (y, [4;5;6;7;0;1;2;3]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %! assert (ifftshift (y), x); |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
92 |
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
93 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
94 %! x = [0:6]'; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 %! y = ifftshift (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
96 %! assert (y, [3;4;5;6;0;1;2]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
97 %! assert (ifftshift (y), [6;0;1;2;3;4;5]); |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
98 |
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
99 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
100 %! x = [0:3]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 %! x = [x;2*x;3*x+1;4*x+1]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %! y = ifftshift (x); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 %! assert (y, [[7 10 1 4];[9 13 1 5];[2 3 0 1];[4 6 0 2]]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
104 %! assert (ifftshift (y), x); |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
105 |
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
106 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 %! x = [0:3]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
108 %! x = [x;2*x;3*x+1;4*x+1]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
109 %! y = ifftshift (x,1); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
110 %! assert (y, [[1 4 7 10];[1 5 9 13];[0 1 2 3];[0 2 4 6]]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
111 %! assert (ifftshift (y,1), x); |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
112 |
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
113 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
114 %! x = [0:3]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
115 %! x = [x;2*x;3*x+1;4*x+1]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
116 %! y = ifftshift (x,2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
117 %! assert (y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
118 %! assert (ifftshift (y,2), x); |
12541
dd2c70b30f28
Add tests for ifftshift.m
Robert T. Short <octave@phaselockedsystems.com.com>
parents:
12536
diff
changeset
|
119 |
20447
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
120 %!test |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
121 %! x = "efgabcd"; |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
122 %! y = ifftshift (x); |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
123 %! assert (y, "abcdefg"); |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
124 %! assert (ifftshift (y), "defgabc"); |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
125 |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
126 ## Test N-dimensional input (bug #45207) |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
127 %!test |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
128 %! x = [0:3]; |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
129 %! x = x + x' + reshape (x, [1 1 4]); |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
130 %! y1 = [4 5 2 3; 5 6 3 4; 2 3 0 1; 3 4 1 2]; |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
131 %! y = ifftshift (x); |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
132 %! assert (y, reshape ([y1 + 2, y1 + 3, y1, y1 + 1], [4 4 4])); |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
133 %! assert (ifftshift (y), x); |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
134 |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
135 %% Test input validation |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
136 %!error ifftshift () |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
137 %!error ifftshift (1, 2, 3) |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
138 %!error ifftshift (0:3, -1) |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
139 %!error ifftshift (0:3, 0:3) |
abbe33bf0c74
fftshift.m, ifftshift.m: Restore support for N-dimensional arrays (bug #45207)
Mike Miller <mtmiller@octave.org>
parents:
20375
diff
changeset
|
140 |