-
Notifications
You must be signed in to change notification settings - Fork 34
/
pesq_cd.m
2727 lines (2234 loc) · 102 KB
/
pesq_cd.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
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
function [ scores ] = pesq_cd( ref_wav, deg_wav )
% ----------------------------------------------------------------------
% PESQ objective speech quality measure
% (narrowband and wideband implementations)
%
% This function implements the PESQ measure based on the ITU standards
% P.862 [1] and P.862.1 [2] for narrowband speech and P.862.2 for
% wideband speech [3].
%
%
% Usage: scores = pesq( cleanFile, enhancedFile )
%
% cleanFile - clean input file in .wav format sampled at
% sampling frequency Fs=8 kHz or Fs=16 kHz
% for narrowband or wideband assessment,
% respectively.
%
% enhancedFile - enhanced output file in .wav format sampled
% at same sampling frequency as the cleanFile
%
% scores - For narrowband speech, two scores are returned,
% one for the raw PESQ value [1] (first value) and
% one for the MOS-mapped score value [2] (second value).
% For wideband speech, only the MOS-mapped value
% is returned [3].
%
% Example call: scores = pesq('sp04.wav', 'enhanced.wav')
%
%
% References:
%
% [1] ITU (2000). Perceptual evaluation of speech quality (PESQ), and
% objective method for end-to-end speech quality assessment of
% narrowband telephone networks and speech codecs. ITU-T
% Recommendation P.862
%
% [2] ITU (2003). Mapping function for transforming P.862 raw result
% scores to MOS-LQO, ITU-T Recommendation P. 862.1
%
% [3] ITU (2007). Wideband extension to Recommendation P.862 for the
% assessment of wideband telephone networks and speech codecs. ITU-T
% Recommendation P.862.2
%
%
% Authors: Yi Hu, Kamil Wojcicki and Philipos C. Loizou
%
%
% Copyright (c) 2006, 2012 by Philipos C. Loizou
% $Revision: 2.0 $ $Date: 5/14/2012 $
% ----------------------------------------------------------------------
if nargin ==0, fprintf('Usage: pesq( ref_wav, deg_wav )\n');
fprintf(' ref_wav = reference input filename\n');
fprintf(' deg_wav = degraded output filename\n\n');
fprintf('For more help, type: help pesq\n\n');
return;
elseif nargin > 2, error('%s.m: Incorrect number of input arguments.\nFor usage help type: help %s',mfilename,mfilename);
end
if ~isstr( ref_wav ), error( '%s.m: First input argumnet has to be a reference wav filename as string.\nFor usage help type: help %s',mfilename,mfilename); end;
if ~isstr( deg_wav ), error( '%s.m: Second input argumnet has to be a processed wav filename as string.\nFor usage help type: help %s',mfilename,mfilename); end;
if ~exist( ref_wav, 'file' ), error( '%s.m: Reference wav file: %s not found.',mfilename,ref_wav); end;
if ~exist( deg_wav, 'file' ), error( '%s.m: Processed wav file: %s not found.',mfilename,deg_wav); end;
[ ref_data, ref_sampling_rate ] = audioread( ref_wav );
[ deg_data, deg_sampling_rate ] = audioread( deg_wav );
if ref_sampling_rate ~= deg_sampling_rate, error( '%s.m: Sampling rate mismatch.\nThe sampling rate of the reference wav file (%i Hz) has to match sampling rate of the degraded wav file (%i Hz).',mfilename,ref_sampling_rate,deg_sampling_rate);
else, sampling_rate = ref_sampling_rate; end;
if sampling_rate==8E3, mode='narrowband';
elseif sampling_rate==16E3, mode='wideband';
else, error( '%s.m: Unsupported sampling rate (%i Hz).\nOnly sampling rates of 8000 Hz (for narrowband assessment)\nand 16000 Hz (for wideband assessment) are supported.',mfilename,sampling_rate);
end
clearvars -global Downsample DATAPADDING_MSECS SEARCHBUFFER Fs WHOLE_SIGNAL Align_Nfft Window
global Downsample DATAPADDING_MSECS SEARCHBUFFER Fs WHOLE_SIGNAL
global Align_Nfft Window
setup_global( sampling_rate );
TWOPI= 6.28318530717959;
for count = 0: Align_Nfft- 1
Window(1+ count) = 0.5 * (1.0 - cos((TWOPI * count) / Align_Nfft));
end
ref_data= ref_data(:).';
ref_data= ref_data* 32768;
ref_Nsamples= length( ref_data)+ 2* SEARCHBUFFER* Downsample;
ref_data= [zeros( 1, SEARCHBUFFER* Downsample), ref_data, ...
zeros( 1, DATAPADDING_MSECS* (Fs/ 1000)+ SEARCHBUFFER* Downsample)];
deg_data= deg_data(:).';
deg_data= deg_data* 32768;
deg_Nsamples= length( deg_data)+ 2* SEARCHBUFFER* Downsample;
deg_data= [zeros( 1, SEARCHBUFFER* Downsample), deg_data, ...
zeros( 1, DATAPADDING_MSECS* (Fs/ 1000)+ SEARCHBUFFER* Downsample)];
maxNsamples= max( ref_Nsamples, deg_Nsamples);
ref_data= fix_power_level( ref_data, ref_Nsamples, maxNsamples);
deg_data= fix_power_level( deg_data, deg_Nsamples, maxNsamples);
% KKW ---------
switch lower( mode )
case { [], '', 'nb', '+nb', 'narrowband', '+narrowband' }
standard_IRS_filter_dB= [0, -200; 50, -40; 100, -20; 125, -12; 160, -6; 200, 0;...
250, 4; 300, 6; 350, 8; 400, 10; 500, 11; 600, 12; 700, 12; 800, 12;...
1000, 12; 1300, 12; 1600, 12; 2000, 12; 2500, 12; 3000, 12; 3250, 12;...
3500, 4; 4000, -200; 5000, -200; 6300, -200; 8000, -200];
ref_data= apply_filter( ref_data, ref_Nsamples, standard_IRS_filter_dB);
deg_data= apply_filter( deg_data, deg_Nsamples, standard_IRS_filter_dB);
case { 'wb', '+wb', 'wideband', '+wideband' }
ref_data = apply_filters_WB( ref_data, ref_Nsamples );
deg_data = apply_filters_WB( deg_data, deg_Nsamples );
otherwise
error( sprintf('Mode: "%s" is unsupported.', mode) );
end
% -------------
%
% fid= fopen( 'log_mat_ref.txt', 'wt');
% fprintf( fid, '%f\n', ref_data);
% fclose( fid);
%
% fid= fopen( 'log_mat_deg.txt', 'wt');
% fprintf( fid, '%f\n', deg_data);
% fclose( fid);
% % to save time, read from data file ========
% fid= fopen( 'log_mat_ref.txt', 'rt');
% ref_data= fscanf( fid, '%f\n');
% ref_data= ref_data';
% fclose( fid);
% ref_Nsamples= length( ref_data)- DATAPADDING_MSECS* (Fs/ 1000);
%
% fid= fopen( 'log_mat_deg.txt', 'rt');
% deg_data= fscanf( fid, '%f\n');
% deg_data= deg_data';
% fclose( fid);
% deg_Nsamples= length( deg_data)- DATAPADDING_MSECS* (Fs/ 1000);
% % the above part will be commented after debugging ========
% for later use in psychoacoustical model
model_ref= ref_data;
model_deg= deg_data;
[ref_data, deg_data]= input_filter( ref_data, ref_Nsamples, deg_data, ...
deg_Nsamples);
% fid= fopen( 'log_mat_ref_tovad.txt', 'wt');
% fprintf( fid, '%f\n', ref_data);
% fclose( fid);
%
% fid= fopen( 'log_mat_deg_tovad.txt', 'wt');
% fprintf( fid, '%f\n', deg_data);
% fclose( fid);
[ref_VAD, ref_logVAD]= apply_VAD( ref_data, ref_Nsamples);
[deg_VAD, deg_logVAD]= apply_VAD( deg_data, deg_Nsamples);
% subplot( 2, 2, 1); plot( ref_VAD); title( 'ref\_VAD');
% subplot( 2, 2, 2); plot( ref_logVAD); title( 'ref\_logVAD');
%
% subplot( 2, 2, 3); plot( deg_VAD); title( 'deg\_VAD');
% subplot( 2, 2, 4); plot( deg_logVAD); title( 'deg\_logVAD');
%
% fid= fopen( 'mat_ref_vad.txt', 'wt');
% fprintf( fid, '%f\n', ref_VAD);
% fclose( fid);
%
% fid= fopen( 'mat_ref_logvad.txt', 'wt');
% fprintf( fid, '%f\n', ref_logVAD);
% fclose( fid);
%
% fid= fopen( 'mat_deg_vad.txt', 'wt');
% fprintf( fid, '%f\n', deg_VAD);
% fclose( fid);
%
% fid= fopen( 'mat_deg_logvad.txt', 'wt');
% fprintf( fid, '%f\n', deg_logVAD);
% fclose( fid);
%
crude_align (ref_logVAD, ref_Nsamples, deg_logVAD, deg_Nsamples,...
WHOLE_SIGNAL);
utterance_locate (ref_data, ref_Nsamples, ref_VAD, ref_logVAD,...
deg_data, deg_Nsamples, deg_VAD, deg_logVAD);
ref_data= model_ref;
deg_data= model_deg;
% make ref_data and deg_data equal length
if (ref_Nsamples< deg_Nsamples)
newlen= deg_Nsamples+ DATAPADDING_MSECS* (Fs/ 1000);
ref_data( newlen)= 0;
elseif (ref_Nsamples> deg_Nsamples)
newlen= ref_Nsamples+ DATAPADDING_MSECS* (Fs/ 1000);
deg_data( newlen)= 0;
end
pesq_mos= pesq_psychoacoustic_model (ref_data, ref_Nsamples, deg_data, ...
deg_Nsamples );
% KKW ---------
switch lower( mode )
case { [], '', 'nb', '+nb', 'narrowband', '+narrowband' }
% NB: P.862.1->P.800.1 (PESQ_MOS->MOS_LQO)
mos_lqo = 0.999 + ( 4.999-0.999 ) ./ ( 1+exp(-1.4945*pesq_mos+4.6607) );
scores = [ pesq_mos, mos_lqo ];
case { 'wb', '+wb', 'wideband', '+wideband' }
% WB: P.862.2->P.800.1 (PESQ_MOS->MOS_LQO)
mos_lqo = 0.999 + ( 4.999-0.999 ) ./ ( 1+exp(-1.3669*pesq_mos+3.8224) );
scores = [ mos_lqo ];
otherwise
error( sprintf('Mode: "%s" is unsupported.', mode) );
end
% -------------
%fprintf( '\tPrediction PESQ_MOS = %4.3f\n', pesq_mos );
clearvars -global Downsample DATAPADDING_MSECS SEARCHBUFFER Fs WHOLE_SIGNAL Align_Nfft Window
function align_filtered= apply_filter( data, data_Nsamples, align_filter_dB)
global Downsample DATAPADDING_MSECS SEARCHBUFFER Fs
align_filtered= data;
n= data_Nsamples- 2* SEARCHBUFFER* Downsample+ DATAPADDING_MSECS* (Fs/ 1000);
% now find the next power of 2 which is greater or equal to n
pow_of_2= 2^ (ceil( log2( n)));
[number_of_points, trivial]= size( align_filter_dB);
overallGainFilter= interp1( align_filter_dB( :, 1), align_filter_dB( :, 2), ...
1000);
x= zeros( 1, pow_of_2);
x( 1: n)= data( SEARCHBUFFER* Downsample+ 1: SEARCHBUFFER* Downsample+ n);
x_fft= fft( x, pow_of_2);
freq_resolution= Fs/ pow_of_2;
factorDb( 1: pow_of_2/2+ 1)= interp1( align_filter_dB( :, 1), ...
align_filter_dB( :, 2), (0: pow_of_2/2)* freq_resolution)- ...
overallGainFilter;
factor= 10.^ (factorDb/ 20);
factor= [factor, fliplr( factor( 2: pow_of_2/2))];
x_fft= x_fft.* factor;
y= ifft( x_fft, pow_of_2);
align_filtered( SEARCHBUFFER* Downsample+ 1: SEARCHBUFFER* Downsample+ n)...
= y( 1: n);
% fid= fopen( 'log_mat.txt', 'wt');
% fprintf( fid, '%f\n', y( 1: n));
% fclose( fid);
function mod_data= apply_filters( data, Nsamples)
%IIRFilt( InIIR_Hsos, InIIR_Nsos, data, data_Nsamples);
global InIIR_Hsos InIIR_Nsos DATAPADDING_MSECS Fs
% data_Nsamples= Nsamples+ DATAPADDING_MSECS* (Fs/ 1000);
% now we construct the second order section matrix
sosMatrix= zeros( InIIR_Nsos, 6);
sosMatrix( :, 4)= 1; %set a(1) to 1
% each row of sosMatrix holds [b(1*3) a(1*3)] for each section
sosMatrix( :, 1: 3)= InIIR_Hsos( :, 1: 3);
sosMatrix( :, 5: 6)= InIIR_Hsos( :, 4: 5);
%sosMatrix
% now we construct second order section direct form II filter
iirdf2= dfilt.df2sos( sosMatrix);
mod_data= filter( iirdf2, data);
% KKW ---------
function mod_data= apply_filters_WB( data, Nsamples)
global WB_InIIR_Hsos WB_InIIR_Nsos DATAPADDING_MSECS Fs
% now we construct the second order section matrix
sosMatrix= zeros( WB_InIIR_Nsos, 6);
sosMatrix( :, 4)= 1; %set a(1) to 1
% each row of sosMatrix holds [b(1*3) a(1*3)] for each section
sosMatrix( :, 1: 3)= WB_InIIR_Hsos( :, 1: 3);
sosMatrix( :, 5: 6)= WB_InIIR_Hsos( :, 4: 5);
%sosMatrix
% now we construct second order section direct form II filter
iirdf2= dfilt.df2sos( sosMatrix);
mod_data= filter( iirdf2, data);
% -------------
function [VAD, logVAD]= apply_VAD( data, Nsamples)
global Downsample MINSPEECHLGTH JOINSPEECHLGTH
Nwindows= floor( Nsamples/ Downsample);
%number of 4ms window
VAD= zeros( 1, Nwindows);
for count= 1: Nwindows
VAD( count)= sum( data( (count-1)* Downsample+ 1: ...
count* Downsample).^ 2)/ Downsample;
end
%VAD is the power of each 4ms window
LevelThresh = sum( VAD)/ Nwindows;
%LevelThresh is set to mean value of VAD
LevelMin= max( VAD);
if( LevelMin > 0 )
LevelMin= LevelMin* 1.0e-4;
else
LevelMin = 1.0;
end
%fprintf( 1, 'LevelMin is %f\n', LevelMin);
VAD( find( VAD< LevelMin))= LevelMin;
for iteration= 1: 12
LevelNoise= 0;
len= 0;
StDNoise= 0;
VAD_lessthan_LevelThresh= VAD( find( VAD<= LevelThresh));
len= length( VAD_lessthan_LevelThresh);
LevelNoise= sum( VAD_lessthan_LevelThresh);
if (len> 0)
LevelNoise= LevelNoise/ len;
StDNoise= sqrt( sum( ...
(VAD_lessthan_LevelThresh- LevelNoise).^ 2)/ len);
end
LevelThresh= 1.001* (LevelNoise+ 2* StDNoise);
end
%fprintf( 1, 'LevelThresh is %f\n', LevelThresh);
LevelNoise= 0;
LevelSig= 0;
len= 0;
VAD_greaterthan_LevelThresh= VAD( find( VAD> LevelThresh));
len= length( VAD_greaterthan_LevelThresh);
LevelSig= sum( VAD_greaterthan_LevelThresh);
VAD_lessorequal_LevelThresh= VAD( find( VAD<= LevelThresh));
LevelNoise= sum( VAD_lessorequal_LevelThresh);
if (len> 0)
LevelSig= LevelSig/ len;
else
LevelThresh= -1;
end
%fprintf( 1, 'LevelSig is %f\n', LevelSig);
if (len< Nwindows)
LevelNoise= LevelNoise/( Nwindows- len);
else
LevelNoise= 1;
end
%fprintf( 1, 'LevelNoise is %f\n', LevelNoise);
VAD( find( VAD<= LevelThresh))= -VAD( find( VAD<= LevelThresh));
VAD(1)= -LevelMin;
VAD(Nwindows)= -LevelMin;
start= 0;
finish= 0;
for count= 2: Nwindows
if( (VAD(count) > 0.0) && (VAD(count-1) <= 0.0) )
start = count;
end
if( (VAD(count) <= 0.0) && (VAD(count-1) > 0.0) )
finish = count;
if( (finish - start)<= MINSPEECHLGTH )
VAD( start: finish- 1)= -VAD( start: finish- 1);
end
end
end
%to make sure finish- start is more than 4
if( LevelSig >= (LevelNoise* 1000) )
for count= 2: Nwindows
if( (VAD(count)> 0) && (VAD(count-1)<= 0) )
start= count;
end
if( (VAD(count)<= 0) && (VAD(count-1)> 0) )
finish = count;
g = sum( VAD( start: finish- 1));
if( g< 3.0* LevelThresh* (finish - start) )
VAD( start: finish- 1)= -VAD( start: finish- 1);
end
end
end
end
start = 0;
finish = 0;
for count= 2: Nwindows
if( (VAD(count) > 0.0) && (VAD(count-1) <= 0.0) )
start = count;
if( (finish > 0) && ((start - finish) <= JOINSPEECHLGTH) )
VAD( finish: start- 1)= LevelMin;
end
end
if( (VAD(count) <= 0.0) && (VAD(count-1) > 0.0) )
finish = count;
end
end
start= 0;
for count= 2: Nwindows
if( (VAD(count)> 0) && (VAD(count-1)<= 0) )
start= count;
end
end
if( start== 0 )
VAD= abs(VAD);
VAD(1) = -LevelMin;
VAD(Nwindows) = -LevelMin;
end
count = 4;
while( count< (Nwindows-1) )
if( (VAD(count)> 0) && (VAD(count-2) <= 0) )
VAD(count-2)= VAD(count)* 0.1;
VAD(count-1)= VAD(count)* 0.3;
count= count+ 1;
end
if( (VAD(count)<= 0) && (VAD(count-1)> 0) )
VAD(count)= VAD(count-1)* 0.3;
VAD(count+ 1)= VAD(count-1)* 0.1;
count= count+ 3;
end
count= count+ 1;
end
VAD( find( VAD< 0))= 0;
% fid= fopen( 'mat_vad.txt', 'wt');
% fprintf( fid, '%f\n', VAD);
% fclose( fid);
if( LevelThresh<= 0 )
LevelThresh= LevelMin;
end
logVAD( find( VAD<= LevelThresh))= 0;
VAD_greaterthan_LevelThresh= find( VAD> LevelThresh);
logVAD( VAD_greaterthan_LevelThresh)= log( VAD( ...
VAD_greaterthan_LevelThresh)/ LevelThresh);
function crude_align( ref_logVAD, ref_Nsamples, deg_logVAD, ...
deg_Nsamples, Utt_id)
global Downsample
global Nutterances Largest_uttsize Nsurf_samples Crude_DelayEst
global Crude_DelayConf UttSearch_Start UttSearch_End Utt_DelayEst
global Utt_Delay Utt_DelayConf Utt_Start Utt_End
global MAXNUTTERANCES WHOLE_SIGNAL
global pesq_mos subj_mos cond_nr
if (Utt_id== WHOLE_SIGNAL )
nr = floor( ref_Nsamples/ Downsample);
nd = floor( deg_Nsamples/ Downsample);
startr= 1;
startd= 1;
elseif Utt_id== MAXNUTTERANCES
startr= UttSearch_Start(MAXNUTTERANCES);
startd= startr+ Utt_DelayEst(MAXNUTTERANCES)/ Downsample;
if ( startd< 0 )
startr= 1- Utt_DelayEst(MAXNUTTERANCES)/ Downsample;
startd= 1;
end
nr= UttSearch_End(MAXNUTTERANCES)- startr;
nd= nr;
if( startd+ nd> floor( deg_Nsamples/ Downsample) )
nd= floor( deg_Nsamples/ Downsample)- startd;
end
% fprintf( 'nr,nd is %d,%d\n', nr, nd);
else
startr= UttSearch_Start(Utt_id);
startd= startr+ Crude_DelayEst/ Downsample;
if ( startd< 0 )
startr= 1- Crude_DelayEst/ Downsample;
startd= 1;
end
nr= UttSearch_End(Utt_id)- startr;
nd = nr;
if( startd+ nd> floor( deg_Nsamples/ Downsample)+ 1)
nd = floor( deg_Nsamples/ Downsample)- startd+ 1;
end
end
startr = max(1,startr); % <- KKW
startd = max(1,startd); % <- KKW
max_Y= 0.0;
I_max_Y= nr;
if( (nr> 1) && (nd> 1) )
Y= FFTNXCorr( ref_logVAD, startr, nr, deg_logVAD, startd, nd);
[max_Y, I_max_Y]= max( Y);
if (max_Y<= 0)
max_Y= 0;
I_max_Y= nr;
end
end
% fprintf( 'max_Y, I_max_Y is %f, %d\n', max_Y, I_max_Y);
if( Utt_id== WHOLE_SIGNAL )
Crude_DelayEst= (I_max_Y- nr)* Downsample;
Crude_DelayConf= 0.0;
% fprintf( 1, 'I_max_Y, nr, Crude_DelayEst is %f, %f, %f\n', ...
% I_max_Y, nr, Crude_DelayEst);
elseif( Utt_id == MAXNUTTERANCES )
Utt_Delay(MAXNUTTERANCES)= (I_max_Y- nr)* Downsample+ ...
Utt_DelayEst(MAXNUTTERANCES);
% fprintf( 'startr, startd, nr, nd, I_max, Utt_Delay[%d] is %d, %d, %d, %d, %d, %d\n', ...
% MAXNUTTERANCES, startr, startd, nr, nd, ...
% I_max_Y, Utt_Delay(MAXNUTTERANCES) );
else
% fprintf( 'I_max_Y, nr is %d, %d\n', I_max_Y, nr);
Utt_DelayEst(Utt_id)= (I_max_Y- nr)* Downsample+ ...
Crude_DelayEst;
end
function mod_data= DC_block( data, Nsamples)
global Downsample DATAPADDING_MSECS SEARCHBUFFER
ofs= SEARCHBUFFER* Downsample;
mod_data= data;
%compute dc component, it is a little weird
facc= sum( data( ofs+ 1: Nsamples- ofs))/ Nsamples;
mod_data( ofs+ 1: Nsamples- ofs)= data( ofs+ 1: Nsamples- ofs)- facc;
mod_data( ofs+ 1: ofs+ Downsample)= mod_data( ofs+ 1: ofs+ Downsample).* ...
( 0.5+ (0: Downsample- 1))/ Downsample;
mod_data( Nsamples- ofs: -1: Nsamples- ofs-Downsample+ 1)= ...
mod_data( Nsamples- ofs: -1: Nsamples- ofs-Downsample+ 1).* ...
( 0.5+ (0: Downsample- 1))/ Downsample;
function Y= FFTNXCorr( ref_VAD, startr, nr, deg_VAD, startd, nd)
% this function has other simple implementations, current implementation is
% consistent with the C version
% % one way to do this (in time domain) =====
% % fprintf( 1, 'startr, nr is %d, %d\n', startr, nr);
% x1= ref_VAD( startr: startr+ nr- 1);
% x2= deg_VAD( startd: startd+ nd- 1);
% x1= fliplr( x1);
% Y= conv( x2, x1);
% % done =====
% the other way to do this (in freq domain)===
Nx= 2^ (ceil( log2( max( nr, nd))));
x1= zeros( 1, 2* Nx);
x2= zeros( 1, 2* Nx);
startd=max(1,startd); %<<< PL: Added to avoid index 0
startr=max(1,startr);
x1( 1: nr)= fliplr( ref_VAD( startr: startr+ nr- 1));
x2( 1: nd)= deg_VAD( startd: startd+ nd- 1);
if (nr== 491) && false
fid= fopen( 'mat_debug.txt', 'wt');
fprintf( fid, '%f\n', x1);
fclose( fid);
end
x1_fft= fft( x1, 2* Nx);
x2_fft= fft( x2, 2* Nx);
tmp1= ifft( x1_fft.* x2_fft, 2* Nx);
Ny= nr+ nd- 1;
Y= tmp1( 1: Ny);
% done ===========
function mod_data= fix_power_level( data, data_Nsamples, maxNsamples)
% this function is used for level normalization, i.e., to fix the power
% level of data to a preset number, and return it to mod_data.
global Downsample DATAPADDING_MSECS SEARCHBUFFER Fs
global TARGET_AVG_POWER
TARGET_AVG_POWER= 1e7;
align_filter_dB= [0,-500; 50, -500; 100, -500; 125, -500; 160, -500; 200, -500;
250, -500; 300, -500; 350, 0; 400, 0; 500, 0; 600, 0; 630, 0;
800, 0; 1000, 0; 1250, 0; 1600, 0; 2000, 0; 2500, 0; 3000, 0;
3250, 0; 3500, -500; 4000, -500; 5000, -500; 6300, -500; 8000, -500];
align_filtered= apply_filter( data, data_Nsamples, align_filter_dB);
power_above_300Hz = pow_of (align_filtered, SEARCHBUFFER* Downsample+ 1, ...
data_Nsamples- SEARCHBUFFER* Downsample+ DATAPADDING_MSECS* (Fs/ 1000), ...
maxNsamples- 2* SEARCHBUFFER* Downsample+ DATAPADDING_MSECS* (Fs/ 1000));
global_scale= sqrt( TARGET_AVG_POWER/ power_above_300Hz);
% fprintf( 1, '\tglobal_scale is %f\n', global_scale);
mod_data= data* global_scale;
function id_searchwindows( ref_VAD, ref_Nsamples, deg_VAD, deg_Nsamples);
global MINUTTLENGTH Downsample MINUTTLENGTH SEARCHBUFFER
global Crude_DelayEst Nutterances UttSearch_Start UttSearch_End
Utt_num = 1;
speech_flag = 0;
VAD_length= floor( ref_Nsamples/ Downsample);
del_deg_start= MINUTTLENGTH- Crude_DelayEst/ Downsample;
del_deg_end= floor((deg_Nsamples- Crude_DelayEst)/ Downsample)-...
MINUTTLENGTH;
for count= 1: VAD_length
VAD_value= ref_VAD(count);
if( (VAD_value> 0) && (speech_flag== 0) )
speech_flag= 1;
this_start= count;
UttSearch_Start(Utt_num)= count- SEARCHBUFFER;
% if( UttSearch_Start(Utt_num)< 0 )
% UttSearch_Start(Utt_num)= 0;
% end
if( UttSearch_Start(Utt_num)< 1 )
UttSearch_Start(Utt_num)= 1;
end
end
if( ((VAD_value== 0) || (count == (VAD_length-1))) && ...
(speech_flag == 1) )
speech_flag = 0;
UttSearch_End(Utt_num) = count + SEARCHBUFFER;
% if( UttSearch_End(Utt_num) > VAD_length - 1 )
% UttSearch_End(Utt_num) = VAD_length -1;
% end
if( UttSearch_End(Utt_num) > VAD_length )
UttSearch_End(Utt_num) = VAD_length;
end
if( ((count - this_start) >= MINUTTLENGTH) &&...
(this_start < del_deg_end) &&...
(count > del_deg_start) )
Utt_num= Utt_num + 1;
end
end
end
Utt_num= Utt_num- 1;
Nutterances = Utt_num;
% fprintf( 1, 'Nutterances is %d\n', Nutterances);
% fid= fopen( 'mat_utt.txt', 'wt');
% fprintf( fid, '%d\n', UttSearch_Start( 1: Nutterances));
% fprintf( fid, '\n');
% fprintf( fid, '%d\n', UttSearch_End( 1: Nutterances));
% fclose(fid);
function id_utterances( ref_Nsamples, ref_VAD, deg_Nsamples)
global Largest_uttsize MINUTTLENGTH MINUTTLENGTH Crude_DelayEst
global Downsample SEARCHBUFFER Nutterances Utt_Start
global Utt_End Utt_Delay
Utt_num = 1;
speech_flag = 0;
VAD_length = floor( ref_Nsamples / Downsample);
% fprintf( 1, 'VAD_length is %d\n', VAD_length);
del_deg_start = MINUTTLENGTH - Crude_DelayEst / Downsample;
del_deg_end = floor((deg_Nsamples- Crude_DelayEst)/ Downsample) ...
- MINUTTLENGTH;
for count = 1: VAD_length
VAD_value = ref_VAD(count);
if( (VAD_value > 0.0) && (speech_flag == 0) )
speech_flag = 1;
this_start = count;
Utt_Start (Utt_num) = count;
end
if( ((VAD_value == 0) || (count == VAD_length)) && ...
(speech_flag == 1) )
speech_flag = 0;
Utt_End (Utt_num) = count;
if( ((count - this_start) >= MINUTTLENGTH) && ...
(this_start < del_deg_end) && ...
(count > del_deg_start) )
Utt_num = Utt_num + 1;
end
end
end
Utt_Start(1) = SEARCHBUFFER+ 1;
Nutterances=max(1,Nutterances); %<<< PL: Added to avoid index 0
Utt_End(Nutterances) = VAD_length - SEARCHBUFFER+ 1;
for Utt_num = 2: Nutterances
this_start = Utt_Start(Utt_num)- 1;
last_end = Utt_End(Utt_num - 1)- 1;
count = floor( (this_start + last_end) / 2);
Utt_Start(Utt_num) = count+ 1;
Utt_End(Utt_num - 1) = count+ 1;
end
this_start = (Utt_Start(1)- 1) * Downsample + Utt_Delay(1);
if( this_start < (SEARCHBUFFER * Downsample) )
count = SEARCHBUFFER + floor( ...
(Downsample - 1 - Utt_Delay(1)) / Downsample);
Utt_Start(1) = count+ 1;
end
last_end = (Utt_End(Nutterances)- 1) * Downsample + 1 + ...
Utt_Delay(Nutterances);
% fprintf( 'Utt_End(%d) is %d\n', Nutterances, Utt_End(Nutterances));
% fprintf( 'last_end is %d\n', last_end);
% fprintf( 'Utt_Delay(%d) is %d\n', Nutterances, Utt_Delay(Nutterances));
if( last_end > (deg_Nsamples - SEARCHBUFFER * Downsample+ 1) )
count = floor( (deg_Nsamples - Utt_Delay(Nutterances)) / Downsample) ...
- SEARCHBUFFER;
Utt_End(Nutterances) = count+ 1;
end
for Utt_num = 2: Nutterances
this_start = (Utt_Start(Utt_num)- 1) * Downsample + Utt_Delay(Utt_num);
last_end = (Utt_End(Utt_num - 1)- 1) * Downsample + Utt_Delay(Utt_num - 1);
if( this_start < last_end )
count = floor( (this_start + last_end) / 2);
this_start = floor( (Downsample- 1+ count- Utt_Delay(Utt_num))...
/ Downsample);
last_end = floor( (count - Utt_Delay(Utt_num - 1))...
/ Downsample);
Utt_Start(Utt_num) = this_start+ 1;
Utt_End(Utt_num- 1) = last_end+ 1;
end
end
Largest_uttsize= max( Utt_End- Utt_Start);
function [mod_ref_data, mod_deg_data]= input_filter( ref_data, ref_Nsamples, ...
deg_data, deg_Nsamples)
mod_ref_data= DC_block( ref_data, ref_Nsamples);
mod_deg_data= DC_block( deg_data, deg_Nsamples);
mod_ref_data= apply_filters( mod_ref_data, ref_Nsamples);
mod_deg_data= apply_filters( mod_deg_data, deg_Nsamples);
function pesq_mos= pesq_psychoacoustic_model (ref_data, ref_Nsamples, deg_data, ...
deg_Nsamples )
global CALIBRATE Nfmax Nb Sl Sp
global nr_of_hz_bands_per_bark_band centre_of_band_bark
global width_of_band_hz centre_of_band_hz width_of_band_bark
global pow_dens_correction_factor abs_thresh_power
global Downsample SEARCHBUFFER DATAPADDING_MSECS Fs Nutterances
global Utt_Start Utt_End Utt_Delay NUMBER_OF_PSQM_FRAMES_PER_SYLLABE
global Fs Plot_Frame
% Plot_Frame= 75; % this is the frame whose spectrum will be plotted
Plot_Frame= -1;
FALSE= 0;
TRUE= 1;
NUMBER_OF_PSQM_FRAMES_PER_SYLLABE= 20;
maxNsamples = max (ref_Nsamples, deg_Nsamples);
Nf = Downsample * 8;
MAX_NUMBER_OF_BAD_INTERVALS = 1000;
start_frame_of_bad_interval= zeros( 1, MAX_NUMBER_OF_BAD_INTERVALS);
stop_frame_of_bad_interval= zeros( 1, MAX_NUMBER_OF_BAD_INTERVALS);
start_sample_of_bad_interval= zeros( 1, MAX_NUMBER_OF_BAD_INTERVALS);
stop_sample_of_bad_interval= zeros( 1, MAX_NUMBER_OF_BAD_INTERVALS);
number_of_samples_in_bad_interval= zeros( 1, MAX_NUMBER_OF_BAD_INTERVALS);
delay_in_samples_in_bad_interval= zeros( 1, MAX_NUMBER_OF_BAD_INTERVALS);
number_of_bad_intervals= 0;
there_is_a_bad_frame= FALSE;
Whanning= hann( Nf, 'periodic');
Whanning= Whanning';
D_POW_F = 2;
D_POW_S = 6;
D_POW_T = 2;
A_POW_F = 1;
A_POW_S = 6;
A_POW_T = 2;
D_WEIGHT= 0.1;
A_WEIGHT= 0.0309;
CRITERIUM_FOR_SILENCE_OF_5_SAMPLES = 500;
samples_to_skip_at_start = 0;
sum_of_5_samples= 0;
while ((sum_of_5_samples< CRITERIUM_FOR_SILENCE_OF_5_SAMPLES) ...
&& (samples_to_skip_at_start < maxNsamples / 2))
sum_of_5_samples= sum( abs( ref_data( samples_to_skip_at_start...
+ SEARCHBUFFER * Downsample + 1: samples_to_skip_at_start...
+ SEARCHBUFFER * Downsample + 5)));
if (sum_of_5_samples< CRITERIUM_FOR_SILENCE_OF_5_SAMPLES)
samples_to_skip_at_start = samples_to_skip_at_start+ 1;
end
end
% fprintf( 'samples_to_skip_at_start is %d\n', samples_to_skip_at_start);
samples_to_skip_at_end = 0;
sum_of_5_samples= 0;
while ((sum_of_5_samples< CRITERIUM_FOR_SILENCE_OF_5_SAMPLES) ...
&& (samples_to_skip_at_end < maxNsamples / 2))
sum_of_5_samples= sum( abs( ref_data( maxNsamples - ...
SEARCHBUFFER* Downsample + DATAPADDING_MSECS* (Fs/ 1000) ...
- samples_to_skip_at_end - 4: maxNsamples - ...
SEARCHBUFFER* Downsample + DATAPADDING_MSECS* (Fs/ 1000) ...
- samples_to_skip_at_end)));
if (sum_of_5_samples< CRITERIUM_FOR_SILENCE_OF_5_SAMPLES)
samples_to_skip_at_end = samples_to_skip_at_end+ 1;
end
end
% fprintf( 'samples_to_skip_at_end is %d\n', samples_to_skip_at_end);
start_frame = floor( samples_to_skip_at_start/ (Nf/ 2));
stop_frame = floor( (maxNsamples- 2* SEARCHBUFFER* Downsample ...
+ DATAPADDING_MSECS* (Fs/ 1000)- samples_to_skip_at_end) ...
/ (Nf/ 2))- 1;
% number of frames in speech data plus DATAPADDING_MSECS
% fprintf( 'start/end frame is %d/%d\n', start_frame, stop_frame);
D_disturbance= zeros( stop_frame+ 1, Nb);
DA_disturbance= zeros( stop_frame+ 1, Nb);
power_ref = pow_of (ref_data, SEARCHBUFFER* Downsample, ...
maxNsamples- SEARCHBUFFER* Downsample+ DATAPADDING_MSECS* (Fs/ 1000),...
maxNsamples- 2* SEARCHBUFFER* Downsample+ DATAPADDING_MSECS* (Fs/ 1000));
power_deg = pow_of (deg_data, SEARCHBUFFER * Downsample, ...
maxNsamples- SEARCHBUFFER* Downsample+ DATAPADDING_MSECS* (Fs/ 1000),...
maxNsamples- 2* SEARCHBUFFER* Downsample+ DATAPADDING_MSECS* (Fs/ 1000));
% fprintf( 'ref/deg power is %f/%f\n', power_ref, power_deg);
hz_spectrum_ref = zeros( 1, Nf/ 2);
hz_spectrum_deg = zeros( 1, Nf/ 2);
frame_is_bad = zeros( 1, stop_frame + 1);
smeared_frame_is_bad = zeros( 1, stop_frame + 1);
silent = zeros( 1, stop_frame + 1);
pitch_pow_dens_ref = zeros( stop_frame + 1, Nb);
pitch_pow_dens_deg = zeros( stop_frame + 1, Nb);
frame_was_skipped = zeros( 1, stop_frame + 1);
frame_disturbance = zeros( 1, stop_frame + 1);
frame_disturbance_asym_add = zeros( 1, stop_frame + 1);
avg_pitch_pow_dens_ref = zeros( 1, Nb);
avg_pitch_pow_dens_deg = zeros( 1, Nb);
loudness_dens_ref = zeros( 1, Nb);
loudness_dens_deg = zeros( 1, Nb);
deadzone = zeros( 1, Nb);
disturbance_dens = zeros( 1, Nb);
disturbance_dens_asym_add = zeros( 1, Nb);
time_weight = zeros( 1, stop_frame + 1);
total_power_ref = zeros( 1, stop_frame + 1);
% fid= fopen( 'tmp_mat.txt', 'wt');
for frame = 0: stop_frame
start_sample_ref = 1+ SEARCHBUFFER * Downsample + frame* (Nf/ 2);
hz_spectrum_ref= short_term_fft (Nf, ref_data, Whanning, ...
start_sample_ref);
utt = Nutterances;
while ((utt >= 1) && ((Utt_Start(utt)- 1)* Downsample+ 1 ...
> start_sample_ref))
utt= utt - 1;
end
if (utt >= 1)
delay = Utt_Delay(utt);
else
delay = Utt_Delay(1);
end
start_sample_deg = start_sample_ref + delay;
if ((start_sample_deg > 0) && (start_sample_deg + Nf- 1 < ...
maxNsamples+ DATAPADDING_MSECS* (Fs/ 1000)))
hz_spectrum_deg= short_term_fft (Nf, deg_data, Whanning, ...
start_sample_deg);
else
hz_spectrum_deg( 1: Nf/ 2)= 0;
end
pitch_pow_dens_ref( frame+ 1, :)= freq_warping (...
hz_spectrum_ref, Nb, frame);
%peak = maximum_of (pitch_pow_dens_ref, 0, Nb);
pitch_pow_dens_deg( frame+ 1, :)= freq_warping (...
hz_spectrum_deg, Nb, frame);
total_audible_pow_ref = total_audible (frame, pitch_pow_dens_ref, 1E2);
total_audible_pow_deg = total_audible (frame, pitch_pow_dens_deg, 1E2);
silent(frame+ 1) = (total_audible_pow_ref < 1E7);
% fprintf( fid, 'total_audible_pow_ref[%d] is %f\n', frame, ...
% total_audible_pow_ref);
if (frame== Plot_Frame)
figure;
freq_resolution= Fs/ Nf;
axis_freq= ( 0: Nf/2- 1)* freq_resolution;
subplot( 1, 2, 1);
plot( axis_freq, 10* log10( hz_spectrum_ref+ eps));
axis( [0 Fs/2 -10 120]); %xlabel( 'Hz'); ylabel( 'Db');
title( 'reference signal power spectrum');
subplot( 1, 2, 2);
plot( axis_freq, 10* log10( hz_spectrum_deg+ eps));
axis( [0 Fs/2 -10 120]); %xlabel( 'Hz'); ylabel( 'Db');
title( 'degraded signal power spectrum');
figure;
subplot( 1, 2, 1);
plot( centre_of_band_hz, 10* log10( eps+ ...
pitch_pow_dens_ref( frame+ 1, :)));
axis( [0 Fs/2 0 95]); %xlabel( 'Hz'); ylabel( 'Db');
title( 'reference signal bark spectrum');
subplot( 1, 2, 2);
plot( centre_of_band_hz, 10* log10( eps+ ...
pitch_pow_dens_deg( frame+ 1, :)));
axis( [0 Fs/2 0 95]); %xlabel( 'Hz'); ylabel( 'Db');
title( 'degraded signal bark spectrum');
end
end
% fclose( fid);
avg_pitch_pow_dens_ref= time_avg_audible_of (stop_frame + 1, ...
silent, pitch_pow_dens_ref, floor((maxNsamples- 2* SEARCHBUFFER* ...
Downsample+ DATAPADDING_MSECS* (Fs/ 1000))/ (Nf / 2))- 1);
avg_pitch_pow_dens_deg= time_avg_audible_of (stop_frame + 1, ...
silent, pitch_pow_dens_deg, floor((maxNsamples- 2* SEARCHBUFFER* ...
Downsample+ DATAPADDING_MSECS* (Fs/ 1000))/ (Nf/ 2))- 1);
% fid= fopen( 'tmp_mat.txt', 'wt');
% fprintf( fid, '%f\n', avg_pitch_pow_dens_deg);
% fclose( fid);