DTW ( v1, v2 )  {
//where the vectors v1=(a1,…,an), v2=(b1,…,bm) are the time series with n and m
time points
  Let a two dimensional data matrix S be the store of similarity measures
such that S [ 0,…,n, 0,…,m ] , and i, j, are loop index, cost is an integer.
  // initialize the data matrix
  S [ 0, 0 ] = 0
  FOR i = 1 to m DO LOOP
    S [ 0, i ] =
  END
  FOR i = 1 to n DO LOOP
      S [ i, 0 ] =
  END
  // Using pairwise method, incrementally fill in the similarity matrix
  with the differences of the two time series
  FOR i = 1 to n DO LOOP
    FOR j = 1 to m DO LOOP
    // function to measure the distance between the two points
      cost = d ( v1 [ i ] , v2 [ j ] )
      S[i, j] = cost + MIN ( S [ i-1, j ] ,      / / increment
                     S [ i, j−1 ] ,   / / decrement
                     S [ i-1, j−1 ] ) / / match
    END
  END
  Return S [ n, m ]
}
Algorithm 1: Pseudo code of dynamic time wrap algorithm.