Irregular Time Series to Regular using Interpolation
Show older comments
Hi,
I'm pretty new to Matlab so forgive me if I'm asking an easy question!
I currently have two sets of data in csv format - both of which are not regularly spaced at all. What I need is to interpolate the data so I can have a value from both sets for specific times and at equal intervals of 15 minutes.
Table1.csv sample:
20/12/2014 08:55, 0.63
20/12/2014 14:37, 0.61
20/12/2014 16:51, 0.61
20/12/2014 17:15, 0.61
20/12/2014 17:45, 0.6
20/12/2014 18:00, 0.6
20/12/2014 18:15, 0.6
20/12/2014 18:30, 0.6
Table2.csv sample (a bit more regularly spaced, but there are some irregular gaps) -
20/12/2014 03:00, 0.1
20/12/2014 06:00, 0
20/12/2014 09:00, 0.2
20/12/2014 12:54, 0.7
20/12/2014 15:00, 0.5
20/12/2014 18:00, 0.3
20/12/2014 21:19, 0
Now what I'd love to get out is something that looks like this:
Datetime, Table1 Value, Table2 Value
20/12/2014 18:00, 0.5 , 0.1
20/12/2014 18:15, 0.3 , 0.2
20/12/2014 18:30, 0.2 , 0.6
20/12/2014 18:45, 0.1 , 0.2
20/12/2014 19:00, 0.2 , 0.0
Can anybody help? Thanks :)
Accepted Answer
More Answers (2)
John D'Errico
on 28 Jan 2015
Convert the date/time info to a serial numeric value using datenum.
datenum('20/12/2014 08:55')
ans =
9654.37152777778
Then use interp1 to interpolate, at values that are 15 minutes apart.
That interpolation increment is one quarter of an hour, so based on a day as 1, we get an increment of...
1/24/4
ans =
0.0104166666666667
Chris Barnhart
on 28 Jan 2015
You can convert your dates and times into serial date with:
dsn = datenum('20/12/2014 18:45','dd/mm/yyyy HH:MM');
Then you can verify it worked with:
datestr(dsn)
The serial date format is in unit of days since sometime long ago. Thus in serial date, 1 hour is 1/24
Once the times are in serial format, they be come the x values for the interpolation function interp1 I'll call these time_in
The y values are from your table(s). Not sure if y can be a Nx2 or it must be 1D (Nx1) I'll call these y1_in, y2_in
Finally you need the output times in serial date format. Pick a start and stop time. This could be manually:
dstart = datenum('20/12/2014 14:00','dd/mm/yyyy HH:MM')
dend = datenum('20/12/2014 23:00','dd/mm/yyyy HH:MM')
Or by finding min and max of the time_in values. However, you'll need to 'round' the min and max :
dmin = datenum('20/12/2014 14:05','dd/mm/yyyy HH:MM')
dmax = datenum('20/12/2014 22:25','dd/mm/yyyy HH:MM')
dstart = floor(dmin*24) / 24;
dend = ceil(dmax*24) / 24;
% check your calculations
datestr(dstart)
datestr(dend)
Finally create the output time series on 15 min intervals:
time_out = dstart : 0.25/24 : dend ;
datestr(time_out)
Put assemble the output values with: y1_out = interp1(time_in, y1_in, time_out)
Use doc interp1 to see what some of the interpolation options are.
Categories
Find more on Dates and Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!