Last active
August 29, 2015 14:10
-
-
Save timsgardner/8e6e066e82c6c0961e72 to your computer and use it in GitHub Desktop.
dynamic time warping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns dtw.core) | |
(defn distance [a b] | |
(Math/Pow (- a b) 2)) | |
(defn dtw [s t] | |
(let [n (count s) | |
m (count t) | |
bottom (apply hash-map | |
(mapcat (fn [i] [[i 0] Double/PositiveInfinity]) | |
(range 1 n))) | |
left (apply hash-map | |
(mapcat (fn [i] [[0 i] Double/PositiveInfinity]) | |
(range 1 m))) | |
d (merge {[0 0] 0} bottom left)] | |
((reduce | |
(fn [acc [i j]] | |
(assoc acc | |
[i j] | |
(+ (distance (nth s i) (nth t j)) | |
(min | |
(acc [(dec i) j]) | |
(acc [i (dec j)]) | |
(acc [(dec i) (dec j)]))))) | |
d | |
(for [i (range 1 n) | |
j (range 1 m)] | |
[i j])) | |
[(dec n) (dec m)]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
adapted from https://gist.github.com/nasser/a11a27e0e28538bc573e