]> Creatis software - CreaPhase.git/blob - octave_packages/m/signal/unwrap.m
update packages
[CreaPhase.git] / octave_packages / m / signal / unwrap.m
1 ## Copyright (C) 2000-2012 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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
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})
23 ##
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.
26 ##
27 ## Unwrap will work along the dimension @var{dim}.  If @var{dim}
28 ## is unspecified it defaults to the first non-singleton dimension.
29 ## @end deftypefn
30
31 ## Author: Bill Lash <lash@tellabs.com>
32
33 function retval = unwrap (x, tol, dim)
34
35   if (nargin < 1 || nargin > 3)
36     print_usage ();
37   endif
38
39   if (!isnumeric(x))
40     error ("unwrap: X must be a numeric matrix or vector");
41   endif
42
43   if (nargin < 2 || isempty (tol))
44     tol = pi;
45   endif
46
47   ## Don't let anyone use a negative value for TOL.
48   tol = abs (tol);
49
50   nd = ndims (x);
51   sz = size (x);
52   if (nargin == 3)
53     if (!(isscalar (dim) && dim == fix (dim))
54         || !(1 <= dim && dim <= nd))
55       error ("unwrap: DIM must be an integer and a valid dimension");
56     endif
57   else
58     ## Find the first non-singleton dimension.
59     (dim = find (sz > 1, 1)) || (dim = 1);
60   endif
61
62   rng = 2*pi;
63   m = sz(dim);
64
65   ## Handle case where we are trying to unwrap a scalar, or only have
66   ## one sample in the specified dimension.
67   if (m == 1)
68     retval = x;
69     return;
70   endif
71
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);
75   idx{dim} = [1,1:m-1];
76   d = x(idx{:}) - x;
77
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));
82
83   ## Now need to "integrate" this so that the deltas become steps.
84   r = cumsum (p, dim);
85
86   ## Now add the "steps" to the original data and put output in the
87   ## same shape as originally.
88   retval = x + r;
89
90 endfunction
91
92 %!function t = __xassert(a,b,tol)
93 %!  if (nargin == 1)
94 %!    t = all(a(:));
95 %!  else
96 %!    if (nargin == 2)
97 %!      tol = 0;
98 %!    endif
99 %!    if (any (size(a) != size(b)))
100 %!      t = 0;
101 %!    elseif (any (abs(a(:) - b(:)) > tol))
102 %!      t = 0;
103 %!    else
104 %!      t = 1;
105 %!    endif
106 %!  endif
107 %!endfunction
108 %!
109 %!test
110 %!
111 %! i = 0;
112 %! t = [];
113 %!
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
117 %!
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
123 %!
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
127 %!
128 %! ## verify that setting tolerance too low will cause bad results.
129 %! t(++i) = __xassert(any(abs(r - unwrap(w,0.8)) > 100));
130 %!
131 %! assert(all(t));
132 %!
133 %!test
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));
138 %!
139 %!test
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);
149 %!
150 %!test
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));
155 %!
156 %!error unwrap()