Function Repository Resource:

AkimaSpline

Source Notebook

Smooth curve interpolation based on local procedures for a multiple-valued curve

Contributed by: Robert B. Nachbar (Wolfram Solutions)

ResourceFunction["AkimaSpline"][{x1,y1},{x2,y2},}]

represents an Akima-spline function defined by the data {xi, yi}.

Details and Options

ResourceFunction["AkimaSpline"] returns a CompiledFunction object, which can be used like any other pure function.
ResourceFunction["AkimaSpline"][][u] gives a point {x[u], y[u]} on the spline in the xy-plane corresponding to the parameter u.
The interpolation function returned by ResourceFunction["AkimaInterpolation"][data] is set up so as to agree with data at every point explicitly specified in data.
The function arguments must be real numbers.
The interpolation works by fitting a third-degree polynomial curve between successive data points.
The interpolation automatically makes use of local numerical derivatives, thus ensuring a smooth as well as continuous result.
If the data is periodic, only the data for a single fundamental period is required. The option PeriodicInterpolation is used.

Examples

Basic Examples (3) 

Construct an Akima-spline curve using a list of data points:

In[1]:=
pts = {{1, 1}, {2, 2}, {3, 0.5}, {4, 0.5}, {5, 0}};
In[2]:=
f = ResourceFunction["AkimaSpline"][pts]
Out[2]=

Apply the function to find a point on the curve:

In[3]:=
f[0.5]
Out[3]=

Plot the Akima-spline curve with the data points:

In[4]:=
Show[ParametricPlot[f[t], {t, 0, 1}], ListPlot[pts, PlotStyle -> Red]]
Out[4]=

Scope (2) 

Ordinate values can be repeated:

In[5]:=
pts = {{-30, 70}, {-30, 40}, {-30, 20}, {-18, 4}, {0, 0}, {18, 4}, {30, 20}, {30, 40}, {30, 50}, {30, 70}};
In[6]:=
g = ResourceFunction["AkimaSpline"][pts];

Plot the Akima-spline curve with the data points:

In[7]:=
Show[ParametricPlot[g[t], {t, 0, 1}], ListPlot[pts, PlotStyle -> Red]]
Out[7]=

Options (1) 

PeriodicInterpolation (1) 

In[8]:=
pts = With[{n = 12}, N@Table[(1 + 1/4 (-1)^i) {Cos[(2 \[Pi] i)/n], Sin[(2 \[Pi] i)/n]}, {i, 0, n}]];
In[9]:=
fp = ResourceFunction["AkimaSpline"][pts, PeriodicInterpolation -> True]
Out[9]=

Plot the Akima-spline curve with the data points:

In[10]:=
Show[ParametricPlot[fp[t], {t, 0, 1}], ListPlot[pts, PlotStyle -> Red]]
Out[10]=

Applications (2) 

Interpolate random data:

In[11]:=
f = ResourceFunction["AkimaSpline"][r = RandomReal[1, {20, 2}]]
Out[11]=
In[12]:=
Show[ParametricPlot[f[t], {t, 0, 1}], ListPlot[r, PlotStyle -> Red]]
Out[12]=

Sort the points into a traveling salesman tour:

In[13]:=
tour = Last@FindShortestTour[r]
Out[13]=
In[14]:=
ft = ResourceFunction["AkimaSpline"][r[[tour]]]
Out[14]=
In[15]:=
Show[ParametricPlot[ft[t], {t, 0, 1}], ListPlot[r, PlotStyle -> Red]]
Out[15]=

Neat Examples (2) 

Interesting knot-like figures can be drawn:

In[16]:=
input = {{1, 1}, {0, -0.5}, {-1, 1}, {0.5, 0}, {-1, -1}, {0, 0.5}, {1, -1}, {-0.5, 0}, {1, 1}};
ifun = ResourceFunction["AkimaSpline"][input, PeriodicInterpolation -> True];
ParametricPlot[ifun[z], {z, 0, 1}, Axes -> False, Frame -> True, AspectRatio -> 1, PlotRange -> All, Epilog -> {Red, PointSize[0.02], Point[input]}]
Out[18]=

A “braided” Spikey:

