B-Spline Interpolation: Charles Frye Introduction To Splines
B-Spline Interpolation: Charles Frye Introduction To Splines
Charles Frye
Introduction to Splines
a a
B-Splines
Basis splines or b-splines are a set of piecewise functions which form a basis for the set of all
possible spline functions; any given spline function can be described as a linear combination of
B-splines. B-splines are relatively simple to create and do not suffer from the polynomial wiggle
error present in some other interpolation techniques.
Given an infinite set of knots {…< t i−2 <t i−1<t i <t i+1 <t i +2< … }, the ith 0 degree b-spline is given
by:
1 ,t i ≤ x <t i+1
B0i ( x ) = (1)
0 , otherwise
All subsequent higher level B-splines can be constructed through the recurrence relation:
x−t i d −1 t −x d−1
Bdi ( x )= Bi ( x ) + i +d +1 B ( x) (2)
t i +d−t i t i+d +1−t i+ 1 i +1
Figure 1
A single spline piece plotted for degrees 0 to 3. With each increase in degree, the spline has influence up to an addition knot.
Of course, in a real interpolation problem we will not have an infinite set of knots. Instead we
can easily define a set of knots { t 0 <t 1 <t 2 <…<t N } over [a,b]- the interval over which we want to
interpolate the data. Calculating an 'd' degree spline function from equation (2) will require 'd'
extra knots to be placed on each side of the interval.
Figure 2
The graph shows the basis for all degree 3 spline functions. The pattern continues to
infinity in both directions. A linear combination of the pictured functions can be used
to interpolate a set of data.
Equation (2) creates the basis functions for an 'd' degree spline (Figure 2). A linear combination
of these functions can be used to interpolate a data set. In order to interpolate the data, the b-
spline curve must include the given data points ( x j , y j ):
Equation (3) provides a system of equations from which we can calculate the coefficients c di of
the b-spline curve. With the coefficients determined, we can extrapolate values f ( x ) between
the given data points:
f ( x )=∑ c di B di (x ) (4)
i
Program 1: 'basisspline' interpolates a set of points ( x j , y j ). with a degree k spline for a knot
sequence { t 0 <t 1 <t 2 <…<t N }.
In general, it is best to avoid a knot sequence where t i=t i+1 for any knots in the sequence
{t 0 ,t 1 ,t 2 ,… , t N }. The repetition of knots causes a decrease in continuity at x=t i for each
repeated knot. For example, a cubic spline will only be continuous to its first derivitive at
x=t i ¿ t i+1 and will be discontinuous at its first derivative if t i ¿ t i+1 ¿t i+2 .
Derivatives of B-Splines
d d d
dx
c i Bdi ( x )= (
t i +d−t i i
)
B d−1 ( x) −
( ) B d−1 (x)
t i+d +1−t i+1 i+1
(5)
Which implies:
d c −c
∑ c i Bdi =∑ d i i−1 Bd−1 (6)
dx i i t i+ d−t i i
Program 3: 'dbspline' plots the interpolating b-spline and its derivative using equation (6) to
calculate the coefficients.
The spline coefficients c di can be determined through a least squares fitting of the data. Suppose
we want to interpolate N+1 data points ( x j , y j ) with a degree 'd' spline consisting of M+1
pieces. We of course want to minimize the least squares error:
N M 2
E=∑
j =0
(∑
i =0
d d
c B ( x j )− y j
i i )
We take the partial derivative with respect to the coefficient c nk where k ∈ 0≤ i≤ M
N M 2 N M
d
∑
dx j=0 (∑i=0
d d
)
c B ( x j ) − y j =2 ∑
i i
j=0
(∑
i=0
)
cid Bid ( x j )− y j Bdk ( x j )
N M N
¿ 2 ∑ ∑ c di Bdi ( x j ) Bdk ( x j ) −2 ∑ y j B dk ( x j )
j=0 i=0 j=0
d
Make the substitution q j ,i =B i ( x j ) then:
N M N
¿ 2 ∑ ∑ c di q j ,i q j , k −2 ∑ y j qa j ,k
j=0 i=0 j=0
¿ 2 Q ' Q Cd −2 Q' Y
Note that we can drop the factor of 2 at this point. Setting this derivative equal to zero and
using the linearization A=Q ' Q ; B=−Q ' Y ; X =C d gives the characteristic AX+ B=0 system of
equations. Solving for the matrix C d yields:
Note that with the least squares technique the matrix ( Q ' Q )−1can become ill-conditioned when
uneven knot spacing is used and Gaussian elimination may result in extreme error.
Nevertheless, an exact solution to (7) will interpolate the data.
Smoothing Splines
When fitting noisy data, we may not want an exact interpolation of the measured data points.
In that case we can use a smoothing spline:
N b
2
∑ ( S ( x j )− y j ) + λ∫ S '' ( x)2 dx (8)
j=0 a
The smoothing spline is a least squares spline fitting with a smoothing parameter λ attached to
the end. The smoothing spline will no longer exactly interpolate the points; as a result, the
b
'' 2
spline function will no longer minimize ∫ S ( x) dx through ( x j , y j ). Program 4 'sspline'
a
Program 0: BasisSplineCreate Creates the b-spline basis, this core program is referenced by the
other programs.
function Y=basissplinecreate(x,k,t)
%x is the sapled points
%k is degree
%t are knots: 'k' extra knots on each end for degree k spline
%values of B splines at points in x are in columns of Y=[B1 B2 B3]
m=length(t);n=length(x);
m=m-1;
Y=zeros(m,n);Z=Y;
for i=1:m
Y(i,:)=(x>t(i))&(x<=t(i+1));
Y(i,:)=Y(i,:)+0.;
end
for j=1:k
Z=zeros(m-j,n);
for i=1:m-j
Z(i,:)=((x-t(i))/(t(i+j)-t(i))).*Y(i,:)+((t(i+j+1)-x)/(t(i+j+1)-
t(i+1))).*Y(i+1,:);
end
Y=Z;
end
Y=Y';
end
Program 1: BSpline Interpolates a data set (x,y) and graphs the spline curve.
function [c,B]=bspline(x,y,k,t,xx)
%INPUT
%(x,y) are known data points. (use rows)
%k is degree
%t are knots: 'k' extra knots on each end for degree k spline
%xx= area to be evaluated
%Output
%c gives the coefficients
%B is %values of B splines at points xx B=[xx yy]
%Spline
Y=basissplinecreate(x,k,t);
c=Y\y';
%Interval
Y=basissplinecreate(xx,k,t);
yy=Y*c;
plot(xx,yy,x,y,'o')
B=[xx' yy];
end
Program 2: DBSpline Plots both the interpolation curve and its derivative.
function [d,B]=dbspline(c,k,t,xx)
%INPUT
%c is b-spline coefficients
%k is degree
%t are knots: 'k' extra knots on each end for degree k spline
%xx= area to be evaluated
%Output
%d gives the coefficients
%B is %values of B splines at points xx B=[xx yy]
%Derivative coefficients
d=zeros(length(t)-2*(k-1),1);
for i=2:length(c);
d(i-1)=k*(c(i-1)-c(i))./(t(i+k)-t(i));
end
d=-1*[0;d];
%Derivative Interval
Y=basissplinecreate(xx,k-1,t);
yy=Y*d;
plot(xx,yy,'r')
B=[xx' yy];
end
Program 3: LsBSpline Interpolation using least squares technique to determine the coefficients.
function [c,B]=lsbspline(x,y,k,t,xx)
%INPUT
%(x,y) are known data points. (use rows)
%k is degree
%t are knots: 'k' extra knots on each end for degree k spline
%xx= area to be evaluated
%Output
%c gives the coefficients
%B is %values of B splines at points xx B=[xx yy]
%Spline
Q=basissplinecreate(x,k,t);
%Least squares solving for c
A=Q'*Q;
B=(Q'*y');
[ c ] = A\B;
%Plot the Interval
Y=basissplinecreate(xx,k,t);
yy=Y*c;
plot(xx,yy,x,y,'o')
B=[xx' yy];
end
Program 4: SSpline Smoothing spline interpolation
function [C,B]=sspline(x,y,k,t,xx,l)
%INPUT
%(x,y) are known data points. (use rows)
%k is degree
%t are knots: 'k' knots on each end for degree k spline
%xx= area to be evaluated
%l is lambda, the smoothing coefficient
%Output
%C gives the coefficients
%B is values of B splines at points xx B=[xx yy]
Q=basissplinecreate(x,k,t);
P=[];
[m,n]=size(Q);
for j=1:n
c=1;
d=zeros(1,2);
e=zeros(1,3);
d(1)=k*(c(1))./(t(j+k)-t(j));
d(2)=k*(-c(1))./(t(j+k+1)-t(j+1));
e(1)= k*(d(1))./(t(j+k-1)-t(j));
e(2)=k*(d(2)-d(1))./(t(j+k)-t(j+1));
e(3)=k*(-d(2))./(t(j+k+1)-t(j+2));
H=basissplinecreate(xx,k-2,t(j:j+k+1));
size(H);size(e);
P(:,j)=H*e';
end
%Solve for C
A=Q'*Q+l.*(P'*P);
B=Q'*y';
[C]=A\B;
Y=basissplinecreate(xx,k,t);
yy=Y*C;
plot(xx,yy,x,y,'o')
B=[xx' yy];
end
Referenced Programs
Source: NUMERICAL METHODS: Matlab Programs (c) 2004 by John H. Mathews and Kurtis D. Fink
if A(p,p)==0
'A is singular. No unique solution'
break
end
%Calculate multiplier and place in subdiagonal portion of A
for k=p+1:N
mult=A(k,p)/A(p,p);
A(k,p) = mult;
A(k,p+1:N)=A(k,p+1:N)-mult*A(p,p+1:N);
end
end
%Solve for Y
Y(1) = B(R(1));
for k=2:N
Y(k)= B(R(k))-A(k,1:k-1)*Y(1:k-1);
end
%Solve for X
X(N)=Y(N)/A(N,N);
for k=N-1:-1:1
X(k)=(Y(k)-A(k,k+1:N)*X(k+1:N))/A(k,k);
end
function [ X ] = backsub( A, B )
%Back Substitution for Upper Triangular
% Solving AX=B
n=length(B);
X=zeros(n,1);
X(n)=B(n)/A(n,n);
for k=n-1:-1:1
X(k)=(B(k)-A(k,k+1:n)*X(k+1:n))/A(k,k);
end
end
References