MATLAB: Irregular Time Series to Regular using Interpolation

irregular time serieslinear interpolationMATLABtime series

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 🙂

Best Answer

This will get you part way there:
Table1 = {'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};
DTn = datenum(Table1(:,1), 'dd/mm/yyyy HH:MM');
ti = 1/(60/15 * 24); % Time Interval
DTiv = [DTn(1):ti:DTn(end)]'; % Interpolation Vector
Table12 = cell2mat(Table1(:,2)); % Vector: Column #2 Of Table1
DT15 = interp1(DTn, Table12, DTiv); % Interpolated Column #2
NewTable1 = {datestr(DTiv, 'dd/mm/yyyy HH:MM') DT15};
Result = [NewTable1{1} repmat(' ', size(NewTable1{2})) num2str(NewTable1{2}, '%.2f')];
Result5 = Result(1:5,:) % Display First 5 Rows
produces:
Result5 =
20/12/2014 08:55 0.63
20/12/2014 09:10 0.63
20/12/2014 09:25 0.63
20/12/2014 09:40 0.63
20/12/2014 09:55 0.63
I don’t understand how you get the ‘Table2’ values in your desired output. The values in column #2 for ‘Table2’ go from 0.3 at 18:00 to 0 at 21:19, so they would monotonically decrease (linearly by default) over the interval you list.
I will leave you to resolve that, and the way you want to merge the two tables. There is obviously something I do not understand in what you want to do. Meanwhile, my code snippet will perform the interpolation. You may have to experiment with it to get the result you want.