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