Skip to content

Commit

Permalink
added some tdoa stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilia Shumailov committed Apr 19, 2018
1 parent e3e6681 commit 7d38bea
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 13 deletions.
19 changes: 11 additions & 8 deletions audiphil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)


Expand Down Expand Up @@ -64,14 +65,16 @@ 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')
plt.xlabel('Predicted label')
plt.show()




10 changes: 5 additions & 5 deletions ml_au.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
91 changes: 91 additions & 0 deletions tdoa.py
Original file line number Diff line number Diff line change
@@ -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


0 comments on commit 7d38bea

Please sign in to comment.