Skip to content

Instantly share code, notes, and snippets.

@mutaku
Created March 14, 2017 16:42
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 mutaku/b61fd5a1912401e77e3d6f3c46dc6861 to your computer and use it in GitHub Desktop.
Save mutaku/b61fd5a1912401e77e3d6f3c46dc6861 to your computer and use it in GitHub Desktop.
class DTW_WINDOW(object):
"""Perform Distance Time Warping"""
def __init__(self, v1, v2, dist=dtw.DISTANCES['euclidean'], win=50):
set1, set2 = np.asarray(v1), np.asarray(v2)
self.cost_matrix = sys.maxint * np.ones((set1.size, set2.size))
self.cost_matrix[0, 0] = dist(set1[0], set2[0])
for i in range(1, set1.size):
self.cost_matrix[i, 0] = self.cost_matrix[i-1, 0] + dist(set1[i], set2[0])
for j in range(1, set2.size):
self.cost_matrix[0, j] = self.cost_matrix[0, j-1] + dist(set1[0], set2[j])
for i in range(1, set1.size):
for j in xrange(max(1, i - win),
min(set2.size, i + win)):
self.cost_matrix[i, j] = min(self.cost_matrix[i - 1, j - 1],
self.cost_matrix[i - 1, j],
self.cost_matrix[i, j - 1]) + dist(set1[i], set2[j])
self.distance = self.cost_matrix[-1, -1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment