cdtw/cdtw.c
float dtw_dist(const float *x, const float *y, int32_t nx, int32_t ny) {
    int32_t i, j, k;
    const int32_t ncol = ny + 2;
    float cost, next_val, prev_val;

    float *cost_mat = malloc(ncol * sizeof(float));
    for (j = 0; j < ncol; j++) {
        cost_mat[j] = INFINITY;
    }
    cost_mat[1] = 0;
    k = 1;
    for (i = 1; i <= nx; i++) {
        k = (2 - i) % ncol;
        if (k < 0) k += ncol;
        if (k == 0) prev_val = cost_mat[ncol - 1];
        else prev_val = cost_mat[k - 1];
        for (j = 1; j <= ny; j++) {
            cost = pow(x[i - 1] - y[j - 1], 2);
            if (k == ncol - 1) next_val = cost_mat[0];
            else next_val = cost_mat[k + 1];
            cost_mat[k] = cost + min3(next_val,         // insertion
                                      prev_val,         // deletion
                                      cost_mat[k]);     // match
            prev_val = cost_mat[k++];
            if (k == ncol) k = 0;
        }
        cost_mat[k] = INFINITY;
    }

    k--;
    if (k < 0) k = ncol - 1;
    float dist = sqrt(cost_mat[k]);

    free(cost_mat);

    return dist;
}
/**
 * Computes the DTW distance between 1-d time series `x` and `y`
 *  using dynamic programming. It's equivalent but more efficient to
 *  calling `dtw_mat(x, y)` and retrieving the last row and column.
 */
Computes the DTW distance between 1-d time series `x` and `y`
using dynamic programming. It's equivalent but more efficient to
calling `dtw_mat(x, y)` and retrieving the last row and column.
Computes the DTW distance between 1 - d time series ` x ` and ` y ` using dynamic programming . It ' s equivalent but more efficient to calling ` dtw_mat ( x y ) ` and retrieving the last row and column .
float dtw_dist(const float *x, const float *y, int32_t nx, int32_t ny) {
    int32_t i, j, k;
    const int32_t ncol = ny + 2;
    float cost, next_val, prev_val;
    float *cost_mat = malloc(ncol * sizeof(float));
    for (j = 0; j < ncol; j++) {
        cost_mat[j] = INFINITY;
    }
    cost_mat[1] = 0;
    k = 1;
    for (i = 1; i <= nx; i++) {
        k = (2 - i) % ncol;
        if (k < 0) k += ncol;
        if (k == 0) prev_val = cost_mat[ncol - 1];
        else prev_val = cost_mat[k - 1];
        for (j = 1; j <= ny; j++) {
            cost = pow(x[i - 1] - y[j - 1], 2);
            if (k == ncol - 1) next_val = cost_mat[0];
            else next_val = cost_mat[k + 1];
            cost_mat[k] = cost + min3(next_val,         
                                      prev_val,         
                                      cost_mat[k]);     
            prev_val = cost_mat[k++];
            if (k == ncol) k = 0;
        }
        cost_mat[k] = INFINITY;
    }
    k--;
    if (k < 0) k = ncol - 1;
    float dist = sqrt(cost_mat[k]);
    free(cost_mat);
    return dist;
}
float dtw_dist ( const float * x , const float * y , int32_t nx , int32_t ny ) { int32_t i , j , k ; const int32_t ncol = ny + 2 ; float cost , next_val , prev_val ; float * cost_mat = malloc ( ncol * sizeof ( float ) ) ; for ( j = 0 ; j < ncol ; j ++ ) { cost_mat [ j ] = INFINITY ; } cost_mat [ 1 ] = 0 ; k = 1 ; for ( i = 1 ; i <= nx ; i ++ ) { k = ( 2 - i ) % ncol ; if ( k < 0 ) k += ncol ; if ( k == 0 ) prev_val = cost_mat [ ncol - 1 ] ; else prev_val = cost_mat [ k - 1 ] ; for ( j = 1 ; j <= ny ; j ++ ) { cost = pow ( x [ i - 1 ] - y [ j - 1 ] , 2 ) ; if ( k == ncol - 1 ) next_val = cost_mat [ 0 ] ; else next_val = cost_mat [ k + 1 ] ; cost_mat [ k ] = cost + min3 ( next_val , prev_val , cost_mat [ k ] ) ; prev_val = cost_mat [ k ++ ] ; if ( k == ncol ) k = 0 ; } cost_mat [ k ] = INFINITY ; } k -- ; if ( k < 0 ) k = ncol - 1 ; float dist = sqrt ( cost_mat [ k ] ) ; free ( cost_mat ) ; return dist ; }
Computes the DTW distance between 1-d time series `x` and `y`
  using dynamic programming.
Computes the DTW distance between 1 - d time series ` x ` and ` y ` using dynamic programming .
// insertion // deletion // match