Mercurial > hg > emotion-detection-top-level
diff Code/Descriptors/yin/calculate_pitchYIN.m @ 0:ea0c737c6323
first commit
author | Dawn Black <dawn.black@eecs.qmul.ac.uk> |
---|---|
date | Thu, 26 Jul 2012 14:46:25 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Code/Descriptors/yin/calculate_pitchYIN.m Thu Jul 26 14:46:25 2012 +0100 @@ -0,0 +1,51 @@ +function [framePrd] = calculate_pitchYIN( sampleFileName, x, fs, frameLength, noOfFrames ) + +% sampleFileName = sampleWavFileName( 1 : length( sampleWavFileName ) - 4 ); +% pitchFileName = [ sampleFileName '_YIN_pitch.txt']; +% pitchFileID = fopen( pitchFileName, 'w'); + +[periodInSamples, fileinfo] = yin ([ '../' sampleFileName '.wav']); + +% YIN produces one pitch estimate for every 32 samples. For our purposes +% this must be scaled to one sample every frame (approx 10ms but also a +% power of two). + + frameLength = 2^(ceil(log2(fileinfo.sr/100))); + noOfFrames = floor(fileinfo.nsamples / frameLength); %skip the final samples if necessary + +yinPrdSamplesPerFrame = length(periodInSamples) / noOfFrames; + + +start = 1; +stop = length(periodInSamples); +framePrd = []; + +for i=1:noOfFrames + % find the first and last periodInSamples frames to be averaged for + % every master frame of length frameLength + start = floor((i-1) * (length(periodInSamples) / noOfFrames)) + 1; + stop = ceil(i * (length(periodInSamples) / noOfFrames)); + if( stop <= length(periodInSamples) ) + % exclude NaN frames + prdChunk = periodInSamples( start : stop ); + goodPrdIdx = find( isnan( prdChunk )==0 ); + if( length( goodPrdIdx ) > 0 ) + framePrd(i,2) = mean( prdChunk(goodPrdIdx)); + else + framePrd(i,2) = NaN; + end + framePrd(i,1) = i; % record the frame number + % might need to know this in case of frame mismatch + prdFrames(i) = stop - start; + % write to file +% fprintf(pitchFileID, '%d %f \n', framePrd(i,1), framePrd(i,2) ); + else + break; + end +end + +% plot(periodInSamples,'x'); hold on; +% xaxisdiv=((length(periodInSamples)/length(framePrd))/2):(length(periodInSamples)/length(framePrd)):length(periodInSamples) + ((length(periodInSamples)/length(framePrd))/2); +% plot(xaxisdiv(1:length(framePrd)),framePrd(:,2),'rx'); + +% fclose( pitchFileID ); \ No newline at end of file