-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtw_c.c
195 lines (164 loc) · 4.18 KB
/
dtw_c.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Copyright (C) 2013 Quan Wang <wangq10@rpi.edu>,
* Signal Analysis and Machine Perception Laboratory,
* Department of Electrical, Computer, and Systems Engineering,
* Rensselaer Polytechnic Institute, Troy, NY 12180, USA
*/
/**
* This is the C/MEX code of dynamic time warping of two signals
*
* compile:
* mex dtw_c.c
*
* usage:
* d=dtw_c(s,t) or d=dtw_c(s,t,w)
* where s is signal 1, t is signal 2, w is window parameter
*/
#include "mex.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
double vectorDistance(double *s, double *t, int ns, int nt, int k, int i, int j)
{
double result=0;
double ss,tt;
int x;
for(x=0;x<k;x++)
{
ss=s[i+ns*x];
tt=t[j+nt*x];
result+=((ss-tt)*(ss-tt));
}
result=sqrt(result);
return result;
}
double dtw_c(double *s, double *t, int w, int ns, int nt, int k)
{
double d=0;
int sizediff=ns-nt>0 ? ns-nt : nt-ns;
double ** D;
int i,j;
int j1,j2;
double cost,temp;
// printf("ns=%d, nt=%d, w=%d, s[0]=%f, t[0]=%f\n",ns,nt,w,s[0],t[0]);
if(w!=-1 && w<sizediff) w=sizediff; // adapt window size
// create D
D=(double **)malloc((ns+1)*sizeof(double *));
for(i=0;i<ns+1;i++)
{
D[i]=(double *)malloc((nt+1)*sizeof(double));
}
// initialization
for(i=0;i<ns+1;i++)
{
for(j=0;j<nt+1;j++)
{
D[i][j]=-1;
}
}
D[0][0]=0;
// dynamic programming
for(i=1;i<=ns;i++)
{
if(w==-1)
{
j1=1;
j2=nt;
}
else
{
j1= i-w>1 ? i-w : 1;
j2= i+w<nt ? i+w : nt;
}
for(j=j1;j<=j2;j++)
{
cost=vectorDistance(s,t,ns,nt,k,i-1,j-1);
temp=D[i-1][j];
if(D[i][j-1]!=-1)
{
if(temp==-1 || D[i][j-1]<temp) temp=D[i][j-1];
}
if(D[i-1][j-1]!=-1)
{
if(temp==-1 || D[i-1][j-1]<temp) temp=D[i-1][j-1];
}
D[i][j]=cost+temp;
}
}
d=D[ns][nt];
/* view matrix D */
/*
for(i=0;i<ns+1;i++)
{
for(j=0;j<nt+1;j++)
{
printf("%f ",D[i][j]);
}
printf("\n");
}
*/
// free D
for(i=0;i<ns+1;i++)
{
free(D[i]);
}
free(D);
return d;
}
/* the gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *s,*t;
int w;
int ns,nt,k;
double *dp;
/* check for proper number of arguments */
if(nrhs!=2&&nrhs!=3)
{
mexErrMsgIdAndTxt( "MATLAB:dtw_c:invalidNumInputs",
"Two or three inputs required.");
}
if(nlhs>1)
{
mexErrMsgIdAndTxt( "MATLAB:dtw_c:invalidNumOutputs",
"dtw_c: One output required.");
}
/* check to make sure w is a scalar */
if(nrhs==2)
{
w=-1;
}
else if(nrhs==3)
{
if( !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) ||
mxGetN(prhs[2])*mxGetM(prhs[2])!=1 )
{
mexErrMsgIdAndTxt( "MATLAB:dtw_c:wNotScalar",
"dtw_c: Input w must be a scalar.");
}
/* get the scalar input w */
w = (int) mxGetScalar(prhs[2]);
}
/* create a pointer to the input matrix s */
s = mxGetPr(prhs[0]);
/* create a pointer to the input matrix t */
t = mxGetPr(prhs[1]);
/* get the dimensions of the matrix input s */
ns = mxGetM(prhs[0]);
k = mxGetN(prhs[0]);
/* get the dimensions of the matrix input t */
nt = mxGetM(prhs[1]);
if(mxGetN(prhs[1])!=k)
{
mexErrMsgIdAndTxt( "MATLAB:dtw_c:dimNotMatch",
"dtw_c: Dimensions of input s and t must match.");
}
/* set the output pointer to the output matrix */
plhs[0] = mxCreateDoubleMatrix( 1, 1, mxREAL);
/* create a C pointer to a copy of the output matrix */
dp = mxGetPr(plhs[0]);
/* call the C subroutine */
dp[0]=dtw_c(s,t,w,ns,nt,k);
return;
}