interpolate data in timeseries

111 views (last 30 days)
Nikolas Spiliopoulos
Nikolas Spiliopoulos on 2 Dec 2021
Edited: dpb on 2 Dec 2021
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!!

Accepted Answer

dpb
dpb on 2 Dec 2021
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.

More Answers (1)

Jon
Jon on 2 Dec 2021
Edited: dpb on 2 Dec 2021
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 Comments
Jon
Jon on 2 Dec 2021
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 on 2 Dec 2021
Edited: dpb on 2 Dec 2021
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

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!