3942
|
1 ## Copyright (C) 2000 Bill Lash |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3942
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {@var{b} =} unwrap (@var{a}, @var{tol}, @var{dim}) |
|
22 ## |
|
23 ## Unwrap radian phases by adding multiples of 2*pi as appropriate to |
|
24 ## remove jumps greater than @var{tol}. @var{tol} defaults to pi. |
|
25 ## |
4862
|
26 ## Unwrap will unwrap along the first non-singleton dimension of |
|
27 ## @var{a}, unless the optional argument @var{dim} is given, in |
|
28 ## which case the data will be unwrapped along this dimension |
3942
|
29 ## @end deftypefn |
|
30 |
|
31 ## Author: Bill Lash <lash@tellabs.com> |
|
32 |
|
33 function retval = unwrap (a, tol, dim) |
|
34 |
|
35 if (nargin < 1 || nargin > 3) |
|
36 usage ("unwrap (a [, tol [, dim]])") |
|
37 endif |
|
38 |
4862
|
39 nd = ndims (a); |
|
40 sz = size (a); |
|
41 |
|
42 if (nargin == 3) |
|
43 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
44 dim < (nd + 1)) |
|
45 error ("unwrap: dim must be an integer and valid dimension"); |
|
46 endif |
|
47 else |
|
48 ## Find the first non-singleton dimension |
|
49 dim = 1; |
|
50 while (dim < nd + 1 && sz (dim) == 1) |
|
51 dim = dim + 1; |
|
52 endwhile |
|
53 if (dim > nd) |
3942
|
54 dim = 1; |
|
55 endif |
|
56 endif |
|
57 |
4862
|
58 if (nargin < 2 || isempty (tol)) |
3942
|
59 tol = pi; |
|
60 endif |
|
61 |
|
62 ## Don't let anyone use a negative value for TOL. |
|
63 tol = abs (tol); |
|
64 |
|
65 rng = 2*pi; |
4862
|
66 m = sz (dim); |
3942
|
67 |
|
68 ## Handle case where we are trying to unwrap a scalar, or only have |
|
69 ## one sample in the specified dimension. |
|
70 if (m == 1) |
|
71 retval = a; |
|
72 return; |
|
73 endif |
|
74 |
|
75 ## Take first order difference to see so that wraps will show up |
|
76 ## as large values, and the sign will show direction. |
4862
|
77 idx = cell (); |
|
78 for i = 1:nd |
|
79 idx {i} = 1:sz(i); |
|
80 endfor |
|
81 idx {dim} = [1,1:m-1]; |
|
82 d = a (idx {:}) - a; |
3942
|
83 |
|
84 ## Find only the peaks, and multiply them by the range so that there |
|
85 ## are kronecker deltas at each wrap point multiplied by the range |
|
86 ## value. |
|
87 p = rng * (((d > tol) > 0) - ((d < -tol) > 0)); |
|
88 |
|
89 ## Now need to "integrate" this so that the deltas become steps. |
4862
|
90 r = cumsum (p, dim); |
3942
|
91 |
|
92 ## Now add the "steps" to the original data and put output in the |
|
93 ## same shape as originally. |
4862
|
94 retval = a + r; |
3942
|
95 |
|
96 endfunction |