-
Notifications
You must be signed in to change notification settings - Fork 1
/
dtw.cpp
75 lines (64 loc) · 1.91 KB
/
dtw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// $Id: dtw.cpp 192 2010-10-06 22:11:34Z jbao $
#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
//#include <gsl/gsl_matrix.h>
#include "dtw.h"
DTW::DTW(vector<double>& s, vector<double>& t) : s_(s), t_(t){
//return_ = new double();
}
DTW::~DTW(){
//delete return_;
}
double DTW::distance(){
double gap = 0;
// actual cost at each position
const int row = s_.size();
const int col = t_.size();
double cost[row][col];
int i = 0, j;
for (; i < s_.size(); ++i)
for (j = 0; j < t_.size(); ++j)
cost[i][j] = fabs(s_[i]-t_[j]);
// distance matrix for backtracing
double dist[row][col];
// initialize the 1st row and column of dist
dist[0][0] = cost[0][0];
double dist_max = dist[0][0];
for (i = 0; i < s_.size(); ++i){
dist[i][0] = cost[i][0] + dist[i-1][0] + gap;
if (dist[i][0] > dist_max)
dist_max = dist[i][0];
}
for (j = 0; j < t_.size(); ++j){
dist[0][j] = cost[0][j] + dist[0][j-1] + gap;
if (dist[0][j] > dist_max)
dist_max = dist[0][j];
}
// fill the distance matrix
double value;
for (i = 1; i < s_.size(); ++i){
for (j = 1; j < t_.size(); ++j){
value = ((dist[i-1][j] + gap) < (dist[i][j-1] + gap)) ? dist[i-1][j] :
dist[i][j-1];
value = (value < dist[i-1][j-1]) ? value : dist[i-1][j-1];
value += cost[i][j];
dist[i][j] = value;
if (dist[i][j] > dist_max)
dist_max = dist[i][j];
//delete value;
}
}
double return_ = dist[row - 1][col - 1]; /// dist_max;
/*
std::cerr << row << ' ' << col << std::endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j)
std::cerr << cost[i][j] << ' ';
std::cerr << std::endl;
}
*/
//delete cost, dist;
return return_;
}