Esempio n. 1
0
def Lsp_get_quant(lspcb1: List[List[int]], lspcb2: List[List[int]], code0,
                  code1, code2, fg: List[List[int]],
                  freq_prev: List[List[int]], lspq: List[int],
                  fg_sum: List[int]) -> None:
    """
    # (i) Q13 : first stage LSP codebook
    # (i) Q13 : Second stage LSP codebook
    # (i)     : selected code of first stage
    # (i)     : selected code of second stage
    # (i)     : selected code of second stage
    # (i) Q15 : MA prediction coef.
    # (i) Q13 : previous LSP vector
    # (o) Q13 : quantized LSP parameters
    # (i) Q15 : present MA prediction coef.
    """

    buf = [0] * M  # Q13

    for j in range(0, NC):
        buf[j] = add(lspcb1[code0][j], lspcb2[code1][j])

    for j in range(NC, M):
        buf[j] = add(lspcb1[code0][j], lspcb2[code2][j])

    Lsp_expand_1_2(buf, GAP1)
    Lsp_expand_1_2(buf, GAP2)

    Lsp_prev_compose(buf, lspq, fg, freq_prev, fg_sum)

    Lsp_prev_update(buf, freq_prev)

    Lsp_stability(lspq)
Esempio n. 2
0
def Lsp_expand_1_2(buf: List[int], gap) -> None:
    """
    # (i/o) Q13 : LSP vectors
    # (i)   Q13 : gap
    """

    for j in range(1, M):
        diff = sub(buf[j - 1], buf[j])
        tmp = shr(add(diff, gap), 1)

        if tmp > 0:
            buf[j - 1] = sub(buf[j - 1], tmp)
            buf[j] = add(buf[j], tmp)
Esempio n. 3
0
def Inv_sqrt(L_x) -> int:
    """
    # (o) Q30 : output value   (range: 0<=val<1)
    # (i) Q0  : input value    (range: 0<=val<=7fffffff)
    """

    if L_x <= 0:
        return basic_op.MAX_INT_30

    exp = basic_op.norm_l(L_x)
    L_x = basic_op.L_shl(L_x, exp)  # L_x is normalize

    exp = basic_op.sub(30, exp)
    if (exp & 1) == 0:  # If exponent even -> shift right
        L_x = basic_op.L_shr(L_x, 1)

    exp = basic_op.shr(exp, 1)
    exp = basic_op.add(exp, 1)

    L_x = basic_op.L_shr(L_x, 9)
    i = basic_op.extract_h(L_x)  # Extract b25-b31
    L_x = basic_op.L_shr(L_x, 1)
    a = basic_op.extract_l(L_x)  # Extract b10-b24
    a = a & 0x7fff

    i = basic_op.sub(i, 16)

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

    L_y = basic_op.L_shr(L_y, exp)  # denormalization

    return L_y
Esempio n. 4
0
def Pred_lt_3(exc: List[int], T0: int, frac: int, L_subfr: int) -> None:

    x0 = exc[-T0]

    frac = basic_op.negate(frac)
    if frac < 0:
        frac = basic_op.add(frac, UP_SAMP)
        x0 = x0 - 1

    for j in range(0, L_subfr):
        x1 = x0
        x0 = x0 + 1
        x2 = x0

        c1 = tab_ld8a.inter_31[frac]
        c2 = tab_ld8a.inter_31[basic_op.sub(UP_SAMP, frac)]

        s = 0
        k = 0
        for i in range(0, L_INTER10):
            s = basic_op.L_mac(s, x1[-i], c1[k])
            s = basic_op.L_mac(s, x2[i], c2[k])

            k = k + UP_SAMP

        exc[j] = basic_op.round(s)
