import numpy as np
import matplotlib.pyplot as plt
from tslearn.metrics import dtw_path, soft_dtw
from tslearn.barycenters import dtw_barycenter_averaging, softdtw_barycenter
# https://tslearn.readthedocs.io/en/stable/gen_modules/barycenters/tslearn.barycenters.softdtw_barycenter.html#tslearn.barycenters.softdtw_barycenter
size = 50
x0 = np.zeros((size, ))
x0[20:20+size//2] = np.sin(np.linspace(0, 2 * np.pi, size // 2))
x1 = np.zeros((size, ))
x1[5:5+size//2] = np.sin(np.linspace(0, 2 * np.pi, size // 2))
plt.figure()
plt.plot(x0, 'r-', label='x0') # red
plt.plot(x1, 'b-', label='x1') # blue
plt.legend()
plt.show()
path, dist = dtw_path(x0, x1)
plt.figure()
plt.plot(x0, 'r-', label='x0') # red
plt.plot(x1, 'b-', label='x1') # blue
for i, j in path:
plt.plot([i, j], [x0[i], x1[j]], "k-", alpha=.5)
plt.legend()
plt.show()
print(dist)
print(path)
print(len(path))
# 1. Define A_pi
A_pi = np.zeros(shape=(len(x0), len(x1)))
for (i, j) in path:
A_pi[i, j] = 1
# 2. Visualize A_pi
plt.imshow(A_pi)
euclidean_barycenter = (x0 + x1) /2
plt.figure()
plt.plot(x0, 'r-', label='x0') # red
plt.plot(x1, 'b-', label='x1') # blue
plt.plot(euclidean_barycenter, 'k-', label='Euclidean barycenter')
plt.legend()
plt.show()
dtw_barycenter = dtw_barycenter_averaging([x0, x1])
plt.figure()
plt.plot(x0, 'r-', label='x0') # red
plt.plot(x1, 'b-', label='x1') # blue
plt.plot(dtw_barycenter[:, 0], 'k-', label='DTW barycenter')
plt.legend()
plt.show()
softdtw_bar = softdtw_barycenter([x0, x1],gamma=10)
plt.figure()
plt.plot(x0, 'r-', label='x0') # red
plt.plot(x1, 'b-', label='x1') # blue
plt.plot(softdtw_bar[:, 0], 'k-', label='soft-DTW barycenter')
plt.legend()
plt.show()
from tslearn.metrics import dtw
dtw(x0, softdtw_bar) ** 2 + dtw(x1, softdtw_bar) ** 2
import IPython.display as ipd
# The short snippet
ipd.Audio('audio/StairwayToHeaven_MakesMeWonder.mp3')
# The 1min-long part
ipd.Audio('audio/StairwayToHeaven_1min.mp3')
from utils_audio import load_chroma_features, get_subset_wav
x_short, x_long = load_chroma_features()
# View chromagram of x_short
plt.figure(figsize=(12, 6))
plt.imshow(x_short.T)
plt.show()
from scipy.spatial.distance import cdist
def subsequence_dtw(short_seq, long_seq):
# Compute cross distance matrix
distances = cdist(short_seq, long_seq, "sqeuclidean") # distances[i, j] = distance between short_seq[i] and long_seq[j]
gamma_mat = np.zeros((len(short_seq), len(long_seq)))
for i in range(len(short_seq)):
if i == 0:
gamma_mat[i, :] = distances[i, :]
else:
for j in range(len(long_seq)):
gamma_mat[i, j] = distances[i, j] + min(
gamma_mat[i-1, j],
gamma_mat[i, j-1] if j > 0 else np.inf,
gamma_mat[i-1, j-1] if j > 0 else np.inf
)
idx_end_of_match = np.argmin(gamma_mat[-1, :])
optimal_cost = gamma_mat[-1, idx_end_of_match]
return optimal_cost, idx_end_of_match
dist, idx_end = subsequence_dtw(x_short, x_long)
ipd.Audio(get_subset_wav(idx_end), rate=22050)