view 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
line wrap: on
line source
function [] = readFormantValueFromFile( statsFileID, metricFileName, metricName, vuv )

DEBUG=0;

FileID = fopen( metricFileName );

if( FileID <= 0 ) %does the file exist?
    % if not
    
    message = ['WARNING: MISSING ' metricName ' FILE'];
    disp( message );
    fprintf( statsFileID, '\t \s missing', metricName );
   
 else
    % file format is not straight forward
    noOfValues = 0;
    formants = [];
    while( ~(feof(FileID)) )

        % search for numberOfFormants
        thestr = fgetl(FileID);

        if( strfind( thestr , 'numberOfFormants' ) > 0 )
            noOfValues = noOfValues + 1;
            %numberOfFormants found
            pos = find( thestr == '=' );
            numberOfFormants = str2num(thestr(pos+2:end));
            formants( noOfValues, 1 ) = numberOfFormants;
            % discard the 'formant []' line
            thestr = fgetl(FileID);
            % now read the formant positions
            for (i=0:numberOfFormants-1)
                thestr = fgetl(FileID);
                pos = find( thestr == '=' );
                formants( noOfValues, i+2 ) =  str2num(thestr( pos+2 : end ));
            end

            % discard the 'bandwidth []' line
            thestr = fgetl(FileID);
            % now read the formant bandwidths
            for (i=0:numberOfFormants-1)
                thestr = fgetl(FileID);
                pos = find( thestr == '=' );
                formants( noOfValues, i+2+numberOfFormants ) =  str2num(thestr( pos+2 : end ));
            end

        end
    end
    fclose(FileID);    


    if(DEBUG)
        timeShift = length(formants) / length(vuv);
        subplot(211); hold off;
        subplot(212); hold off;
        for( idx = 1: length( formants )/10 )
            frameIdx = floor( idx / timeShift ) + 1;
            if( frameIdx <= length(vuv) )
                if( vuv( frameIdx ) == 0 )
                    % frame is silent
                    subplot(211); plot( idx, formants( idx,2 ), 'w.' ); hold on;
                    subplot(211); plot( idx, formants( idx,3 ), 'w.' );
                    subplot(211); plot( idx, formants( idx,4 ), 'w.' );
                    % plot bandwidths
                    subplot(212); plot( idx, formants( idx,5 ), 'w.' ); hold on;
                    subplot(212); plot( idx, formants( idx,6 ), 'w.' );
                    subplot(212); plot( idx, formants( idx,7 ), 'w.' );
                elseif( vuv( frameIdx ) == 1 )
                    % frame is voiced
                    subplot(211); plot( idx, formants( idx,2 ), 'y.' ); hold on;
                    subplot(211); plot( idx, formants( idx,3 ), 'c.' );
                    subplot(211); plot( idx, formants( idx,4 ), 'g.' );
                    % plot bandwidths
                    subplot(212); plot( idx, formants( idx,5 ), 'y.' ); hold on;
                    subplot(212); plot( idx, formants( idx,6 ), 'c.' );
                    subplot(212); plot( idx, formants( idx,7 ), 'g.' );
                else
                    %frame is unvoiced
                    subplot(211); plot( idx, formants( idx,2 ), 'r.' ); hold on;
                    subplot(211); plot( idx, formants( idx,3 ), 'r.' );
                    subplot(211); plot( idx, formants( idx,4 ), 'r.' );
                    % plot bandwidths
                    subplot(212); plot( idx, formants( idx,5 ), 'r.' ); hold on;
                    subplot(212); plot( idx, formants( idx,6 ), 'r.' );
                    subplot(212); plot( idx, formants( idx,7 ), 'r.' );
                end
            else
                frameIdx
            end
        end
    end

    [ noOfSamples formantDataSize ] = size( formants );
    maxNoOfFormants = (formantDataSize - 1)/2; % the array contains the number of formants, then the position of each formant, then the bandwidth of each formant.
    voicedFormant = [];
    voicedBandwidth = [];
    % find the basic metric set for the voiced frames only
    validSamples = min( noOfSamples, length(vuv) );
    for( idx = 1: validSamples )
        if( vuv( idx ) == 1 )
            % frame is voiced
            formantsThisFrame = formants(idx,1);
            theFormants = formants( idx, 2 : formantsThisFrame+1 );
            theBandwidths = formants( idx, formantsThisFrame+2 : (formantsThisFrame*2)+1 );
            % we want to ignore any results that contain less than the
            % max number of formants as then one of the formants is set to
            % zero and the detection is not reliable
            % DON'T THINK THIS IS STILL VALID
            % DO WE WANT TO DISCARD FORMANT SETS THAT CONTAIN '0' FORMANT
            % POSITIONS FOR VOICED SEGMENTS?
            if( formantsThisFrame == maxNoOfFormants )
                voicedFormant = [ voicedFormant ; theFormants ];
                voicedBandwidth = [ voicedBandwidth; theBandwidths ];
            else
                % pad with zeros
                diff = maxNoOfFormants - formantsThisFrame;
                theFormants = [ theFormants zeros( 1, diff )];
                voicedFormant = [ voicedFormant ; theFormants ];
                theBandwidths = [ theBandwidths zeros( 1, diff )];
                voicedBandwidth = [ voicedBandwidth; theBandwidths ];
            end

        end
    end

    % get the basic formant metrics
    formantFrequencyMeans = [];
    formantGradientMean = [];
    formantBandwidthMean = [];

    fprintf( statsFileID, '\t %s %d ', 'maxNoOfFormants', maxNoOfFormants );
    
    for( formantIdx = 1:maxNoOfFormants )
        % formant position
        [ metrics ] = basicMetricSet( voicedFormant(:,formantIdx), statsFileID );
        formantFrequencyMeans = [ formantFrequencyMeans metrics(1) ];
        % formant gradient
        formantGradient = gradient( voicedFormant(:,formantIdx) );
        [ metrics ] = basicMetricSet( formantGradient, statsFileID );
        formantGradientMean = [formantGradientMean metrics(1)];
        % formant second derivative
        formantGradient = gradient( formantGradient );
        [ metrics ] = basicMetricSet( formantGradient, statsFileID );
        % formant bandwidth
        [ metrics ] = basicMetricSet( voicedBandwidth(:,formantIdx), statsFileID );
        formantBandwidthMean = [formantBandwidthMean metrics(1)];
        % formant bandwidth gradient
        formantGradient = gradient( voicedBandwidth(:,formantIdx));
        [ metrics ] = basicMetricSet( formantGradient, statsFileID );
        % formant bandwidth second derivative
        formantGradient = gradient( formantGradient );
        [ metrics ] = basicMetricSet( formantGradient, statsFileID );
    end

    % calculate the basic metrics set for the ...
    % ... mean of all formant frequencies
    meanMetrics = basicMetricSet( formantFrequencyMeans, statsFileID );
    % ... mean of all formant frequency gradients
    meanMetrics = basicMetricSet( formantGradientMean, statsFileID );
    % ... mean of all formant bandwidths
    meanMetrics = basicMetricSet( formantBandwidthMean, statsFileID );
end