18 views (last 30 days)

Show older comments

Hi, I'm trying to make my own algorithm for 2d interpolation.

The upper left figure shows the data used for the interpolation. The upper right is the data interpolated with a linear method. The lower left is the result of matlab interp 2 cubic method and the lower right is an algorithm I made for cubic interpolation in 2d.

I wanted to know what interp2 does when you have data on the edge (for example the point where the X is). You need 2 points both sides to interpolate with the cubic method so what I did is interpolate linearly on the edge of the map. I'm on MATLAB 2016.

At the moment, my method uses separable convolution so I interpolate on rows first and then I interpolate on columns.

From what I understand from the code, interp2 extrapolates to be able to interpolate near edges, but I don't know which method is used to do so? Is it simply linear extrapolation?

Bruno Luong
on 7 Jul 2021

Edited: Bruno Luong
on 7 Jul 2021

The boundary handling is descriibed in the section Boundary Condition of this Cubic interpolation reference

Especially the equation (19) gives the value of the left outside pixel from the inside pixels

c(-1) = 3*f(x0) - 3*f(x1) + f(x2)

Likewise eqt (25) is for the right-side

c(N+1) = 3*f(x_N) - 3*f(x_{N-1}) + f(x_{N-2})

Bruno Luong
on 9 Jul 2021

interp(...'pchip') is NOT the same as interp1(..., 'v5cubic'). The later uses convolution formula on the paper. And 'pchip' preserves data monotony and it is a non-linear method.

So 'cubic' in R2013 cannot match both IMO.

Anyway it is not big deal, I'm only interested in what happens now.

Kelly Kearney
on 7 Jul 2021

interp2 does not extrapolate. By default, it sets any points outside the data bounds to NaN; you can alternatively choose a different scalar value to assign to all extrapolated points. Alternatively, you could use griddedInterpolant, which allows you to explicitly assign an algorithm to the extrapolated points:

[x,y,z] = peaks(5);

[xi,yi] = meshgrid(linspace(-4,4,50));

zi1 = interp2(x,y,z,xi,yi,'cubic');

F = griddedInterpolant(x',y',z,'cubic','linear');

zi2 = F(xi',yi');

subplot(2,1,1);

hi = imagesc(xi(1,:),yi(:,1),zi1, 'AlphaData', ~isnan(zi1), 'alphadatamapping', 'scaled');

hold on;

scatter(x(:), y(:), [], z(:), 'filled', 'markeredgecolor', 'w');

title('interp2: cubic')

subplot(2,1,2);

hi = imagesc(xi(1,:),yi(:,1),zi2, 'AlphaData', ~isnan(zi2), 'alphadatamapping', 'scaled');

hold on;

scatter(x(:), y(:), [], z(:), 'filled', 'markeredgecolor', 'w');

title('griddedInterpolant: cubic, linear')

Matt J
on 7 Jul 2021

Edited: Matt J
on 7 Jul 2021

Based on a little reverse engineering, I believe I have discovered that the 'cubic' method of interp1 uses quadratic extrapolation to pad 1 element at the beginning and end of the array. I think we can be pretty confident that the obvious generalization to the 2D case is used for interp2.

To verify this, the test below checks that the extrapolation rule leads to the same interpolated values regardless of whether the signal starts at the beginning of the array or in the middle:

v1=[rand(1,5) 0 0 0 0 0],

t1=0:numel(v1)-1;

F1=@(x) interp1(t1,v1,x,'cubic');

p=polyfit(0:2, v1(1:3),2);

extrap=polyval(p,-1), %quadratic extrapolation

v2=circshift(v1,5); %shift v1 and insert extrapolated value

v2(5)=extrap,

t2=t1-5;

F2=@(x) interp1(t2,v2,x,'cubic');

t=linspace(0,1,30);

plot(t,F1(t),'-',t,F2(t),'o')

xlabel t

ylabel 'Interpolated Values'

legend('v1(t1)','v2(t2)')

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!