def DTW_multiple_alignment(Sref, S): cost = dtw(Sref, S) L = len(Sref) alignment = [None] * len(Sref) i, j = len(Sref) - 1, len(S) - 1 while i >= 0 and j >= 0: if alignment[i] != None: alignment[i].extend([S[j]]) else: alignment[i] = [S[j]] if i == 0: j = j - 1 elif j == 0: i = i - 1 else: score = min(cost[i - 1][j - 1], cost[i][j - 1], cost[i - 1][j]) if score == cost[i - 1][j - 1]: i = i - 1 j = j - 1 elif score == cost[i - 1][j]: i = i - 1 else: j = j - 1 return alignment
def sliding_window(sequence, templates, threshold): matches = {} sliding_window_size = 1 #sec step_size = 50 * (sliding_window_size / 4) # 50 Hz, 25% overlap for i in range(len(sequence), step_size): segment = sequence[i:i + step_size] for template in templates: dist = dtw(segment, template)[0] if dist < threshold: matches[segment] = template
# print(k) word1 = get_formants("Search.wav") counter = 0 speaker = "" recorded_word = "" found = 0 with open("Database.txt", "r") as f: for line in f: if counter % 3 == 0: speaker = line elif counter % 3 == 1: recorded_word = line else: word2 = numpy.loadtxt(line[:-1]) warnings.simplefilter("ignore") dist = dtw(word1, word2, euclidean_distances) if dist < 0.485: found = 1 print("Prepoznao rec ", recorded_word, " koju je izgovorio ", speaker, " a razdaljina iznosi: ", dist) counter += 1 if found == 0: print("Nije prepoznao rec") elif option == "Multisearch": record_to_file("Search.wav") k = split("Search.wav") print("Recording finished, search in progress") print(k) word1 = get_formants("Search.wav") counter = 0
def test_dtw_path(s1, s2): assert (dtw(s1, s2, None, None)[1] == fastdtw.dtw(s1, s2)[1]) print "Test path passed"
def test_dtw_distance(s1, s2): assert (dtw(s1, s2, None, None)[0] == fastdtw.dtw(s1, s2)[0]) print "Test distance passed"
from DTW import dtw import fastdtw import numpy as np x = np.array([1, 2, 3, 4, 5], dtype='float') y = np.array([1, 2, 3, 4, 5], dtype='float') window = [] print fastdtw.dtw(x, y) print dtw(x, y, None, None) def test_dtw_distance(s1, s2): assert (dtw(s1, s2, None, None)[0] == fastdtw.dtw(s1, s2)[0]) print "Test distance passed" def test_dtw_path(s1, s2): assert (dtw(s1, s2, None, None)[1] == fastdtw.dtw(s1, s2)[1]) print "Test path passed" test_dtw_distance(x, y) test_dtw_path(x, y) x = np.array([1, 2, 3, 4, 5], dtype='float') y = np.array([2, 3, 4], dtype='float') test_dtw_distance(x, y) test_dtw_path(x, y) x = np.array([[1, 2, 3, 4, 5]], dtype='float')
y1, sr1 = librosa.load(ref) y2, sr2 = librosa.load(test1) y3, sr3 = librosa.load(test2) y_harmonic1, y_percussive1 = librosa.effects.hpss(y1) y_harmonic2, y_percussive2 = librosa.effects.hpss(y2) y_harmonic3, y_percussive3 = librosa.effects.hpss(y3) mfcc1 = librosa.feature.mfcc(y=y_percussive1, sr=sr1, n_mfcc=13) mfcc2 = librosa.feature.mfcc(y=y_percussive2, sr=sr2, n_mfcc=13) mfcc3 = librosa.feature.mfcc(y=y_percussive3, sr=sr3, n_mfcc=13) x = np.amax(mfcc1, axis=1) y = np.amax(mfcc2, axis=1) z = np.amax(mfcc3, axis=1) x_matrix = np.array(dtw(x, y)) y_matrix = np.array(dtw(x, z)) print(x_matrix) print() print(y_matrix) print() print("The distance between", ref[ref.find(start) + len(start):ref.rfind(end)], "and", test1[test1.find(start) + len(start):test1.rfind(end)], "is equal to", x_matrix[x_matrix.shape[0] - 1, x_matrix.shape[1] - 1]) print("The distance between", ref[ref.find(start) + len(start):ref.rfind(end)], "and", test2[ref.find(start) + len(start):test2.rfind(end)], "is equal to", y_matrix[y_matrix.shape[0] - 1, y_matrix.shape[1] - 1])