def stHarmonic(frame, fs):
    """
    Computes harmonic ratio and pitch
    """
    M = numpy.round(0.016 * fs) - 1
    R = numpy.correlate(frame, frame, mode='full')

    g = R[len(frame)-1]
    R = R[len(frame):-1]

    # estimate m0 (as the first zero crossing of R)
    [a, ] = numpy.nonzero(numpy.diff(numpy.sign(R)))

    if len(a) == 0:
        m0 = len(R)-1
    else:
        m0 = a[0]
    if M > len(R):
        M = len(R) - 1

    Gamma = numpy.zeros((M), dtype=numpy.float64)
    CSum = numpy.cumsum(frame ** 2)
    Gamma[m0:M] = R[m0:M] / (numpy.sqrt((g * CSum[M:m0:-1])) + eps)

    ZCR = stZCR(Gamma)

    if ZCR > 0.15:
        HR = 0.0
        f0 = 0.0
    else:
        if len(Gamma) == 0:
            HR = 1.0
            blag = 0.0
            Gamma = numpy.zeros((M), dtype=numpy.float64)
        else:
            HR = numpy.max(Gamma)
            blag = numpy.argmax(Gamma)

        # Get fundamental frequency:
        f0 = fs / (blag + eps)
        if f0 > 5000:
            f0 = 0.0
        if HR < 0.1:
            f0 = 0.0

    return (HR, f0)
def stHarmonic ( frame , fs ) : M = numpy . round ( 0.016 * fs ) - 1 R = numpy . correlate ( frame , frame , mode = 'full' ) g = R [ len ( frame ) - 1 ] R = R [ len ( frame ) : - 1 ] [ a , ] = numpy . nonzero ( numpy . diff ( numpy . sign ( R ) ) ) if len ( a ) == 0 : m0 = len ( R ) - 1 else : m0 = a [ 0 ] if M > len ( R ) : M = len ( R ) - 1 Gamma = numpy . zeros ( ( M ) , dtype = numpy . float64 ) CSum = numpy . cumsum ( frame ** 2 ) Gamma [ m0 : M ] = R [ m0 : M ] / ( numpy . sqrt ( ( g * CSum [ M : m0 : - 1 ] ) ) + eps ) ZCR = stZCR ( Gamma ) if ZCR > 0.15 : HR = 0.0 f0 = 0.0 else : if len ( Gamma ) == 0 : HR = 1.0 blag = 0.0 Gamma = numpy . zeros ( ( M ) , dtype = numpy . float64 ) else : HR = numpy . max ( Gamma ) blag = numpy . argmax ( Gamma ) f0 = fs / ( blag + eps ) if f0 > 5000 : f0 = 0.0 if HR < 0.1 : f0 = 0.0 return ( HR , f0 )
Computes harmonic ratio and pitch