dawn@0: function [framePrd] = calculate_pitchYIN( sampleFileName, x, fs, frameLength, noOfFrames ) dawn@0: dawn@0: % sampleFileName = sampleWavFileName( 1 : length( sampleWavFileName ) - 4 ); dawn@0: % pitchFileName = [ sampleFileName '_YIN_pitch.txt']; dawn@0: % pitchFileID = fopen( pitchFileName, 'w'); dawn@0: dawn@0: [periodInSamples, fileinfo] = yin ([ '../' sampleFileName '.wav']); dawn@0: dawn@0: % YIN produces one pitch estimate for every 32 samples. For our purposes dawn@0: % this must be scaled to one sample every frame (approx 10ms but also a dawn@0: % power of two). dawn@0: dawn@0: frameLength = 2^(ceil(log2(fileinfo.sr/100))); dawn@0: noOfFrames = floor(fileinfo.nsamples / frameLength); %skip the final samples if necessary dawn@0: dawn@0: yinPrdSamplesPerFrame = length(periodInSamples) / noOfFrames; dawn@0: dawn@0: dawn@0: start = 1; dawn@0: stop = length(periodInSamples); dawn@0: framePrd = []; dawn@0: dawn@0: for i=1:noOfFrames dawn@0: % find the first and last periodInSamples frames to be averaged for dawn@0: % every master frame of length frameLength dawn@0: start = floor((i-1) * (length(periodInSamples) / noOfFrames)) + 1; dawn@0: stop = ceil(i * (length(periodInSamples) / noOfFrames)); dawn@0: if( stop <= length(periodInSamples) ) dawn@0: % exclude NaN frames dawn@0: prdChunk = periodInSamples( start : stop ); dawn@0: goodPrdIdx = find( isnan( prdChunk )==0 ); dawn@0: if( length( goodPrdIdx ) > 0 ) dawn@0: framePrd(i,2) = mean( prdChunk(goodPrdIdx)); dawn@0: else dawn@0: framePrd(i,2) = NaN; dawn@0: end dawn@0: framePrd(i,1) = i; % record the frame number dawn@0: % might need to know this in case of frame mismatch dawn@0: prdFrames(i) = stop - start; dawn@0: % write to file dawn@0: % fprintf(pitchFileID, '%d %f \n', framePrd(i,1), framePrd(i,2) ); dawn@0: else dawn@0: break; dawn@0: end dawn@0: end dawn@0: dawn@0: % plot(periodInSamples,'x'); hold on; dawn@0: % xaxisdiv=((length(periodInSamples)/length(framePrd))/2):(length(periodInSamples)/length(framePrd)):length(periodInSamples) + ((length(periodInSamples)/length(framePrd))/2); dawn@0: % plot(xaxisdiv(1:length(framePrd)),framePrd(:,2),'rx'); dawn@0: dawn@0: % fclose( pitchFileID );