annotate scripts/signal/fftshift.m @ 12540:b3c158b8941c

Add tests for fftshift.m.
author Robert T. Short <octave@phaselockedsystems.com.com>
date Sat, 26 Mar 2011 06:42:35 -0700
parents 189baf055143
children 2783fa95cab7 914c0b103a3d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11469
diff changeset
1 ## Copyright (C) 1997-2011 Vincent Cautaerts
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3499
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3191
diff changeset
14 ##
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
18
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
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} {} fftshift (@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} {} fftshift (@var{x}, @var{dim})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
22 ## Perform a shift of the vector @var{x}, for use with the @code{fft}
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
23 ## and @code{ifft} functions, in order the move the frequency 0 to the
3499
3e3e14ad5149 [project @ 2000-01-31 05:18:07 by jwe]
jwe
parents: 3449
diff changeset
24 ## center of the vector or matrix.
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
25 ##
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
26 ## If @var{x} is a vector of @math{N} elements corresponding to @math{N}
3499
3e3e14ad5149 [project @ 2000-01-31 05:18:07 by jwe]
jwe
parents: 3449
diff changeset
27 ## time samples spaced of @math{Dt} each, then @code{fftshift (fft
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
28 ## (@var{x}))} corresponds to frequencies
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
29 ##
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
30 ## @example
4077
b5267e631ba8 [project @ 2002-09-27 22:48:46 by jwe]
jwe
parents: 4030
diff changeset
31 ## f = ((1:N) - ceil(N/2)) / N / Dt
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
32 ## @end example
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
33 ##
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
34 ## If @var{x} is a matrix, the same holds for rows and columns. If
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 ## @var{x} is an array, then the same holds along each dimension.
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
36 ##
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
37 ## The optional @var{dim} argument can be used to limit the dimension
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
38 ## along which the permutation occurs.
3449
858695b3ed62 [project @ 2000-01-18 04:08:59 by jwe]
jwe
parents: 3426
diff changeset
39 ## @end deftypefn
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
40
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
41 ## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp>
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
42 ## Created: July 1997
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
43 ## Adapted-By: jwe
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
44
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
45 function retval = fftshift (x, dim)
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
46
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
47 retval = 0;
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
48
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
49 if (nargin != 1 && nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5307
diff changeset
50 print_usage ();
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
51 endif
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
52
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
53 if (nargin == 2)
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
54 if (!isscalar (dim))
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
55 error ("fftshift: dimension must be an integer scalar");
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
56 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
57 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
58 sz = size (x);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
59 sz2 = ceil (sz(dim) / 2);
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
60 idx = cell ();
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7208
diff changeset
61 for i = 1:nd
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
62 idx{i} = 1:sz(i);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
63 endfor
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
64 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
65 retval = x(idx{:});
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
66 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
67 if (isvector (x))
12463
189baf055143 fftshift.m: Fix broken function documented in bug 32442
Karsten Trulsen <karstent@math.uio.no>
parents: 11587
diff changeset
68 xl = length (x);
189baf055143 fftshift.m: Fix broken function documented in bug 32442
Karsten Trulsen <karstent@math.uio.no>
parents: 11587
diff changeset
69 xx = ceil (xl/2);
189baf055143 fftshift.m: Fix broken function documented in bug 32442
Karsten Trulsen <karstent@math.uio.no>
parents: 11587
diff changeset
70 retval = x([xx+1:xl, 1:xx]);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
71 elseif (ismatrix (x))
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
72 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
73 sz = size (x);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
74 sz2 = ceil (sz ./ 2);
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
75 idx = cell ();
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7208
diff changeset
76 for i = 1:nd
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
77 idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)];
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
78 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
79 retval = x(idx{:});
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
80 else
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
81 error ("fftshift: expecting vector or matrix argument");
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4077
diff changeset
82 endif
3191
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
83 endif
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
84
e4f4b2d26ee9 [project @ 1998-10-23 05:43:59 by jwe]
jwe
parents:
diff changeset
85 endfunction
12540
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
86
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
87 %!test
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
88 %! x = [0:7];
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
89 %! y = fftshift (x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
90 %! assert(y, [4 5 6 7 0 1 2 3]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
91 %! assert(fftshift (y), x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
92
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
93 %!test
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
94 %! x = [0:6];
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
95 %! y = fftshift (x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
96 %! assert(y, [4 5 6 0 1 2 3]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
97 %! assert(fftshift (y), [1 2 3 4 5 6 0]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
98
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
99 %!test
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
100 %! x = [0:7]';
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
101 %! y = fftshift (x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
102 %! assert(y, [4;5;6;7;0;1;2;3]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
103 %! assert(fftshift (y), x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
104
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
105 %!test
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
106 %! x = [0:6]';
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
107 %! y = fftshift (x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
108 %! assert(y, [4;5;6;0;1;2;3]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
109 %! assert(fftshift (y), [1;2;3;4;5;6;0]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
110
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
111 %!test
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
112 %! x = [0:3];
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
113 %! x = [x;2*x;3*x+1;4*x+1];
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
114 %! y = fftshift (x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
115 %! assert(y, [[7 10 1 4];[9 13 1 5];[2 3 0 1];[4 6 0 2]]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
116 %! assert(fftshift (y), x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
117
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
118 %!test
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
119 %! x = [0:3];
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
120 %! x = [x;2*x;3*x+1;4*x+1];
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
121 %! y = fftshift (x,1);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
122 %! assert(y, [[1 4 7 10];[1 5 9 13];[0 1 2 3];[0 2 4 6]]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
123 %! assert(fftshift (y,1), x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
124
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
125 %!test
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
126 %! x = [0:3];
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
127 %! x = [x;2*x;3*x+1;4*x+1];
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
128 %! y = fftshift (x,2);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
129 %! assert(y, [[2 3 0 1];[4 6 0 2];[7 10 1 4];[9 13 1 5]]);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
130 %! assert(fftshift (y,2), x);
b3c158b8941c Add tests for fftshift.m.
Robert T. Short <octave@phaselockedsystems.com.com>
parents: 12463
diff changeset
131