-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project_2_Code.m
502 lines (478 loc) · 14 KB
/
Project_2_Code.m
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
%% Part 1.1(a) Music score for the guitar in the GNR clip
[y, Fs] = audioread('GNR.m4a');
tr_gnr = length(y)/Fs; % record time in seconds
plot((1:length(y))/Fs,y);
xlabel('Time [sec]'); ylabel('Amplitude');
title('Sweet Child O'' Mine');
p8 = audioplayer(y,Fs); playblocking(p8);
L = tr_gnr; n = length(y);
k = (1/L)*[0:n/2-1 -n/2:-1];
ks = fftshift(k);
t2 = linspace(0,L,n+1); t = t2(1:n);
% Produce the GNR Spectrogram
a = 50;
tau = 0:0.1:tr_gnr;
freq_k= [];
for j = 1:length(tau)
g = exp(-a*(t - tau(j)).^2); % Window function
Sg = g.*y';
Sgt = fft(Sg);
Sgt_spec(:,j) = fftshift(abs(Sgt));
[M,I] = max(abs(Sgt));
freq_k(j) = abs(k(I));
end
figure(2)
pcolor(tau,ks,Sgt_spec)
shading interp
set(gca,'ylim',[0 1000],'Fontsize',16)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('GNR Spectrogram')
yyaxis right
ylabel('Music score for the guitar')
yticks([277.0, 311.0, 369.0, 415.0, 556.0, 701.0, 742.0])
yticklabels({'C#4','D#4','F#4','G#4','C#5','F5','F#5'})
print(gcf,'-dpng','GNR Spectrogram.png')
% Produce the music score diagram for the guitar in the GNR clip
figure(3)
plot(tau, freq_k, '.','Color','b','LineWidth', 3)
title('Music score for the guitar in the GNR clip','FontSize', 30)
ylim([150 900])
xlabel('time (sec)','FontSize', 12)
ylabel('frequency (Hz)','FontSize', 12)
yyaxis right
ylabel('Music score for the guitar')
yticks([277.0, 311.0, 369.0, 415.0, 556.0, 701.0, 742.0])
yticklabels({'C#4','D#4','F#4','G#4','C#5','F5','F#5'})
set(gca,'FontSize',12)
print(gcf,'-dpng','fig3.png')
%% Part 1.1(b) Music score for the guitar in the GNR clip w/o overtone
close all; clear all; clc;
figure(1)
[y, Fs] = audioread('GNR.m4a');
tr_gnr = length(y)/Fs; % record time in seconds
L = tr_gnr; n = length(y);
k = (1/L)*[0:n/2-1 -n/2:-1];
ks = fftshift(k);
t2 = linspace(0,L,n+1); t = t2(1:n);
a = 50;
tau = 0:0.1:tr_gnr;
for j = 1:length(tau)
g = exp(-a*(t - tau(j)).^2); % Window function
Sg = g.*y';
Sgt = fft(Sg);
Sgt_spec(:,j) = fftshift(abs(Sgt);
[M1,I1] = max(abs(Sgt));
filter = exp(-0.01*(k - k(I1)).^2);
Sgtf = filter.*Sgt;
Sgt_spec(:,j) = fftshift(abs(Sgtf));
end
figure(1)
pcolor(tau,ks,Sgt_spec)
shading interp
set(gca,'ylim',[0 1000],'Fontsize',16)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('GNR Spectrogram w/o Overtones')
yyaxis right
ylabel('Music score for the guitar')
yticks([277.0, 311.0, 369.0, 415.0, 556.0, 701.0, 742.0])
yticklabels({'C#4','D#4','F#4','G#4','C#5','F5','F#5'})
print(gcf,'-dpng','GNR Spectrogram no overtone.png')
%% Part 1.2 Music score for the bass in the Floyd clip
close all; clear all; clc;
figure(1)
[y, Fs] = audioread('Floyd.m4a');
tr_gnr = length(y)/Fs; % record time in seconds
plot((1:length(y))/Fs,y);
xlabel('Time [sec]'); ylabel('Amplitude');
title('Comfortably Numb');
p8 = audioplayer(y,Fs); playblocking(p8);
L = tr_gnr; n = length(y);
k = (1/L)*[0:n/2-1 -n/2:-1];
ks = fftshift(k);
t2 = linspace(0,L,n+1); t = t2(1:n-1);
% Divide Floyd Spectrogram into 4 time periods with 25s each
k1 = (1/15)*[0:n/8-1 -n/8:-1];
ks1 = fftshift(k1);
k2 = (1/15)*[0:n/8-1 -n/8:-1];
ks2 = fftshift(k2);
k3 = (1/15)*[0:n/8-1 -n/8:-1];
ks3 = fftshift(k3);
k4 = (1/(L-45))*[0:n/8-1 -n/8:-1];
ks4 = fftshift(k4);
y1 = y(1:(length(y)-1)/4);
y2 = y((length(y)-1)/4+1:(length(y)-1)/2);
y3 = y((length(y)-1)/2+1:3*(length(y)-1)/4);
y4 = y(3*(length(y)-1)/4+1:length(y)-1);
t_1st_quarter = t(1:length(t)/4);
t_2nd_quarter = t(length(t)/4+1: length(t)/2);
t_3rd_quarter = t(length(t)/2+1:3*length(t)/4);
t_4th_quarter = t(3*length(t)/4+1:length(t));
tau1 = 0:0.1:15;
tau2 = 15:0.1:30;
tau3 = 30:0.1:45;
tau4 = 45:0.1:tr_gnr;
a = 100;
% Floyd 0 - 15s Spectrogram
for j = 1:length(tau1)
g1 = exp(-a*(t_1st_quarter - tau1(j)).^2); % Window function
Sg1 = g1.*y1';
Sgt1 = fft(Sg1);
Sgt_spec1(:,j) = fftshift(abs(Sgt1));
end
figure(2)
pcolor(tau1,ks1,Sgt_spec1)
shading interp
set(gca,'ylim',[0 300],'Fontsize',16)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram 1-15s')
print(gcf,'-dpng',' Floyd Spectrogram 1-15.png')
% Floyd 15 - 30s Spectrogram
for j = 1:length(tau2)
g2 = exp(-a*(t_2nd_quarter - tau2(j)).^2); % Window function
Sg2 = g2.*y2';
Sgt2 = fft(Sg2);
Sgt_spec2(:,j) = fftshift(abs(Sgt2));
end
figure(3)
pcolor(tau2,ks2,Sgt_spec2)
shading interp
set(gca,'ylim',[0 300],'Fontsize',16)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram 15-30s')
print(gcf,'-dpng',' Floyd Spectrogram 15-30s.png')
% Floyd 30 - 45s Spectrogram
for j = 1:length(tau3)
g3 = exp(-a*(t_3rd_quarter - tau3(j)).^2); % Window function
Sg3 = g3.*y3';
Sgt3 = fft(Sg3);
Sgt_spec3(:,j) = fftshift(abs(Sgt3));
end
figure(4)
pcolor(tau3,ks3,Sgt_spec3)
shading interp
set(gca,'ylim',[0 300],'Fontsize',16)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram 30-45s')
print(gcf,'-dpng',' Floyd Spectrogram 30-45s.png')
% Floyd 45 - 60s Spectrogram
for j = 1:length(tau4)
g4 = exp(-a*(t_4th_quarter - tau4(j)).^2); % Window function
Sg4 = g4.*y4';
Sgt4 = fft(Sg4);
Sgt_spec4(:,j) = fftshift(abs(Sgt4));
end
figure(5)
pcolor(tau4,ks4,Sgt_spec4)
shading interp
set(gca,'ylim',[0 300],'Fontsize',16)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram 45-60s')
print(gcf,'-dpng',' Floyd Spectrogram 45-60s.png')
%% Part 2 Music score for the bass in the Floyd clip w/o overtone
close all; clear all; clc;
figure(1)
[y, Fs] = audioread('Floyd.m4a');
tr_gnr = length(y)/Fs; % record time in seconds
plot((1:length(y))/Fs,y);
xlabel('Time [sec]'); ylabel('Amplitude');
title('Comfortably Numb');
p8 = audioplayer(y,Fs); playblocking(p8);
L = tr_gnr; n = length(y);
k = (1/L)*[0:n/2-1 -n/2:-1];
ks = fftshift(k);
t2 = linspace(0,L,n+1); t = t2(1:n-1);
% Divide Floyd Clip into four 15-secs periods
k1 = (1/15)*[0:n/8-1 -n/8:-1];
ks1 = fftshift(k1);
k2 = (1/15)*[0:n/8-1 -n/8:-1];
ks2 = fftshift(k2);
k3 = (1/15)*[0:n/8-1 -n/8:-1];
ks3 = fftshift(k3);
k4 = (1/(L-45))*[0:n/8-1 -n/8:-1];
ks4 = fftshift(k4);
y1 = y(1:(length(y)-1)/4);
y2 = y((length(y)-1)/4+1:(length(y)-1)/2);
y3 = y((length(y)-1)/2+1:3*(length(y)-1)/4);
y4 = y(3*(length(y)-1)/4+1:length(y)-1);
t_1st_quarter = t(1:length(t)/4);
t_2nd_quarter = t(length(t)/4+1: length(t)/2);
t_3rd_quarter = t(length(t)/2+1:3*length(t)/4);
t_4th_quarter = t(3*length(t)/4+1:length(t));
tau1 = 0:0.1:15;
tau2 = 15:0.1:30;
tau3 = 30:0.1:45;
tau4 = 45:0.1:tr_gnr;
a = 50;
% Floyd 1 - 15s w/o overtone Spectrogram + Score
freq_k1= [];
for j = 1:length(tau1)
g1 = exp(-a*(t_1st_quarter - tau1(j)).^2); % Window function
Sg1 = g1.*y1';
Sgt1 = fft(Sg1);
[M1,I1] = max(abs(Sgt1));
filter1 = exp(-0.01*(k1 - k1(I1)).^2);
Sgtf1 = filter1.*Sgt1;
[M,I] = max(abs(Sgtf1));
freq_k1(j) = abs(k1(I));
Sgt_spec1(:,j) = fftshift(abs(Sgtf1));
end
figure(2)
pcolor(tau1,ks1,Sgt_spec1)
shading interp
set(gca,'ylim',[0 200],'Fontsize',12)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram w/o Overtones 1-15s')
yyaxis right
ylabel('Music score for bass')
yticks([82.0, 90.7, 97.3, 110.0, 124.0])
yticklabels({'E2','F#2','G2','A2','B2'})
print(gcf,'-dpng','1-15_no_overtone.png')
figure(3)
plot(tau1, freq_k1, '.','Color','b','LineWidth', 3)
title('Music score for Bass in Floyd 1-15s Clip','FontSize',12)
ylim([0 200])
xlabel('time (sec)','FontSize', 12)
ylabel('frequency (Hz)','FontSize', 12)
yyaxis right
ylabel('Music score for bass')
yticks([82.0, 90.7, 97.3, 110.0, 124.0])
yticklabels({'E2','F#2','G2','A2','B2'})
set(gca,'FontSize',12)
print(gcf,'-dpng','1-15_notes.png')
% Floyd 15 - 30s w/o overtone Spectrogram + Score
freq_k2= [];
for j = 1:length(tau2)
g2 = exp(-a*(t_2nd_quarter - tau2(j)).^2); % Window function
Sg2 = g2.*y2';
Sgt2 = fft(Sg2);
[M2,I2] = max(abs(Sgt2));
filter2 = exp(-0.01*(k2 - k2(I2)).^2);
Sgtf2 = filter2.*Sgt2;
[M,I] = max(abs(Sgtf2));
freq_k2(j) = abs(k2(I));
Sgt_spec2(:,j) = fftshift(abs(Sgtf2));
end
figure(4)
pcolor(tau2,ks2,Sgt_spec2)
shading interp
set(gca,'ylim',[0 200],'Fontsize',12)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram w/o Overtones 15-30s')
yyaxis right
ylabel('Music score for bass')
yticks([82.0, 90.7, 97.3, 110.0, 124.0])
yticklabels({'E2','F#2','G2','A2','B2'})
print(gcf,'-dpng','15-30_no_overtone.png.png')
figure(5)
plot(tau2, freq_k2, '.','Color','b','LineWidth', 3)
title('Music score for Bass in Floyd 15-30s Clip','FontSize', 12)
ylim([0 200])
xlabel('time (sec)','FontSize', 12)
ylabel('frequency (Hz)','FontSize', 12)
yyaxis right
ylabel('Music score for bass')
yticks( )
yticklabels({'E2','F#2','G2','A2','B2'})
set(gca,'FontSize',12)
print(gcf,'-dpng','15-30_notes.png')
% Floyd 30 - 45s w/o overtone Spectrogram + Score
for j = 1:length(tau3)
g3 = exp(-a*(t_3rd_quarter - tau3(j)).^2); % Window function
Sg3 = g3.*y3';
Sgt3 = fft(Sg3);
[M3,I3] = max(abs(Sgt3));
filter3 = exp(-0.01*(k4 - k3(I3)).^2);
Sgtf3 = filter3.*Sgt3;
Sgt_spec3(:,j) = fftshift(abs(Sgtf3));
end
figure(6)
pcolor(tau3,ks3,Sgt_spec3)
shading interp
set(gca,'ylim',[0 200],'Fontsize',12)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram w/o Overtones 30-45s')
yyaxis right
ylabel('Music score for bass')
yticks([82.0, 90.7, 97.3, 110.0, 124.0])
yticklabels({'E2','F#2','G2','A2','B2'})
print(gcf,'-dpng','30-45_no_overtone.png')
% Floyd 45 - 60s w/o overtone Spectrogram + Score
figure(7)
for j = 1:length(tau4)
g4 = exp(-a*(t_4th_quarter - tau4(j)).^2); % Window function
Sg4 = g4.*y4';
Sgt4 = fft(Sg4);
[M4,I4] = max(abs(Sgt4));
filter4 = exp(-0.01*(k4 - k4(I4)).^2);
Sgtf4 = filter4.*Sgt4;
Sgt_spec4(:,j) = fftshift(abs(Sgtf4));
end
pcolor(tau4,ks4,Sgt_spec4)
shading interp
set(gca,'ylim',[0 200],'Fontsize',12)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram w/o Overtones 45-60s')
yyaxis right
ylabel('Music score for bass')
yticks([82.0, 90.7, 97.3, 110.0, 124.0])
yticklabels({'E2','F#2','G2','A2','B2'})
print(gcf,'-dpng','45-60_no_overtone.png')
%% Find Guitar solo
% Floyd 0 - 15s Spectrogram for Guitar
freq_k1= [];
for j = 1:length(tau1)
g1 = exp(-a*(t_1st_quarter - tau1(j)).^2); % Window function
Sg1 = g1.*y1';
Sgt1 = fft(Sg1);
[M1,I1] = max(abs(Sgt1));
filter1 = exp(-0.01*(k1 - k1(I1)).^2);
Sgtf1 = filter1.*Sgt1;
[M,I] = max(abs(Sgtf1));
freq_k1(j) = abs(k1(I));
Sgt_spec1(:,j) = fftshift(abs(Sgtf1));
end
figure(2)
pcolor(tau1,ks1,Sgt_spec1)
shading interp
set(gca,'ylim',[150 1000],'Fontsize',12)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram 1-15s for Guitar')
yyaxis right
ylabel('Music score for guitar')
yticks([246.2, 330.0, 372.0, 589.0])
yticklabels({'B3','E4','F#4','D5'})
print(gcf,'-dpng','1-15_guitar.png')
err_bond = 3;
target = [82.0, 90.7, 97.3, 110.0, 124.0];
for i = 1:length(freq_k1)
for j = 1:length(target)
if abs(freq_k1(i)-target(j)) < err_bond
freq_k1(i) = 0;
end
end
end
figure(3)
plot(tau1, freq_k1, '.','Color','b','LineWidth', 3)
title('Music score for Guitar in Floyd 1-15s Clip','FontSize', 12)
ylim([0 1000])
xlabel('time (sec)','FontSize', 12)
ylabel('frequency (Hz)','FontSize', 12)
yyaxis right
ylabel('Music score for guitar')
yticks([246.2, 330.0, 372.0, 589.0])
yticklabels({'B3','E4','F#4','D5'})
print(gcf,'-dpng','1-15_guitar_notes.png')
% Floyd 15 - 30s Spectrogram
freq_k2= [];
for j = 1:length(tau2)
g2 = exp(-a*(t_2nd_quarter - tau2(j)).^2); % Window function
Sg2 = g2.*y2';
Sgt2 = fft(Sg2);
[M2,I2] = max(abs(Sgt2));
filter2 = exp(-0.01*(k2 - k2(I2)).^2);
Sgtf2 = filter2.*Sgt2;
[M,I] = max(abs(Sgtf2));
freq_k2(j) = abs(k2(I));
Sgt_spec2(:,j) = fftshift(abs(Sgtf2));
end
figure(4)
pcolor(tau2,ks2,Sgt_spec2)
shading interp
set(gca,'ylim',[150 1000],'Fontsize',12)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram 15-30s for Guitar')
yyaxis right
ylabel('Music score for guitar')
yticks([164.5, 246.2, 372.0, 589.0, 749.0])
yticklabels({'E3','B3','F#4','D5','F5'})
print(gcf,'-dpng','15-30_guitar.png')
err_bond = 3;
target = [82.0, 90.7, 97.3, 110.0, 124.0];
for i = 1:length(freq_k2)
for j = 1:length(target)
if abs(freq_k2(i)-target(j)) < err_bond
freq_k2(i) = 0;
end
end
end
figure(5)
plot(tau2, freq_k2, '.','Color','b','LineWidth', 3)
title('Music score for Guitar in Floyd 15-30s Clip','FontSize', 12)
ylim([0 1000])
xlabel('time (sec)','FontSize', 12)
ylabel('frequency (Hz)','FontSize', 12)
yyaxis right
ylabel('Music score for guitar')
yticks([164.5, 246.2, 372.0, 589.0, 749.0])
yticklabels({'E3','B3','F#4','D5','F5'})
print(gcf,'-dpng','15-30_guitar_notes.png')
%Floyd 30 - 45s Spectrogram
for j = 1:length(tau3)
g3 = exp(-a*(t_3rd_quarter - tau3(j)).^2); % Window function
Sg3 = g3.*y3';
Sgt3 = fft(Sg3);
[M3,I3] = max(abs(Sgt3));
filter3 = exp(-0.01*(k4 - k3(I3)).^2);
Sgtf3 = filter3.*Sgt3;
Sgt_spec3(:,j) = fftshift(abs(Sgtf3));
end
figure(6)
pcolor(tau3,ks3,Sgt_spec3)
shading interp
set(gca,'ylim',[150 1000],'Fontsize',12)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram 30-45s for Guitar')
yyaxis right
ylabel('Music score for guitar')
yticks([164.5, 246.2, 330.0, 372.0, 589.0])
yticklabels({'E3','B3','E4','F#4','D5'})
print(gcf,'-dpng','30-45_guitar.png')
% Floyd 45 - 60s Spectrogram
for j = 1:length(tau4)
g4 = exp(-a*(t_4th_quarter - tau4(j)).^2); % Window function
Sg4 = g4.*y4';
Sgt4 = fft(Sg4);
[M4,I4] = max(abs(Sgt4));
filter4 = exp(-0.01*(k4 - k4(I4)).^2);
Sgtf4 = filter4.*Sgt4;
Sgt_spec4(:,j) = fftshift(abs(Sgtf4));
end
figure(7)
pcolor(tau4,ks4,Sgt_spec4)
shading interp
set(gca,'ylim',[150 1000],'Fontsize',12)
colormap(hot)
colorbar
xlabel('time (sec)'), ylabel('frequency (Hz)')
title ('Floyd Spectrogram 45-60s for Guitar')
yyaxis right
ylabel('Music score for guitar')
yticks([164.5, 246.2, 330.0, 372.0, 589.0])
yticklabels({'E3','B3','E4','F#4','D5'})
print(gcf,'-dpng','45-60_guitar.png')