dawn@0
|
1 function [framePrd] = calculate_pitchYIN( sampleFileName, x, fs, frameLength, noOfFrames )
|
dawn@0
|
2
|
dawn@0
|
3 % sampleFileName = sampleWavFileName( 1 : length( sampleWavFileName ) - 4 );
|
dawn@0
|
4 % pitchFileName = [ sampleFileName '_YIN_pitch.txt'];
|
dawn@0
|
5 % pitchFileID = fopen( pitchFileName, 'w');
|
dawn@0
|
6
|
dawn@0
|
7 [periodInSamples, fileinfo] = yin ([ '../' sampleFileName '.wav']);
|
dawn@0
|
8
|
dawn@0
|
9 % YIN produces one pitch estimate for every 32 samples. For our purposes
|
dawn@0
|
10 % this must be scaled to one sample every frame (approx 10ms but also a
|
dawn@0
|
11 % power of two).
|
dawn@0
|
12
|
dawn@0
|
13 frameLength = 2^(ceil(log2(fileinfo.sr/100)));
|
dawn@0
|
14 noOfFrames = floor(fileinfo.nsamples / frameLength); %skip the final samples if necessary
|
dawn@0
|
15
|
dawn@0
|
16 yinPrdSamplesPerFrame = length(periodInSamples) / noOfFrames;
|
dawn@0
|
17
|
dawn@0
|
18
|
dawn@0
|
19 start = 1;
|
dawn@0
|
20 stop = length(periodInSamples);
|
dawn@0
|
21 framePrd = [];
|
dawn@0
|
22
|
dawn@0
|
23 for i=1:noOfFrames
|
dawn@0
|
24 % find the first and last periodInSamples frames to be averaged for
|
dawn@0
|
25 % every master frame of length frameLength
|
dawn@0
|
26 start = floor((i-1) * (length(periodInSamples) / noOfFrames)) + 1;
|
dawn@0
|
27 stop = ceil(i * (length(periodInSamples) / noOfFrames));
|
dawn@0
|
28 if( stop <= length(periodInSamples) )
|
dawn@0
|
29 % exclude NaN frames
|
dawn@0
|
30 prdChunk = periodInSamples( start : stop );
|
dawn@0
|
31 goodPrdIdx = find( isnan( prdChunk )==0 );
|
dawn@0
|
32 if( length( goodPrdIdx ) > 0 )
|
dawn@0
|
33 framePrd(i,2) = mean( prdChunk(goodPrdIdx));
|
dawn@0
|
34 else
|
dawn@0
|
35 framePrd(i,2) = NaN;
|
dawn@0
|
36 end
|
dawn@0
|
37 framePrd(i,1) = i; % record the frame number
|
dawn@0
|
38 % might need to know this in case of frame mismatch
|
dawn@0
|
39 prdFrames(i) = stop - start;
|
dawn@0
|
40 % write to file
|
dawn@0
|
41 % fprintf(pitchFileID, '%d %f \n', framePrd(i,1), framePrd(i,2) );
|
dawn@0
|
42 else
|
dawn@0
|
43 break;
|
dawn@0
|
44 end
|
dawn@0
|
45 end
|
dawn@0
|
46
|
dawn@0
|
47 % plot(periodInSamples,'x'); hold on;
|
dawn@0
|
48 % xaxisdiv=((length(periodInSamples)/length(framePrd))/2):(length(periodInSamples)/length(framePrd)):length(periodInSamples) + ((length(periodInSamples)/length(framePrd))/2);
|
dawn@0
|
49 % plot(xaxisdiv(1:length(framePrd)),framePrd(:,2),'rx');
|
dawn@0
|
50
|
dawn@0
|
51 % fclose( pitchFileID ); |