How to create a new data set, from an existing data set, with a different time increment than the original

Suppose you have a very large x, y data set where "x" is time and "y" is some known value. The time increment for "x" is 0.02 seconds. "y" is known at every 0.02 seconds. I would like to change the time increment to 0.010269 seconds and use the known "y" values to find the new "y" values for the 0.010269 increment based off of a linear interpolation between the known original "y" values.

 Accepted Answer

  1. Convert x to duration array
  2. Build a timetable with x and y
  3. Use retime with timestep set to 0.010269
Provide data if you want code.
As a sidenote: based on the very specific timestep I'm guessing you want to align this time-series with another time-series. If that is the case, then simply use the duration array of the other time-series as input to retime.

7 Comments

...and here is the code as promised
%%Load data
data = load('MATLAB Answers question data.mat');
x = data.dataset(:,1);
y = data.dataset(:,2);
%%Convert x to duration format and build timetable
TT1 = timetable(seconds(x),y);
%%Change timestep
TT2 = retime(TT1,'regular','linear','TimeStep',seconds(0.010269));
%%Display first rows
TT2(1:5,:)
ans =
5×1 timetable
Time y
__________ ______
15 sec 4.31
15.01 sec 4.2792
15.021 sec 4.2511
15.031 sec 4.2716
15.041 sec 4.2873
It's a really ambiguous time-step, but whatever works for you!
Any idea what this error might be when I try using your code?
Error using timetable/retime (line 103) 'regular' is not a valid specification of target time vector for synchronization.
I have seen that it is because I am using version R2016b and not R2018b. I wonder what is the correct option to place for my version.
retime in R2016b only took character vectors, such as 'hourly', as new timestep. This method should however still work:
data = load('MATLAB Answers question data.mat');
x = data.dataset(:,1);
y = data.dataset(:,2);
t=seconds(x);
ts_new=seconds(0.010269);
t_new=t(1):ts_new:t(end);
TT1=timetable(t,y);
TT2 = retime(TT1,t_new','linear')
It looks like it worked! Thank you so much! I have been trying to do this for a week.
You're welcome! Duration (format), datetime (format) and timetables are extremely convenient when working with time-series.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 22 Aug 2018

Commented:

on 22 Aug 2018

Community Treasure Hunt

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

Start Hunting!