-
Notifications
You must be signed in to change notification settings - Fork 10
/
DTW.cpp
executable file
·167 lines (133 loc) · 3.87 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <DTW.h>
#include <cmath>
#include <cstdio>
#include "../common.h"
namespace yazz {
namespace math {
double DTW::calcDistance(double* seq1, uint32_t seq1size, double* seq2, uint32_t seq2size) {
// Create diff matrix
double** diffM = new double*[seq1size];
for (uint32_t i = 0; i < seq1size; i++) {
diffM[i] = new double[seq2size];
}
for (uint32_t i = 0; i < seq1size; i++) {
for (uint32_t j = 0; j < seq2size; j++) {
diffM[i][j] = fabs(seq1[i] - seq2[j]);
}
}
// Compute distance
double distance = findDistance(seq1size, seq2size, diffM);
// Clean up
for (uint32_t i = 0; i < seq1size; i++) {
delete [] diffM[i];
}
delete [] diffM;
return distance;
}
double DTW::calcDistanceVector(double* seq1, uint32_t seq1size,
double* seq2, uint32_t seq2size, uint8_t vectorSize) {
uint32_t seq1sizeV = seq1size / vectorSize;
uint32_t seq2sizeV = seq2size / vectorSize;
// Create diff matrix
double** diffM = new double*[seq1sizeV];
for (uint32_t i = 0; i < seq1sizeV; i++) {
diffM[i] = new double[seq2sizeV];
}
for (uint32_t i = 0; i < seq1sizeV; i++) {
for (uint32_t j = 0; j < seq2sizeV; j++) {
// Calc distance between vectors
double distance = 0.;
for (uint32_t k = 0; k < vectorSize; k++) {
distance += pow(seq1[i * vectorSize + k] - seq2[j * vectorSize + k], 2);
}
diffM[i][j] = sqrt(distance);
}
}
// Compute distance
double distance = findDistance(seq1sizeV, seq2sizeV, diffM);
// Clean up
for (uint32_t i = 0; i < seq1sizeV; i++) {
delete [] diffM[i];
}
delete [] diffM;
return distance;
}
double DTW::findDistance(uint32_t seq1size, uint32_t seq2size, double** diffM) {
// Create distance matrix (forward direction)
double** pathM = new double*[seq1size];
for (uint32_t i = 0; i < seq1size; i++) {
pathM[i] = new double[seq2size];
}
pathM[0][0] = diffM[0][0];
for (uint32_t i = 1; i < seq1size; i++) {
pathM[i][0] = diffM[i][0] + pathM[i - 1][0];
}
for (uint32_t j = 1; j < seq2size; j++) {
pathM[0][j] = diffM[0][j] + pathM[0][j - 1];
}
for (uint32_t i = 1; i < seq1size; i++) {
for (uint32_t j = 1; j < seq2size; j++) {
if (pathM[i - 1][j - 1] < pathM[i - 1][j]) {
if (pathM[i - 1][j - 1] < pathM[i][j - 1]) {
pathM[i][j] = diffM[i][j] + pathM[i - 1][j - 1];
} else {
pathM[i][j] = diffM[i][j] + pathM[i][j - 1];
}
} else {
if (pathM[i - 1][j] < pathM[i][j - 1]) {
pathM[i][j] = diffM[i][j] + pathM[i - 1][j];
} else {
pathM[i][j] = diffM[i][j] + pathM[i][j - 1];
}
}
}
}
// Find the warping path (backward direction)
uint32_t warpSize = seq1size * seq2size;
double* warpPath = new double[warpSize];
uint32_t warpPathIndex = 0;
uint32_t i = seq1size - 1, j = seq2size - 1;
warpPath[warpPathIndex] = pathM[i][j];
DEBUG("Path (%d, %d): %f", i, j, pathM[i][j]);
do {
if (i > 0 && j > 0) {
if (pathM[i - 1][j - 1] < pathM[i - 1][j]) {
if (pathM[i - 1][j - 1] < pathM[i][j - 1]) {
i--;
j--;
} else {
j--;
}
} else {
if (pathM[i - 1][j] < pathM[i][j - 1]) {
i--;
} else {
j--;
}
}
} else {
if (0 == i) {
j--;
} else {
i--;
}
}
warpPath[++warpPathIndex] = pathM[i][j];
DEBUG("Path (%d, %d): %f", i, j, pathM[i][j]);
} while (i > 0 || j > 0);
// Calculate path measure
double distance = 0.;
for (uint32_t k = 0; k < warpPathIndex + 1; k++) {
distance += warpPath[k];
}
distance = distance / (warpPathIndex + 1);
// Clean up
delete[] warpPath;
for (uint32_t i = 0; i < seq1size; i++) {
delete[] pathM[i];
}
delete[] pathM;
return distance;
}
} /* namespace math */
} /* namespace yazz */