-
Notifications
You must be signed in to change notification settings - Fork 4
/
voicerecognition.asv
1783 lines (1578 loc) · 74.4 KB
/
voicerecognition.asv
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% Project: Voice Recognition and Identification system
% By Mahima Garg, Omar Razi, Supriya Phutela, Vaibhav Kapoor, Varun Chopra
%--------------------------------------------------------------------------
%% Main Function Voice Recognition
function []=voicerecognition()
% For clear screen
clc;
% Ronaldo is a variable used to directly set the minimum distance for
% speech recognition.
ronaldo=10;
% Drogba is a variable used to directly set the maximum number of
% users stored in database
drogba=8;
% st, st1, st2, st3, will be used for filenames related purposes reducing
% code redundancy.
char st; char st1; char st2; char st3;
disp('Project: Voice Recognition and Identification system');
disp('By Mahima Garg & Omar Razi & Supriya Phutela & Vaibhav Kapoor & Varun Chopra ');
disp(' ');
pause(0.5);
disp('LOADING ');
pause(1);
disp('... ');
pause(1);
disp('... ');
pause(1);
disp('... ');
pause(1);
disp('... ');
msgbox('We have tried to make this project as user friendly as possible. We Hope you Appreciate and Enjoy !!!','Voice Recognition and Identification System');
% Preallocating array
str = {8}; fstr = {8}; nbtr = {8};
ste = {8}; fste = {8}; nbte = {8};
ctr = {8}; dtr={8};
cte = {8}; dte={8};
data = {drogba,4};
code = {8};
for i = 1:8
% Read audio data from train folder for performing operations
st=strcat('train\s',num2str(i),'.wav');
[s1 fs1 nb1]=wavread(st);
str{i} = s1; fstr{i} = fs1; nbtr{i} = nb1;
% Read audio data from test folder for performing operations
st = strcat('test\s',num2str(i),'.wav');
[st1 fst1 nbt1] = wavread(st);
ste{i} = st1; fste{i} = fst1; nbte{i} = nbt1;
% Compute MFCC of the audio data to be used in Speech Processing for Train
% Folder
ctr{i} = mfcc(str{i},fstr{i});
% Compute MFCC of the audio data to be used in Speech Processing for Test
% Folder
cte{i} = mfcc(ste{i},fste{i});
% Compute Vector Quantization of the audio data to be used in Speech
% Processing for Train Folder
dtr{i} = vqlbg(ctr{i},16);
% Compute Vector Quantization of the audio data to be used in Speech
% Processing for Test Folder
dte{i} = vqlbg(cte{i},16);
end
% For making Choice
ch=0;
poss=11;
while ch~=poss
ch=menu('Speaker Recognition System','1: Human speaker recognition',...
'2: Technical data of samples',...
'3: Power Spectrum','4: Power Spectrum with different M and N',...
'5: Mel-Spaced Filter Bank',...
'6: Spectrum before and after Mel-Frequency wrapping',...
'7: 2D plot of acoustic vectors',...
'8: Plot of VQ codewords','9: Recognition rate of the computer',...
'10: Test with other speech files','11: Exit');
disp(' ');
%----------------------------------------------------------------------
%% 1: Human speaker recognition
% Play each sound file in the TRAIN folder. Can you distinguish the voices
% of those eight speakers? Now play each sound in the TEST folder in a
% random order without looking at the file name (pretending that you do not
% know the speaker) and try to identify the speaker using your knowledge of
% their voices that you just learned from the TRAIN folder. This is exactly
% what the computer will do in our system.
% What is your (human performance) recognition rate ?
% Record this result so that it can be used later to be compared with the
% computer's performance of our system. Both of us seem to be unable to
% recognise random people just by listening at their voice. Our success
% rates for the provided samples were 1 person out of 8 each.
%
% However, for the samples we used in question 10, we were easily able to
% recognise each speaker. This is probably because we knew all the persons.
% We also realized that we did not identify speakers by the frequencies
% they use to talk, but rather by other characteristics, like accent,
% speed, etc.
if ch==1
disp('> 1: Human speaker recognition');
disp('Play each sound file in the TRAIN folder.');
disp('Can you distinguish the voices of those eight speakers?');
disp('Now play each sound in the TEST folder in a random order without looking at the file name ');
disp('and try to identify the speaker using your knowledge of their voices that you have just heard,');
disp('from the TRAIN folder. This is exactly what the computer will do in our system.');
disp(' ');
disp(' ');
disp('All of us seem to be unable to recognise random people just by listening to their voice. ');
disp('We also realize that we do not identify speakers by the frequencies with which they use to talk, ');
disp('but rather by other characteristics, like accent, speed, etc.');
pause(1);
ch2=0;
while ch2~=4
ch2=menu('Select Folder','Train','Test','User','Exit');
if ch2==1
ch3=0;
while ch3~=9
ch3=menu('Train :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
p=audioplayer(str{ch3},fstr{ch3},nbtr{ch3});
play(p);
end
end
end
if ch2==2
ch3=0;
while ch3~=9
ch3=menu('Test :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
p=audioplayer(ste{ch3},fste{ch3},nbte{ch3});
play(p);
end
end
close all;
end
if ch2==3
if (exist('sound_database.dat','file')==2)
load('sound_database.dat','-mat');
ch32=0;
while ch32 ~=2
ch32=menu('Database Information','Database','Exit');
if ch32==1
st=strcat('Sound Database has : #',num2str(sound_number),'words. Enter a database number : #');
prompt = {st};
dlg_title = 'Database Information';
num_lines = 1;
def = {'1'};
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
an = inputdlg(prompt,dlg_title,num_lines,def);
an=cell2mat(an);
a = str2double(an);
if (isempty(an))
else
if (a <= sound_number)
st=strcat('u',num2str(an));
[s fs nb]=wavread(st);
p=audioplayer(s,fs,nb);
play(p);
else
warndlg('Invalid Word ','Warning');
end
end
end
close all;
end
else
warndlg('Database is empty.',' Warning ')
end
end
end
end
%----------------------------------------------------------------------
%% 2: Technical data of samples
% Read a sound file into Matlab. Check it by playing the sound file in
% Matlab using the function: sound. What is the sampling rate? What is the
% highest frequency that the recorded sound can capture with fidelity? With
% that sampling rate, how many msecs of actual speech are contained in a
% block of 256 samples?
%
% Plot the signal to view it in the time domain. It should be obvious that
% the raw data in the time domain has a great amount of data and for this
% reason it is difficult to analyse the voice characteristic. So the
% purpose of this step (speech feature extraction) should be clear now!
%
% Intermediate steps that follows:
%
% Frames blocking phase
% To obtain a matrix M containing all the frames, we used the following script:
% That way we obtain the 256 x 129 matrix M.
%
% Windowing phase
% We create the Hamming matrix and transform the matrix M into the new
% matrix M2, where the column vectors of M2 are the original frame vectors
% transformed by the Hamming filter.
%
% FFT Phase
% We create a new matrix M3 where the column vectors are the FFTs of the
% column vectors of M2.
if ch==2
disp('> 2: Technical data of samples');
ch23=0;
while ch23~=4
ch23=menu('Select Folder','Train','Test','User','Exit');
if ch23==1
poss2=9;
ch2=0;
while ch2~=poss2
ch2=menu('Technical data of samples for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch2~=9
t = 0:1/fstr{ch2}:(length(str{ch2}) - 1)/fstr{ch2};
plot(t, str{ch2}), axis([0, (length(str{ch2}) - 1)/fstr{ch2} -0.4 0.5]);
st=sprintf('Plot of signal s%d.wav',ch2);
title(st);
xlabel('Time [s]');
ylabel('Amplitude (normalized)')
end
end
close all
end
if ch23==2
poss2=9;
ch2=0;
while ch2~=poss2
ch2=menu('Technical data of samples for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch2~=9
t = 0:1/fste{ch2}:(length(ste{ch2}) - 1)/fste{ch2};
plot(t, ste{ch2}), axis([0, (length(ste{ch2}) - 1)/fste{ch2} -0.4 0.5]);
st=sprintf('Plot of signal s%d.wav',ch2);
title(st);
xlabel('Time [s]');
ylabel('Amplitude (normalized)')
end
end
close all
end
if ch23==3
if (exist('sound_database.dat','file')==2)
load('sound_database.dat','-mat');
ch32=0;
while ch32 ~=2
ch32=menu('Database Information','Database','Exit');
if ch32==1
st=strcat('Sound Database has : #',num2str(sound_number),'words. Enter a database number : #');
prompt = {st};
dlg_title = 'Database Information';
num_lines = 1;
def = {'1'};
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
an = inputdlg(prompt,dlg_title,num_lines,def);
an=cell2mat(an);
a = str2double(an);
if (isempty(an))
else
if (a <= sound_number)
st=strcat('u',num2str(an));
[s fs]=wavread(st);
t = 0:1/fs:(length(s) - 1)/fs;
plot(t, s), axis([0, (length(s) - 1)/fs -0.4 0.5]);
st=sprintf('Plot of signal %s',st);
title(st);
xlabel('Time [s]');
ylabel('Amplitude (normalized)')
else
warndlg('Invalid Word ','Warning');
end
end
end
end
close all;
else
warndlg('Database is empty.',' Warning ')
end
end
end
end
%----------------------------------------------------------------------
%% 3: linear and logarithmic power spectrum plot
% After successfully running the preceding process, what is the
% interpretation you can make of the result obtained? Compute the power
% spectrum and plot it out using the imagesc command. Note that it is
% better to view the power spectrum on the log scale. Locate the region in
% the plot that contains most of the energy. Translate this location into
% the actual ranges in time (msec) and frequency (in Hz) of the input
% speech signal.
%
% As stated before, the columns matrix M3 contain the frames of the
% original signal, filtered by the Hamming filter and transformed with the
% FFT. The elements of M3 are complex numbers and symmetrical because FFT
% was used to transform the data.
% Each column in M3 is a power spectrum representation of the original signal.
% To plot the power spectrum, we take the absolute values of the matrix
% elements. Since the spectrum is symmetric, we only plot half of it. Note
% that we plot simultaneously with the linear and the logarithmic spectrum
% scale.
%
% The result obtained is the plot in this section.
% In this plot, the areas containing the highest level of energy are
% displayed in red. As we can see on the plot, the red area is located
% between 0.3 and 0.7 seconds. The plot also shows that most of the energy
% is concentrated in the lower frequencies (between 50 Hz and 1 kHz).
if ch==3
M = 100;
N = 256;
disp('> 3: Power Spectrum Plot');
disp(' ');
disp('>Linear and Logarithmic spectrum plot');
ch23=0;
while ch23~=4
ch23=menu('Select Folder','Train','Test','User','Exit');
if ch23==1
poss3=9;
ch3=0;
while ch3~=poss3
ch3=menu('Linear and Logarithmic Power Spectrum Plot for : ','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
% 3 (linear)
frames = blockFrames(str{ch3}, fstr{ch3}, M, N);
t = N / 2;
tm = length(str{ch3}) / fstr{ch3};
subplot(121);
imagesc([0 tm], [0 fstr{ch3}/2], abs(frames(1:t, :)).^2), axis xy;
title('Power Spectrum (M = 100, N = 256)');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
% 3 (logarithmic)
subplot(122);
imagesc([0 tm], [0 fstr{ch3}/2], 20 * log10(abs(frames(1:t, :)).^2)), axis xy;
title('Logarithmic Power Spectrum (M = 100, N = 256)');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
% D=get(gcf,'Position');
% set(gcf,'Position',round([D(1)*.5 D(2)*.5 D(3)*2 D(4)*1.3]))
end
end
close all
end
if ch23==2
poss3=9;
ch3=0;
while ch3~=poss3
ch3=menu('Linear and Logarithmic Power Spectrum Plot for : ','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
% 3 (linear)
frames = blockFrames(ste{ch3}, fste{ch3}, M, N);
t = N / 2;
tm = length(ste{ch3}) / fste{ch3};
subplot(121);
imagesc([0 tm], [0 fste{ch3}/2], abs(frames(1:t, :)).^2), axis xy;
title('Power Spectrum (M = 100, N = 256)');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
% 3 (logarithmic)
subplot(122);
imagesc([0 tm], [0 fste{ch3}/2], 20 * log10(abs(frames(1:t, :)).^2)), axis xy;
title('Logarithmic Power Spectrum (M = 100, N = 256)');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
% D=get(gcf,'Position');
% set(gcf,'Position',round([D(1)*.5 D(2)*.5 D(3)*2 D(4)*1.3]))
end
end
close all;
end
if ch23==3
if (exist('sound_database.dat','file')==2)
load('sound_database.dat','-mat');
ch32=0;
while ch32 ~=2
ch32=menu('Database Information','Database','Exit');
if ch32==1
st=strcat('Sound Database has : #',num2str(sound_number),'words. Enter a database number : #');
prompt = {st};
dlg_title = 'Database Information';
num_lines = 1;
def = {'1'};
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
an = inputdlg(prompt,dlg_title,num_lines,def);
an=cell2mat(an);
a = str2double(an);
if (isempty(an))
else
if (a <= sound_number)
st=strcat('u',num2str(an));
[s fs]=wavread(st);
frames = blockFrames(s, fs, M, N);
t = N / 2;
tm = length(s) / fs;
subplot(121);
imagesc([0 tm], [0 fs/2], abs(frames(1:t, :)).^2), axis xy;
title('Power Spectrum (M = 100, N = 256)');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
%Question 3 (logarithmic)
subplot(122);
imagesc([0 tm], [0 fs/2], 20 * log10(abs(frames(1:t, :)).^2)), axis xy;
title('Logarithmic Power Spectrum (M = 100, N = 256)');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
else
warndlg('Invalid Word ','Warning');
end
end
end
end
close all;
else
warndlg('Database is empty.',' Warning ')
end
end
end
end
%----------------------------------------------------------------------
%% 4: Plots for different values for N
% Compute and plot the power spectrum of a speech file using different
% frames sizes: for example N = 128, 256 and 512. In each case, set the
% frame increement M to be about N/3. Can you describe and explain the
% differences between these spectra ?
%
% For N = 128 we have a high resolution of time. Furthermore each frame
% lasts a very short period of time. This result shows that the signal for
% a frame doesn't change its nature (i.e. it will be for the same vowel or
% consonant). On the other hand, there are only 65 distinct frequencies
% samples. This means that we have a poor frequency resolution.
%
% For N = 256 we have a compro mise between the resolution in time and the
% frequency resolution.
% For N = 512 we have an excellent frequency resolution (256 different
% values) but there are lesser frames, meaning that the resolution in
% time is strongly reduced.
% It seems that a value of 256 for N is an acceptable compromise.
% Furthermore the number of frames is relatively small, which will reduce
% computing time.
if ch==4
disp('> 4: Plots for different values for M and N');
lN = [128 256 512];
ch23=0;
while ch23~=4
ch23=menu('Select Folder','Train','Test','User','Exit');
if ch23==1
poss3=9;
ch3=0;
while ch3~=poss3
ch3=menu('Plots for different values of M and N for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
u=220;
for i = 1:length(lN)
N = lN(i);
M = round(N / 3);
frames = blockFrames(str{ch3}, fstr{ch3}, M, N);
t = N / 2;
tm = length(str{ch3}) / fstr{ch3};
temp = size(frames);
nbframes = temp(2);
u=u+1;
subplot(u)
imagesc([0 tm], [0 fstr{ch3}/2], 20 * log10(abs(frames(1:t, :)).^2)), axis xy;
title(sprintf('Power Spectrum (M = %i, N = %i, frames = %i)', M, N, nbframes));
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar
end
% D=get(gcf,'Position');
% set(gcf,'Position',round([D(1)*.5 D(2)*.5 D(3)*1.5 D(4)*1.5]))
end
end
close all
end
if ch23==2
poss3=9;
ch3=0;
while ch3~=poss3
ch3=menu('Plots for different values of M and N for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
u=220;
for i = 1:length(lN)
N = lN(i);
M = round(N / 3);
frames = blockFrames(ste{ch3}, fste{ch3}, M, N);
t = N / 2;
tm = length(ste{ch3}) / fste{ch3};
temp = size(frames);
nbframes = temp(2);
u=u+1;
subplot(u)
imagesc([0 tm], [0 fste{ch3}/2], 20 * log10(abs(frames(1:t, :)).^2)), axis xy;
title(sprintf('Power Spectrum (M = %i, N = %i, frames = %i)', M, N, nbframes));
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar
end
% D=get(gcf,'Position');
% set(gcf,'Position',round([D(1)*.5 D(2)*.5 D(3)*1.5 D(4)*1.5]))
end
end
close all;
end
if ch23==3
if (exist('sound_database.dat','file')==2)
load('sound_database.dat','-mat');
ch32=0;
while ch32 ~=2
ch32=menu('Database Information','Database','Exit');
if ch32==1
st=strcat('Sound Database has : #',num2str(sound_number),'words. Enter a database number : #');
prompt = {st};
dlg_title = 'Database Information';
num_lines = 1;
def = {'1'};
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
an = inputdlg(prompt,dlg_title,num_lines,def);
an=cell2mat(an);
a = str2double(an);
if (isempty(an))
else
if (a <= sound_number)
st=strcat('u',num2str(an));
[s fs]=wavread(st);
u=220;
for i = 1:length(lN)
N = lN(i);
M = round(N / 3);
frames = blockFrames(s, fs, M, N);
t = N / 2;
tm = length(s) / fs;
temp = size(frames);
nbframes = temp(2);
u=u+1;
subplot(u)
imagesc([0 tm], [0 fs/2], 20 * log10(abs(frames(1:t, :)).^2)), axis xy;
title(sprintf('Power Spectrum (M = %i, N = %i, frames = %i)', M, N, nbframes));
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar
end
else
warndlg('Invalid Word ','Warning');
end
end
end
end
close all;
else
warndlg('Database is empty.',' Warning ')
end
end
end
end
%----------------------------------------------------------------------
%% 5: Mel Space
% Type 'help melfb' at the Matlab prompt for more information about this
% function. Follow the guidelines to plot out the mel-spaced filter bank.
% What is the behaviour of this filter bank? Compare it with the
% theoretical part. To plot this filter bank, we use melfb command.
% This filter bank behaves like a succession of histograms on the spectrum.
% Each filter of the filter bank has a triangular frequency response. It
% quantifies the zone of the frequency spectrum.
%
% The filter bank is used to transform the spectrum of a signal into a
% representation which reflects more closely the behaviour of the human
% ear. As the human ear (or the associated neurons) favours low frequencies
% for analysing speech, the filters are denser for the lower frequencies.
% To mimic the human ear, the filters are linearly distributed for low
% frequencies (below 1kHz). For higher frequencies (above 1 kHz) the
% distribution of the filters is logarithmic.
% However we calculated 20 filters (instead of 12).
if ch==5
disp('> 5: Mel Space');
disp(' ');
disp('Mel Space is function of sampling rate and since all signals ');
disp('are recorded at same sampling rate so they have same Mel Space.');
ch23=0;
while ch23~=4
ch23=menu('Select Folder','Train','Test','User','Exit');
if ch23==1
poss3=9;
ch3=0;
while ch3~=poss3
ch3=menu('Mel Space for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
plot(linspace(0, (fstr{ch3}/2), 129), (melfb(20, 256, fstr{ch3})));
title('Mel-Spaced Filterbank');
xlabel('Frequency [Hz]');
end
end
close all
end
if ch23==2
poss3=9;
ch3=0;
while ch3~=poss3
ch3=menu('Mel Space for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
plot(linspace(0, (fste{ch3}/2), 129), (melfb(20, 256, fste{ch3})));
title('Mel-Spaced Filterbank');
xlabel('Frequency [Hz]');
end
end
close all;
end
if ch23==3
if (exist('sound_database.dat','file')==2)
load('sound_database.dat','-mat');
ch32=0;
while ch32 ~=2
ch32=menu('Database Information','Database','Exit');
if ch32==1
st=strcat('Sound Database has : #',num2str(sound_number),'words. Enter a database number : #');
prompt = {st};
dlg_title = 'Database Information';
num_lines = 1;
def = {'1'};
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
an = inputdlg(prompt,dlg_title,num_lines,def);
an=cell2mat(an);
a=str2double(an);
if (isempty(an))
else
if (a <= sound_number)
st=strcat('u',num2str(an));
[s fs]=wavread(st);
plot(linspace(0, (fs/2), 129), (melfb(20, 256, fs)));
title('Mel-Spaced Filterbank');
xlabel('Frequency [Hz]');
else
warndlg('Invalid Word ','Warning');
end
end
end
end
close all;
else
warndlg('Database is empty.',' Warning ')
end
end
end
end
%----------------------------------------------------------------------
%% 6: Modified spectrum
% Compute and plot the spectrum of a speech file before and after the mel-
% frequency wrapping step. Describe and explain the impact of the melfb
% program.
%
% As we can see in the first plot, most of the information is contained in
% the lower frequencies. This information has been extracted and amplified
% in the second plot. The second plot therefore shows the main
% characteristics of the speech signal.
% Note that the transformation produced an acoustic vector of 20 dimensions.
if ch==6
disp('> 6: Modified spectrum');
disp(' ');
disp('Spectrum before and after Mel-Frequency wrapping');
M = 100;
N = 256;
n2 = 1 + floor(N / 2);
ch23=0;
while ch23~=4
ch23=menu('Select Folder','Train','Test','User','Exit');
if ch23==1
poss3=9;
ch3=0;
while ch3~=poss3
ch3=menu('Mel Space for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
frames = blockFrames(str{ch3}, fstr{ch3}, M, N);
m = melfb(20, N, fstr{ch3});
z = m * abs(frames(1:n2, :)).^2;
tm = length(str{ch3}) / fstr{ch3};
subplot(121)
imagesc([0 tm], [0 fstr{ch3}/2], abs(frames(1:n2, :)).^2), axis xy;
title('Power Spectrum unmodified');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
subplot(122)
imagesc([0 tm], [0 20], z), axis xy;
title('Power Spectrum modified through Mel Cepstrum filter');
xlabel('Time [s]');
ylabel('Number of Filter in Filter Bank');
% colorbar;D=get(gcf,'Position');
% set(gcf,'Position',[0 D(2) D(3)/2 D(4)])
end
end
close all
end
if ch23==2
poss3=9;
ch3=0;
while ch3~=poss3
ch3=menu('Mel Space for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch3~=9
frames = blockFrames(str{ch3}, fstr{ch3}, M, N);
m = melfb(20, N, fstr{ch3});
z = m * abs(frames(1:n2, :)).^2;
tm = length(str{ch3}) / fstr{ch3};
subplot(121)
imagesc([0 tm], [0 fstr{ch3}/2], abs(frames(1:n2, :)).^2), axis xy;
title('Power Spectrum unmodified');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
subplot(122)
imagesc([0 tm], [0 20], z), axis xy;
title('Power Spectrum modified through Mel Cepstrum filter');
xlabel('Time [s]');
ylabel('Number of Filter in Filter Bank');
% colorbar;D=get(gcf,'Position');
% set(gcf,'Position',[0 D(2) D(3)/2 D(4)])
end
end
close all;
end
if ch23==3
if (exist('sound_database.dat','file')==2)
load('sound_database.dat','-mat');
ch32=0;
while ch32 ~=2
ch32=menu('Database Information','Database','Exit');
if ch32==1
st=strcat('Sound Database has : #',num2str(sound_number),'words. Enter a database number : #');
prompt = {st};
dlg_title = 'Database Information';
num_lines = 1;
def = {'1'};
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
an = inputdlg(prompt,dlg_title,num_lines,def);
an=cell2mat(an);
a=str2double(an);
if (isempty(an))
else
if (a <= sound_number)
st=strcat('u',num2str(an));
[s fs]=wavread(st);
frames = blockFrames(s, fs, M, N);
m = melfb(20, N, fs);
z = m * abs(frames(1:n2, :)).^2;
tm = length(s) / fs;
subplot(121)
imagesc([0 tm], [0 fs/2], abs(frames(1:n2, :)).^2), axis xy;
title('Power Spectrum unmodified');
xlabel('Time [s]');
ylabel('Frequency [Hz]');
colorbar;
subplot(122)
imagesc([0 tm], [0 20], z), axis xy;
title('Power Spectrum modified through Mel Cepstrum filter');
xlabel('Time [s]');
ylabel('Number of Filter in Filter Bank');
colorbar;
else
warndlg('Invalid Word ','Warning');
end
end
end
end
close all;
else
warndlg('Database is empty.',' Warning ')
end
end
end
end
%----------------------------------------------------------------------
%% 7: 2D plot of accustic vectors
% To inspect the acoustic space (MFCC vectors) we can pick any two
% dimensions (say the 5th and the 6th) and plot the data points in a 2D
% plane. Use acoustic vectors of two different speakers and plot data
% points in two different colours. Do the data regions from the two
% speakers overlap each other? Are they in clusters?
%
% Mostly the two areas overlap. But certain regions seem to be used
% exclusively by one or the other speaker. This is what will allow us to
% distinguish the different speakers.
% The points don't form actual clusters, but there are areas where the
% density of points is higher.
% Note: This is only a two dimensional plot. The actual vector
% contains 20 dimensions.
if ch==7
disp('> 7: 2D plot of accustic vectors');
ch23=0;
while ch23~=4
ch23=menu('Select Folder','Train','Test','User','Exit');
if ch23==1
poss3=3;
ch3=0;
while ch3~=poss3
ch3=menu('2D plot of accustic vectors representation : ','1. One Signal',...
'2. Two Signal','3. Exit');
if ch3==1
ch31=0;
while ch31~=9
ch31=menu('2D plot of accustic vectors for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch31~=9
plot(ctr{ch31}(5, :), ctr{ch31}(6, :), 'or');
xlabel('5th Dimension');
ylabel('6th Dimension');
st=sprintf('Signal %d ',ch31);
legend(st);
title('2D plot of accoustic vectors');
end
end
close all;
end
if ch3==2
ch32=0;
while ch32~=8
ch32=menu('2D plot of accustic vectors for :','Signal 1 & Signal 2',...
'Signal 2 & Signal 3','Signal 3 & Signal 4','Signal 4 & Signal 5',...
'Signal 5 & Signal 6','Signal 6 & Signal 7','Signal 7 & Signal 8','Exit');
if ch32~=8
plot(ctr{ch32}(5, :), ctr{ch32}(6, :), 'or');
hold on;
plot(ctr{ch32+1}(5, :), ctr{ch32+1}(6, :), 'xb');
xlabel('5th Dimension');
ylabel('6th Dimension');
st=sprintf('Signal %d,',ch32);
st1=sprintf('Signal %d', (ch32+1) );
legend(st,st1);
title('2D plot of accoustic vectors');
hold off
end
end
end
close all
end
end
if ch23==2
poss3=3;
ch3=0;
while ch3~=poss3
ch3=menu('2D plot of accustic vectors representation : ','1. One Signal',...
'2. Two Signal','3. Exit');
if ch3==1
ch31=0;
while ch31~=9
ch31=menu('2D plot of accustic vectors for :','Signal 1','Signal 2','Signal 3',...
'Signal 4','Signal 5','Signal 6','Signal 7','Signal 8','Exit');
if ch31~=9
plot(cte{ch31}(5, :), cte{ch31}(6, :), 'or');
xlabel('5th Dimension');
ylabel('6th Dimension');
st=sprintf('Signal %d ',ch31);
legend(st);
title('2D plot of accoustic vectors');
end
end
close all;
end
if ch3==2
ch32=0;
while ch32~=8
ch32=menu('2D plot of accustic vectors for :','Signal 1 & Signal 2',...
'Signal 2 & Signal 3','Signal 3 & Signal 4','Signal 4 & Signal 5',...
'Signal 5 & Signal 6','Signal 6 & Signal 7','Signal 7 & Signal 8','Exit');
if ch32~=8
plot(cte{ch32}(5, :), cte{ch32}(6, :), 'or');
hold on;
plot(cte{ch32+1}(5, :), cte{ch32+1}(6, :), 'xb');
xlabel('5th Dimension');
ylabel('6th Dimension');
st=sprintf('Signal %d,',ch32);
st1=sprintf('Signal %d', (ch32+1) );
legend(st,st1);
title('2D plot of accoustic vectors');
hold off