Dawn@4: function [HNR] = detect_HNR( sampleWavFileName, OVERWRITE ) Dawn@4: Dawn@4: sampleFileName = sampleWavFileName( 1 : length( sampleWavFileName ) - 4 ); Dawn@4: [x, fs, frameLength, noOfFrames] = openFile( sampleWavFileName ); Dawn@4: Dawn@4: %---------------- GET THE SILENT FRAME VALUES ------------------- Dawn@4: Dawn@4: % only wish to consider pitch values from voiced frames. Dawn@4: % silent and unvoiced frames will produce pitch values that Dawn@4: % are random and therefore will bias our results Dawn@4: Dawn@4: segmentFrames = detect_Silence( sampleWavFileName, 0 ); Dawn@4: [ silentFrames ] = getSilentDataArray( segmentFrames, noOfFrames ); Dawn@4: Dawn@4: Dawn@4: %---------------- GET THE PITCH VALUES -------------------------- Dawn@4: Dawn@4: [ pitch ] = detect_pitch( sampleWavFileName, 0 ); Dawn@4: Dawn@4: % calculate the HNR Dawn@4: Dawn@4: % open original calculation Dawn@4: fileName = [ sampleFileName '_HNR.txt']; Dawn@4: % read pitch metrics from file Dawn@4: fileID = fopen( fileName ); Dawn@4: Dawn@4: if(( fileID <= 0 ) || ( OVERWRITE )) %does the file exist? Dawn@4: % no Dawn@4: disp('WARNING: MISSING HNR FILE'); Dawn@4: %calculate it Dawn@4: % [x, fs, frameLength, noOfFrames] = openFile( [ sampleFileName '.wav' ] ); Dawn@4: y = buffer(x,frameLength); Dawn@4: harmonicToNoiseRatio = calculate_HNR( y, fs, noOfFrames, silentFrames, pitch ); Dawn@4: Dawn@4: fileID = fopen( fileName, 'w'); Dawn@4: for i = 1 : (noOfFrames-1) Dawn@4: fprintf(fileID, '%d %s \n' , i, num2str( harmonicToNoiseRatio(i) )); Dawn@4: end Dawn@4: fclose( fileID ); Dawn@4: fileID = fopen( fileName ); Dawn@4: end Dawn@4: Dawn@4: Dawn@4: HNR = fscanf( fileID, '%f', inf ); Dawn@4: HNR = HNR(2:2:end); Dawn@4: fclose(fileID);