Is there a way to linearly interpolate dates and times?
26 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matt
am 3 Jul. 2012
Kommentiert: Nahid Atashi
am 31 Okt. 2018
I am trying to interpolate discharge data using gage information. The time that corresponds to the discharge data is listed in Excel as:
1/10/08 15:33 (for example)
In MATLAB I've used the datestr command on the data and the dates are now listed as:
11-Jan-0108 15:33:42 (for example)
The data is generally given in 10 minute increments, so my question is as follows: is there a way to linearly interpolate the discharge data so it returns data for every 2 minutes, for example? I've tried using interp1 but I'm not sure exactly how to input the date so it reads properly.
0 Kommentare
Akzeptierte Antwort
Kye Taylor
am 3 Jul. 2012
Bearbeitet: Kye Taylor
am 3 Jul. 2012
If you convert the dates to serial date numbers as the cyclist suggests, then you might be able to benefit from this example...
% Create a dataset that resembles yours
dates = today:10:today+100; % dates as serial date numbers
data = sin(linspace(0,2*pi,length(dates))); % create some fake data
disp('The dates include')
for i = 1:length(dates)
disp(datestr(dates(i)))
end
finerDates = dates(1):2:dates(end); % get a finer grid
% map x-data to 0,1
scaledDates = (dates - min(dates))/(max(dates)-min(dates));
scaledFinerDates = (finerDates - min(finerDates))/(max(finerDates)-min(finerDates));
% get interpolated data -- spline is an option, could be 'linear'
interpolatedData = interp1(scaledDates, data, scaledFinerDates,'spline');
% visualize it
figure,hold on
plot(dates,data,'ro')
plot(finerDates, interpolatedData,'b.-')
legend('original data','interpolated data')
datetick('x');
3 Kommentare
Kye Taylor
am 5 Jul. 2012
The serial date numbers are on the order of 10^5. The interpolant that I use is comprised of polynomials (like p(x) = a*x + b*x^2 + c*x^3 -- a cubic spline). One needs to scale the data so that you don't end up squaring or cubing 10^5. as this will result in too wide a range of values to get a meaningful interpolant. If you use 'linear' as the option to interp1, the scaling should make no difference, but if you want a more sophisticated interpolant, you should scale.
Weitere Antworten (1)
the cyclist
am 3 Jul. 2012
One method would be to use the datenum() command to convert the dates to pure numbers, which can be consumed by interp1.
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!