Interpolating data fails: grid vectors do not define a grid of points that match the given values

4.9k views Asked by At

I have some time/frequency data and I try to interpolate it using the interp2 function of Matlab. The data [F,T,data] is obtained from a different Matlab routine of Matlab, spectrogram in case you are interested.

[~,F,T,data] = spectrogram(...)
data = 10*log10(data);

I can plot the data using surf. The data is fine, I believe. However interpolating the data seems to be a problem. Even using interp2(F,T,data,F,T) (so actually no interpolating) gives the error below.

What is going wrong here?

I have the data which I use here: https://www.dropbox.com/s/zr5zpfhp6qyarzw/test.mat

interp2(F,T,data,f,t)
Error using griddedInterpolant
The grid vectors do not define a grid of points that match the given values.
Error in interp2>makegriddedinterp (line 228)
    F = griddedInterpolant(varargin{:});
Error in interp2 (line 128)
        F = makegriddedinterp({X, Y}, V, method,extrap);
>> size(F),size(T),size(data),size(f),size(t)
ans =
   129     1
ans =
     1    52
ans =
   129    52
ans =
   200     1
ans =
     1   121
1

There are 1 answers

0
m7913d On BEST ANSWER

The problem is that you should swap F and T:

interp2(T,F,data,t,f);

The first argument corresponds with the columns of the matrix and the second argument with the rows as documented here:

If X and Y are grid vectors, then V must be a matrix containing length(Y) rows and length(X) columns.

As an alternative, you may take the transpose of data:

interp2(F,T,data',f,t);

Reasoning behind (strange) argument order

interp2(X,Y,V,Xq,Yq) is interpreted as the interpolation of a function, represented by the matrix V, i.e. the sample values. The problem is that the arguments/indexes of a function/matrix are rather supplied in an opposite order:

matrix(row, column)

versus

function(x,y)

x (first argument) often represents the horizontal axes and therefore corresponds with the column (second argument) argument and idem for y and row.