Skip to content

Instantly share code, notes, and snippets.

@fffej
Created July 21, 2014 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fffej/e2b737d341ef102b5a94 to your computer and use it in GitHub Desktop.
Save fffej/e2b737d341ef102b5a94 to your computer and use it in GitHub Desktop.
DynamicTimeWarpingWin.hs
dtwWin :: V.Vector a -> V.Vector a -> (a -> a -> Int) -> Int -> Array (Int,Int) Int
dtwWin x y cost window = runSTArray $ do
let n = V.length x
m = V.length y
maxCost = maxBound
w = max window (abs (n - m)) -- constrain window size
d <- newArray ((0,0),(m,n)) maxCost
writeArray d (0,0) 0
forM_ [1..n] $ \i ->
forM_ [max 1 (i-w) .. min m (i+w)] $ \j -> do
let c = cost (x ! (i - 1)) (y ! (j - 1))
insertion <- readArray d (j,i-1)
deletion <- readArray d (j-1,i)
match <- readArray d (j-1,i-1)
writeArray d (j,i) (c + minimum [insertion,deletion,match])
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment