pyACA: Documentation 0.3.1
Source Code for Audio Content Analysis
Loading...
Searching...
No Matches
Functions
pyACA.ToolSimpleDtw Namespace Reference

Functions

 ToolSimpleDtw (D)
 helper function: dynamic time warping
 

Function Documentation

◆ ToolSimpleDtw()

ToolSimpleDtw ( D)

helper function: dynamic time warping

Parameters
Ddistance matrix
Returns
p: path with matrix indices
C: accumulated cost matrix

Definition at line 12 of file ToolSimpleDtw.py.

12def ToolSimpleDtw(D):
13
14 # init directions for back-tracking [diag, vert, hori]
15 iDec = np.array([[-1, -1], [-1, 0], [0, -1]])
16
17 # cost initialization
18 C = np.zeros(D.shape)
19 C[0, :] = np.cumsum(D[0, :])
20 C[:, 0] = np.cumsum(D[:, 0])
21
22 # traceback initialization
23 DeltaP = np.zeros(D.shape, dtype=int)
24 DeltaP[0, :] = 2 # (0,-1)
25 DeltaP[:, 0] = 1 # (-1,0)
26 DeltaP[0, 0] = 0 # (-1,-1)
27
28 # recursion
29 for n_A in range(1, D.shape[0]):
30 for n_B in range(1, D.shape[1]):
31 # find preceding min (diag, column, row)
32 DeltaP[n_A, n_B] = int(np.argmin([C[n_A - 1, n_B - 1], C[n_A - 1, n_B], C[n_A, n_B - 1]]))
33 prevC_index = [n_A, n_B] + iDec[DeltaP[n_A, n_B], :]
34 C[n_A, n_B] = D[n_A, n_B] + C[prevC_index[0], prevC_index[1]]
35
36 # traceback init
37 p = np.asarray(D.shape, dtype=int) - 1 # start with the last element
38 n = p
39
40 while (n[0] >= 0) or (n[1] >= 0):
41 n = n + iDec[DeltaP[n[0], n[1]], :]
42
43 # update path
44 tmp = np.vstack([n, p])
45 p = tmp
46
47 return p[1:, :], C