-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtwImplementation.c
More file actions
133 lines (109 loc) · 2.81 KB
/
dtwImplementation.c
File metadata and controls
133 lines (109 loc) · 2.81 KB
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
//Copyright 2016, Simon Iyamu Perisanidis, All rights reserved.
#include <stdlib.h>
#include <stdio.h>
#include "dtwInterface.h"
#ifdef REC
double dtw(double *A,int i,double *B,int j,double c){
int imeionj; /* |i-j| */
if(i-j>0)
imeionj=i-j;
else
imeionj=-(i-j);
if (imeionj>c)
return 1.0/0.0;
else{
if(i==1 && j==1){
return d(*A,*B);
}else if(i>1 && j==1) /* -1 because we start counting from 0 */
return d(*(A+i-1),*B) + dtw(A,i-1,B,1,c);
else if(i==1 && j>1)
return d(*A,*(B+j-1)) + dtw(A,1,B,j-1,c);
else if(i>1 && j>1){
return d(*(A+i-1),*(B+j-1)) + min( dtw(A,i-1,B,j,c), dtw(A,i,B,j-1,c), dtw(A,i-1,B,j-1,c) );
}
}
}
#else
double dtw(double *A,int i,double *B,int j,double c){
double **D,rtrn; /* D is an array of two dimensions where the dtw distances are saved*/
int x,y,imeionj;
D=malloc(j*sizeof(double));
if(D==NULL){
printf("Memory Error\n");
return 1;
}
for(x=0;x<j;x++){
D[x]=malloc(i*sizeof(double));
if(D[x]==NULL){
printf("Memory Error\n");
return 1;
}
}
if(c==1.0/0.0){ /*If theres no c limit*/
D[0][0]=d(A[0],B[0]); /* d is not inf in the diagwnios */
for(x=1;x<i;x++){
if(x<=c)
D[0][x]=d(A[x],B[0])+D[0][x-1]; /*The first line of the array gets completed */
else
D[0][x]=1.0/0.0;
}
for(y=1;y<j;y++){ /* The first row of the array gets completed*/
if(y<=c)
D[y][0]=d(A[0],B[y])+D[y-1][0];
else
D[y][0]=1.0/0.0;
}
for(y=1;y<j;y++){ /* The rest array gets completed*/
for(x=1;x<i;x++){
if(x-y>0)
imeionj=x-y;
else
imeionj=-(x-y);
if(imeionj<=c)
D[y][x]=d(*(A+x),*(B+y))+min( D[y][x-1], D[y-1][x], D[y-1][x-1] );
else
D[y][x]=1.0/0.0;
}
}
}else{
for(y=c+1,x=0;y<j;y++,x++){ /* Fills the perimeter of the window with inf */
D[y][x]=1.0/0.0;
D[x][y]=1.0/0.0;
}
D[0][0]=d(A[0],B[0]); /* d is not inf in the diagwnios */
for(x=1;x<=c;x++){ /* First line */
D[0][x]=d(A[x],B[0])+D[0][x-1];
}
for(y=1;y<=c;y++){ /* and first row */
D[y][0]=d(A[0],B[y])+D[y-1][0];
}
/* The rest array gets completed */
for(y=1;y<=c;y++){ /* This is for the part of the array that*/
for(x=1;x<=y+c;x++){ /* has a constant beginning for x*/
D[y][x]=d(*(A+x),*(B+y))+min( D[y][x-1], D[y-1][x], D[y-1][x-1] );
}
}
for(y=c+1;y<j;y++){ /* In the rest array the place where x*/
for(x=y-c;x<=y+c && x<i;x++){ /* starts changes in every loop */
D[y][x]=d(*(A+x),*(B+y))+min( D[y][x-1], D[y-1][x], D[y-1][x-1] );
}
}
}
rtrn=D[j-1][i-1];
for(y=0;y<j;y++)
free(D[y]);
free(D);
return rtrn;
}
#endif
double min(double x,double y,double z){
if(x<=y && x<=z)
return x;
else if(y<x && y<=z)
return y;
else
return z;
}
double d(double a,double b){
return (a-b)*(a-b);
}