Simple Interpolation with interp1

Hi,
I'm having trouble with interp1. I have velocity recorded as a function of time. Some of the velocities were thrown out from a filtering process. I would like to go back and fill in the missing values with a linear interpolation. The input time series may only have one missing value between observations or it may have large gaps of missing values. I expect to end up with a time series with no missing values.
A cubic interpolation fills in all the gaps... interpX=interp1(time,velocity,time, 'cubic')
but this still leaves gaps interpX=interp1(time,velocity,time, 'linear')
What am I doing wrong?
Thanks,
Tim

 채택된 답변

Matt Tearle
Matt Tearle 2011년 9월 29일

0 개 추천

I'm guessing that you're passing the entire arrays (ie NaNs and all) into interp1. For cubic interpolation, interp1 calls pchip which excises any NaNs in the data. However, interp1 just performs linear interpolation itself.
For linear interpolation, the interpolated value is simply the weighted average of the points on either side. If either of those is a NaN, the result is a NaN.
Here's some example code to show what's happening, using the data sample you posted:
% Set method to 'linear' or 'cubic'
method = 'linear';
% Data
x = [0.595 0.2243
0.605 0.2421
0.615 NaN
0.625 NaN
0.635 0.2181
0.645 NaN
0.655 0.1911
0.665 0.2479];
t = x(:,1);
y = x(:,2);
% Interpolation, first cutting out NaNs
idx = isnan(y);
y(idx) = interp1(t(~idx),y(~idx),t(idx),method)
subplot(2,1,1)
plot(t,y,'o-')
% Interpolation on raw data
y = x(:,2);
y(idx) = interp1(t,y,t(idx),method)
subplot(2,1,2)
plot(t,y,'o-')

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2011년 9월 29일

0 개 추천

time = [0 sort(randi(120,1,5))]
velocity = randi(120,1,6)
t = linspace(time(1),time(end),100);
rl = interp1(time,velocity,t);
rc = interp1(time,velocity,t,'cubic');
plot([rl',rc']),grid on
ADDED
tvy = [0.595 0.2243
0.605 0.2421
0.615 NaN
0.625 NaN
0.635 0.2181
0.645 NaN
0.655 0.1911
0.665 0.2479
]
tvw = tvy(~isnan(tvy(:,2)),:)
t = sort([tvw(2:end-1,1); linspace(tvw(1,1),tvw(end,1),100)']);
rl = interp1(tvw(:,1),tvw(:,2),t);
rc = interp1(tvw(:,1),tvw(:,2),t,'cubic');
plot(t,rl,t,rc,tvw(:,1),tvw(:,2),'go'),grid on

댓글 수: 1

Tim
Tim 2011년 9월 29일
Thanks Andrei, but I'm still not 100 percent clear. Is the problem that I'm using the same time variable in the interp function twice? The time variable I'm using contains all the times I want a velocity at...that is the time variable has no missing values. See below...[time, velocity].
0.595 0.2243
0.605 0.2421
0.615 NaN
0.625 NaN
0.635 0.2181
0.645 NaN
0.655 0.1911
0.665 0.2479

댓글을 달려면 로그인하십시오.

Tim
Tim 2011년 9월 29일

0 개 추천

Thanks Andrei, I was able to put your code to use.
But if anybody can explain why A cubic interpolation fills in all the gaps but switching the option to 'linear' does not, I'd appreciate it.
interpX=interp1(time,velocity,time, 'cubic')

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

질문:

Tim
2011년 9월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by