Esempio n. 5
0
def Lsp_stability(buf: List[int]) -> None:
    """
    # (i/o) Q13 : quantized LSP parameters
    """

    for j in range(0, M - 1):
        L_acc = L_deposit_l(buf[j + 1])
        L_accb = L_deposit_l(buf[j])
        L_diff = L_sub(L_acc, L_accb)

        if L_diff < 0:
            # exchange buf[j]<->buf[j+1]
            tmp = buf[j + 1]
            buf[j + 1] = buf[j]
            buf[j] = tmp

    if sub(buf[0], L_LIMIT) < 0:
        buf[0] = L_LIMIT
        # printf("lsp_stability warning Low \n")

    for j in range(j, M - 1):
        L_acc = L_deposit_l(buf[j + 1])
        L_accb = L_deposit_l(buf[j])
        L_diff = L_sub(L_acc, L_accb)

        if L_sub(L_diff, GAP3) < 0:
            buf[j + 1] = add(buf[j], GAP3)

    if sub(buf[M - 1], M_LIMIT) > 0:
        buf[M - 1] = M_LIMIT
Esempio n. 6
0
def Check_Parity_Pitch(pitch_index: int, parity: int) -> int:
    """
    #  output: 0 = no error, 1= error 
    #  input : index of parameter     
    #  input : parity bit  
    """

    temp = basic_op.shr(pitch_index, 1)

    sum = 1
    for i in range(0, 6):
        temp = basic_op.shr(temp, 1)
        bit = temp & 1
        sum = basic_op.add(sum, bit)

    sum = basic_op.add(sum, parity)
    sum = sum & 1

    return sum
Esempio n. 7
0
def Parity_Pitch(pitch_index: int) -> int:
    """
    #  output: parity bit (XOR of 6 MSB bits)    
    #  input : index for which parity to compute 
    """

    temp = basic_op.shr(pitch_index, 1)

    result = 1
    for i in range(0, 6):
        temp = basic_op.shr(temp, 1)
        bit = temp & 1
        result = basic_op.add(result, bit)

    result = result & 1

    return result
