001    package org.hackystat.projectbrowser.page.trajectory.dtw;
002    
003    /**
004     * Does DTW alignment.
005     * 
006     * @author Pavel Senin.
007     * 
008     */
009    public class DTWFactory {
010    
011      /**
012       * Performs the alignment of two timeseries.
013       * 
014       * @param query the query series.
015       * @param template the template series.
016       * @param stepPattern the step pattern to use.
017       * @return the dtw alignment of the series.
018       * 
019       * @throws DTWException if error occures.
020       */
021      public static DTWAlignment doDTW(double[][] query, double[][] template, String stepPattern)
022          throws DTWException {
023    
024        // get a new record and memorize series
025        DTWAlignment res = new DTWAlignment();
026        res.setQuery(query);
027        res.setTemplate(template);
028        res.setStepFunction(stepPattern);
029    
030        // [1.0] get the matrix of the global point-to-point distances.
031        //
032        double[][] localCostMatrix = new double[query.length][template.length];
033        for (int i = 0; i < query.length; i++) {
034          for (int j = 0; j < template.length; j++) {
035            localCostMatrix[i][j] = EuclideanDistance.pointDistance(query[i], template[j]);
036          }
037        }
038        res.setDistanceMatrix(localCostMatrix);
039    
040        res.doAlignment();
041    
042        return res;
043      }
044    
045    }