MATLAB: Interp1 yields NaN for two signals interpolation of the same sampling frequency

interp1nan

Dear friends,
I am trying to interpolate two signals of the same sampling rate (1000 Hz). One signal (X1) has the length of 863 while the other (X2) has a length of 1121. I would like to make both of them equal in length. This is the code I wrote for interpolation:
t1=0:1/1000:(length(X1)-1)/1000; t2=0:1/1000:(length(X2)-1)/1000;
NewX1=interp1(t1,X1,X2);
The problem is all values are 'NaN' after the 863th in signal NewX1. "Interp1" works if the original signal (X1) is of different sampling rate. Why does does not it work for two signals of the same sampling rate?
Thank you very much for your assistance in advance.
Abbas Orand

Best Answer

I think you have the wrong idea about interpolation here. interp1 assumes that X1 has been sampled at a lower rate than X2. So the limits of the time (or spatial) vector "t" are the same for both X1 and X2, but X2 has been sampled on a finer grid. The inputs to interp1 should NOT be a time vector and the two signals, the inputs should be:
1.) the coarser time vector
2.) the signal vector with the coarser sampling
3.) the more finely sampled time vector.
For example:
t = 0:0.1:2*pi;
x = cos(t);
ti = 0:0.01:2*pi;
y = interp1(t,x,ti);
plot(t,x,'b'); hold on;
plot(ti,y,'k')
You have observed two signals over different time scales, one over 863 milliseconds and one over 1.12 seconds. How do you know how X1 will behave over the remaining 257 milliseconds?