-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDissertationFigures.py
366 lines (308 loc) · 14.1 KB
/
DissertationFigures.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""
Functions to make some of the figures I used in my dissertation
and in my ISMIR 2017 paper
"""
from BlockWindowFeatures import *
from Covers80Experiments import *
from CSMSSMTools import *
from Covers80 import *
from SongComparator import *
import scipy.io.wavfile
import librosa
def plotCSM(CSM, artist1, artist2, songName):
[I2, J2] = np.meshgrid(np.arange(CSM.shape[1]), np.arange(CSM.shape[0]))
CSM2 = np.array(CSM)
#CSM2[np.abs(I2 - J2) > 300] = np.inf
idx = np.unravel_index(np.argmin(CSM2), CSM2.shape)
print(idx)
plt.imshow(CSM, cmap = 'afmhot', interpolation = 'nearest')
plt.hold(True)
plt.scatter(idx[1], idx[0], 50)
plt.xlabel(artist2 + " Block Index")
plt.ylabel(artist1 + " Block Index")
plt.title("CSM " + songName)
return idx
def getSampleSSMs():
Kappa = 0.1
hopSize = 512
TempoBias1 = 180
TempoBias2 = 180
DPixels = 400
BeatsPerBlock = 8
p = np.arange(DPixels)
[I, J] = np.meshgrid(p, p)
FeatureParams = {'MFCCBeatsPerBlock':BeatsPerBlock, 'MFCCSamplesPerBlock':200, 'DPixels':DPixels, 'ChromaBeatsPerBlock':20, 'ChromasPerBlock':40}
CSMTypes = {'MFCCs':'Euclidean', 'SSMs':'Euclidean', 'CurvsSS':'Euclidean', 'TorsSS':'Euclidean', 'D2s':'EMD1D', 'Chromas':'CosineOTI'}
fin = open('covers32k/list1.list', 'r')
files1 = [f.strip() for f in fin.readlines()]
fin.close()
fin = open('covers32k/list2.list', 'r')
files2 = [f.strip() for f in fin.readlines()]
fin.close()
cmap = 'Spectral'
#67 is a good male/female example
for index in [11]:
fileprefix = "Covers80%i"%index
filename1 = "covers32k/" + files1[index] + ".mp3"
filename2 = "covers32k/" + files2[index] + ".mp3"
artist1 = getCovers80ArtistName(files1[index])
artist2 = getCovers80ArtistName(files2[index])
songName = getCovers80SongName(files1[index])
print("Getting features for %s..."%filename1)
(XAudio1, Fs1) = getAudio(filename1)
(tempo, beats1) = getBeats(XAudio1, Fs1, TempoBias1, hopSize)
(Features1, O1) = getBlockWindowFeatures((XAudio1, Fs1, tempo, beats1, hopSize, FeatureParams))
bRatio1 = float(Fs1)/hopSize
print("Getting features for %s..."%filename2)
(XAudio2, Fs2) = getAudio(filename2)
(tempo, beats2) = getBeats(XAudio2, Fs2, TempoBias2, hopSize)
(Features2, O2) = getBlockWindowFeatures((XAudio2, Fs2, tempo, beats2, hopSize, FeatureParams))
bRatio2 = float(Fs2)/hopSize
#Make SSM CSM
plt.figure()
CSM = getCSM(Features1['SSMs'], Features2['SSMs'])
idx = plotCSM(CSM, artist1, artist2, songName)
plt.savefig("DissertationFigures/CSM%i_SSM.svg"%index, bbox_inches = 'tight')
D1 = np.zeros((DPixels, DPixels))
D1[I < J] = Features1['SSMs'][idx[0]]
D1 = D1 + D1.T
t1l = beats1[idx[0]]/bRatio1
t1r = beats1[idx[0]+BeatsPerBlock]/bRatio1
s1 = beats1[idx[0]]*hopSize
s2 = beats1[idx[0]+BeatsPerBlock]*hopSize
x1 = XAudio1[s1:s2]
scipy.io.wavfile.write("DissertationFigures/%i_1.wav"%index, Fs1, x1)
D2 = np.zeros((DPixels, DPixels))
D2[I < J] = Features2['SSMs'][idx[1]]
D2 = D2 + D2.T
t2l = beats2[idx[1]]/bRatio2
t2r = beats2[idx[1]+BeatsPerBlock]/bRatio2
s1 = beats2[idx[1]]*hopSize
s2 = beats2[idx[1]+BeatsPerBlock]*hopSize
x2 = XAudio2[s1:s2]
scipy.io.wavfile.write("DissertationFigures/%i_2.wav"%index, Fs2, x2)
#Plot spectrograms
plt.clf()
plt.figure(figsize=(12, 5))
plt.subplot(211)
S1 = librosa.logamplitude(np.abs(librosa.stft(x1)))
#librosa.display.specshow(S1, x_axis='time', y_axis='log')
plt.subplot(212)
S2 = librosa.logamplitude(np.abs(librosa.stft(x2)))
#librosa.display.specshow(S2, x_axis='time', y_axis='log')
plt.savefig("DissertationFigures/Spectrograms%i.svg"%index, bbox_inches='tight')
#Plot SSMs
plt.clf()
plt.subplot(121)
plt.title(artist1)
plt.imshow(D1, interpolation = 'nearest', cmap = cmap, extent = (t1l, t1r, t1r, t1l))
plt.xlabel("Time (sec)")
plt.ylabel("Time (sec)")
plt.subplot(122)
plt.title(artist2)
plt.imshow(D2, interpolation = 'nearest', cmap = cmap, extent = (t2l, t2r, t2r, t2l))
plt.xlabel("Time (sec)")
plt.ylabel("Time (sec)")
plt.savefig("DissertationFigures/SSMs%i.svg"%index, bbox_inches = 'tight')
# #Make HPCP CSM
# off1 = 400
# off2 = 700
# F1 = Features1['Chromas'][off1:off1+200]
# F2 = Features2['Chromas'][off2:off2+200]
# CSM = getCSMType(F1, O1, F2, O2, 'CosineOTI')
# idx = plotCSM(CSM, artist1, artist2, songName)
# plt.savefig("DissertationFigures/CSM%i_HPCP.svg"%index, bbox_inches = 'tight')
#
# #Plot HPCP Blocks
# plt.clf()
# HPCP1 = Features1['Chromas'][idx[0] + off1]
# HPCP2 = Features2['Chromas'][idx[1] + off2]
# HPCP1 = np.reshape(HPCP1, [len(HPCP1)/12, 12])
# HPCP2 = np.reshape(HPCP2, [len(HPCP2)/12, 12])
# plt.subplot(211)
# librosa.display.specshow(HPCP1.T, y_axis = 'chroma')
# plt.title("HPCP %s"%artist1)
# plt.subplot(212)
# librosa.display.specshow(HPCP2.T, y_axis = 'chroma')
# plt.title("HPCP %s"%artist2)
# plt.savefig("DissertationFigures/HPCP_%i.svg"%index, bbox_inches = 'tight')
def makeCSMWinSizeVideo():
Kappa = 0.1
hopSize = 512
TempoBias = 180
index1 = 6
index2 = 62
fin = open('covers32k/list1.list', 'r')
files1 = [f.strip() for f in fin.readlines()]
fin.close()
fin = open('covers32k/list2.list', 'r')
files2 = [f.strip() for f in fin.readlines()]
fin.close()
filename1 = "covers32k/" + files1[index1] + ".mp3"
filename2 = "covers32k/" + files2[index1] + ".mp3"
filename3 = "covers32k/" + files2[index2] + ".mp3"
artist1 = getCovers80ArtistName(files1[index1])
artist2 = getCovers80ArtistName(files2[index1])
artist3 = getCovers80ArtistName(files2[index2])
songName1 = getCovers80SongName(files1[index1])
songName2 = getCovers80SongName(files2[index1])
songName3 = getCovers80SongName(files2[index2])
FeatureParams = {'MFCCBeatsPerBlock':4, 'DPixels':50}
CSMTypes = {'MFCCs':'Euclidean', 'SSMs':'Euclidean', 'SSMsDiffusion':'Euclidean', 'Geodesics':'Euclidean', 'Jumps':'Euclidean', 'Curvs':'Euclidean', 'Tors':'Euclidean', 'CurvsSS':'Euclidean', 'TorsSS':'Euclidean', 'D2s':'EMD1D', 'Chromas':'CosineOTI'}
(XAudio1, Fs1) = getAudio(filename1)
(tempo1, beats1) = getBeats(XAudio1, Fs1, TempoBias, hopSize)
(XAudio2, Fs2) = getAudio(filename2)
(tempo2, beats2) = getBeats(XAudio2, Fs2, TempoBias, hopSize)
(XAudio3, Fs3) = getAudio(filename3)
(tempo3, beats3) = getBeats(XAudio3, Fs3, TempoBias, hopSize)
FeatureName = 'SSMs'
plt.figure(figsize=(15, 12))
N1 = len(beats1)
N2 = len(beats2)
N3 = len(beats3)
for Win in range(4, 30):
FeatureParams['MFCCBeatsPerBlock'] = Win
(Features1, O1) = getBlockWindowFeatures((XAudio1, Fs1, tempo1, beats1, hopSize, FeatureParams))
(Features2, O2) = getBlockWindowFeatures((XAudio2, Fs2, tempo2, beats2, hopSize, FeatureParams))
(Features3, O3) = getBlockWindowFeatures((XAudio3, Fs3, tempo3, beats3, hopSize, FeatureParams))
res1 = getCSMSmithWatermanScores(Features1[FeatureName], O1, Features2[FeatureName], O2, Kappa, CSMTypes[FeatureName], True)
res2 = getCSMSmithWatermanScores(Features1[FeatureName], O1, Features3[FeatureName], O3, Kappa, CSMTypes[FeatureName], True)
#[artist1, artist2, artist3] = ["", "", ""]
plt.clf()
plt.subplot(231)
plt.imshow(res1['CSM'], cmap = 'afmhot', interpolation = 'nearest')
plt.title("True Cover, BeatsPerBlock = %i\n%s"%(Win, songName1))
plt.xlabel("%s Beat Index"%artist2)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N2])
plt.ylim([N1, 0])
plt.subplot(232)
plt.title("KNN Binary Matrix")
plt.imshow(1 - res1['DBinary'], cmap = 'gray')
plt.xlabel("%s Beat Index"%artist2)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N2])
plt.ylim([N1, 0])
plt.subplot(233)
plt.imshow(res1['D'], cmap = 'afmhot', interpolation = 'nearest')
plt.title("SMWat Score = %i"%res1['score'])
plt.xlabel("%s Beat Index"%artist2)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N2])
plt.ylim([N1, 0])
plt.subplot(234)
plt.imshow(res2['CSM'], cmap = 'afmhot', interpolation = 'nearest')
plt.title("False Cover, BeatsPerBlock = %i\n%s vs\n %s"%(Win, songName1, songName3))
plt.xlabel("%s Beat Index"%artist3)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N3])
plt.ylim([N1, 0])
plt.subplot(235)
plt.title("KNN Binary Matrix")
plt.imshow(1 - res2['DBinary'], cmap = 'gray')
plt.xlabel("%s Beat Index"%artist3)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N3])
plt.ylim([N1, 0])
plt.subplot(236)
plt.imshow(res2['D'], cmap = 'afmhot', interpolation = 'nearest')
plt.title("SMWat Score = %i"%res2['score'])
plt.xlabel("%s Beat Index"%artist3)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N3])
plt.ylim([N1, 0])
plt.savefig("%i.png"%Win, bbox_inches = 'tight')
def makeCSMSSMSizeVideo():
Kappa = 0.1
hopSize = 512
TempoBias = 180
index = 6
fin = open('covers32k/list1.list', 'r')
files1 = [f.strip() for f in fin.readlines()]
fin.close()
fin = open('covers32k/list2.list', 'r')
files2 = [f.strip() for f in fin.readlines()]
fin.close()
filename1 = "covers32k/" + files1[index] + ".mp3"
filename2 = "covers32k/" + files2[index] + ".mp3"
artist1 = getCovers80ArtistName(files1[index])
artist2 = getCovers80ArtistName(files2[index])
songName = getCovers80SongName(files1[index])
FeatureParams = {'MFCCBeatsPerBlock':20, 'DPixels':50}
CSMTypes = {'MFCCs':'Euclidean', 'SSMs':'Euclidean', 'SSMsDiffusion':'Euclidean', 'Geodesics':'Euclidean', 'Jumps':'Euclidean', 'Curvs':'Euclidean', 'Tors':'Euclidean', 'CurvsSS':'Euclidean', 'TorsSS':'Euclidean', 'D2s':'EMD1D', 'Chromas':'CosineOTI'}
(XAudio1, Fs1) = getAudio(filename1)
(tempo1, beats1) = getBeats(XAudio1, Fs1, TempoBias, hopSize)
(XAudio2, Fs2) = getAudio(filename2)
(tempo2, beats2) = getBeats(XAudio2, Fs2, TempoBias, hopSize)
FeatureName = 'SSMs'
plt.figure(figsize=(15, 6))
N1 = len(beats1)
N2 = len(beats2)
count = 0
for DPixels in [400, 350, 300, 250, 200, 150, 100, 90, 80, 70, 60, 50, 45, 40, 35, 30, 25, 20, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]:
FeatureParams['DPixels'] = DPixels
(Features1, O1) = getBlockWindowFeatures((XAudio1, Fs1, tempo1, beats1, hopSize, FeatureParams))
(Features2, O2) = getBlockWindowFeatures((XAudio2, Fs2, tempo2, beats2, hopSize, FeatureParams))
res = getCSMSmithWatermanScores(Features1[FeatureName], O1, Features2[FeatureName], O2, Kappa, CSMTypes[FeatureName], True)
plt.clf()
plt.subplot(131)
plt.imshow(res['CSM'], cmap = 'afmhot', interpolation = 'nearest')
plt.title("%i x %i SSMs"%(DPixels, DPixels))
plt.xlabel("%s Beat Index"%artist2)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N2])
plt.ylim([N1, 0])
plt.subplot(132)
plt.title("KNN Binary Matrix")
plt.imshow(1 - res['DBinary'], cmap = 'gray')
plt.xlabel("%s Beat Index"%artist2)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N2])
plt.ylim([N1, 0])
plt.subplot(133)
plt.imshow(res['D'], cmap = 'afmhot', interpolation = 'nearest')
plt.title("SMWat Score = %i"%res['score'])
plt.xlabel("%s Beat Index"%artist2)
plt.ylabel("%s Beat Index"%artist1)
plt.xlim([0, N2])
plt.ylim([N1, 0])
plt.savefig("%i.png"%count, bbox_inches = 'tight')
count += 1
def getFalseCoversPair():
Kappa = 0.1
hopSize = 512
TempoBias1 = 180
TempoBias2 = 180
index1 = 6
index2 = 62
fin = open('covers32k/list1.list', 'r')
files1 = [f.strip() for f in fin.readlines()]
fin.close()
fin = open('covers32k/list2.list', 'r')
files2 = [f.strip() for f in fin.readlines()]
fin.close()
filename1 = "covers32k/" + files1[index1] + ".mp3"
filename2 = "covers32k/" + files2[index2] + ".mp3"
fileprefix = "Covers80_%i_%i"%(index1, index2)
artist1 = getCovers80ArtistName(files1[index1])
artist2 = getCovers80ArtistName(files2[index2])
songName1 = getCovers80SongName(files1[index1])
songName2 = getCovers80SongName(files2[index2])
#filename1 = 'MIREX_CSIBSF/GotToGiveItUp.mp3'
#filename2 = 'MIREX_CSIBSF/BlurredLines.mp3'
#fileprefix = "BlurredLines"
#FeatureParams = {'DPixels':200, 'NCurv':400, 'NJump':400, 'NTors':400, 'D2Samples':50, 'CurvSigma':20, 'D2Samples':40, 'MFCCSamplesPerBlock':200, 'GeodesicDelta':10, 'NGeodesic':400, 'lifterexp':0.6, 'MFCCBeatsPerBlock':12, 'ChromaBeatsPerBlock':20, 'ChromasPerBlock':40}
#FeatureParams = {'ChromaBeatsPerBlock':20, 'ChromasPerBlock':40, 'DPixels':200, 'MFCCBeatsPerBlock':20}
CurvSigmas = [10, 60]
FeatureParams = {'MFCCBeatsPerBlock':20, 'MFCCSamplesPerBlock':200, 'DPixels':50, 'ChromaBeatsPerBlock':20, 'ChromasPerBlock':40}
CSMTypes = {'MFCCs':'Euclidean', 'SSMs':'Euclidean', 'SSMsDiffusion':'Euclidean', 'Geodesics':'Euclidean', 'Jumps':'Euclidean', 'Curvs':'Euclidean', 'Tors':'Euclidean', 'CurvsSS':'Euclidean', 'TorsSS':'Euclidean', 'D2s':'EMD1D', 'Chromas':'CosineOTI'}
for sigma in CurvSigmas:
CSMTypes['Jumps%g'%sigma] = 'Euclidean'
CSMTypes['Curvs%g'%sigma] = 'Euclidean'
CSMTypes['Tors%g'%sigma] = 'Euclidean'
compareTwoSongs(filename1, TempoBias1, filename2, TempoBias2, hopSize, FeatureParams, CSMTypes, Kappa, fileprefix, songName1, songName2)
if __name__ == '__main__':
getSampleSSMs()
#getFalseCoversPair()
#makeCSMWinSizeVideo()
#makeCSMSSMSizeVideo()