Example #1
0
def Chebps_10(x: int, f: List[int], n: int) -> int:
    # Note: All computation are done in Q23.

    b2_h = 128      # b2 = 1.0 in Q23 DPF
    b2_l = 0

    t0 = basic_op.L_mult(x, 256)            # 2*x in Q23          
    t0 = basic_op.L_mac(t0, f[1], 4096)     # + f[1] in Q23       
    b1_h, b1_l = oper_32b.L_Extract(t0)     # b1 = 2*x + f[1]     

    for i in range(2, n):
        t0 = oper_32b.Mpy_32_16(b1_h, b1_l, x)      # t0 = 2.0*x*b1              
        t0 = basic_op.L_shl(t0, 1)
        t0 = basic_op.L_mac(t0, b2_h, -32768)       # t0 = 2.0*x*b1 - b2         
        t0 = basic_op.L_msu(t0, b2_l, 1)
        t0 = basic_op.L_mac(t0, f[i], 4096)         # t0 = 2.0*x*b1 - b2 + f[i] 

        b0_h, b0_l = oper_32b.L_Extract(t0)         # b0 = 2.0*x*b1 - b2 + f[i] 

        b2_l = b1_l     # b2 = b1 
        b2_h = b1_h
        b1_l = b0_l     # b1 = b0 
        b1_h = b0_h

    t0 = oper_32b.Mpy_32_16(b1_h, b1_l, x)          # t0 = x*b1              
    t0 = basic_op.L_mac(t0, b2_h, -32768)           # t0 = x*b1 - b2          
    t0 = basic_op.L_msu(t0, b2_l, 1)
    t0 = basic_op.L_mac(t0, f[i], 2048)             # t0 = x*b1 - b2 + f[i]/2 

    t0 = basic_op.L_shl(t0, 7)                      # Q23 to Q30 with saturation 
    cheb = basic_op.extract_h(t0)

    return cheb
Example #2
0
def Residu(a: List[int], x: List[int], y: List[int], lg: int) -> None:
    """
    # (i) Q12 : prediction coefficients 
    # (i)     : speech (values x[-m..-1] are needed
    # (o)     : residual signal 
    # (i)     : size of filtering
    """

    for i in range(0, lg):
        s = basic_op.L_mult(x[i], a[0])
        
        for j in range(1, ld8a.M + 1):
            s = basic_op.L_mac(s, a[j], x[i-j])

        s = basic_op.L_shl(s, 3)
        y[i] = basic_op.round(s)
Example #3
0
def Syn_filt(a: List[int], x: List[int], y: List[int], lg: int, mem: List[int], update: int) -> None:
    """
    # (i) Q12 : a[m+1] prediction coefficients   (m=10)
    # (i)     : input signal
    # (o)     : output signal
    # (i)     : size of filtering
    # (i/o)   : memory associated with this filtering.
    # (i)     : 0=no update, 1=update of memory.
    """

    #Word16 i, j
    #Word32 s
    #Word16 tmp[100]     # This is usually done by memory allocation (lg+M) 
    #Word16 *yy

    tmp = [0] * 100

    # Copy mem[] to yy[] (yy points to tmp)
    for i in range(0, ld8a.M):
        tmp[i] = mem[i]

    # Do the filtering. 
    for i in range(0, lg):
        s = basic_op.L_mult(x[i], a[0])

        for j in range(1, ld8a.M + 1):
            # this negative index needs tested
            s = basic_op.L_msu(s, a[j], tmp[-j])

        s = basic_op.L_shl(s, 3)
        tmp[i] = basic_op.round(s)
    
    for i in range (0, lg):
        y[i] = tmp[i+M]

    # Update of memory if update==1 

    if update != 0:
        for i in range(0, ld8a.M):
            mem[i] = y[lg-M+i]
Example #4
0
def Pow2(exponent: int, fraction: int) -> int:
    """
    # (o) Q0  : result       (range: 0<=val<=0x7fffffff)
    # (i) Q0  : Integer part.      (range: 0<=val<=30)
    # (i) Q15 : Fractional part.   (range: 0.0<=val<1.0)
    """

    L_x = basic_op.L_mult(fraction, 32)  # L_x = fraction<<6
    i = basic_op.extract_h(L_x)  # Extract b10-b15 of fraction
    L_x = basic_op.L_shr(L_x, 1)
    a = basic_op.extract_l(L_x)  # Extract b0-b9   of fraction
    a = a & 0x7fff

    L_x = basic_op.L_deposit_h(tab_ld8a.tabpow[i])  # tabpow[i] << 16
    tmp = basic_op.sub(tab_ld8a.tabpow[i],
                       tab_ld8a.tabpow[i + 1])  # tabpow[i] - tabpow[i+1]
    L_x = basic_op.L_msu(L_x, tmp, a)  # L_x -= tmp*a*2

    exp = basic_op.sub(30, exponent)
    L_x = basic_op.L_shr_r(L_x, exp)

    return L_x
