📄 g7231_lpc.c
字号:
#include "typedef.h"
#include "G7231_basop.h"
#include "G7231_cst_lbc.h"
#include "G7231_tab_lbc.h"
#include "G7231_lbccodec.h"
#include "G7231_coder.h"
#include "G7231_decod.h"
#include "G7231_util_lbc.h"
#include "G7231_lpc.h"
#include "G7231_cod_cng.h"
#include "intrindefs.h"
void G7231Comp_Lpc( Word16 *UnqLpc, Word16 *PrevDat, Word16 *DataBuff )
{
int i,j,k ;
Word16 Dpnt[G7231Frame+G7231LpcFrame-G7231SubFrLen] ;
Word16 Vect[G7231LpcFrame] ;
Word16 Acf_sf[G7231LpcOrderP1*G7231SubFrames];
Word16 ShAcf_sf[G7231SubFrames];
Word16 Exp ;
Word16 *curAcf;
Word16 Pk2;
Word32 Acc0,Acc1 ;
/*
* Generate a buffer of 360 samples. This consists of 120 samples
* from the previous frame and 240 samples from the current frame.
*/
for ( i = 0 ; i < G7231LpcFrame-G7231SubFrLen ; i ++ )
Dpnt[i] = PrevDat[i] ;
for ( i = 0 ; i < G7231Frame ; i ++ )
Dpnt[i+G7231LpcFrame-G7231SubFrLen] = DataBuff[i] ;
/*
* Repeat for all subframes
*/
curAcf = Acf_sf;
for ( k = 0 ; k < G7231SubFrames ; k ++ ) {
/*
* Do windowing
*/
/* Get block of 180 samples centered around current subframe */
for ( i = 0 ; i < G7231LpcFrame ; i ++ )
Vect[i] = Dpnt[k*G7231SubFrLen+i] ;
/* Normalize */
ShAcf_sf[k] = G7231Vec_Norm( Vect, (Word16) G7231LpcFrame ) ;
/* Apply the Hamming window */
for ( i = 0 ; i < G7231LpcFrame ; i ++ )
Vect[i] = G7231mult_r(Vect[i], G7231HammingWindowTable[i]) ;
/*
* Compute the autocorrelation coefficients
*/
/* Compute the zeroth-order coefficient (energy) */
Acc1 = (Word32) 0 ;
for ( i = 0 ; i < G7231LpcFrame ; i ++ ) {
Acc0 = G7231L_mult( Vect[i], Vect[i] ) ;
Acc0 = G7231L_shr( Acc0, (Word16) 1 ) ;
Acc1 = G7231L_add( Acc1, Acc0 ) ;
}
/* Apply a white noise correction factor of (1025/1024) */
Acc0 = G7231L_shr( Acc1, (Word16) G7231RidgeFact ) ;
Acc1 = G7231L_add( Acc1, Acc0 ) ;
/* Normalize the energy */
Exp = G7231norm_l( Acc1 ) ;
Acc1 = G7231L_shl( Acc1, Exp ) ;
curAcf[0] = G7231round( Acc1 ) ;
if(curAcf[0] == 0) {
for ( i = 1 ; i <= G7231LpcOrder ; i ++ )
curAcf[i] = 0;
ShAcf_sf[k] = 40;
}
else {
/* Compute the rest of the autocorrelation coefficients.
Multiply them by a binomial coefficients lag window. */
for ( i = 1 ; i <= G7231LpcOrder ; i ++ )
{
Acc1 = (Word32) 0 ;
for ( j = i ; j < G7231LpcFrame ; j ++ )
{
Acc0 = G7231L_mult( Vect[j], Vect[j-i] ) ;
Acc0 = G7231L_shr( Acc0, (Word16) 1 ) ;
Acc1 = G7231L_add( Acc1, Acc0 ) ;
}
Acc0 = G7231L_shl( Acc1, Exp ) ;
Acc0 = G7231L_mls( Acc0, G7231BinomialWindowTable[i-1] ) ;
curAcf[i] = G7231round(Acc0) ;
}
/* Save Acf scaling factor */
ShAcf_sf[k] = G7231add(Exp, G7231shl(ShAcf_sf[k], 1));
}
/*
* Apply the Levinson-Durbin algorithm to generate the LPC
* coefficients
*/
G7231Durbin( &UnqLpc[k*G7231LpcOrder], &curAcf[1], curAcf[0], &Pk2 );
G7231CodStat.SinDet <<= 1;
if ( Pk2 > 0x799a ) {
G7231CodStat.SinDet ++ ;
}
curAcf += G7231LpcOrderP1;
}
/* Update sine detector */
G7231CodStat.SinDet &= 0x7fff ;
j = G7231CodStat.SinDet ;
k = 0 ;
for ( i = 0 ; i < 15 ; i ++ ) {
k += j & 1 ;
j >>= 1 ;
}
if ( k >= 14 )
G7231CodStat.SinDet |= 0x8000 ;
/* Update CNG Acf memories */
G7231Update_Acf(Acf_sf, ShAcf_sf);
}
Word16 G7231Durbin( Word16 *Lpc, Word16 *Corr, Word16 Err, Word16 *Pk2 )
{
int i,j ;
Word16 Temp[G7231LpcOrder] ;
Word16 Pk ;
Word32 Acc0,Acc1,Acc2 ;
/*
* Initialize the LPC vector
*/
for ( i = 0 ; i < G7231LpcOrder ; i ++ )
Lpc[i] = (Word16) 0 ;
/*
* Do the recursion. At the ith step, the algorithm computes the
* (i+1)th - order MMSE linear prediction filter.
*/
for ( i = 0 ; i < G7231LpcOrder ; i ++ ) {
/*
* Compute the partial correlation (parcor) coefficient
*/
/* Start parcor computation */
Acc0 = G7231L_deposit_h( Corr[i] ) ;
Acc0 = G7231L_shr( Acc0, (Word16) 2 ) ;
for ( j = 0 ; j < i ; j ++ )
Acc0 = G7231L_msu( Acc0, Lpc[j], Corr[i-j-1] ) ;
Acc0 = G7231L_shl( Acc0, (Word16) 2 ) ;
/* Save sign */
Acc1 = Acc0 ;
Acc0 = G7231L_abs( Acc0 ) ;
/* Finish parcor computation */
Acc2 = G7231L_deposit_h( Err ) ;
if ( Acc0 >= Acc2 ) {
*Pk2 = 32767;
break ;
}
Pk = G7231div_l( Acc0, Err ) ;
if ( Acc1 >= 0 )
Pk = G7231negate(Pk) ;
/*
* Sine detector
*/
if ( i == 1 ) *Pk2 = Pk;
/*
* Compute the ith LPC coefficient
*/
Acc0 = G7231L_deposit_h( G7231negate(Pk) ) ;
Acc0 = G7231L_shr( Acc0, (Word16) 2 ) ;
Lpc[i] = G7231round( Acc0 ) ;
/*
* Update the prediction error
*/
Acc1 = G7231L_mls( Acc1, Pk ) ;
Acc1 = G7231L_add( Acc1, Acc2 ) ;
Err = G7231round( Acc1 ) ;
/*
* Compute the remaining LPC coefficients
*/
for ( j = 0 ; j < i ; j ++ )
Temp[j] = Lpc[j] ;
for ( j = 0 ; j < i ; j ++ ) {
Acc0 = G7231L_deposit_h( Lpc[j] ) ;
Acc0 = G7231L_mac( Acc0, Pk, Temp[i-j-1] ) ;
Lpc[j] = G7231round( Acc0 ) ;
}
}
return Err ;
}
void G7231Wght_Lpc( Word16 *PerLpc, Word16 *UnqLpc )
{
int i,j ;
/*
* Do for all subframes
*/
for ( i = 0 ; i < G7231SubFrames ; i ++ ) {
/*
* Compute the jth FIR coefficient by multiplying the jth LPC
* coefficient by (0.9)^j.
*/
for ( j = 0 ; j < G7231LpcOrder ; j ++ )
PerLpc[j] = G7231mult_r( UnqLpc[j], G7231PerFiltZeroTable[j] ) ;
PerLpc += G7231LpcOrder ;
/*
* Compute the jth IIR coefficient by multiplying the jth LPC
* coefficient by (0.5)^j.
*/
for ( j = 0 ; j < G7231LpcOrder ; j ++ )
PerLpc[j] = G7231mult_r( UnqLpc[j], G7231PerFiltPoleTable[j] ) ;
PerLpc += G7231LpcOrder ;
UnqLpc += G7231LpcOrder ;
}
}
void G7231Error_Wght( Word16 *Dpnt, Word16 *PerLpc )
{
int i,j,k ;
Word32 Acc0 ;
/*
* Do for all subframes
*/
for ( k = 0 ; k < G7231SubFrames ; k ++ ) {
for ( i = 0 ; i < G7231SubFrLen ; i ++ ) {
/*
* Do the FIR part
*/
/* Filter */
Acc0 = G7231L_mult( *Dpnt, (Word16) 0x2000 ) ;
for ( j = 0 ; j < G7231LpcOrder ; j ++ )
Acc0 = G7231L_msu( Acc0, PerLpc[j], G7231CodStat.WghtFirDl[j] ) ;
/* Update memory */
for ( j = G7231LpcOrder-1 ; j > 0 ; j -- )
G7231CodStat.WghtFirDl[j] = G7231CodStat.WghtFirDl[j-1] ;
G7231CodStat.WghtFirDl[0] = *Dpnt ;
/*
* Do the IIR part
*/
/* Filter */
for ( j = 0 ; j < G7231LpcOrder ; j ++ )
Acc0 = G7231L_mac( Acc0, PerLpc[G7231LpcOrder+j],
G7231CodStat.WghtIirDl[j] ) ;
for ( j = G7231LpcOrder-1 ; j > 0 ; j -- )
G7231CodStat.WghtIirDl[j] = G7231CodStat.WghtIirDl[j-1] ;
Acc0 = G7231L_shl( Acc0, (Word16) 2 ) ;
/* Update memory */
G7231CodStat.WghtIirDl[0] = G7231round( Acc0 ) ;
*Dpnt ++ = G7231CodStat.WghtIirDl[0] ;
}
PerLpc += 2*G7231LpcOrder ;
}
}
void G7231Comp_Ir( Word16 *ImpResp, Word16 *QntLpc, Word16 *PerLpc, G7231PWDEF Pw )
{
int i;
unsigned int j;
Word16 FirDl[G7231LpcOrder] ;
Word16 IirDl[G7231LpcOrder] ;
Word16 Temp[G7231PitchMax+G7231SubFrLen] ;
Word32 Acc0,Acc1 ;
/*
* Clear all memory. Impulse response calculation requires
* an all-zero initial state.
*/
/* Perceptual weighting filter */
for ( i = 0 ; i < G7231LpcOrder ; i ++ )
FirDl[i] = IirDl[i] = (Word16) 0 ;
/* Harmonic noise shaping filter */
for ( i = 0 ; i < G7231PitchMax+G7231SubFrLen ; i ++ )
Temp[i] = (Word16) 0 ;
/*
* Input a single impulse
*/
Acc0 = (Word32) 0x04000000L ;
/*
* Do for all elements in a subframe
*/
for ( i = 0 ; i < G7231SubFrLen ; i ++ ) {
/*
* Synthesis filter
*/
for ( j = 0 ; j < G7231LpcOrder ; j ++ )
Acc0 = G7231L_mac( Acc0, QntLpc[j], FirDl[j] ) ;
Acc1 = G7231L_shl( Acc0, (Word16) 2 ) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -