Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sampling a spline

13 views
Skip to first unread message

Hagwood, Charles R.

unread,
Dec 31, 2009, 3:12:43 AM12/31/09
to
How does one uniformly sample a spline gotten from the command SplineFit?

Charles Hagwood


dh

unread,
Jan 1, 2010, 5:33:03 AM1/1/10
to
Hi Charles,
as I undersatnd your question, you want to sample the spline at
equidistant points.
Short question, long answer.
The reason this is not simple is that a spline is not a function, but
a graphics object. Therefore, operations that are defined for
functions do not work with a SplineFunction.
To obtain a equidistant sampling we may re-parametrize the spline with
the curve length.
Toward this aim, we describe the spline by an approximating numerical
function. Assume we have data and fit a spline:
d = RandomReal[1, {5, 2}]
sp = SplineFit[d, Cubic];
The spline is a vector function. We can approximate it ny a numerical
function by:
f1[t_?NumericQ] := sp[t][[1]];
f2[t_?NumericQ] := sp[t][[2]];
fun = Evaluate@{Evaluate@
FunctionInterpolation[f1[t], {t, 0, 4},
InterpolationOrder -> 4][#],
Evaluate@
FunctionInterpolation[f2[t], {t, 0, 4},
InterpolationOrder -> 4][#]} &;
Note that we need to increase the default order to obtain a reasonable
accuracy.
We can now calculate the length of the tangent in the original
parametrisation. Using this we can get the curve length as a function
of the original parameter:
ltan = FunctionInterpolation[Norm[fun'[tt]], {tt, 0, 4}]
s = FunctionInterpolation[ Integrate[ltan[ttt], {ttt, 0, tt}], {tt, 0,
4}]
To obtain the original parameter as a fzunction of the curve length,
we need to invert this function. W may do this using the fact that the
derivative of the inverse function is the inverse of the original
function:
param = par /. NDSolve[{par'[x] == 1/ltan[par[x]], par[0] == 0},
par, {x, 0, s[4]}][[1]]
This gives us the original parameter as a function of the curve
length.

Here is the whole example with a plot where the data points are black
and the equidistant points are red:
Needs["Splines`"];
d = RandomReal[1, {5, 2}]
sp = SplineFit[d, Cubic];

f1[t_?NumericQ] := sp[t][[1]];
f2[t_?NumericQ] := sp[t][[2]];
fun = Evaluate@{Evaluate@
FunctionInterpolation[f1[t], {t, 0, 4},
InterpolationOrder -> 4][#],
Evaluate@
FunctionInterpolation[f2[t], {t, 0, 4},
InterpolationOrder -> 4][#]} &;

ltan = FunctionInterpolation[Norm[fun'[tt]], {tt, 0, 4}]
s = FunctionInterpolation[
Integrate[ltan[ttt], {ttt, 0, tt}], {tt, 0, 4}]
param = par /.
NDSolve[{par'[x] == 1/ltan[par[x]], par[0] == 0},
par, {x, 0, s[4]}][[1]]

ParametricPlot[fun[param[t]], {t, 0, s[4]},
Epilog -> {Red,
Point[Table[sp[Min[4, Max[0, param[t]]]], {t, 0, s[4], s[4]/100}]],
Black, Point[d]}]

Daniel


On 31 Dez., 09:12, "Hagwood, Charles R." <charles.hagw...@nist.gov>
wrote:

David Annetts

unread,
Jan 1, 2010, 5:36:52 AM1/1/10
to
Hi Charles,

> How does one uniformly sample a spline gotten from the
> command SplineFit?

Did you try Table?

Needs["Splines`"]
pnt = {{0, 0}, {1, 1}, {2, 4}, {3, 9}, {4, 16}};
spl = SplineFit[pnt, Cubic]
tbl = Table[spl[t], {t, 0, 4, .2}]
Show[{
ListPlot[pnt, Joined -> True],
ListPlot[tbl, PlotStyle -> Red]
}
]

Regards,

Dave.


0 new messages