Example #5
0
def Az_lsp(a: List[int], lsp: List[int], old_lsp: List[int]) -> None:
    """
    # (i) Q12 : predictor coefficients
    # (o) Q15 : line spectral pairs
    # (i)     : old lsp[] (in case not found 10 roots)
    """

    #Word16 i, j, nf, ip
    #Word16 xlow, ylow, xhigh, yhigh, xmid, ymid, xint
    #Word16 x, y, sign, exp
    #Word16 *coef
    #Word16 f1[M / 2 + 1], f2[M / 2 + 1]
    #Word32 t0, L_temp
    #Flag ovf_coef
    #Word16 (*pChebps)(Word16 x, Word16 f[], Word16 n)

    f1 = [0] * ((ld8a.M / 2) + 1)
    f2 = [0] * ((ld8a.M / 2) + 1)

    #########
    # find the sum and diff. pol. F1(z) and F2(z)                
    # F1(z) <--- F1(z)/(1+z**-1) & F2(z) <--- F2(z)/(1-z**-1)  
    # 
    # f1[0] = 1.0                                                
    # f2[0] = 1.0                                                
    # 
    # for (i = 0 i< NC i++)                                     
    # {                                                           
    #   f1[i+1] = a[i+1] + a[M-i] - f1[i]                       
    #   f2[i+1] = a[i+1] - a[M-i] + f2[i]                        
    # }                                                           
    ########

    ovf_coef = 0
    pChebps = 'Chebps_11'

    f1[0] = 2048        # f1[0] = 1.0 is in Q11 
    f2[0] = 2048        # f2[0] = 1.0 is in Q11

    for i in range(0, ld8a.NC):
        basic_op.setOverflow(0)
        t0 = basic_op.L_mult(a[i + 1], 16384)       # x = (a[i+1] + a[M-i]) >> 1        
        t0 = basic_op.L_mac(t0, a[M - i], 16384)    #    -> From Q12 to Q11             
        x = basic_op.extract_h(t0)

        if basic_op.getOverflow() != 0:
            ovf_coef = 1

        basic_op.setOverflow(0)
        f1[i + 1] = basic_op.sub(x, f1[i])          # f1[i+1] = a[i+1] + a[M-i] - f1[i]

        if basic_op.getOverflow() != 0: 
            ovf_coef = 1

        basic_op.setOverflow(0)
        t0 = basic_op.L_mult(a[i + 1], 16384)       # x = (a[i+1] - a[M-i]) >> 1        
        t0 = basic_op.L_msu(t0, a[M - i], 16384)    #    -> From Q12 to Q11             
        x = basic_op.extract_h(t0)

        if basic_op.getOverflow() != 0: 
            ovf_coef = 1

        basic_op.setOverflow(0)
        f2[i + 1] = basic_op.add(x, f2[i]) # f2[i+1] = a[i+1] - a[M-i] + f2[i] 
        
        if basic_op.getOverflow() != 0: 
            ovf_coef = 1

    if ovf_coef == 1:
        #printf("===== OVF ovf_coef =====\n")

        pChebps = 'Chebps_10'

        f1[0] = 1024                # f1[0] = 1.0 is in Q10 
        f2[0] = 1024                # f2[0] = 1.0 is in Q10 

        for i in range(0, ld8a.NC):
            t0 = basic_op.L_mult(a[i + 1], 8192)        # x = (a[i+1] + a[M-i]) >> 1        
            t0 = basic_op.L_mac(t0, a[M - i], 8192)     #    -> From Q11 to Q10             
            x = basic_op.extract_h(t0)
            f1[i + 1] = basic_op.sub(x, f1[i])          # f1[i+1] = a[i+1] + a[M-i] - f1[i] 

            t0 = basic_op.L_mult(a[i + 1], 8192)        # x = (a[i+1] - a[M-i]) >> 1        
            t0 = basic_op.L_msu(t0, a[M - i], 8192)     #    -> From Q11 to Q10             
            x = basic_op.extract_h(t0)
            f2[i + 1] = basic_op.add(x, f2[i])          # f2[i+1] = a[i+1] - a[M-i] + f2[i] 


    ########
    # find the LSPs using the Chebichev pol. evaluation
    ########

    nf = 0      # number of found frequencies 
    ip = 0      # indicator for f1 or f2      

    coef = f1   # Alias for f1 or f2

    xlow = grid[0]
    if pChebps == 'Chebps_11':
        ylow = Chebps_11(xlow, coef, NC)
    else:
        ylow = Chebps_10(xlow, coef, NC)

    j = 0
    while nf < ld81.M and j < ld8a.GRID_POINTS:
        j = basic_op.add(j, 1)
        xhigh = xlow
        yhigh = ylow
        xlow = grid[j]
        if pChebps == 'Chebps_11':
            ylow = Chebps_11(xlow, coef, ld8a.NC)
        else:
            ylow = Chebps_10(xlow, coef, ld8a.NC)

        L_temp = basic_op.L_mult(ylow, yhigh)

        if L_temp <= 0:
            # divide 2 times the interval

            for i in range(0, 2):
                xmid = basic_op.add(basic_op.shr(xlow, 1), basic_op.shr(xhigh, 1))  # xmid = (xlow + xhigh)/2 

                if pChebps == 'Chebps_11':
                    ymid = Chebps_11(xmid, coef, ld8a.NC)
                else:
                    ymid = Chebps_10(xmid, coef, ld8a.NC)

                L_temp = basic_op.L_mult(ylow, ymid)
                if L_temp <= 0:
                    yhigh = ymid
                    xhigh = xmid
                else:
                    ylow = ymid
                    xlow = xmid

            ########
            # Linear interpolation                    
            #   xint = xlow - ylow*(xhigh-xlow)/(yhigh-ylow) 
            ########

            x = basic_op.sub(xhigh, xlow)
            y = basic_op.sub(yhigh, ylow)

            if y == 0:
                xint = xlow
            else:
                sign = y
                y = basic_op.abs_s(y)
                exp = basic_op.norm_s(y)
                y = basic_op.shl(y, exp)
                y = basic_op.div_s(16383, y)
                t0 = basic_op.L_mult(x, y)
                t0 = basic_op.L_shr(t0, basic_op.sub(20, exp))
                y = basic_op.extract_l(t0)                      # y= (xhigh-xlow)/(yhigh-ylow) in Q11 

                if sign < 0:
                    y = basic_op.negate(y)

                t0 = basic_op.L_mult(ylow, y)                       # result in Q26 
                t0 = basic_op.L_shr(t0, 11)                         # result in Q15 
                xint = basic_op.sub(xlow, basic_op.extract_l(t0))   # xint = xlow - ylow*y 

            lsp[nf] = xint
            xlow = xint
            nf = basic_op.add(nf, 1)

            if ip == 0:
                ip = 1
                coef = f2
            else:
                ip = 0
                coef = f1
            
            if pChebps == 'Chebps_11':
                ylow = Chebps_11(xlow, coef, ld8a.NC)
            else:
                ylow = Chebps_10(xlow, coef, ld8a.NC)

    # Check if M roots found 

    if basic_op.sub(nf, ld8a.M) < 0:
        for i in range (0, ld8a.M):
            lsp[i] = old_lsp[i]
