WeightedDynamicTimeWarping 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]; double[] weights = this.calculateWeights(Math.max(n, m)); 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 = weights[Math.abs(i - j)] * this.d.distance(a[i - 1], b[j - 1]); double minimum = Math.min(matrix[i - 1][j], Math.min(matrix[i][j - 1], matrix[i - 1][j - 1])); matrix[i][j] = cost + minimum; } } return matrix[n][m]; } WeightedDynamicTimeWarping(final double g, final double maximumWeight, final IScalarDistance d); @Override double distance(final double[] a, final double[] b);  }
@Test public void testCorrectnessForDistanceCalculation() { double[] timeSeries1 = { 1, 1, 1, 1, 1, 1 }; double[] timeSeries2 = { 1, 1, 1, 1, 1, 1 }; double expectation = 0; double g = 1; double maximumWeight = 1; IScalarDistance d = ScalarDistanceUtil.getSquaredDistance(); WeightedDynamicTimeWarping wdtw = new WeightedDynamicTimeWarping(g, maximumWeight, d); double distance = wdtw.distance(timeSeries1, timeSeries2); assertEquals(expectation, distance, 1.0E-5); }