X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fgeneral-1.3.1%2Funresamp2.m;fp=octave_packages%2Fgeneral-1.3.1%2Funresamp2.m;h=af836e956b837401bbf98517c942d5edefbbfea0;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/general-1.3.1/unresamp2.m b/octave_packages/general-1.3.1/unresamp2.m new file mode 100644 index 0000000..af836e9 --- /dev/null +++ b/octave_packages/general-1.3.1/unresamp2.m @@ -0,0 +1,64 @@ +## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn{Function File} {[@var{xs}, @var{ys}] =} unresamp2 (@var{x}, @var{y}, @var{n}) +## Perform a uniform resampling of a planar curve. +## The arrays @var{x} and @var{y} specify x and y coordinates of the points of the curve. +## On return, the same curve is approximated by @var{xs}, @var{ys} that have length @var{n} +## and the distances between successive points are approximately equal. +## @end deftypefn + +## Author: Jaroslav Hajek + +function [xs, ys] = unresamp2 (x, y, n) + if (! isvector (x) || ! size_equal (x, y) || ! isscalar (n)) + print_usage (); + endif + + if (rows (x) == 1) + rowvec = true; + x = x.'; y = y.'; + else + rowvec = false; + endif + + # first differences + dx = diff (x); dy = diff (y); + # arc lengths + ds = hypot (dx, dy); + # cumulative integral + s = cumsum ([0; ds]); + # generate sample points + i = linspace (0, s(end), n); + if (! rowvec) + i = i.'; + endif + # and resample + xs = interp1 (s, x, i); + ys = interp1 (s, y, i); +endfunction + +%!demo +%! R = 2; r = 3; d = 1.5; +%! th = linspace (0, 2*pi, 1000); +%! x = (R-r) * cos (th) + d*sin ((R-r)/r * th); +%! y = (R-r) * sin (th) + d*cos ((R-r)/r * th); +%! x += 0.3*exp (-(th-0.8*pi).^2); +%! y += 0.4*exp (-(th-0.9*pi).^2); +%! +%! [xs, ys] = unresamp2 (x, y, 40); +%! plot (x, y, "-", xs, ys, "*"); +%! title ("uniform resampling")