-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
75 lines (50 loc) · 1.51 KB
/
code.py
File metadata and controls
75 lines (50 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
import scipy.io.wavfile
import os
def update_W(W, x, learning_rate):
updated_W = W + learning_rate * (np.linalg.inv(W.T) - np.outer(np.sign(W.dot(x)), x.T))
return updated_W
def unmix(X, W):
S = np.zeros(X.shape)
S = X.dot(W.T)
return S
Fs = 11025
def normalize(dat):
return 0.99 * dat / np.max(np.abs(dat))
def load_data():
mix = np.loadtxt('mix.dat')
return mix
def save_W(W):
np.savetxt('output/W.txt',W)
def save_sound(audio, name):
scipy.io.wavfile.write('output/{}.wav'.format(name), Fs, audio)
def unmixer(X):
M, N = X.shape
W = np.eye(N)
anneal = [0.1 , 0.1, 0.1, 0.05, 0.05, 0.05, 0.02, 0.02, 0.01 , 0.01, 0.005, 0.005, 0.002, 0.002, 0.001, 0.001]
print('Separating tracks ...')
for lr in anneal:
print(lr)
rand = np.random.permutation(range(M))
for i in rand:
x = X[i]
W = update_W(W, x, lr)
return W
def main():
# Seed the randomness of the simulation so this outputs the same thing each time
np.random.seed(0)
X = normalize(load_data())
print(X.shape)
for i in range(X.shape[1]):
save_sound(X[:, i], 'mixed_{}'.format(i))
W = unmixer(X)
print(W)
save_W(W)
S = normalize(unmix(X, W))
assert S.shape[1] == 5
for i in range(S.shape[1]):
if os.path.exists('split_{}'.format(i)):
os.unlink('split_{}'.format(i))
save_sound(S[:, i], 'split_{}'.format(i))
if __name__ == '__main__':
main()