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 );