interpolate data in timeseries

117 次查看(过去 30 天)
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