In[19]:=
spikeyData = Graphics[{
GraphicsComplex[{{0, 0}, {(Rational[
         5, 8] + Rational[-1, 8] 5^Rational[1, 2])^Rational[1, 2], Rational[
        1, 4] (-1 - 5^Rational[1, 2])}, {0, -(
         2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
         1, 2]}, {(
        Rational[1, 4] (1 + 5^Rational[1, 2])) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[
         1, 2], -((Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2]) (
          2 + Rational[1, 2] (1 + 5^Rational[1, 2])))^Rational[
         1, 2]}, {(
        Rational[1, 4] (-1 + 5^Rational[1, 2])) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[
         1, 2], -((Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2]) (
          2 + Rational[1, 2] (1 + 5^Rational[1, 2])))^Rational[
         1, 2]}, {(
         Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2])^Rational[
         1, 2] (1 + (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2]), (Rational[1, 4] (-1 - 5^Rational[1, 2])) (
        1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2])}, {0, -1 - (
        2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[1, 2]}, {(
        Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2])^Rational[
        1, 2], Rational[
        1, 4] (-1 + 5^Rational[1, 2])}, {((
         Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2]) (
         2 + Rational[1, 2] (-1 + 5^Rational[1, 2])))^Rational[
        1, 2], (Rational[1, 4] (1 - 5^Rational[1, 2])) (
         2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
         1, 2]}, {(
        Rational[1, 4] (1 + 5^Rational[1, 2])) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[1, 2], ((
         Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2]) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2])))^Rational[
        1, 2]}, {(2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[
        1, 2], 0}, {(
         Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2])^Rational[
         1, 2] (1 + (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2]), (Rational[1, 4] (-1 + 5^Rational[1, 2])) (
        1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2])}, {(
         Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2])^Rational[
         1, 2] (1 + (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2]), (Rational[1, 4] (1 - 5^Rational[1, 2])) (
        1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2])}, {0, 1}, {((Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2]) (
         2 + Rational[1, 2] (-1 + 5^Rational[1, 2])))^Rational[
        1, 2], (Rational[1, 4] (1 + 5^Rational[1, 2])) (
         2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
         1, 2]}, {(
        Rational[1, 4] (1 - 5^Rational[1, 2])) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[1, 2], ((
         Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2]) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2])))^Rational[
        1, 2]}, {(
        Rational[1, 4] (-1 + 5^Rational[1, 2])) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[1, 2], ((
         Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2]) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2])))^Rational[
        1, 2]}, {0, 1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
         1, 2]}, {(
         Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2])^Rational[
         1, 2] (1 + (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2]), (Rational[1, 4] (1 + 5^Rational[1, 2])) (
        1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2])}, {-(
         Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2])^Rational[
         1, 2], Rational[
        1, 4] (-1 + 5^Rational[1, 2])}, {-((
          Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2]) (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2])))^Rational[
         1, 2], (Rational[1, 4] (1 + 5^Rational[1, 2])) (
         2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
         1, 2]}, {-(
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[1, 2], 0}, {(Rational[1, 4] (-1 - 5^Rational[1, 2])) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[1, 2], ((
         Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2]) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2])))^Rational[
        1, 2]}, {(-(
          Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2])^Rational[
          1, 2]) (1 + (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2]), (Rational[1, 4] (-1 + 5^Rational[1, 2])) (
        1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2])}, {(-(
          Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2])^Rational[
          1, 2]) (1 + (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2]), (Rational[1, 4] (1 + 5^Rational[1, 2])) (
        1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2])}, {-(
         Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2])^Rational[
         1, 2], Rational[
        1, 4] (-1 - 5^Rational[
         1, 2])}, {-((
          Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2]) (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2])))^Rational[
         1, 2], (Rational[1, 4] (1 - 5^Rational[1, 2])) (
         2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
         1, 2]}, {(
        Rational[1, 4] (1 - 5^Rational[1, 2])) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[
         1, 2], -((Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2]) (
          2 + Rational[1, 2] (1 + 5^Rational[1, 2])))^Rational[
         1, 2]}, {(
        Rational[1, 4] (-1 - 5^Rational[1, 2])) (
         2 + Rational[1, 2] (1 + 5^Rational[1, 2]))^Rational[
         1, 2], -((Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2]) (
          2 + Rational[1, 2] (1 + 5^Rational[1, 2])))^Rational[
         1, 2]}, {(-(
          Rational[5, 8] + Rational[-1, 8] 5^Rational[1, 2])^Rational[
          1, 2]) (1 + (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2]), (Rational[1, 4] (-1 - 5^Rational[1, 2])) (
        1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2])}, {(-(
          Rational[5, 8] + Rational[1, 8] 5^Rational[1, 2])^Rational[
          1, 2]) (1 + (
          2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2]), (Rational[1, 4] (1 - 5^Rational[1, 2])) (
        1 + (2 + Rational[1, 2] (-1 + 5^Rational[1, 2]))^Rational[
          1, 2])}}, {
GrayLevel[0], {{
Line[{1, 2, 3, 26}], 
Line[{3, 7, 28}], 
Line[{2, 5, 7}], 
Line[{2, 4, 6, 5}], 
Line[{1, 8, 9, 2}], 
Line[{9, 13, 4}], 
Line[{8, 11, 13}], 
Line[{8, 10, 12, 11}], 
Line[{1, 14, 15, 8}], 
Line[{15, 19, 10}], 
Line[{14, 17, 19}], 
Line[{14, 16, 18, 17}], 
Line[{1, 20, 21, 14}], 
Line[{21, 25, 16}], 
Line[{20, 23, 25}], 
Line[{20, 22, 24, 23}], 
Line[{1, 26, 27, 20}], 
Line[{27, 31, 22}], 
Line[{26, 29, 31}], 
Line[{26, 28, 30, 29}]}}, Null}]}, AspectRatio -> Automatic, ImageSize -> Medium];
In[20]:=
points = FirstCase[spikeyData, GraphicsComplex[p_, _] :> p, {}, \[Infinity]];
edges = UndirectedEdge @@@ Join @@ (Partition[#, 2, 1] &) /@ Cases[spikeyData, Line[p_] :> p, \[Infinity]];
gr = Graph[edges, ImageSize -> Small]
Out[22]=
In[23]:=
cycles = FindCycle[gr, {6}, All];
vertexLists = Function[cycle, With[{v = First /@ cycle}, Append[v, First@v]]] /@ cycles;
lists = With[{keyVertices = {3, 9, 15, 21, 27}}, Cases[Cases[vertexLists, {___, #, ___}], l_ /; FreeQ[l, Alternatives @@ DeleteCases[keyVertices, #]]] & /@
     keyVertices];
In[24]:=
curves = ResourceFunction["AkimaSpline"][N@points[[#]], PeriodicInterpolation -> True] & /@ Join @@ lists; ParametricPlot[
 Through[curves[u]], {u, 0, 1}, Axes -> False]
Out[24]=

Publisher

Robert Nachbar

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 31 January 2019

Source Metadata

Related Resources

License Information