插值matlab时间序列对象时,质量向量更新不正确

时间:2013-08-20 13:15:41

标签: matlab time-series interpolation floating-point-precision

运行以下matlab代码:

% define new timeseries object, set Quality vector to zero
a=timeseries( ...
  [1:6]', ...
  [datenum('2010-1-1 0:00'):1/24:datenum('2010-1-1 05:59')]', ...
  zeros(6,1) ...
);
% set meta data for new timeseries object
a.QualityInfo.Code = 0:1;
a.TimeInfo.Units = 'days';
a.TimeInfo.Increment = 1/24; 
a.TimeInfo.Format = 'dd-mmm-yyyy HH:MM:SS';
a.TimeInfo.StartDate = datestr(0);

% extrapolate timevector and interpolate for half hours. 
b=resample(a, ...
  datenum('2009-12-31 21:00'):1/48:datenum('2010-1-1 05:59'), ...
  'linear', ...
  1 ...
)

提供输出:

    Time vector characteristics

          Length                18
          Start date            31-Dec-2009 21:00:00
          End date              01-Jan-2010 05:30:00

    Data characteristics

          Interpolation method  linear
          Size                  [18   1]
          Data type             double

    Time                              Data                              Quality
    ---------------------------------------------------------------------------
    31-Dec-2009 21:00:00              NaN                                 1
    31-Dec-2009 21:30:00              NaN                                 1
    31-Dec-2009 22:00:00              NaN                                 1
    31-Dec-2009 22:30:00              NaN                                 1
    31-Dec-2009 23:00:00              NaN                                 1
    31-Dec-2009 23:30:00              NaN                                 1
    01-Jan-2010 00:00:00              1                                   0
    01-Jan-2010 00:30:00              1.5                                 1
    01-Jan-2010 01:00:00              2                                   1
    01-Jan-2010 01:30:00              2.5                                 1
    01-Jan-2010 02:00:00              3                                   1
    01-Jan-2010 02:30:00              3.5                                 1
    01-Jan-2010 03:00:00              4                                   1
    01-Jan-2010 03:30:00              4.5                                 1
    01-Jan-2010 04:00:00              5                                   1
    01-Jan-2010 04:30:00              5.5                                 1
    01-Jan-2010 05:00:00              6                                   1
    01-Jan-2010 05:30:00              NaN                                 1

虽然我期望输出:

    Time vector characteristics

          Length                18
          Start date            31-Dec-2009 21:00:00
          End date              01-Jan-2010 05:30:00

    Data characteristics

          Interpolation method  linear
          Size                  [18   1]
          Data type             double

    Time                              Data                              Quality
    ---------------------------------------------------------------------------
    31-Dec-2009 21:00:00              NaN                                 1
    31-Dec-2009 21:30:00              NaN                                 1
    31-Dec-2009 22:00:00              NaN                                 1
    31-Dec-2009 22:30:00              NaN                                 1
    31-Dec-2009 23:00:00              NaN                                 1
    31-Dec-2009 23:30:00              NaN                                 1
    01-Jan-2010 00:00:00              1                                   0
    01-Jan-2010 00:30:00              1.5                                 1
    01-Jan-2010 01:00:00              2                                   0
    01-Jan-2010 01:30:00              2.5                                 1
    01-Jan-2010 02:00:00              3                                   0
    01-Jan-2010 02:30:00              3.5                                 1
    01-Jan-2010 03:00:00              4                                   0
    01-Jan-2010 03:30:00              4.5                                 1
    01-Jan-2010 04:00:00              5                                   0
    01-Jan-2010 04:30:00              5.5                                 1
    01-Jan-2010 05:00:00              6                                   0
    01-Jan-2010 05:30:00              NaN                                 1

给定输出和预期输出之间的差异是,对于插值和外插值,质量向量应仅设置为1,而不应设置为未更改的值。

在Matlab文档中写道,只有插入函数才能改变已经改变的观测值的质量代码。如该示例所示,情况并非如此。

有解决方法吗?

我的matlab版本是:MATLAB版本7.9.1.705(R2009b)Service Pack 1

1 个答案:

答案 0 :(得分:0)

我自己找到了一个解决方法:

当您查看第192行的内置Matlab函数\ toobox \ matlab \ timeseries \ @timeseries \ resample.m时:

I = isnan(yin) | abs(yout-yin)>eps;

在此行上确定哪个指标(存储在I中)将获得质量代码1。

通常,你和阴之间的差异要大得多,在这种情况下差别是:

yout-yin

返回:

    ans =

      1.0e-008 *

             0
       -0.1118
       -0.1118
       -0.1118

