Interp2 a set of randomly sampled points?

Hello All,
I am given the following information with no guarantee on any particular order or sample spacing (there could be holes. Roughly on a hexagonal grid, but not precicely...)
- Vectors of X and Y locations corresponding to a same-sized vector of complex values which I want to interpolate.
I've written the following test case which works great:
% Simple test case first to test handling of phasors:
test = [0 2*pi 0.1;
-0.1 0 -2*pi;
0 0 0;]
test = exp(1i*test);
[testX, testY] = meshgrid(1:3)
interpX = [1.5; 2.5; 2.1];
interpY = [1.5; 1.5; 2];
testInterp = interp2(testX,testY,test, interpX, interpY);
angle(testInterp)
% It works!
Unfortunately, I don't get my data in these nicely formatted and arranged square matricies. I get them in vectors, which don't have any particular order. I modified the above code to mimic this, and get errors complaining that the vectors are no longer monotonically increasing... I've tried sorting the data by x or y, but I keep getting the errors. Since the x coords are coupled with the y coords, I can't sort both x and y at the same time (does that make sense?). Any way, maybe the code below helps:
test = [0 2*pi 0.1;
-0.1 0 -2*pi;
0 0 0;]
test = exp(1i*test);
[testX, testY] = meshgrid(1:3)
interpX = [1.5; 2.5; 2.1];
interpY = [1.5; 1.5; 2];
combinedVector = [testX(:), testY(:), test(:)];
combinedVector = sortrows(combinedVector, [2 1]);
testInterp = interp2(combinedVector(:,1),combinedVector(:,2),combinedVector(:,3), interpX, interpY);
angle(testInterp)
Thoughts?? -Mike

 Accepted Answer

Interesting function! I modified my script (below) to accept this, but it doesn't give the results I expect. They are close, but don't match the first test script I wrote (and is correct). Thoughts?
% Simple test case first to test handling of phasors:
test = [0 2*pi 0.1;
-0.1 0 -2*pi;
0 0 0;]
test = exp(1i*test);
testR = real(test);
testI = imag(test);
[testX, testY] = meshgrid(1:3)
interpX = [1.5; 2.5; 2.1];
interpY = [1.5; 1.5; 2];
Fr = scatteredInterpolant(testX(:), testY(:), testR(:))
Fi = scatteredInterpolant(testX(:), testY(:), testI(:))
angle(Fr(interpX, interpY) + 1i*Fi(interpX, interpY))
Thanks!

3 Comments

It looks like your code is right. The main issue is that you are doing interpolation and depending upon your method of interpolation, you'll get different results. interp2 and scatteredInterpolant are using different interpolation methods.
It wasn't really clear to me from the documentation but it is likely that scatteredInterpolant is using delaunay triangulation, converting the data to barycentric coordinates, and then doing interpolation using those results.
Basically, for every unknown point, you are finding three close points that are known that surround the unknown point in a triangle (delaunay triangulation), and then calculating the unknown point as a weight (barycentric coordinates) of each of the known points, based on how close the unknown point is to each of the known points. Interp2 on the other hand is using bilinear interpolation which weights the points as function of 4 known points (not 3).
You might also be interested in the FEX function gridfit which solves the same problem but has a few more features that might give better results.
Jim has it basically between interp2 and triscattered
I don't know about the FEX submission but you can also look at
*griddata"
There's a decent background section in the doc's on interpolating scattered data that outlines at least the basics of the methods implemented
Thanks All!! That was the guidance I needed!! -Mike

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!