DynamicTimeWarping implements IDistanceMetric { @Override public double distance(final double[] a, final double[] b) { int n = a.length; int m = b.length; double[][] matrix = new double[n + 1][m + 1]; for (int i = 1; i <= n; i++) { matrix[i][0] = Double.MAX_VALUE; } for (int j = 1; j <= m; j++) { matrix[0][j] = Double.MAX_VALUE; } matrix[0][0] = 0d; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { double cost = this.delta.distance(a[i - 1], b[j - 1]); double mini = Math.min(matrix[i - 1][j], Math.min(matrix[i][j - 1], matrix[i - 1][j - 1])); matrix[i][j] = cost + mini; } } return matrix[n][m]; } DynamicTimeWarping();  DynamicTimeWarping(final IScalarDistance delta); @Override double distance(final double[] a, final double[] b); double distanceWithWindow(final double[] a, final double[] b, int w);  }
@Test public void testCorrectnessForDistanceCalculation() { double[] timeSeries1 = { 1, 1, 1, 1, 1, 1 }; double[] timeSeries2 = { 1, 1, 1, 1, 1, 1 }; double expectation = 0; DynamicTimeWarping dtw = new DynamicTimeWarping(); double distance = dtw.distance(timeSeries1, timeSeries2); assertEquals(expectation, distance, 1.0E-5); }
@Test public void testCorrectnessForDistanceCalculation2() { double[] timeSeries1 = { 0.50, 0.87, 0.90, 0.82, 0.70 }; double[] timeSeries2 = { 0.10, 0.10, 0.10, 0.10, 0.10 }; double expectation = 3.29; DynamicTimeWarping dtw = new DynamicTimeWarping(); double distance = dtw.distance(timeSeries1, timeSeries2); assertEquals(expectation, distance, 1.0E-5); }
@Test public void testCorrectnessForDistanceCalculation3() { double[] timeSeries5 = { 1, 1, 2, 2, 3, 5 }; double[] timeSeries6 = { 1, 2, 3, 5, 5, 5, 6 }; double expectation = 1; DynamicTimeWarping dtw = new DynamicTimeWarping(); double distance = dtw.distance(timeSeries5, timeSeries6); assertEquals(expectation, distance, 1.0E-5); }