1 ## Copyright (C) 2000-2012 Bill Lash
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {@var{b} =} unwrap (@var{x})
21 ## @deftypefnx {Function File} {@var{b} =} unwrap (@var{x}, @var{tol})
22 ## @deftypefnx {Function File} {@var{b} =} unwrap (@var{x}, @var{tol}, @var{dim})
24 ## Unwrap radian phases by adding multiples of 2*pi as appropriate to
25 ## remove jumps greater than @var{tol}. @var{tol} defaults to pi.
27 ## Unwrap will work along the dimension @var{dim}. If @var{dim}
28 ## is unspecified it defaults to the first non-singleton dimension.
31 ## Author: Bill Lash <lash@tellabs.com>
33 function retval = unwrap (x, tol, dim)
35 if (nargin < 1 || nargin > 3)
40 error ("unwrap: X must be a numeric matrix or vector");
43 if (nargin < 2 || isempty (tol))
47 ## Don't let anyone use a negative value for TOL.
53 if (!(isscalar (dim) && dim == fix (dim))
54 || !(1 <= dim && dim <= nd))
55 error ("unwrap: DIM must be an integer and a valid dimension");
58 ## Find the first non-singleton dimension.
59 (dim = find (sz > 1, 1)) || (dim = 1);
65 ## Handle case where we are trying to unwrap a scalar, or only have
66 ## one sample in the specified dimension.
72 ## Take first order difference to see so that wraps will show up
73 ## as large values, and the sign will show direction.
74 idx = repmat ({':'}, nd, 1);
78 ## Find only the peaks, and multiply them by the appropriate amount
79 ## of ranges so that there are kronecker deltas at each wrap point
80 ## multiplied by the appropriate amount of range values.
81 p = ceil(abs(d)./rng) .* rng .* (((d > tol) > 0) - ((d < -tol) > 0));
83 ## Now need to "integrate" this so that the deltas become steps.
86 ## Now add the "steps" to the original data and put output in the
87 ## same shape as originally.
92 %!function t = __xassert(a,b,tol)
99 %! if (any (size(a) != size(b)))
101 %! elseif (any (abs(a(:) - b(:)) > tol))
114 %! r = [0:100]; # original vector
115 %! w = r - 2*pi*floor((r+pi)/(2*pi)); # wrapped into [-pi,pi]
116 %! tol = 1e3*eps; # maximum expected deviation
118 %! t(++i) = __xassert(r, unwrap(w), tol); #unwrap single row
119 %! t(++i) = __xassert(r', unwrap(w'), tol); #unwrap single column
120 %! t(++i) = __xassert([r',r'], unwrap([w',w']), tol); #unwrap 2 columns
121 %! t(++i) = __xassert([r;r], unwrap([w;w],[],2), tol); #check that dim works
122 %! t(++i) = __xassert(r+10, unwrap(10+w), tol); #check r(1)>pi works
124 %! t(++i) = __xassert(w', unwrap(w',[],2)); #unwrap col by rows should not change it
125 %! t(++i) = __xassert(w, unwrap(w,[],1)); #unwrap row by cols should not change it
126 %! t(++i) = __xassert([w;w], unwrap([w;w])); #unwrap 2 rows by cols should not change them
128 %! ## verify that setting tolerance too low will cause bad results.
129 %! t(++i) = __xassert(any(abs(r - unwrap(w,0.8)) > 100));
134 %! A = [pi*(-4), pi*(-2+1/6), pi/4, pi*(2+1/3), pi*(4+1/2), pi*(8+2/3), pi*(16+1), pi*(32+3/2), pi*64];
135 %! assert (unwrap(A), unwrap(A, pi));
136 %! assert (unwrap(A, pi), unwrap(A, pi, 2));
137 %! assert (unwrap(A', pi), unwrap(A', pi, 1));
140 %! A = [pi*(-4); pi*(2+1/3); pi*(16+1)];
141 %! B = [pi*(-2+1/6); pi*(4+1/2); pi*(32+3/2)];
142 %! C = [pi/4; pi*(8+2/3); pi*64];
143 %! D = [pi*(-2+1/6); pi*(2+1/3); pi*(8+2/3)];
144 %! E(:, :, 1) = [A, B, C, D];
145 %! E(:, :, 2) = [A+B, B+C, C+D, D+A];
146 %! F(:, :, 1) = [unwrap(A), unwrap(B), unwrap(C), unwrap(D)];
147 %! F(:, :, 2) = [unwrap(A+B), unwrap(B+C), unwrap(C+D), unwrap(D+A)];
148 %! assert (unwrap(E), F);
151 %! A = [0, 2*pi, 4*pi, 8*pi, 16*pi, 65536*pi];
152 %! B = [pi*(-2+1/6), pi/4, pi*(2+1/3), pi*(4+1/2), pi*(8+2/3), pi*(16+1), pi*(32+3/2), pi*64];
153 %! assert (unwrap(A), zeros(1, length(A)));
154 %! assert (diff(unwrap(B), 1)<2*pi, true(1, length(B)-1));