⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exc2.cpp

📁 G711语音压缩源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
*/


/*
**
**  Function:  G_code()
**
**  Description: Compute the gain of innovative code.
**
**
**  Links to the text: Section 2.16
**
** Input arguments:
**
**      float  X[]        Code target.  (in Q0)
**      float  Y[]        Filtered innovation code. (in Q12)
**
** Output:
**
**      float  *gain_q    Gain of innovation code.  (in Q0)
**
**  Return value:
**
**      int     index of innovation code gain
**
*/
int G_code(float X[], float Y[], float *gain_q)
{
    int     i;
    float   xy, yy, gain_nq;
    int     gain;
    float   dist, dist_min;

    /*  Compute scalar product <X[],Y[]> */

//    xy = DotProd(X,Y,SubFrLen);
    xy = DotProd10(X,Y,SubFrLen);

    if (xy <= 0)
    {
        gain = 0;
        *gain_q =FcbkGainTable[gain];
        return(gain);
    }

    /*  Compute scalar product <Y[],Y[]>  */

//    yy = DotProd(Y,Y,SubFrLen);
    yy = DotProd10(Y,Y,SubFrLen);

    if (yy > (float) FLT_MIN)
        gain_nq = xy/yy;
    else
        gain_nq = (float)0.0;

    gain = 0;
    dist_min = (float)fabs(gain_nq - FcbkGainTable[0]);

    for (i=1; i <NumOfGainLev; i++)
    {
        dist = (float)fabs(gain_nq - FcbkGainTable[i]);
        if (dist < dist_min)
        {
            dist_min = dist;
            gain = i;
        }
    }
    *gain_q = FcbkGainTable[gain];
    return(gain);
}


/*
**
**  Function:       search_T0()
**
**  Description:          Gets parameters of pitch synchronous filter
**
**  Links to the text:    Section 2.16
**
**  Arguments:
**
**      int    T0         Decoded pitch lag
**      int    Gid        Gain vector index in adaptive gain vector codebook
**      float  *gain_T0   Pitch synchronous gain
**
**  Outputs:
**
**      float  *gain_T0   Pitch synchronous filter gain
**
**  Return Value:
**
**      int    T0_mod     Pitch synchronous filter lag
*/
int search_T0 (int T0, int Gid, float *gain_T0)
{
    int T0_mod;

    T0_mod = T0+epsi170[Gid];
    *gain_T0 = gain170[Gid];

    return(T0_mod);
}


/*
**
** Function:        Get_Rez()
**
** Description:     Gets delayed contribution from the previous excitation
**                  vector.
**
** Links to text:   Sections 2.14, 2.18 & 3.4
**
** Arguments:
**
**  float  *Tv      delayed excitation
**  float  *PrevExc Previous excitation vector
**  int    Lag      Closed loop pitch lag
**
** Outputs:
**
**  float  *Tv      delayed excitation
**
** Return value:    None
**
*/
void  Get_Rez(float *Tv, float *PrevExc, int Lag)
{
    int i;

    for (i=0; i < ClPitchOrd/2; i++)
        Tv[i] = PrevExc[PitchMax - Lag - ClPitchOrd/2 + i];

    for (i=0; i < SubFrLen+ClPitchOrd/2; i++)
        Tv[ClPitchOrd/2+i] = PrevExc[PitchMax - Lag + i%Lag];
}


