mikeP
mikeP

Reputation: 801

Interpolating irregularly spaced 3D matrix in matlab

I have a time series of temperature profiles that I want to interpolate, I want to ask how to do this if my data is irregularly spaced.

Here are the specifics of the matrix:

Both time and depth are irregularly spaced. I want to ask how I can interpolate them into a regular grid?

I have looked at interp2 and TriScatteredInterp in Matlab, however the problem are the following:

  1. interp2 works only if data is in a regular grid.
  2. TriscatteredInterp works only if the vectors are column vectors. Although time and depth are both column vectors, temperature is not.

Thanks.

Upvotes: 3

Views: 5328

Answers (3)

Bryan P
Bryan P

Reputation: 6220

Try the GridFit tool on MATLAB central by John D'Errico. To use it, pass in your 2 independent data vectors (time & temperature), the dependent data matrix (depth) along with the regularly spaced X & Y data points to use. By default the tool also does smoothing for overlapping (or nearly) data points. If this is not desired, you can override this (and other options) through a wide range of configuration options. Example code:

%Establish regularly spaced points
num_points = 20;
time_pts = linspace(min(time),max(time),num_points);
depth_pts = linspace(min(depth),max(depth),num_points);

%Run interpolation (with smoothing)
Pest = gridfit(depth, time, temp, time_pts, depth_pts);

Upvotes: 0

CitizenInsane
CitizenInsane

Reputation: 4855

Function Interp2 does not require for a regularly spaced measurement grid at all, it only requires a monotonic one. That is, sampling positions stored in vectors depths and times must increase (or decrease) and that's all.

Assuming this is indeed is the situation* and that you want to interpolate at regular positions** stored in vectors rdepths and rtimes, you can do:

[JT, JD] = meshgrid(times, depths); %% The irregular measurement grid
[RT, RD] = meshgrid(rtimes, rdepths); %% The regular interpolation grid
TemperaturesOnRegularGrid = interp2(JT, JD, TemperaturesOnIrregularGrid, RT, RD);


* : If not, you can sort on rows and columns to come back to a monotonic grid.
*
*: In fact Interp2 has no restriction for output grid (it can be irregular or even non-monotonic).

Upvotes: 2

macduff
macduff

Reputation: 4685

I would use your data to fit to a spline or polynomial and then re-sample at regular intervals. I would highly recommend the polyfitn function. Actually, anything by this John D'Errico guy is incredible. Aside from that, I have used this function in the past when I had data on a irregularly spaced 3D problem and it worked reasonably well. If your data set has good support, which I suspect it does, this will be a piece of cake. Enjoy! Hope this helps!

Upvotes: 0

Related Questions