Esempio n. 8
0
def Levinson(Rh: List[int], Rl: List[int], A: List[int], rc: List[int]) -> None:
    """
    # (i)     : Rh[M+1] Vector of autocorrelations (msb)
    # (i)     : Rl[M+1] Vector of autocorrelations (lsb)
    # (o) Q12 : A[M]    LPC coefficients  (m = 10)
    # (o) Q15 : rc[M]   Reflection coefficients.
    """

    # LPC coef. in double prec. 
    Ah = [0] * (M + 1)
    Al = [0] * (M + 1)

    # LPC coef.for next iteration in double prec.
    Anh = [0] * (M + 1)
    Anl = [0] * (M + 1)

    # K = A[1] = -R[1] / R[0] 

    t1 = oper_32b.L_Comp(Rh[1], Rl[1])          # R[1] in Q31      
    t2 = basic_op.L_abs(t1)                     # abs R[1]         
    t0 = oper_32b.Div_32(t2, Rh[0], Rl[0])      # R[1]/R[0] in Q31

    if t1 > 0:
        t0 = basic_op.L_negate(t0)              # -R[1]/R[0]

    Kh, Kl = oper_32b.L_Extract(t0)             # K in DPF         
    rc[0] = Kh
    t0 = basic_op.L_shr(t0, 4)                  # A[1] in Q27      
    Ah[1], Al[1] = oper_32b.L_Extract(t0)       # A[1] in DPF      

    #  Alpha = R[0] * (1-K**2) 

    t0 = oper_32b.Mpy_32(Kh, Kl, Kh, Kl)            # K*K      in Q31 
    t0 = basic_op.L_abs(t0)                         # Some case <0 !! 
    t0 = basic_op.L_sub(basic_op.MAX_INT_32, t0)    # 1 - K*K  in Q31 
    hi, lo = L_Extract(t0)                          # DPF format      
    t0 = oper_32b.Mpy_32(Rh[0], Rl[0], hi, lo)      # Alpha in Q31    

    # Normalize Alpha 

    alp_exp = basic_op.norm_l(t0)
    t0 = basic_op.L_shl(t0, alp_exp)
    alp_h, alp_l = L_Extract(t0)                    # DPF format    

    ########
    # ITERATIONS  I=2 to M
    ########

    for i in range(2, ld8a.M + 1):
        # t0 = SUM ( R[j]*A[i-j] ,j=1,i-1 ) +  R[i] 

        t0 = 0
        for j in range(1, i):
            t0 = basic_op.L_add(t0, oper_32b.Mpy_32(Rh[j], Rl[j], Ah[i - j], Al[i - j]))

        t0 = basic_op.L_shl(t0, 4)                  # result in Q27 -> convert to Q31 
                                                    # No overflow possible            
        t1 = oper_32b.L_Comp(Rh[i], Rl[i])
        t0 = basic_op.L_add(t0, t1)                 # add R[i] in Q31                 

        # K = -t0 / Alpha

        t1 = basic_op.L_abs(t0)
        t2 = oper_32b.Div_32(t1, alp_h, alp_l)      # abs(t0)/Alpha
        
        if t0 > 0:
            t2 = basic_op.L_negate(t2)              # K =-t0/Alpha                    
        t2 = basic_op.L_shl(t2, alp_exp)            # denormalize compare to Alpha   
        Kh, Kl = L_Extract(t2)                      # K in DPF                        
        rc[i - 1] = Kh

        # Test for unstable filter. If unstable keep old A(z) 

        if basic_op.sub(basic_op.abs_s(Kh), 32750) > 0:
            for j in range(0, ld8a.M + 1):
                A[j] = old_A[j]
            
            rc[0] = old_rc[0]       # only two rc coefficients are needed 
            rc[1] = old_rc[1]
            return
        

        ##########
        # Compute new LPC coeff. -> An[i]         *
        # An[j]= A[j] + K*A[i-j]     , j=1 to i-1 *
        # An[i]= K                                *
        ##########

        for j in range (1, i):
            t0 = oper_32b.Mpy_32(Kh, Kl, Ah[i - j], Al[i - j])
            t0 = basic_op.L_add(t0, oper_32b.L_Comp(Ah[j], Al[j]))
            Anh[j], Anl[j] = L_Extract(t0)
        
        t2 = basic_op.L_shr(t2, 4)              # t2 = K in Q31 ->convert to Q27  
        Anh[i], Anl[i] = L_Extract(t2)          # An[i] in Q27                    

        #  Alpha = Alpha * (1-K**2) 

        t0 = oper_32b.Mpy_32(Kh, Kl, Kh, Kl)            # K*K      in Q31 
        t0 = basic_op.L_abs(t0)                         # Some case <0 !! 
        t0 = basic_op.L_sub(basic_op.MAX_INT_32, t0)    # 1 - K*K  in Q31 
        hi, lo = L_Extract(t0)                          # DPF format      
        t0 = oper_32b.Mpy_32(alp_h, alp_l, hi, lo)      # Alpha in Q31    

        # Normalize Alpha 

        j = basic_op.norm_l(t0)
        t0 = basic_op.L_shl(t0, j)
        alp_h, alp_l = L_Extract(t0)            # DPF format    
        alp_exp = basic_op.add(alp_exp, j)      # Add normalization to alp_exp 

        # A[j] = An[j] 

        for j in range(1, i + 1):
            Ah[j] = Anh[j]
            Al[j] = Anl[j]

    # Truncate A[i] in Q27 to Q12 with rounding 

    A[0] = 4096

    for i in range(1, ld8a.M + 1):
        t0 = oper_32b.L_Comp(Ah[i], Al[i])
        old_A[i] = A[i] = basic_op.round(L_shl(t0, 1))
    
    old_rc[0] = rc[0]
    old_rc[1] = rc[1]
Esempio n. 9
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]