在R包中需要dtw

时间:2017-01-07 18:39:39

标签: c++ r

有一个dtw包的功能

dtw(x, y=NULL, dist.method="Euclidean", step.pattern=symmetric2, window.type="none", keep.internals=FALSE, distance.only=FALSE, open.end=FALSE, open.begin=FALSE, ... )

在该功能中,有三种计算距离的方法

symmetric1 , symmetric2 , asymmetric

我对方法step.pattern = symmetric2感兴趣。

我有一个与symmetric1

完全相同的C ++函数
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double dtw_rcpp(const NumericVector& x, const NumericVector& y) {
    size_t n = x.size(), m = y.size();
    NumericMatrix res = no_init(n + 1, m + 1);
    std::fill(res.begin(), res.end(), R_PosInf);
    res(0, 0) = 0;
    double cost = 0;
    size_t w = std::abs(static_cast<int>(n - m));
    for (size_t i = 1; i <= n; ++i) {
        for (size_t j = std::max(1, static_cast<int>(i - w)); j <= std::min(m, i + w); ++j) {
            cost = std::abs(x[i - 1] - y[j - 1]);
            res(i, j) = cost + std::min(std::min(res(i - 1, j), res(i, j - 1)), res(i - 1, j - 1));
        }
    }
    return res(n, m);
}

在考虑距离symmetric2方法的с++函数中,我需要更改什么。

我不明白它是如何运作的symmetric2

here关于它的说法很少

1. Well-known step patterns
These common transition types are used in quite a lot of implementations.
symmetric1 (or White-Neely) is the commonly used quasi-symmetric, no local constraint, non-normalizable. It is biased in favor of oblique steps. symmetric2 is normalizable, symmetric, with no local slope constraints. Since one diagonal step costs as much as the two equivalent steps along the sides, it can be normalized dividing by N+M (query+reference lengths).
source code中的

,我无法理解,因为我是初学程序员

我不会说英语,请原谅我的错误。

谢谢

1 个答案:

答案 0 :(得分:1)

OP询问R中的动态时间扭曲对齐。打印symmetric2对象应该澄清递归规则:

g[i,j] = min(
     g[i-1,j-1] + 2 * d[i  ,j  ] ,
     g[i  ,j-1] +     d[i  ,j  ] ,
     g[i-1,j  ] +     d[i  ,j  ] ,
  )

g是全局费用矩阵,d是本地距离。我无法对您的其余代码发表评论。

如果您只需要此特定步骤模式下的距离值,而不需要其他功能,则代码可能会大大简化(请参阅维基百科上的伪代码)。