-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTW.java
More file actions
29 lines (25 loc) · 896 Bytes
/
Copy pathDTW.java
File metadata and controls
29 lines (25 loc) · 896 Bytes
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
import java.util.Arrays;
public class DTW {
public static double calculateDTW(double[][] seq1, double[][] seq2) {
int n = seq1.length;
int m = seq2.length;
double[][] dtw = new double[n + 1][m + 1];
for (double[] row : dtw)
Arrays.fill(row, Double.POSITIVE_INFINITY);
dtw[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
double cost = euclideanDistance(seq1[i - 1], seq2[j - 1]);
dtw[i][j] = cost + Math.min(dtw[i - 1][j], Math.min(dtw[i][j - 1], dtw[i - 1][j - 1]));
}
}
return dtw[n][m];
}
private static double euclideanDistance(double[] a, double[] b) {
double sum = 0;
for (int i = 0; i < a.length; i++) {
sum += Math.pow(a[i] - b[i], 2);
}
return Math.sqrt(sum);
}
}