Mercurial > hg > octave-lyh
annotate scripts/signal/unwrap.m @ 11540:b0ef6f28e09a
deprecate krylovb function
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 15 Jan 2011 03:40:32 -0500 |
parents | fd0a3ac60b0e |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 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 -*- | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
20 ## @deftypefn {Function File} {@var{b} =} unwrap (@var{x}) |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{b} =} unwrap (@var{x}, @var{tol}) |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{b} =} unwrap (@var{x}, @var{tol}, @var{dim}) |
3942 | 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 ## | |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10690
diff
changeset
|
27 ## Unwrap will work along the dimension @var{dim}. If @var{dim} |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
28 ## is unspecified it defaults to the first non-singleton dimension. |
3942 | 29 ## @end deftypefn |
30 | |
31 ## Author: Bill Lash <lash@tellabs.com> | |
32 | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
33 function retval = unwrap (x, tol, dim) |
3942 | 34 |
35 if (nargin < 1 || nargin > 3) | |
6046 | 36 print_usage (); |
3942 | 37 endif |
38 | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
39 if (!isnumeric(x)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11471
diff
changeset
|
40 error ("unwrap: X must be a numeric matrix or vector"); |
3942 | 41 endif |
42 | |
4862 | 43 if (nargin < 2 || isempty (tol)) |
3942 | 44 tol = pi; |
45 endif | |
46 | |
47 ## Don't let anyone use a negative value for TOL. | |
48 tol = abs (tol); | |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
49 |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
50 nd = ndims (x); |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
51 sz = size (x); |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
52 if (nargin == 3) |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10711
diff
changeset
|
53 if (!(isscalar (dim) && dim == fix (dim)) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10711
diff
changeset
|
54 || !(1 <= dim && dim <= nd)) |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
55 error ("unwrap: DIM must be an integer and a valid dimension"); |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
56 endif |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
57 else |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
58 ## Find the first non-singleton dimension |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
59 dim = find (sz > 1, 1); |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
60 if (isempty (dim)) |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
61 dim = 1; |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
62 endif |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
63 endif |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
64 |
3942 | 65 rng = 2*pi; |
7208 | 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) | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
71 retval = x; |
3942 | 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 | |
7208 | 79 idx{i} = 1:sz(i); |
4862 | 80 endfor |
7208 | 81 idx{dim} = [1,1:m-1]; |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
82 d = x(idx{:}) - x; |
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. | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
94 retval = x + r; |
3942 | 95 |
96 endfunction | |
7411 | 97 |
98 %!function t = xassert(a,b,tol) | |
99 %! if (nargin == 1) | |
100 %! t = all(a(:)); | |
101 %! else | |
102 %! if (nargin == 2) | |
103 %! tol = 0; | |
104 %! endif | |
105 %! if (any (size(a) != size(b))) | |
106 %! t = 0; | |
107 %! elseif (any (abs(a(:) - b(:)) > tol)) | |
108 %! t = 0; | |
109 %! else | |
110 %! t = 1; | |
111 %! endif | |
112 %! endif | |
113 %! | |
114 %!test | |
115 %! | |
116 %! i = 0; | |
117 %! t = []; | |
118 %! | |
119 %! r = [0:100]; # original vector | |
120 %! w = r - 2*pi*floor((r+pi)/(2*pi)); # wrapped into [-pi,pi] | |
121 %! tol = 1e3*eps; # maximum expected deviation | |
122 %! | |
123 %! t(++i) = xassert(r, unwrap(w), tol); #unwrap single row | |
124 %! t(++i) = xassert(r', unwrap(w'), tol); #unwrap single column | |
125 %! t(++i) = xassert([r',r'], unwrap([w',w']), tol); #unwrap 2 columns | |
126 %! t(++i) = xassert([r;r], unwrap([w;w],[],2), tol); #verify that dim works | |
127 %! t(++i) = xassert(r+10, unwrap(10+w), tol); #verify that r(1)>pi works | |
128 %! | |
129 %! t(++i) = xassert(w', unwrap(w',[],2)); #unwrap col by rows should not change it | |
130 %! t(++i) = xassert(w, unwrap(w,[],1)); #unwrap row by cols should not change it | |
131 %! t(++i) = xassert([w;w], unwrap([w;w])); #unwrap 2 rows by cols should not change them | |
132 %! | |
133 %! ## verify that setting tolerance too low will cause bad results. | |
134 %! t(++i) = xassert(any(abs(r - unwrap(w,0.8)) > 100)); | |
135 %! | |
136 %! assert(all(t)); | |
137 |