next up previous contents
Next: Wrapper interface Up: Library detailed information Previous: Private data structures   Contents

Implementation of functions

DTAddTemplate

  1. Create a new array of floats of size nSeq.
  2. Copy the elements from tSeq to the new array.
  3. Create a new instance of DTWTemplate, set values point to the new array and set length=nSeq.
  4. Push a pointer to the new DTWTemplate to the templates vector.
  5. Return the last index number from templates.

DTDelTemplate

  1. Check that templates[tIndex] exists and doesn't point to NULL. Otherwise, return 1.
  2. Delete the values array from the DTWTemplate in question.
  3. Delete the DTWTemplate in question.
  4. Set the pointer templates[tIndex] point to NULL.
  5. Return 0.

DTGetTemplate

  1. Check that templates[tIndex] exists and doesn't point to NULL. Otherwise, return NULL.
  2. Create a new array and copy the requested template into it.
  3. Save the length of the requested array to *nSeq.
  4. Return a pointer to the new array

DTCompareAll

  1. If params.dmetrics=DTW_DMETRICS set
    obsder=calcDerivatives(observation,n)
  2. For each template do
    1. Set avgcost=0.
    2. If params.dmetrics=DTW_DMETRICS set
      tempder=calcDerivatives(template,m)
    3. For each allowable start point x,y (depending on params.seoffset) do
      1. createWarpingGrid(x,y,o,sqrDist,observation,template) or createWarpingGrid(x,y,o,sqrDist,obsder,tempder) (Depending on selected metrics)
      2. Search such an end point (a,b) from the allowable end points area that minimizes the quotient
        thiscost=warpinggrid[a][b].costsofar/
        warpinggrid[a][b].lengthsofar
      3. If thiscost < avgcost, set avgcost=thiscost
    4. Push the (template index, avgcost) -pair into a STL vector of DTWAllResultElements.
  3. Sort the STL vector so that template with least avgcost is first etc.
  4. Copy the contents of the STL vector into an array.
  5. Return the array.

DTCompareOne

  1. If params.dmetrics=DTW_DMETRICS set
    obsder=calcDerivatives(observation,n)
    and tempder=calcDerivatives(template,m)
  2. Set avgcost=0.
  3. For each allowable start point x,y (depending on params.seoffset) do
    1. createWarpingGrid(x,y,o,sqrDist,observation,template) or createWarpingGrid(x,y,o,sqrDist,obsder,tempder) (Depending on selected metrics)
    2. Search such an end point (a,b) from the allowable end points area that minimizes the quotient
      thiscost=warpinggrid[a][b].costsofar/
      warpinggrid[a][b].lengthsofar
    3. If thiscost < avgcost, set avgcost=thiscost, sa=a, sb=b, sx=x, sy=y.
  4. createWarpingGrid(sx,sy,o,dist) (creates again the warping grid with best start point)
  5. Reconstruct the warping path:
    i=sa
    j=sb
    do
      warpingpath.push({i,j})
      temp=i
      i=warpinggrid[i][j].previ
      j=warpinggrid[temp][j].prevj
    while(i != sx) OR (j != sy)
    warpingpath.push({i,j})
    
  6. Reverse warpingpath.
  7. Copy elements of warpingpath into an array.
  8. Return the results in a $DTWOneResult$ structure

createWarpingGrid (Internal function)

warpinggrid[x][y].costsofar=dist(x,y)
warpinggrid[x][y].lengthsofar=1
for i = x to n-1 do
 for j = MAX(y ; i-o) to MIN(m-1 ; i+o) do
      
  --Select point (a,b) from points (i-1,j), (i-1,j-1) and 
  --(i,j-1) so that (warpinggrid[a][b].costsofar+dist(i,j))/
  --(warpinggrid[a][b].lengthsofar+1) is minimized

  warpinggrid[i][j].costsofar=
    warpinggrid[a][b].costsofar+dist(i,j)
  warpinggrid[i][j].lengthsofar=
    warpinggrid[a][b].lengthsofar+1
  warpinggrid[i][j].previ=a
  warpinggrid[i][j].prevj=b
 end
end
return warpinggrid

Here,

sqrDist (Internal function)

return (observation[i]-template[j])^2
At this point, only one distance metrics function is needed. When the selected distance metrics is Euclidian metrics, observation and template point to bare sample vectors. When the selected metrics is derivative metrics, observation and template point to vectors containing estimated derivatives that are calculated prior to calling the CreateWarpingGrid function.

calcDerivatives (Internal function)

for i = 1 to l-2 do
  deriv[i]=((vector[i]-vector[i-1])+(vector[i+1]
           -vector[i-1])/2)/2
end
deriv[0]=deriv[1]
deriv[l-1] = deriv[l-2]
return deriv


next up previous contents
Next: Wrapper interface Up: Library detailed information Previous: Private data structures   Contents
2002-03-19