annotate scripts/signal/unwrap.m @ 12312:b10ea6efdc58 release-3-4-x ss-3-3-91

version is now 3.3.91
author John W. Eaton <jwe@octave.org>
date Mon, 31 Jan 2011 08:36:58 -0500
parents c792872f8942
children 9493880928c8
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: 11472
diff changeset
1 ## Copyright (C) 2000-2011 Bill Lash
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
2 ##
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
4 ##
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
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.
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
9 ##
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
14 ##
2ca2d23a49a7 [project @ 2002-05-16 20:27: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/>.
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
18
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
11471
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
20 ## @deftypefn {Function File} {@var{b} =} unwrap (@var{x})
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
21 ## @deftypefnx {Function File} {@var{b} =} unwrap (@var{x}, @var{tol})
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
22 ## @deftypefnx {Function File} {@var{b} =} unwrap (@var{x}, @var{tol}, @var{dim})
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
23 ##
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
24 ## Unwrap radian phases by adding multiples of 2*pi as appropriate to
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
25 ## remove jumps greater than @var{tol}. @var{tol} defaults to pi.
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
26 ##
10711
fbd7843974fa Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents: 10690
diff changeset
27 ## Unwrap will work along the dimension @var{dim}. If @var{dim}
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
28 ## is unspecified it defaults to the first non-singleton dimension.
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
29 ## @end deftypefn
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
30
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
31 ## Author: Bill Lash <lash@tellabs.com>
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
32
11471
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
33 function retval = unwrap (x, tol, dim)
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
34
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
35 if (nargin < 1 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5307
diff changeset
36 print_usage ();
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
37 endif
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
38
11471
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
39 if (!isnumeric(x))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 11471
diff changeset
40 error ("unwrap: X must be a numeric matrix or vector");
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
41 endif
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
42
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4826
diff changeset
43 if (nargin < 2 || isempty (tol))
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
44 tol = pi;
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
45 endif
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
46
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
47 ## Don't let anyone use a negative value for TOL.
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
48 tol = abs (tol);
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
49
11471
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
50 nd = ndims (x);
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
51 sz = size (x);
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
52 if (nargin == 3)
11149
fe3c3dfc07eb style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents: 10711
diff changeset
53 if (!(isscalar (dim) && dim == fix (dim))
fe3c3dfc07eb style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents: 10711
diff changeset
54 || !(1 <= dim && dim <= nd))
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
55 error ("unwrap: DIM must be an integer and a valid dimension");
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
56 endif
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
57 else
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
58 ## Find the first non-singleton dimension
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
59 dim = find (sz > 1, 1);
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
60 if (isempty (dim))
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
61 dim = 1;
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
62 endif
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
63 endif
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
64
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
65 rng = 2*pi;
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
66 m = sz(dim);
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
67
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
68 ## Handle case where we are trying to unwrap a scalar, or only have
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
69 ## one sample in the specified dimension.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
70 if (m == 1)
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
71 retval = x;
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
72 return;
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
73 endif
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
74
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
75 ## Take first order difference to see so that wraps will show up
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
76 ## as large values, and the sign will show direction.
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4826
diff changeset
77 idx = cell ();
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4826
diff changeset
78 for i = 1:nd
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
79 idx{i} = 1:sz(i);
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4826
diff changeset
80 endfor
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
81 idx{dim} = [1,1:m-1];
11471
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
82 d = x(idx{:}) - x;
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
83
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
84 ## Find only the peaks, and multiply them by the range so that there
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
85 ## are kronecker deltas at each wrap point multiplied by the range
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
86 ## value.
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
87 p = rng * (((d > tol) > 0) - ((d < -tol) > 0));
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
88
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
89 ## Now need to "integrate" this so that the deltas become steps.
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4826
diff changeset
90 r = cumsum (p, dim);
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
91
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
92 ## Now add the "steps" to the original data and put output in the
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
93 ## same shape as originally.
11471
994e2a93a8e2 Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
94 retval = x + r;
3942
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
95
2ca2d23a49a7 [project @ 2002-05-16 20:27:59 by jwe]
jwe
parents:
diff changeset
96 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
97
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
98 %!function t = xassert(a,b,tol)
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
99 %! if (nargin == 1)
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
100 %! t = all(a(:));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
101 %! else
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
102 %! if (nargin == 2)
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
103 %! tol = 0;
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
104 %! endif
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
105 %! if (any (size(a) != size(b)))
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
106 %! t = 0;
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
107 %! elseif (any (abs(a(:) - b(:)) > tol))
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
108 %! t = 0;
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
109 %! else
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
110 %! t = 1;
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
111 %! endif
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
112 %! endif
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
113 %!
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
114 %!test
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
115 %!
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
116 %! i = 0;
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
117 %! t = [];
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
118 %!
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
119 %! r = [0:100]; # original vector
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
120 %! w = r - 2*pi*floor((r+pi)/(2*pi)); # wrapped into [-pi,pi]
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
121 %! tol = 1e3*eps; # maximum expected deviation
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
122 %!
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
123 %! t(++i) = xassert(r, unwrap(w), tol); #unwrap single row
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
124 %! t(++i) = xassert(r', unwrap(w'), tol); #unwrap single column
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
125 %! t(++i) = xassert([r',r'], unwrap([w',w']), tol); #unwrap 2 columns
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
126 %! t(++i) = xassert([r;r], unwrap([w;w],[],2), tol); #verify that dim works
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
127 %! t(++i) = xassert(r+10, unwrap(10+w), tol); #verify that r(1)>pi works
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
128 %!
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
129 %! t(++i) = xassert(w', unwrap(w',[],2)); #unwrap col by rows should not change it
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
130 %! t(++i) = xassert(w, unwrap(w,[],1)); #unwrap row by cols should not change it
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
131 %! t(++i) = xassert([w;w], unwrap([w;w])); #unwrap 2 rows by cols should not change them
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
132 %!
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
133 %! ## verify that setting tolerance too low will cause bad results.
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
134 %! t(++i) = xassert(any(abs(r - unwrap(w,0.8)) > 100));
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
135 %!
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
136 %! assert(all(t));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
137