comparison Code/General/readFormantValueFromFile.m @ 4:92ca03a8fa99 tip

Update to ICASSP 2013 benchmark
author Dawn Black
date Wed, 13 Feb 2013 11:02:39 +0000
parents
children
comparison
equal deleted inserted replaced
3:e1cfa7765647 4:92ca03a8fa99
1 function [] = readFormantValueFromFile( statsFileID, metricFileName, metricName, vuv )
2
3 DEBUG=0;
4
5 FileID = fopen( metricFileName );
6
7 if( FileID <= 0 ) %does the file exist?
8 % if not
9
10 message = ['WARNING: MISSING ' metricName ' FILE'];
11 disp( message );
12 fprintf( statsFileID, '\t \s missing', metricName );
13
14 else
15 % file format is not straight forward
16 noOfValues = 0;
17 formants = [];
18 while( ~(feof(FileID)) )
19
20 % search for numberOfFormants
21 thestr = fgetl(FileID);
22
23 if( strfind( thestr , 'numberOfFormants' ) > 0 )
24 noOfValues = noOfValues + 1;
25 %numberOfFormants found
26 pos = find( thestr == '=' );
27 numberOfFormants = str2num(thestr(pos+2:end));
28 formants( noOfValues, 1 ) = numberOfFormants;
29 % discard the 'formant []' line
30 thestr = fgetl(FileID);
31 % now read the formant positions
32 for (i=0:numberOfFormants-1)
33 thestr = fgetl(FileID);
34 pos = find( thestr == '=' );
35 formants( noOfValues, i+2 ) = str2num(thestr( pos+2 : end ));
36 end
37
38 % discard the 'bandwidth []' line
39 thestr = fgetl(FileID);
40 % now read the formant bandwidths
41 for (i=0:numberOfFormants-1)
42 thestr = fgetl(FileID);
43 pos = find( thestr == '=' );
44 formants( noOfValues, i+2+numberOfFormants ) = str2num(thestr( pos+2 : end ));
45 end
46
47 end
48 end
49 fclose(FileID);
50
51
52 if(DEBUG)
53 timeShift = length(formants) / length(vuv);
54 subplot(211); hold off;
55 subplot(212); hold off;
56 for( idx = 1: length( formants )/10 )
57 frameIdx = floor( idx / timeShift ) + 1;
58 if( frameIdx <= length(vuv) )
59 if( vuv( frameIdx ) == 0 )
60 % frame is silent
61 subplot(211); plot( idx, formants( idx,2 ), 'w.' ); hold on;
62 subplot(211); plot( idx, formants( idx,3 ), 'w.' );
63 subplot(211); plot( idx, formants( idx,4 ), 'w.' );
64 % plot bandwidths
65 subplot(212); plot( idx, formants( idx,5 ), 'w.' ); hold on;
66 subplot(212); plot( idx, formants( idx,6 ), 'w.' );
67 subplot(212); plot( idx, formants( idx,7 ), 'w.' );
68 elseif( vuv( frameIdx ) == 1 )
69 % frame is voiced
70 subplot(211); plot( idx, formants( idx,2 ), 'y.' ); hold on;
71 subplot(211); plot( idx, formants( idx,3 ), 'c.' );
72 subplot(211); plot( idx, formants( idx,4 ), 'g.' );
73 % plot bandwidths
74 subplot(212); plot( idx, formants( idx,5 ), 'y.' ); hold on;
75 subplot(212); plot( idx, formants( idx,6 ), 'c.' );
76 subplot(212); plot( idx, formants( idx,7 ), 'g.' );
77 else
78 %frame is unvoiced
79 subplot(211); plot( idx, formants( idx,2 ), 'r.' ); hold on;
80 subplot(211); plot( idx, formants( idx,3 ), 'r.' );
81 subplot(211); plot( idx, formants( idx,4 ), 'r.' );
82 % plot bandwidths
83 subplot(212); plot( idx, formants( idx,5 ), 'r.' ); hold on;
84 subplot(212); plot( idx, formants( idx,6 ), 'r.' );
85 subplot(212); plot( idx, formants( idx,7 ), 'r.' );
86 end
87 else
88 frameIdx
89 end
90 end
91 end
92
93 [ noOfSamples formantDataSize ] = size( formants );
94 maxNoOfFormants = (formantDataSize - 1)/2; % the array contains the number of formants, then the position of each formant, then the bandwidth of each formant.
95 voicedFormant = [];
96 voicedBandwidth = [];
97 % find the basic metric set for the voiced frames only
98 validSamples = min( noOfSamples, length(vuv) );
99 for( idx = 1: validSamples )
100 if( vuv( idx ) == 1 )
101 % frame is voiced
102 formantsThisFrame = formants(idx,1);
103 theFormants = formants( idx, 2 : formantsThisFrame+1 );
104 theBandwidths = formants( idx, formantsThisFrame+2 : (formantsThisFrame*2)+1 );
105 % we want to ignore any results that contain less than the
106 % max number of formants as then one of the formants is set to
107 % zero and the detection is not reliable
108 % DON'T THINK THIS IS STILL VALID
109 % DO WE WANT TO DISCARD FORMANT SETS THAT CONTAIN '0' FORMANT
110 % POSITIONS FOR VOICED SEGMENTS?
111 if( formantsThisFrame == maxNoOfFormants )
112 voicedFormant = [ voicedFormant ; theFormants ];
113 voicedBandwidth = [ voicedBandwidth; theBandwidths ];
114 else
115 % pad with zeros
116 diff = maxNoOfFormants - formantsThisFrame;
117 theFormants = [ theFormants zeros( 1, diff )];
118 voicedFormant = [ voicedFormant ; theFormants ];
119 theBandwidths = [ theBandwidths zeros( 1, diff )];
120 voicedBandwidth = [ voicedBandwidth; theBandwidths ];
121 end
122
123 end
124 end
125
126 % get the basic formant metrics
127 formantFrequencyMeans = [];
128 formantGradientMean = [];
129 formantBandwidthMean = [];
130
131 fprintf( statsFileID, '\t %s %d ', 'maxNoOfFormants', maxNoOfFormants );
132
133 for( formantIdx = 1:maxNoOfFormants )
134 % formant position
135 [ metrics ] = basicMetricSet( voicedFormant(:,formantIdx), statsFileID );
136 formantFrequencyMeans = [ formantFrequencyMeans metrics(1) ];
137 % formant gradient
138 formantGradient = gradient( voicedFormant(:,formantIdx) );
139 [ metrics ] = basicMetricSet( formantGradient, statsFileID );
140 formantGradientMean = [formantGradientMean metrics(1)];
141 % formant second derivative
142 formantGradient = gradient( formantGradient );
143 [ metrics ] = basicMetricSet( formantGradient, statsFileID );
144 % formant bandwidth
145 [ metrics ] = basicMetricSet( voicedBandwidth(:,formantIdx), statsFileID );
146 formantBandwidthMean = [formantBandwidthMean metrics(1)];
147 % formant bandwidth gradient
148 formantGradient = gradient( voicedBandwidth(:,formantIdx));
149 [ metrics ] = basicMetricSet( formantGradient, statsFileID );
150 % formant bandwidth second derivative
151 formantGradient = gradient( formantGradient );
152 [ metrics ] = basicMetricSet( formantGradient, statsFileID );
153 end
154
155 % calculate the basic metrics set for the ...
156 % ... mean of all formant frequencies
157 meanMetrics = basicMetricSet( formantFrequencyMeans, statsFileID );
158 % ... mean of all formant frequency gradients
159 meanMetrics = basicMetricSet( formantGradientMean, statsFileID );
160 % ... mean of all formant bandwidths
161 meanMetrics = basicMetricSet( formantBandwidthMean, statsFileID );
162 end
163
164
165