Is there a way to linearly interpolate dates and times?
27 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Kye Taylor
2012 年 7 月 3 日
編集済み: Kye Taylor
2012 年 7 月 3 日
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 件のコメント
Kye Taylor
2012 年 7 月 5 日
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.
その他の回答 (1 件)
the cyclist
2012 年 7 月 3 日
One method would be to use the datenum() command to convert the dates to pure numbers, which can be consumed by interp1.
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!