interpolate data in timeseries

조회 수: 116 (최근 30일)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2021년 12월 2일
편집: dpb 2021년 12월 2일
Hi all,
I have a timeseries vector lets say with 7 points : v=[0.18 3.15 0.18 0.16 0.17 0.58 0.33 ];
I would like to create a new vector by interpolating data so I have extra 3 points among two values: somethng like:
v_new=[0.18 ... ... ... 3.15 ... ... 0.18... and so on ]
could you help me with that?
thanks!!

채택된 답변

dpb
dpb 2021년 12월 2일
v=[0.18 3.15 0.18 0.16 0.17 0.58 0.33 ]; % example data
% the engine
nI=3; % number points to insert
N=numel(v); % initial vector size
NN=(N-1)*nI+N; % new N for augmented v
xv=linspace(1,N,NN); % uniform spacing along 1:N NN points
vv=interp1(1:N,v,xv); % linear interpolation
vvv=interp1(1:N,v,xv,'pchip'); % or pchip interpolation
Illustrate results...
plot(v,'o-')
hold on
plot(xv,vv,'x')
plot(xv,vvv,'d-')
legend('Original','Linear','PChip')
produces
"Choose your poison" as far as what interpolant you actually want.
If you have Signal Processing TB, there's also resample you could play with.

추가 답변 (1개)

Jon
Jon 2021년 12월 2일
편집: dpb 2021년 12월 2일
Although you say you have a "time series" it looks like v is just an ordinary vector. Assuming you have another "t" vector to go along with this data you could do something like this:
t = [0 9 10 15 20 25 33] % made up example time vector, not necessarily evenly space
v = [0.18 3.15 0.18 0.16 0.17 0.58 0.33 ]; % your v vector
t_new = [0 2 3 9 9.5 9.8 10 11 12 15 20 21 21.2 25 29 30 33] % vector of times you want to have data for including original
v_new = interp1(t,v,t_new)
v_new =
Columns 1 through 11
0.1800 0.8400 1.1700 3.1500 1.6650 0.7740 0.1800 0.1760 0.1720 0.1600 0.1700
Columns 12 through 17
0.2520 0.2684 0.5800 0.4550 0.4237 0.3300
  댓글 수: 2
Jon
Jon 2021년 12월 2일
My answer shows generally how to interpolate extra points. I see @dpb gave you more precisely what you asked for, an approach to insert exactly n points between your existing points assuming the original "t" values (independent variable) is equally spaced. From your description I wasn't sure if you could assume that.
dpb
dpb 2021년 12월 2일
편집: dpb 2021년 12월 2일
You can also accomplish that objective by iterating between time points...
plot(t,v,'o-')
fntv=@(t1,t2,n)linspace(t1,t2,n+2);
tv=cell2mat(arrayfun(@(i)fntv(t(i),t(i+1),nI),1:numel(t)-1,'UniformOutput',false));
vv=interp1(t,v,tv);
hold on
plot(tv,vv,'xr')
gives

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

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by