/*
**
** Function:        Find_B()
**
** Description:     Computes best pitch postfilter backward lag by
**                  backward cross correlation maximization around the
**                  decoded pitch lag
**                  of the subframe 0 (for subrames 0 & 1)
**                  of the subframe 2 (for subrames 2 & 3)
**
** Links to text:   Section 3.6
**
** Arguments:
**
**  float  *Buff    decoded excitation
**  int    Olp      Decoded pitch lag
**  int    Sfc      Subframe index
**
** Outputs:     None
**
** Return value:
**
**  short   Pitch postfilter backward lag
*/
int  Find_B(float *Buff, int Olp, int Sfc)
{
    int  i;

    int  Indx = 0;

    float  Acc0,Acc1;

    if (Olp > (PitchMax-3))
        Olp = (PitchMax-3);

    Acc1 = (float)0.0;

    for (i=Olp-3; i<=Olp+3; i++)
    {
//        Acc0 = DotProd(&Buff[PitchMax+Sfc*SubFrLen],
//                    &Buff[PitchMax+Sfc*SubFrLen-i],SubFrLen);
        Acc0 = DotProd10(&Buff[PitchMax+Sfc*SubFrLen],
                    &Buff[PitchMax+Sfc*SubFrLen-i],SubFrLen);

        /* return index of largest cross correlation */

        if (Acc0 > Acc1)
        {
            Acc1 = Acc0;
            Indx = i;
        }
    }
    return -Indx;
}


/*
**
** Function:        Find_F()
**
** Description:     Computes best pitch postfilter forward lag by
**                  forward cross correlation maximization around the
**                  decoded pitch lag
**                  of the subframe 0 (for subrames 0 & 1)
**                  of the subframe 2 (for subrames 2 & 3)
**
** Links to text:   Section 3.6
**
** Arguments:
**
**  float  *Buff    decoded excitation
**  int    Olp      Decoded pitch lag
**  int    Sfc      Subframe index
**
** Outputs:     None
**
** Return value:
**
**  int       Pitch postfilter forward lag
*/
int  Find_F(float *Buff, int Olp, int Sfc)
{
    int    i;
    int    Indx = 0;
    float  Acc0,Acc1;

    if (Olp > (PitchMax-3))
        Olp = (PitchMax-3);

    Acc1 = (float)0.0;

    for (i=Olp-3; i<=Olp+3; i++)
    {
        if (!((Sfc*SubFrLen+SubFrLen+i) > Frame))
        {
//            Acc0 = DotProd(&Buff[PitchMax+Sfc*SubFrLen],
//                            &Buff[PitchMax+Sfc*SubFrLen+i],SubFrLen);
            Acc0 = DotProd10(&Buff[PitchMax+Sfc*SubFrLen],
                            &Buff[PitchMax+Sfc*SubFrLen+i],SubFrLen);

            /* return index of largest cross correlation */

            if (Acc0 > Acc1)
            {
                Acc1 = Acc0;
                Indx = i;
            }
        }
    }

    return Indx;
}

/*
**
** Function:        Filt_Lpf()
**
** Description:     Applies the pitch postfilter for each subframe.
**
** Links to text:   Section 3.6
**
** Arguments:
**
**  float  *Tv      Pitch postfiltered excitation
**  float  *Buff    decoded excitation
**  PFDEF Pf        Pitch postfilter parameters
**  int    Sfc      Subframe index
**
** Outputs:
**
**  float  *Tv      Pitch postfiltered excitation
**
** Return value: None
**
*/
void  Filt_Lpf(float *Tv, float *Buff, PFDEF Pf, int Sfc)
{
    int  i;

    for (i = 0; i < SubFrLen; i++)
        Tv[Sfc*SubFrLen+i]= Buff[PitchMax+Sfc*SubFrLen+i]*Pf.ScGn +
                            Buff[PitchMax+Sfc*SubFrLen+Pf.Indx+i]*Pf.Gain;
}