而eps为:2.2204e-016

通过降低a.Data,我们可以解决Matlab内置函数中的错误::

% extrapolate timevector and interpolate for half hours. 
CorrectionFactor = 1/max(max(a.Data))/1e7;
a.Data = a.Data * CorrectionFactor ;
b=resample(a, ...
  datenum('2009-12-31 21:00'):1/48:datenum('2010-1-1 05:59'), ...
  'linear', ...
  1 ...
);
b.Data = b.Data / CorrectionFactor;
b

将作为输出:

          Length                18
          Start date            31-Dec-2009 21:00:00
          End date              01-Jan-2010 05:30:00

    Data characteristics

          Interpolation method  linear
          Size                  [18   1]
          Data type             double

    Time                              Data                              Quality
    ---------------------------------------------------------------------------
    31-Dec-2009 21:00:00              NaN                                 1
    31-Dec-2009 21:30:00              NaN                                 1
    31-Dec-2009 22:00:00              NaN                                 1
    31-Dec-2009 22:30:00              NaN                                 1
    31-Dec-2009 23:00:00              NaN                                 1
    31-Dec-2009 23:30:00              NaN                                 1
    01-Jan-2010 00:00:00              1                                   0
    01-Jan-2010 00:30:00              1.5                                 1
    01-Jan-2010 01:00:00              2                                   0
    01-Jan-2010 01:30:00              2.5                                 1
    01-Jan-2010 02:00:00              3                                   1
    01-Jan-2010 02:30:00              3.5                                 1
    01-Jan-2010 03:00:00              4                                   0
    01-Jan-2010 03:30:00              4.5                                 1
    01-Jan-2010 04:00:00              5                                   0
    01-Jan-2010 04:30:00              5.5                                 1
    01-Jan-2010 05:00:00              6                                   1
    01-Jan-2010 05:30:00              NaN                                 1

正如您所看到的, 01-Jan-2010 02:00:00 01-Jan-2010 05:00:00 仍有问题,可以通过调整定义时间向量的方式来解决。我们需要替换:

datenum('2010-1-1 0:00'):1/24:datenum('2010-1-1 05:59')

(datenum('2010-1-1 0:00')*24:datenum('2010-1-1 05:59')*24)/24

完整的解决方案将是:

% define new timeseries object, set Quality vector to zero
a=timeseries( ...
  [1:6]', ...
  [(datenum('2010-1-1 0:00')*24:datenum('2010-1-1 05:59')*24)/24]', ...
  zeros(6,1) ...
);
% set meta data for new timeseries object
a.QualityInfo.Code = 0:1;
a.TimeInfo.Units = 'days';
a.TimeInfo.Increment = 1/24; 
a.TimeInfo.Format = 'dd-mmm-yyyy HH:MM:SS';
a.TimeInfo.StartDate = datestr(0);

% extrapolate timevector and interpolate for half hours. 
CorrectionFactor = 1/max(max(a.Data))/1e7;
a.Data = a.Data * CorrectionFactor ;
b=resample(a, ...
  (datenum('2009-12-31 21:00')*48:datenum('2010-1-1 05:59')*48)/48, ...
  'linear', ...
  1 ...
);
b.Data = b.Data / CorrectionFactor;
b

,这给出了我们将要考虑的输出:

    Time vector characteristics

          Length                18
          Start date            31-Dec-2009 21:00:00
          End date              01-Jan-2010 05:30:00

    Data characteristics

          Interpolation method  linear
          Size                  [18   1]
          Data type             double

    Time                              Data                              Quality
    ---------------------------------------------------------------------------
    31-Dec-2009 21:00:00              NaN                                 1
    31-Dec-2009 21:30:00              NaN                                 1
    31-Dec-2009 22:00:00              NaN                                 1
    31-Dec-2009 22:30:00              NaN                                 1
    31-Dec-2009 23:00:00              NaN                                 1
    31-Dec-2009 23:30:00              NaN                                 1
    01-Jan-2010 00:00:00              1                                   0
    01-Jan-2010 00:30:00              1.5                                 1
    01-Jan-2010 01:00:00              2                                   0
    01-Jan-2010 01:30:00              2.5                                 1
    01-Jan-2010 02:00:00              3                                   0
    01-Jan-2010 02:30:00              3.5                                 1
    01-Jan-2010 03:00:00              4                                   0
    01-Jan-2010 03:30:00              4.5                                 1
    01-Jan-2010 04:00:00              5                                   0
    01-Jan-2010 04:30:00              5.5                                 1
    01-Jan-2010 05:00:00              6                                   0
    01-Jan-2010 05:30:00              NaN                                 1