7017
|
1 ## Copyright (C) 2000, 2002, 2004, 2005, 2006, 2007 Bill Lash |
3942
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
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. |
3942
|
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/>. |
3942
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {@var{b} =} unwrap (@var{a}, @var{tol}, @var{dim}) |
|
21 ## |
|
22 ## Unwrap radian phases by adding multiples of 2*pi as appropriate to |
|
23 ## remove jumps greater than @var{tol}. @var{tol} defaults to pi. |
|
24 ## |
4862
|
25 ## Unwrap will unwrap along the first non-singleton dimension of |
|
26 ## @var{a}, unless the optional argument @var{dim} is given, in |
|
27 ## which case the data will be unwrapped along this dimension |
3942
|
28 ## @end deftypefn |
|
29 |
|
30 ## Author: Bill Lash <lash@tellabs.com> |
|
31 |
|
32 function retval = unwrap (a, tol, dim) |
|
33 |
|
34 if (nargin < 1 || nargin > 3) |
6046
|
35 print_usage (); |
3942
|
36 endif |
|
37 |
4862
|
38 nd = ndims (a); |
|
39 sz = size (a); |
|
40 |
|
41 if (nargin == 3) |
|
42 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
43 dim < (nd + 1)) |
|
44 error ("unwrap: dim must be an integer and valid dimension"); |
|
45 endif |
|
46 else |
|
47 ## Find the first non-singleton dimension |
|
48 dim = 1; |
7208
|
49 while (dim < nd + 1 && sz(dim) == 1) |
4862
|
50 dim = dim + 1; |
|
51 endwhile |
|
52 if (dim > nd) |
3942
|
53 dim = 1; |
|
54 endif |
|
55 endif |
|
56 |
4862
|
57 if (nargin < 2 || isempty (tol)) |
3942
|
58 tol = pi; |
|
59 endif |
|
60 |
|
61 ## Don't let anyone use a negative value for TOL. |
|
62 tol = abs (tol); |
|
63 |
|
64 rng = 2*pi; |
7208
|
65 m = sz(dim); |
3942
|
66 |
|
67 ## Handle case where we are trying to unwrap a scalar, or only have |
|
68 ## one sample in the specified dimension. |
|
69 if (m == 1) |
|
70 retval = a; |
|
71 return; |
|
72 endif |
|
73 |
|
74 ## Take first order difference to see so that wraps will show up |
|
75 ## as large values, and the sign will show direction. |
4862
|
76 idx = cell (); |
|
77 for i = 1:nd |
7208
|
78 idx{i} = 1:sz(i); |
4862
|
79 endfor |
7208
|
80 idx{dim} = [1,1:m-1]; |
|
81 d = a(idx{:}) - a; |
3942
|
82 |
|
83 ## Find only the peaks, and multiply them by the range so that there |
|
84 ## are kronecker deltas at each wrap point multiplied by the range |
|
85 ## value. |
|
86 p = rng * (((d > tol) > 0) - ((d < -tol) > 0)); |
|
87 |
|
88 ## Now need to "integrate" this so that the deltas become steps. |
4862
|
89 r = cumsum (p, dim); |
3942
|
90 |
|
91 ## Now add the "steps" to the original data and put output in the |
|
92 ## same shape as originally. |
4862
|
93 retval = a + r; |
3942
|
94 |
|
95 endfunction |
7411
|
96 |
|
97 %!function t = xassert(a,b,tol) |
|
98 %! if (nargin == 1) |
|
99 %! t = all(a(:)); |
|
100 %! else |
|
101 %! if (nargin == 2) |
|
102 %! tol = 0; |
|
103 %! endif |
|
104 %! if (any (size(a) != size(b))) |
|
105 %! t = 0; |
|
106 %! elseif (any (abs(a(:) - b(:)) > tol)) |
|
107 %! t = 0; |
|
108 %! else |
|
109 %! t = 1; |
|
110 %! endif |
|
111 %! endif |
|
112 %! |
|
113 %!test |
|
114 %! |
|
115 %! i = 0; |
|
116 %! t = []; |
|
117 %! |
|
118 %! r = [0:100]; # original vector |
|
119 %! w = r - 2*pi*floor((r+pi)/(2*pi)); # wrapped into [-pi,pi] |
|
120 %! tol = 1e3*eps; # maximum expected deviation |
|
121 %! |
|
122 %! t(++i) = xassert(r, unwrap(w), tol); #unwrap single row |
|
123 %! t(++i) = xassert(r', unwrap(w'), tol); #unwrap single column |
|
124 %! t(++i) = xassert([r',r'], unwrap([w',w']), tol); #unwrap 2 columns |
|
125 %! t(++i) = xassert([r;r], unwrap([w;w],[],2), tol); #verify that dim works |
|
126 %! t(++i) = xassert(r+10, unwrap(10+w), tol); #verify that r(1)>pi works |
|
127 %! |
|
128 %! t(++i) = xassert(w', unwrap(w',[],2)); #unwrap col by rows should not change it |
|
129 %! t(++i) = xassert(w, unwrap(w,[],1)); #unwrap row by cols should not change it |
|
130 %! t(++i) = xassert([w;w], unwrap([w;w])); #unwrap 2 rows by cols should not change them |
|
131 %! |
|
132 %! ## verify that setting tolerance too low will cause bad results. |
|
133 %! t(++i) = xassert(any(abs(r - unwrap(w,0.8)) > 100)); |
|
134 %! |
|
135 %! assert(all(t)); |
|
136 |