Use of time Series
7 views (last 30 days)
Show older comments
Hi-
I'm looking to do some regression between two or more time series . I'm wondering if i should use the variable in matlab "timeseries". I don't have a point in the same time for each time series . I fixed this issue by creating an uniform time vector. Then , for each of my time series , I create a new time series with the same data but the uniform vector . when I don't have any data for one time - I take the closest data. This is working find but it's slow. Is there any better solution ? I put below the slow code which create my fulltime series Thanks In advance
% Create an uniforme time series - with a %t of Nsec.
%time and data represent my non uniform time series
function [fulltime,sol] = CreationFullTimeSeries(time,data,Nsec,start_date,end_date,start_time,end_time)
t1= datetime(strcat(start_date,start_time),'InputFormat','yyyyMMddHHmmSS');
t2= datetime(strcat(end_date,end_time),'InputFormat','yyyyMMddHHmmSS');
fulltime = transpose(t1:seconds(Nsec):t2);
sol = zeros(length(fulltime),size(data,2));
clear('temp');
j=1;
for i=1:length(fulltime)
while and(datetime(time(j,:))<fulltime(i,1) ,j<size(time,1))
j=j+1;
end
sol(i,:)=data(j,:);
end
0 Comments
Answers (2)
Brian Neiswander
on 17 Aug 2015
Using a simple linear interpolation should be the easiest method to accomplish this. Let's say you have some sample measurement data in vectors named "time" and "data":
%define the starting and ending times of your measurements
start_date = '20100101'; %sstring containing tarting date in yyyyMMdd
end_date = '20100102'; %string containing ending date in yyyyMMdd
start_time = '120000'; %string containing starting time in HHmmSS
end_time = '120000'; %string containing ending time in HHmmSS
%make a time vector for the measurement data
Nsample = 2000;
t1 = datetime(strcat(start_date, start_time), 'InputFormat', 'yyyyMMddHHmmSS');
t2 = datetime(strcat(end_date, end_time), 'InputFormat', 'yyyyMMddHHmmSS');
time = t1:seconds(Nsample):t2;
%make some sample measurement data using a sine wave
data = sin(2*pi*linspace(0,1,length(time)));
Now, let's create a regularly spaced time vector "timeInterp" that you want to interpolate the measured data onto:
%make interpolating time vector
Nsec = 750;
timeInterp = t1:seconds(Nsec):t2;
Note that both the "time" and "timeInterp" vectors contain date strings. These will need to be converted to numerical values before interpolating. This can be done using the "datenum" function:
%convert date string vectors to date numbers
timeNum = datenum(time);
timeInterpNum = datenum(timeInterp);
Now you can use the "fit" function to interpolate the data onto the regularly spaced time vector:
%make a fit object that will linearly inerpolate over the data set
myfit = fit(timeNum(:), data(:), 'linearinterp');
%interpolate the data onto the regularly spaced time vector using the fit object
dataInterp = myfit(timeInterpNum);
Note that the "fit" function is a part of the Curve Fitting Toolbox. You can check the interpolated data against the original data in a plot:
%plot the data
figure;
plot(timeNum, data, 'x', 'markersize',10);
hold on;
plot(timeInterpNum, dataInterp, 'k.');
datetick('x'); %change the x-axis labels to date strings
legend({'Measured data','Interpolated data'});
This approach will be much faster than using "for" and "while" loops. For more information, check the following documentation pages:
0 Comments
Peter Perkins
on 18 Aug 2015
Cedric, while the timeseries class does not support datetime arrays, it does have methods to synchronize or resample time series to a common time vector. You might find it convenient to use timeseries, even if you subsequently pull the time and data back out.
0 Comments
See Also
Categories
Find more on Time Series in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!