changelog shortlog tags changeset files revisions annotate raw

scripts/signal/unwrap.m

changeset 10289: 4b124317dc38
parent:83a8781b529d
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (38 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2000, 2002, 2004, 2005, 2006, 2007, 2008 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{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##
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
28## @end deftypefn
29
30## Author: Bill Lash <lash@tellabs.com>
31
32function retval = unwrap (a, tol, dim)
33
34 if (nargin < 1 || nargin > 3)
35 print_usage ();
36 endif
37
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;
49 while (dim < nd + 1 && sz(dim) == 1)
50 dim = dim + 1;
51 endwhile
52 if (dim > nd)
53 dim = 1;
54 endif
55 endif
56
57 if (nargin < 2 || isempty (tol))
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;
65 m = sz(dim);
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.
76 idx = cell ();
77 for i = 1:nd
78 idx{i} = 1:sz(i);
79 endfor
80 idx{dim} = [1,1:m-1];
81 d = a(idx{:}) - a;
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.
89 r = cumsum (p, dim);
90
91 ## Now add the "steps" to the original data and put output in the
92 ## same shape as originally.
93 retval = a + r;
94
95endfunction
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