0

运行以下 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 文档中写道,只有已更改的观察的质量代码应由 interpolate 函数更改。如本例所示,情况并非如此。

有解决方法吗?

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

4

1 回答 1

0

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

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

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

在这条线上,确定哪个指标(存储在 I 中)将获得质量代码 1。

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

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:0001- 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
于 2013-08-20T13:15:40.557 回答