Mercurial > hg > octave-lyh
annotate scripts/signal/unwrap.m @ 13500:40bd465b6c79
Removed old gui.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Sun, 17 Jul 2011 17:42:43 +0200 |
parents | 2783fa95cab7 |
children | 7cdf39348879 |
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}) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
23 ## |
3942 | 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) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
34 |
3942 | 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 |
12674
9493880928c8
Use common idiom in m-files for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
58 ## Find the first non-singleton dimension. |
9493880928c8
Use common idiom in m-files for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
59 (dim = find (sz > 1, 1)) || (dim = 1); |
10690
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
60 endif |
35adf2a71f3f
Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
61 |
3942 | 62 rng = 2*pi; |
7208 | 63 m = sz(dim); |
3942 | 64 |
65 ## Handle case where we are trying to unwrap a scalar, or only have | |
66 ## one sample in the specified dimension. | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 if (m == 1) |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
68 retval = x; |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
69 return; |
3942 | 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. | |
12676
2783fa95cab7
Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents:
12674
diff
changeset
|
74 idx = repmat ({':'}, nd, 1); |
7208 | 75 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
|
76 d = x(idx{:}) - x; |
3942 | 77 |
78 ## Find only the peaks, and multiply them by the range so that there | |
79 ## are kronecker deltas at each wrap point multiplied by the range | |
80 ## value. | |
81 p = rng * (((d > tol) > 0) - ((d < -tol) > 0)); | |
82 | |
83 ## Now need to "integrate" this so that the deltas become steps. | |
4862 | 84 r = cumsum (p, dim); |
3942 | 85 |
86 ## Now add the "steps" to the original data and put output in the | |
87 ## 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
|
88 retval = x + r; |
3942 | 89 |
90 endfunction | |
7411 | 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 %! | |
108 %!test | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
109 %! |
7411 | 110 %! i = 0; |
111 %! t = []; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
112 %! |
7411 | 113 %! r = [0:100]; # original vector |
114 %! w = r - 2*pi*floor((r+pi)/(2*pi)); # wrapped into [-pi,pi] | |
115 %! tol = 1e3*eps; # maximum expected deviation | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
116 %! |
7411 | 117 %! t(++i) = xassert(r, unwrap(w), tol); #unwrap single row |
118 %! t(++i) = xassert(r', unwrap(w'), tol); #unwrap single column | |
119 %! t(++i) = xassert([r',r'], unwrap([w',w']), tol); #unwrap 2 columns | |
120 %! t(++i) = xassert([r;r], unwrap([w;w],[],2), tol); #verify that dim works | |
121 %! t(++i) = xassert(r+10, unwrap(10+w), tol); #verify that r(1)>pi works | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
122 %! |
7411 | 123 %! t(++i) = xassert(w', unwrap(w',[],2)); #unwrap col by rows should not change it |
124 %! t(++i) = xassert(w, unwrap(w,[],1)); #unwrap row by cols should not change it | |
125 %! t(++i) = xassert([w;w], unwrap([w;w])); #unwrap 2 rows by cols should not change them | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
126 %! |
7411 | 127 %! ## verify that setting tolerance too low will cause bad results. |
128 %! t(++i) = xassert(any(abs(r - unwrap(w,0.8)) > 100)); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
129 %! |
7411 | 130 %! assert(all(t)); |
131 |