Reputation: 453
I know there has already been a lot of questioning about spline extrapolation in Matlab. I have an example where this works great in 2D and I want to understand this behaviour to programm it in C# using Math.Net. Here is my example:
matrix = zeros(128,128);
barWidth = 10;
%Calculate Midpoints
midpointX = floor(size(matrix,2)/2) + 1;
midpointY = floor(size(matrix,1)/2) + 1;
matrix(midpointY-barWidth:midpointY+barWidth,:) = 1;
% Windowing (I know it could be shortened...)
distanceMatY = -midpointY+1:midpointY-2;
distanceMatY = abs(repmat(distanceMatY',1,size(matrix,2)));
Factor = 0.5*(cos(pi*distanceMatY/barWidth)+1);
index = distanceMatY > barWidth;
Factor(index) = 0;
matrix = matrix.*Factor;
% Rotate matrix
alpha = 30 * pi/180;
y = -midpointY+1:midpointY-2; y = y';
x = -midpointX+1:midpointX-1; %set new COS for rotation in Midpoint
xRot = x*cos(alpha) - y*sin(alpha) + nMP; %Determine X and Y matrices for
yRot = x*sin(alpha) + y*cos(alpha) + mMP; %Roatation with angle alpha
rotMatrix = interp2(matrix,xRot,yRot,'linear'); %Interpolate rotated matrix
I have a matrix of zeros with a bar over the whole length in the middle.
I applied a Hann Window to the bar to smooth the edges and then rotated the matrix by 30 degrees with bilinear interpolation.
Now I have values outside the boundaries which are set to NaN. I could set all the NaN values to zero but what I really want is that the bar will be extended automatically. Now I could pad the matrix before rotation and cut it again after rotation to the input matrix size.
Much easier it is to just use interp2(matrix,xRot,yRot,'spline')
and the rotated matrix looks exactly as I want for each angle.
How does the 2D spline interpolation does it and is there a way to program that manually?
Upvotes: 0
Views: 285