-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsvd.py
46 lines (38 loc) · 1.39 KB
/
svd.py
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
import numpy as np
import pickle
from clip import Spectrogram
class SVD:
def __init__(self, spectrogram=None, filename=None):
if spectrogram is None and filename is None:
print "Must specify either spectrogram or filename"
exit(0)
elif spectrogram is not None and filename is not None:
print "Can't specify both spectrogram and filename"
exit(0)
elif spectrogram is not None:
(self.left, self.sv, self.right) = np.linalg.svd(spectrogram.data, full_matrices=False)
self.params = spectrogram.params
else:
f = open(filename, "rb")
self.left = pickle.load(f)
self.sv = pickle.load(f)
self.right = pickle.load(f)
self.params = pickle.load(f)
f.close()
def save(self, filename):
f = open(filename, "wb")
pickle.dump(self.left, f)
pickle.dump(self.sv, f)
pickle.dump(self.right, f)
pickle.dump(self.params, f)
f.close()
def mask(self, k=[0]):
zero_out = np.array([i for i in xrange(self.sv.size) if i not in k])
if zero_out.size:
self.sv[zero_out] = 0.0
def reconstruct(self):
truncated = np.dot(self.left, self.sv[:, np.newaxis] * self.right)
s = Spectrogram(None)
s.params = self.params
s.data = truncated
return s