-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtw.cpp
76 lines (72 loc) · 1.44 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
#include "prlib.h"
#include <cstring>
DTW::DTW ( char* xs, int m , char* ys, int n )
{
xn = m , yn = n;
x = new state_type[m+1];
y = new state_type[n+1];
x = strcpy(x,xs);
y = strcpy(y,ys);
dtw = new probability_type[(m+1)*(n+1)];
//dtw[0,j]=INF;
for ( int i = 0 ; i<=n ; i++ )
{
dtw[i]=INF;
}
//dtw[i,0]=INF;
for ( int i=0 ; i<=m ; i++ )
{
dtw[i*n]=INF;
}
//dtw[0,0]=0;
dtw[0] = 0;
}
DTW::~DTW( )
{
delete(x);
delete(y);
delete(dtw);
}
void DTW::input_state(state_type* xs, int m, state_type* ys, int n)
{
xn = m , yn = n;
x = new state_type[m+1];
y = new state_type[n+1];
x = strcpy(x,xs);
y = strcpy(y,ys);
dtw = new probability_type[(m+1)*(n+1)];
//dtw[0,j]=INF;
for ( int i = 0 ; i<=n ; i++ )
{
dtw[i]=INF;
}
//dtw[i,0]=INF;
for ( int i=0 ; i<=m ; i++ )
{
dtw[i*n]=INF;
}
//dtw[0,0]=0;
dtw[0] = 0;
}
probability_type DTW::DTW_process ( )
{
probability_type cost,temp;
for ( int i=1 ; i<=xn ; i++ )
{
for ( int j=1; j<=yn ; j++ )
{
cost = get_distance(x[i-1],y[j-1]);
//temp = min(dtw[i-1,j], dtw[i-1,j-1], dtw[i,j-1]);
temp = dtw[(i-1)*yn+j-1];
if ( dtw[i*yn+j-1] < temp ) temp = dtw[i*yn+j-1];
if ( dtw[(i-1)*yn+j] < temp ) temp = dtw[(i-1)*yn+j];
//dtw[i,j] = d(x[i],y[j]) + min(dtw[i-1,j], dtw[i-1,j-1], dtw[i,j-1])
dtw[i*yn+j] = cost + temp;
}
}
return dtw[xn*yn+yn]; //dtw[xn,yn];
}
DTW::distance_type DTW::get_distance(state_type x, state_type y)
{
return x-y>0?x-y:y-x;
}