Exemple #1
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)
Exemple #2
0
def Pre_Process(signal: List[int], lg: int) -> None:
    """
    # input/output signal
    # length of signal
    """

    global y2_hi
    global y2_lo
    global y1_hi
    global y1_lo
    global x0
    global x1

    for i in range(0, lg):
        x2 = x1
        x1 = x0
        x0 = signal[i]

        #  y[i] = b[0]*x[i]/2 + b[1]*x[i-1]/2 + b140[2]*x[i-2]/2
        #                     + a[1]*y[i-1] + a[2] * y[i-2]

        L_tmp = Mpy_32_16(y1_hi, y1_lo, a140[1])
        L_tmp = L_add(L_tmp, Mpy_32_16(y2_hi, y2_lo, a140[2]))
        L_tmp = L_mac(L_tmp, x0, b140[0])
        L_tmp = L_mac(L_tmp, x1, b140[1])
        L_tmp = L_mac(L_tmp, x2, b140[2])
        L_tmp = L_shl(L_tmp, 3)  # Q28 --> Q31 (Q12 --> Q15)
        signal[i] = round(L_tmp)

        y2_hi = y1_hi
        y2_lo = y1_lo
        y1_hi, y1_lo = L_Extract(L_tmp)
Exemple #3
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)
Exemple #4
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]
Exemple #5
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]