Example #6
0
def Gain_predict(past_qua_en: List[int], code: List[int],
                 L_subfr: int) -> Tuple[int, int]:
    """
    # (i) Q10 :Past quantized energies
    # (i) Q13 :Innovative vector.
    # (i)     :Subframe length.
    # (o) Qxx :Predicted codebook gain      - omitted param
    # (o)     :Q-Format(gcode0)             - omitted param
    """

    #Word16  i, exp, frac
    #Word32  L_tmp

    ##########
    # Energy coming from code
    ##########

    L_tmp = 0
    for i in range(0, L_subfr):
        L_tmp = basic_op.L_mac(L_tmp, code[i], code[i])

    ##########
    #  Compute: means_ener - 10log10(ener_code/ L_sufr)
    #  Note: mean_ener change from 36 dB to 30 dB because input/2
    #
    # = 30.0 - 10 log10( ener_code / lcode)  + 10log10(2^27)
    #                                          !!ener_code in Q27!!
    # = 30.0 - 3.0103  log2(ener_code) + 10log10(40) + 10log10(2^27)
    # = 30.0 - 3.0103  log2(ener_code) + 16.02  + 81.278
    # = 127.298 - 3.0103  log2(ener_code)
    ##########

    exp, frac = dsp_func.Log2(L_tmp, exp, frac)  # Q27->Q0 ^Q0 ^Q15
    L_tmp = oper_32b.Mpy_32_16(exp, frac, -24660)  # Q0 Q15 Q13 -> ^Q14
    # hi:Q0+Q13+1
    # lo:Q15+Q13-15+1
    # -24660[Q13]=-3.0103
    L_tmp = basic_op.L_mac(basic_op.L_tmp, 32588, 32)  # 32588*32[Q14]=127.298

    ##########
    # Compute gcode0.                                                 *
    #  = Sum(i=0,3) pred[i]*past_qua_en[i] - ener_code + mean_ener    *
    ##########

    L_tmp = basic_op.L_shl(L_tmp, 10)  # From Q14 to Q24
    for i in range(0, 4):
        L_tmp = basic_op.L_mac(L_tmp, pred[i], past_qua_en[i])  # Q13*Q10 ->Q24

    gcode0 = basic_op.extract_h(L_tmp)  # From Q24 to Q8

    ##########
    # gcode0 = pow(10.0, gcode0/20)                                   *
    #        = pow(2, 3.3219*gcode0/20)                               *
    #        = pow(2, 0.166*gcode0)                                   *
    ##########

    L_tmp = basic_op.L_mult(gcode0, 5439)  # *0.166 in Q15, result in Q24
    L_tmp = basic_op.L_shr(L_tmp, 8)  # From Q24 to Q16
    exp, frac = oper_32b.L_Extract(L_tmp, exp,
                                   frac)  # Extract exponent of gcode0

    gcode0 = basic_op.extract_l(dsp_func.Pow2(
        14, frac))  # Put 14 as exponent so that
    # output of Pow2() will be:
    # 16768 < Pow2() <= 32767
    exp_gcode0 = basic_op.sub(14, exp)

    return (gcode0, exp_gcode0)