|
In my last work I have extensively used multivariate b spline to perform data interpolation ad to exploit a parametric model for image mappings. In particular my work required periodic and continuous b-spline. This version of a b-spline requires two less parameters for each dimension since we imposes C2 continuity on the period boundary.
In this article I present a rather smart and compact implementation for R -> R2 periodic b-spline with evenly spaced knots using cubic spline as atomic support (i.e each b-spline covers 5 subsequent knots).
You may use this function as in the following example
a = linspace(0,2*pi, 12); a = a(1:end-1);
p = [cos(a); 2*sin(a)]
cs = closedSpline(a, p, 8); %creates the periodic spline
y = cs.eval( linspace(0,2*pi,100) ); %evaluate the spline in 100 points
plot(p(1,:),p(2,:), 'b.');
plot(y(1,:), y(2,:), '-r');
I am going to post shortly the link to the two required function and possibly to extends this post to add more details!
So here is the matlab function used to create a closed 2D spline.
% Compute a 2D closed spline
% - Pierluigi Taddei (
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
)
%
% Usage: sp = closedSpline(a, p, n)
%
%
% Arguments:
% a - domain values (i.e linspace(0, 2*pi, k) )
% p - image values on the plane (the value of the spline in a)
% n - number of spline knots (should be less than number of
% values)
% 27.03.2009 : Created
function ps = closedSpline(a, p, n)
if(n > length(a))
n = length(a);
end
ps = struct();
%atomic spline element
knots = linspace(0,2*pi, n+1); knots = knots(1:end-1);
baseSpline = spmak(knots(1:5),1);
%%move values to a single bspline equivalent
Afull = repmat(a', 1,length(knots)) - repmat(knots, length(a),1);
Afull2 = mod(Afull, 2*pi); %remove periodicity
A = fnval(baseSpline, Afull2); %evaluate bspline in all points
%least square minimization
ku = A \ p(1,:)';
kv = A \ p(2,:)';
%file the structure
ps.knots = knots;
ps.baseSpline = baseSpline;
ps.paramU = ku;
ps.paramV = kv;
%add the evaluation method to the spline itself
ps.eval = @(x)csval(ps,x);
end
Given a set of initial points the function creates a linear sistem wich will be solved in order to detect the best parameter value in the least square sense.
Once the parameters are known you can call the csval function to evaluate the spline in any point of the domain (thanks to its periodicity). You can both call this function directly or use an object oriented call style on the spline structure.
% Evaluate a closed 2D spline for values a
% - Pierluigi Taddei (
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
)
%
% Usage: y = csval(cs,a)
%
%
% Arguments:
% cs - the closed spline
% a - domain values vector
% 27.03.2009 : Created
function y = csval(cs,a)
k = cs.knots;
aVal = repmat(a, length(k),1) - repmat(k', 1, length(a));
aVal2 = mod(aVal, 2*pi);
bVal = fnval(cs.baseSpline, aVal2);
u = sum( bVal .* repmat(cs.paramU, 1,length(a)),1 );
v = sum( bVal .* repmat(cs.paramV, 1,length(a)),1 );
y = [u;v];
end
|