我试图创建一个动态时间扭曲(DTW)函数,它将计算提供给它的两个信号之间的最小距离。它基于以下算法,
DTW算法:-
int DTWDistance(s: array [1..n], t: array [1..m]) {
DTW := array [0..n, 0..m]
w := abs(n-m)// adapt window size (*)
for i := 0 to n
for j:= 0 to m
DTW[i, j] := infinity
DTW[0, 0] := 0
for i := 1 to n
for j := max(1, i-w) to min(m, i+w)
cost := d(s[i], t[j])
DTW[i, j] := cost + minimum(DTW[i-1, j ], // insertion
DTW[i, j-1], // deletion
DTW[i-1, j-1]) // match
return DTW[n, m]更多信息DTW算法
现在,我能够创建一个八度函数的这个算法和它的工作正常。
八度函数:-
function dtw_distance = dtw2(a,b)
length_a = length(a);
length_b = length(b);
an=zeros(length_a+1,length_b+1);
an(:,:)=9999;
an(1,1)=0;
cost=0;
#Here we have also implemented the window size.
w=abs(length_a-length_b);
for i=1:length_a
for j=max(1,i-w):min(length_b,i+w)
cost=abs(a(i)-b(j));
an(i+1,j+1)=cost+min([an(i,j+1),an(i+1,j),an(i,j)]);
end
end
an;
dtw_distance=an(length_a+1,length_b+1);现在,这段代码的计算时间随着参数大小的增加而增加。因此,我试图创建OCT文件,该文件是用C++编写的,以便更快地执行。
C++ OCT文件:-
#include <octave/oct.h>
octave_idx_type getMax(octave_idx_type a, octave_idx_type b){
return (a>b)?a:b;
}
octave_idx_type getMin(octave_idx_type a, octave_idx_type b){
return (a<b)?a:b;
}
DEFUN_DLD (dtw3, args, , "Find DTW of two Signals With Window")
{
int nargin = args.length();
if (nargin != 2)
print_usage();
else
{
NDArray A = args(0).array_value();
NDArray B = args(1).array_value();
octave_stdout << "Size of A is" << A.length();
octave_stdout << "Size of B is" << B.length();
if (! error_state)
{
octave_idx_type row = A.length()+1;
octave_idx_type col = B.length()+1;
Matrix results (row,col);
for(octave_idx_type i = 0; i <= row ; i++)
{
for(octave_idx_type j=0; j<= col ; j++)
{
results(i,j)=9999;
}
}
octave_stdout << "row col" << results.dim1() << results.dim2() ;
octave_stdout << "row end" << results(row,0) ;
octave_stdout << "col end" << results(0,col) ;
results(0,0)=0;
octave_idx_type win = (row>col)?(row-col):(col-row);
octave_idx_type cost = 0;
for(octave_idx_type i = 1 ; i <= row ; i++)
{
for(octave_idx_type j = getMax(1,i-win) ; j <= getMin(col,i+win) ; j++)
{
cost=(A(i)>B(j))?(A(i)-B(j)):(B(j)-A(i));
results(i,j)= cost + getMin(getMin(results(i-1,j),results(i,j-1)),results(i-1,j-1));
}
}
octave_stdout << "Ans is: " << results(row,col);
return octave_value(results(row,col));
}
}
}样本输入/输出
我的问题:
谢谢。
发布于 2016-04-17 11:47:51
首先,您应该阅读如何调试oct文件(oct档案)
然后你会发现这部分:
Matrix results (row,col);
for(octave_idx_type i = 0; i <= row ; i++)
{
for(octave_idx_type j=0; j<= col ; j++)
{
results(i,j)=9999;
}
}矩阵结果有维度行,但您要写到i<=row and j<=col,即超出界限的1。试试i<row和j<col
您的代码中有太多的问题无法描述,这里是我的更改。我已经替换了一些内置函数的函数:
#include <octave/oct.h>
DEFUN_DLD (dtw3, args, , "Find DTW of two signals with window")
{
int nargin = args.length();
if (nargin != 2)
print_usage();
Matrix A = args(0).array_value();
Matrix B = args(1).array_value();
octave_stdout << "Size of A is " << A.length() << std::endl;;
octave_stdout << "Size of B is " << B.length() << std::endl;
if (! error_state)
{
octave_idx_type n = A.length();
octave_idx_type m = B.length();
Matrix results (n + 1, m + 1);
for(octave_idx_type i = 0; i <= n ; i++)
for(octave_idx_type j = 0; j <= m ; j++)
results(i, j) = octave_Inf;
results(0, 0) = 0;
octave_idx_type win = abs (n-m);
double cost = 0;
for(octave_idx_type i = 1 ; i <= n ; i++)
for(octave_idx_type j = std::max(1, i-win) ; j <= std::min(m, i+win) ; j++)
{
cost = abs(A(i-1) - B(j-1));
results(i, j) = cost + std::min(std::min(results(i-1,j),results(i,j-1)),results(i-1,j-1));
}
//octave_stdout << results << std::endl;
return ovl(results(n, m));
}
}https://stackoverflow.com/questions/36675738
复制相似问题