function [HNR] = calculate_HNR( y, fs, noOfFrames, silenceFrames, pitch )

    %find the maximum lag M
    DEBUG=0;

    maxPeriod = ceil(fs/getVariables('getMinFreq')); %get the default minimum fundatmental frequency
    minPeriod = floor(fs/getVariables('getMaxFreq')); %get the default maximum fundamental frequency

    for i = 1:(noOfFrames - 1)
        if( silenceFrames(i) == 1 )
            
            % frame is not silent
           NA = xcorr(y(:,i), y(:,i), 'coeff');
           NA = NA( ceil(length(NA)/2) : end );
            
           if(DEBUG)
                subplot(211); plot(y(:,i)); title(i);
                subplot(212); plot(NA); hold on;
           end
           
            % find the maximum nearset to the predicted pitch
           f0 = pitch(i);
           if( (f0 < minPeriod) || (isnan(f0)) )
               f0 = minPeriod;
           end
           % perform a search outwards from that point
           startSearch = floor(f0-(f0*0.1));
           endSearch = ceil(f0+(f0*0.1));
           if( startSearch > length(NA) )
               startSearch = length(NA) - 20;
           end
           if( endSearch > length(NA) )
               endSearch = length(NA);
           end
           [max_value max_position] = max( NA( startSearch : endSearch ));
           
           if(DEBUG)
               plot( max_position + startSearch - 1, max_value, 'gx' );
               plot( startSearch : endSearch, NA(startSearch : endSearch), 'r');
               hold off;
           end

           HR(i) = max_value;

           if( HR(i) < 0)
               % if the sample is negative it probably means the frame is
               % silent so just set to  'most noisy so far'
%                if(i>1)
%                     HNR(i) = min( HNR );
%                else
                   HNR(i) = NaN;
%                end
           else
                HNR(i)= 10*log10(HR(i)/(1-HR(i)));
           end
           if(DEBUG) title(HNR(i)); end
        else
            HNR(i) = NaN;
        end
       
    end
    
    
    
    
    
     
    
    
    
