12def ToolSimpleDtw(D):
13
14
15 iDec = np.array([[-1, -1], [-1, 0], [0, -1]])
16
17
18 C = np.zeros(D.shape)
19 C[0, :] = np.cumsum(D[0, :])
20 C[:, 0] = np.cumsum(D[:, 0])
21
22
23 DeltaP = np.zeros(D.shape, dtype=int)
24 DeltaP[0, :] = 2
25 DeltaP[:, 0] = 1
26 DeltaP[0, 0] = 0
27
28
29 for n_A in range(1, D.shape[0]):
30 for n_B in range(1, D.shape[1]):
31
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
37 p = np.asarray(D.shape, dtype=int) - 1
38 n = p
39
40 while (n[0] >= 0) or (n[1] >= 0):
41 n = n + iDec[DeltaP[n[0], n[1]], :]
42
43
44 tmp = np.vstack([n, p])
45 p = tmp
46
47 return p[1:, :], C