/*
**
** Function:        Comp_Info()
**
** Description:     Voiced/unvoiced classifier.
**                  It is based on a cross correlation maximization over the
**                  last 120 samples of the frame and with an index varying
**                  around the decoded pitch lag (from L-3 to L+3). Then the
**                  prediction gain is tested to declare the frame voiced or
**                  unvoiced.
**
** Links to text:   Section 3.10.2
**
** Arguments:
**
**  float  *Buff  decoded excitation
**  int    Olp    Decoded pitch lag
**  float  *Gain
**
** Outputs: None
**
** Return value:
**
**      short    Estimated pitch value
*/
short  Comp_Info(float *Buff, int Olp, float *Gain)
{
    int    i;
    float  Acc0;
    float  Tenr;
    float  Ccr,Enr;
    int    Indx;

    if (Olp > (PitchMax-3))
        Olp = (PitchMax-3);

    Indx = Olp;
    Ccr =  (float)0.0;

    for (i=Olp-3; i <= Olp+3; i++)
    {
//        Acc0 = DotProd(&Buff[PitchMax+Frame-2*SubFrLen],
//                        &Buff[PitchMax+Frame-2*SubFrLen-i],2*SubFrLen);
        Acc0 = DotProd10(&Buff[PitchMax+Frame-2*SubFrLen],
                        &Buff[PitchMax+Frame-2*SubFrLen-i],2*SubFrLen);

        if (Acc0 > Ccr)
        {
            Ccr = Acc0;
            Indx = i;
        }
    }

    /*  Compute target energy  */

//    Tenr = DotProd(&Buff[PitchMax+Frame-2*SubFrLen],
//                    &Buff[PitchMax+Frame-2*SubFrLen],2*SubFrLen);
    Tenr = DotProd10(&Buff[PitchMax+Frame-2*SubFrLen],
                    &Buff[PitchMax+Frame-2*SubFrLen],2*SubFrLen);
    *Gain = Tenr;

    /*  Compute best energy */

//    Enr = DotProd(&Buff[PitchMax+Frame-2*SubFrLen-Indx],
//                    &Buff[PitchMax+Frame-2*SubFrLen-Indx],2*SubFrLen);
    Enr = DotProd10(&Buff[PitchMax+Frame-2*SubFrLen-Indx],
                    &Buff[PitchMax+Frame-2*SubFrLen-Indx],2*SubFrLen);

    if (Ccr <= (float)0.0)
        return 0;

    if (((((float)0.125)*Enr*Tenr) - (Ccr*Ccr)) < (float)0.0)
        return (short) Indx;

    return 0;
}


/*
**
** Function:        Regen()
**
** Description:     Performs residual interpolation depending of the frame
**                  classification.
**                  If the frame is previously declared unvoiced, the excitation
**                  is regenerated using a random number generator. Otherwise
**                  a periodic excitation is generated with the period previously
**                  found.
**
** Links to text:   Section 3.10.2
**
** Arguments:
**
**  float  *DataBuff current subframe decoded excitation
**  float  *Buff     past decoded excitation
**  short Lag       Decoded pitch lag from previous frame
**  float  Gain      Interpolated gain from previous frames
**  int    Ecount    Number of erased frames
**  short *Sd       Random number used in unvoiced cases
**
** Outputs:
**
**  short *DataBuff current subframe decoded excitation
**  short *Buff     updated past excitation
**
** Return value:    None
**
*/
void    Regen(float *DataBuff, float *Buff, short Lag, float Gain,
              int Ecount, short *Sd)
{
    int  i;

    /*  Test for clearing */

    if (Ecount >= ErrMaxNum)
    {
        for (i = 0; i < Frame; i++)
            DataBuff[i] = (float)0.0;
        for (i = 0; i < Frame+PitchMax; i++)
            Buff[i] = (float)0.0;
    }
    else
    {

        /*  Interpolate accordingly to the voicing estimation */

        if (Lag != 0)
        {
            /*  Voiced case */
            for (i = 0; i < Frame; i++)
                Buff[PitchMax+i] = Buff[PitchMax-Lag+i];
            for (i = 0; i < Frame; i++)
                DataBuff[i] = Buff[PitchMax+i] = Buff[PitchMax+i] * (float)0.75;
        }
        else
        {

            /* Unvoiced case */

            for (i = 0; i < Frame; i++)
                DataBuff[i] = Gain*(float)Rand_lbc(Sd)*((float)1.0/(float)32768.0);

            /* Clear buffer to reset memory */

            for (i = 0; i < Frame+PitchMax; i++)
                Buff[i] = (float)0.0;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -