Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe01.iad.POSTED!7564ea0f!not-for-mail
From: Doug Schwarz <see@sig.for.address.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: equally spaced data points over a curve
References: <gf9fl8$4kh$1@fred.mathworks.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
User-Agent: MT-NewsWatcher/3.5.2 (Intel Mac OS X)
Message-ID: <see-AE3628.11371810112008@news.frontiernet.net>
Lines: 62
X-Complaints-To: abuse-news@frontiernet.net
NNTP-Posting-Date: Mon, 10 Nov 2008 16:37:20 UTC
Organization: Frontier
Date: Mon, 10 Nov 2008 11:37:19 -0500
Xref: news.mathworks.com comp.soft-sys.matlab:499999


In article <gf9fl8$4kh$1@fred.mathworks.com>,
 "Dave Brackett" <davebrackett@hotmail.com> wrote:

> Hi, 
> 
> I have plotted a curve which connects up some data points. The curve is not a 
> straight line.
> 
> I would like to compute a new set of data points from the plotted curve, but 
> equally spaced over the length of the curve. The number of points would be 
> specified which would control the resolution. 
> 
> Does anyone know how I could do this? If you need any more information please 
> ask. Thanks.

Here's something I plan on contributing to the FEX when I get a chance 
to clean it up and document it.  It's not especially fast, but if you 
already have x and y in functional form it could be faster.


----------------- linspacearc.m ---------------------------
function [x2,y2] = linspacearc(x,y,n)
m = length(x);
t = linspace(0,1,m);
ppx = spline(t,x);
ppy = spline(t,y);
 
dppx = pp_deriv(ppx);
dppy = pp_deriv(ppy);
integrand = @(tt) sqrt(ppval(dppx,tt).^2 + ppval(dppy,tt).^2);
arc_length = quadgk(integrand,0,1);
s = linspace(0,arc_length,n);
 
inv_arc_len = @(arc,est) fzero(@(u)(quadgk(integrand,0,u)) - arc,est);
 
t2 = zeros(1,n);
t2(1) = inv_arc_len(s(1),0);
for i = 2:n
    t2(i) = inv_arc_len(s(i),t2(i-1));
end
 
x2 = ppval(ppx,t2);
y2 = ppval(ppy,t2);
 
 
function dpp = pp_deriv(pp)
% pp_deriv: derivative of piecewise polynomial (pp)
 
dpp = pp;
n = pp.order;
dpp.coefs = bsxfun(@times,n-1:-1:1,pp.coefs(:,1:n-1));
dpp.order = n - 1;
-------------------------------------------------------------


Simply pass in your x and y vectors and the number of desired output 
points and get back your new x and y vectors.

-- 
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.