From 7d38beae92125d2dedbf983fc710a557cb9b1e1e Mon Sep 17 00:00:00 2001 From: Ilia Shumailov Date: Thu, 19 Apr 2018 14:22:42 +0100 Subject: [PATCH] added some tdoa stuff --- audiphil.py | 19 ++++++----- ml_au.py | 10 +++--- tdoa.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 tdoa.py diff --git a/audiphil.py b/audiphil.py index c05ccce..5e2c33c 100755 --- a/audiphil.py +++ b/audiphil.py @@ -2,6 +2,7 @@ from collections import defaultdict import audio_features as fea +import tdoa import numpy as np import itertools import file_processing as iop @@ -16,13 +17,13 @@ def split_into_channels(sig, num_chan=2): for j in xrange(num_chan): outputs[j].append(sig[i+j]) - return [a(outputs[x]) for x in outputs] + return [np.array(outputs[x]) for x in outputs] def play_signal(signal, fs = 44100, max_vol=None): if max_vol is not None: - signal = np.array(signal).copy() - signal = signal/max(np.abs(signal)) - signal = signal*max_vol + signal = np.array(signal).copy() + signal = signal/max(np.abs(signal)) + signal = signal*max_vol scikits.audiolab.play(signal, fs=fs) @@ -64,10 +65,10 @@ def plot_confusion_matrix(cm, classes, thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): - if normalize: - plt.text(j, i, "%0.2f"% cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") - else: - plt.text(j, i, "%d"% int(cm[i, j]), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") + if normalize: + plt.text(j, i, "%0.2f"% cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") + else: + plt.text(j, i, "%d"% int(cm[i, j]), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') @@ -75,3 +76,5 @@ def plot_confusion_matrix(cm, classes, plt.show() + + diff --git a/ml_au.py b/ml_au.py index d8a2659..b608d9f 100755 --- a/ml_au.py +++ b/ml_au.py @@ -23,13 +23,13 @@ def kfold(X, y, clfs_names, num_splits, cm=False, cm_size=15): #cur_acc = f1_score(y_test, y_res, average="macro") cur_acc = accuracy_score(y_test, y_res) - - if cm: - mycm = confusion_matrix(y_test, y_res, labels=labels) - au.plot_confusion_matrix(mycm, classes=labels, normalize=False, title="{} {}".format(name, "%0.2f" % accuracy_score(y_test, y_res)), cm_size=cm_size) + + if cm: + mycm = confusion_matrix(y_test, y_res, labels=labels) + au.plot_confusion_matrix(mycm, classes=labels, normalize=False, title="{} {}".format(name, "%0.2f" % accuracy_score(y_test, y_res)), cm_size=cm_size) scores.append(cur_acc) - classifiers.append(cur) + classifiers.append(cur) scores = np.array(scores) d[name].append("KFOLD: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) diff --git a/tdoa.py b/tdoa.py new file mode 100644 index 0000000..a072051 --- /dev/null +++ b/tdoa.py @@ -0,0 +1,91 @@ +import numpy as np + +def Gxx(s1, s2): + pad1 = np.zeros(len(s1)).astype("float") + pad2 = np.zeros(len(s2)).astype("float") + + cs1 = np.hstack([s1,pad1]) + cs2 = np.hstack([pad2,s2]) + + f_s1 = np.fft.fft(cs1) + f_s2 = np.fft.fft(cs2) + + f_s2c = np.conj(f_s2) + f_s = f_s1 * f_s2c + + return f_s + +def corr(s1, s2, maxdiff=None): + f_s=Gxx(s1,s2) + + res = np.fft.ifft(f_s).real + + return np.argmax(res) - len(s2) + +def corr_PHAT(s1, s2, maxdiff=None): + f_s = Gxx(s1, s2) + + kf_s = f_s/abs(f_s) + + res = np.fft.ifft(kf_s).real + + return np.argmax(res) - len(s2) + +def corr_weiner(s1, s2, maxdiff=None): + + f_s = Gxx(a(s1), a(s2)) + f_s_1 = Gxx(a(s1), a(s1)) + f_s_2 = Gxx(a(s2), a(s2)) + + c12 = (f_s)**2/(f_s_1*f_s_2) + kf_s = f_s * abs(c12) + + res = (np.fft.ifft(kf_s)).real + + return np.argmax(res) - len(s2) + +def asdf(s1, s2, N=64, maxdiff=None): + res = (-2*np.fft.ifft(np.fft.fft(s1)*np.conj(np.fft.fft(s2))).real + sum(s1**2) + sum(s2**2))/N; + return np.argmin(res) - len(s2) + +def tde_lms(s1, s2, maxdiff=16, _mu=1e-4): + + cfilt = np.zeros(2*maxdiff) + + for i in xrange(maxdiff, len(s1) - maxdiff): + x1 = a(s2[i-maxdiff:i+maxdiff]) + + err = s1[i] - np.dot(cfilt, x1) + cfilt = cfilt + _mu*err*x1 + + return np.argmax(cfilt) - maxdiff + +def tde_aed(s1, s2, maxdiff=16, _mu=1e-3): + + h0 = np.full(maxdiff, 0.5).T + h1 = np.full(maxdiff, 0.5).T + + h0[maxdiff//2] = 1 + h1[maxdiff//2] = 1 + + u = a([h1.T, -h0.T]).T + + for i in xrange(maxdiff, len(s1)): + x0 = a([ s1[i-k] for k in xrange(maxdiff)]).T + x1 = a([ s2[i-k] for k in xrange(maxdiff)]).T + + xk = a([x0.T, x1.T]).T + + ek = np.dot(u.T,xk) + + t = u - _mu*np.dot(xk, ek) + u = (t/np.linalg.norm(t)) + + h1 = u[:, 0] + h0 = u[:, 1] + + dif = np.argmax(h1) - np.argmax(h0) + + return dif + +