将 c++ DTW 代码转换为 java

Posted

技术标签:

【中文标题】将 c++ DTW 代码转换为 java【英文标题】:converting c++ DTW code to java 【发布时间】:2014-03-23 10:30:37 【问题描述】:

我想将代码从 C++ 翻译成 Java。原始代码实现了快速 DTW 算法。我无法弄清楚的代码是I 属性我不确定它的作用,因此我无法转换它。

Java 中的错误出现在语句 l_buff+Iu_buff+I 中,因为在 int Idouble[] l_buff,u_buff 之间不支持加号运算符。

我已经收录了所有涉及I的陈述

int  I;
for(i=0; i<ep; i++)

    /// A bunch of data has been read and pick one of them at a time to use
    d = buffer[i];

    /// Calculate sum and sum square
    ex += d;
    ex2 += d*d;

    /// t is a circular array for keeping current data
    t[i%m] = d;

    /// Double the size for avoiding using modulo "%" operator
    t[(i%m)+m] = d;

    /// Start the task when there are more than m-1 points in the current chunk
    if( i >= m-1 )
    
        mean = ex/m;
        std = ex2/m;
        std = Math.sqrt(std-mean*mean);

        /// compute the start location of the data in the current circular array, t
        j = (i+1)%m;
        /// the start location of the data in the current chunk
        I = i-(m-1);
        lb_k2 = lb_keogh_data_cumulative(order, tz, qo, cb2, l_buff+I, u_buff+I, m, mean, std, bsf);

lb_data_cumlative方法的实现是

public static double lb_keogh_data_cumulative(int[] order, double []tz, double []qo, double []cb, double []l, double []u, int len, double mean, double std, double best_so_far )

    double lb = 0;
    double uu,ll,d;

    for (int i = 0; i < len && lb < best_so_far; i++)
    
        uu = (u[order[i]]-mean)/std;
        ll = (l[order[i]]-mean)/std;
        d = 0;
        if (qo[i] > uu)
            d = dist(qo[i], uu);
        else
        
            if(qo[i] < ll)
                d = dist(qo[i], ll);
        
        lb += d;
        cb[order[i]] = d;
    
    return lb;

这里是代码所依赖的论文SIGKDD TRILLION

【问题讨论】:

【参考方案1】:

l_buff+Iu_buff+I 表示将数组的开头移到 I 元素。 lb_keogh_data_cumulative 参数 lu 不会看到给定数组的第一个 I 元素。

所以你可以写类似的东西

lb_k2 = lb_keogh_data_cumulative(order, tz, qo, cb2, Arrays.copyOfRange(l_buff, I, l_buff.length), Arrays.copyOfRange(u_buff, I, u_buff.length), m, mean, std, bsf);

调用的方法不会修改数组,因此您可以传递一个副本。

【讨论】:

以上是关于将 c++ DTW 代码转换为 java的主要内容,如果未能解决你的问题,请参考以下文章

是否可以将 mex 代码转换为 C++ 代码?

将 python、numpy 和 scipy 代码转换为 C++ 兼容代码?

将代码从 C++ 转换为 C# 的问题

如何将 C++ 转换为 C# 代码而 statmernt 代码递减

如何将从 C++ 发送的 cv::MAT 字节数组转换为 Java 中的位图?

将 Fortran 转换为 C 或 C++