簡體   English   中英

DTW算法OCT文件

[英]DTW Algorithm OCT-file

我正在嘗試創建動態時間規整(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算法

現在,我能夠創建此算法的Octave函數並正常工作。

八度功能:-

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);

現在,此代碼的計算時間隨着參數大小的增加而增加。 因此,我試圖創建用C ++編寫的OCT文件,以提高執行速度。

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));
    }

}

}

樣本輸入/輸出

  1. 輸入 -Arg1:[1 2 3 4 5],Arg2:[1 2 3 4 5 6 7]

    輸出:

    對於八度功能: Ans為3

    對於OCT文件: * /usr/lib/x86_64-linux-gnu/octave/4.0.0/exec/x86_64-pc-linux-gnu/octave-gui': double free or corruption (!prev): 0x00007f24e81eb0a0 *** panic: Aborted -- stopping myself... *** Error in錯誤/usr/lib/x86_64-linux-gnu/octave/4.0.0/exec/x86_64-pc-linux-gnu/octave-gui': double free or corruption (!prev): 0x00007f24e81eb0a0 *** panic: Aborted -- stopping myself... *** Error in /usr/lib/x86_64-linux-gnu/octave/4.0.0/exec/x86_64-pc-linux-gnu/octave-gui'中的/usr/lib/x86_64-linux-gnu/octave/4.0.0/exec/x86_64-pc-linux-gnu/octave-gui': double free or corruption (!prev): 0x00007f24e81eb0a0 *** panic: Aborted -- stopping myself... *** Error in :malloc ():內存損壞:0x00007f24e81eb230 *

  2. 輸入 :Arg1:A = rand(1,221),Args2:B = rand(1,299)

    輸出

    對於八度功能 :Ans是72.63

    對於OCT文件:

    *`/usr/lib/x86_64-linux-gnu/octave/4.0.0/exec/x86_64-pc-linux-gnu/octave-gui'中的錯誤:雙重釋放或損壞(!prev):0x00007f57a06ad940 *恐慌:已中止-停住自己... A的大小為I的大小B的大小為299行col222300行end9999col end9999Ans是:1嘗試將變量保存到'octave-workspace'...保存到'octave-workspace'已完成中止(轉儲內核)

我的問題:

  1. 首先,使用OCT文件時遇到的這個雙重免費損壞錯誤是什么?

  2. Octave文件和OCT文件的答案不同,導致此問題的OCT文件錯誤是什么?

謝謝。

首先,您應該閱讀如何調試oct文件( http://wiki.octave.org/Debugging_Octave#Debugging_oct-files

然后,您將找到以下部分:

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;
    }
}

矩陣結果的維度行為col,但您一直在寫,直到i<=row and j<=col超出范圍1。 嘗試i<rowj<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));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM