]> Creatis software - CreaPhase.git/blob - octave_packages/general-1.3.1/unresamp2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / general-1.3.1 / unresamp2.m
1 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn{Function File} {[@var{xs}, @var{ys}] =} unresamp2 (@var{x}, @var{y}, @var{n})
18 ## Perform a uniform resampling of a planar curve.
19 ## The arrays @var{x} and @var{y} specify x and y coordinates of the points of the curve.
20 ## On return, the same curve is approximated by @var{xs}, @var{ys} that have length @var{n}
21 ## and the distances between successive points are approximately equal.
22 ## @end deftypefn
23
24 ## Author: Jaroslav Hajek <highegg@gmail.com>
25
26 function [xs, ys] = unresamp2 (x, y, n)
27   if (! isvector (x) || ! size_equal (x, y) || ! isscalar (n))
28     print_usage ();
29   endif
30
31   if (rows (x) == 1)
32     rowvec = true;
33     x = x.'; y = y.';
34   else
35     rowvec = false;
36   endif
37
38   # first differences
39   dx = diff (x); dy = diff (y);
40   # arc lengths
41   ds = hypot (dx, dy);
42   # cumulative integral
43   s = cumsum ([0; ds]);
44   # generate sample points
45   i = linspace (0, s(end), n);
46   if (! rowvec)
47     i = i.';
48   endif
49   # and resample
50   xs = interp1 (s, x, i);
51   ys = interp1 (s, y, i);
52 endfunction
53
54 %!demo
55 %! R = 2; r = 3; d = 1.5;
56 %! th = linspace (0, 2*pi, 1000);
57 %! x = (R-r) * cos (th) + d*sin ((R-r)/r * th);
58 %! y = (R-r) * sin (th) + d*cos ((R-r)/r * th);
59 %! x += 0.3*exp (-(th-0.8*pi).^2); 
60 %! y += 0.4*exp (-(th-0.9*pi).^2); 
61 %! 
62 %! [xs, ys] = unresamp2 (x, y, 40);
63 %! plot (x, y, "-", xs, ys, "*");
64 %! title ("uniform resampling")