Skip to content

Commit

Permalink
Sorted DTW algorithm and path extraction without decomposing into mod…
Browse files Browse the repository at this point in the history
…ules
  • Loading branch information
JosephBond committed Oct 5, 2023
1 parent 829e500 commit 20ef3a3
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions fluid/example/dtw/matrix-update.fld
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,29 @@ let feq Infty Infty = True;
let minAndPrev (i, j) im1 jm1 ijm1 =
let minim = fminimal [im1, jm1, ijm1] in
if feq minim im1 then
((i - 1, j), minim)
((i, j + 1), minim)
else
if feq minim jm1 then
((i, j - 1), minim)
else ((i - 1, j - 1), minim);

let localMinUpdate seq1 seq2 matrix (i, j) =
((i + 1, j ), minim)
else ((i, j), minim);
let extractPath indmatrix (i, j) =
let traverser (n,m) matrix accum =
if and (n == 1) (m == 1)
then accum
else
traverser (matrix!(n,m)) matrix ((n - 1,m - 1) : accum)
in traverser (i,j) indmatrix Nil;
let localMinUpdate seq1 seq2 (costmatrix, indmatrix) (i, j) =
let iEntr = nth (i - 1) seq1;
jEntr = nth (j - 1) seq2;
dist = (iEntr - jEntr) * (iEntr - jEntr);
im1 = matrix!(i , j + 1);
jm1 = matrix!(i + 1, j);
im1jm1 = matrix!(i , j );
((prei,prej), minim) = minAndPrev (i + 1, j + 1) im1 jm1 im1jm1;
im1 = costmatrix!(i , j + 1);
jm1 = costmatrix!(i + 1, j);
im1jm1 = costmatrix!(i , j );
(prev, minim) = minAndPrev (i, j) im1 jm1 im1jm1;
newVal = fadd (FNum dist) minim
in matrixUpdate matrix (i + 1,j + 1) newVal
in foldl (localMinUpdate [3,1,2,2,1] [2,0,0,3,3,1,0]) (costMatrixInit 6 8 2) (nextIndices 5 7 2)
in (matrixUpdate costmatrix (i + 1,j + 1) newVal, matrixUpdate indmatrix (i + 1,j + 1) prev)
in
let (finished, indices) = foldl (localMinUpdate [3,1,2,2,1] [2,0,0,3,3,1,0]) (costMatrixInit 6 8 2, [| 0 | (i,j) in (6, 8) |]) (nextIndices 5 7 2)
in
(finished, extractPath indices (6,8))

0 comments on commit 20ef3a3

Please sign in to comment.