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

📄 pitchcp.c

📁 语音压缩编码中的g729p编码程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
   ITU-T G.729 Annex C+ - Reference C code for floating point
                         implementation of G.729 Annex C+
                         (integration of Annexes B, D and E)
                          Version 2.1 of October 1999
*/

/*
 File : PITCHCP.C
*/

/*****************************************************************************/
/*           Long Term Prediction Routines                                   */
/*****************************************************************************/
#include <math.h>
#include "typedef.h"
#include "ld8k.h"
#include "tab_ld8k.h"
#include "ld8cp.h"

/* prototypes for local functions */
static void  norm_corr(FLOAT exc[], FLOAT xn[], FLOAT h[], int l_subfr,
    int t_min, int t_max, FLOAT corr_norm[]);
static FLOAT interpol_3(FLOAT cor[], int frac);
static int   lag_max(FLOAT signal[],int L_frame,int pit_max,
                  int pit_min, FLOAT *cor_max);
static FLOAT inv_sqrt( FLOAT x  );

/*----------------------------------------------------------------------------
 * pitch_ol -  compute the open loop pitch lag
 *----------------------------------------------------------------------------
 */
int pitch_ol(           /* output: open-loop pitch lag */
    FLOAT signal[],        /* input : signal to compute pitch  */
                           /*         s[-PIT_MAX : l_frame-1]  */
    int pit_min,         /* input : minimum pitch lag                          */
    int pit_max,         /* input : maximum pitch lag                          */
    int l_frame          /* input : error minimization window */
)
{
    FLOAT  max1, max2, max3;
    int    p_max1, p_max2, p_max3;
    
    /*--------------------------------------------------------------------*
    *  The pitch lag search is divided in three sections.                *
    *  Each section cannot have a pitch multiple.                        *
    *  We find a maximum for each section.                               *
    *  We compare the maxima of each section by favoring small lag.      *
    *                                                                    *
    *  First section:  lag delay = PIT_MAX to 80                         *
    *  Second section: lag delay = 79 to 40                              *
    *  Third section:  lag delay = 39 to 20                              *
    *--------------------------------------------------------------------*/
    
    p_max1 = lag_max(signal, l_frame, pit_max, 80 , &max1);
    p_max2 = lag_max(signal, l_frame, 79     , 40 , &max2);
    p_max3 = lag_max(signal, l_frame, 39     , pit_min , &max3);
    
    /*--------------------------------------------------------------------*
    * Compare the 3 sections maxima, and favor small lag.                *
    *--------------------------------------------------------------------*/
    
    if ( max1 * THRESHPIT < max2 ) {
        max1 = max2;
        p_max1 = p_max2;
    }
    
    if ( max1 * THRESHPIT < max3 )  p_max1 = p_max3;
    
    return (p_max1);
}
/*----------------------------------------------------------------------------
* lag_max - Find the lag that has maximum correlation
*----------------------------------------------------------------------------
*/
static int lag_max(     /* output: lag found */
    FLOAT signal[],       /* input : Signal to compute the open loop pitch
    signal[-142:-1] should be known.       */
    int l_frame,          /* input : Length of frame to compute pitch       */
    int lagmax,           /* input : maximum lag                            */
    int lagmin,           /* input : minimum lag                            */
    FLOAT *cor_max        /* input : normalized correlation of selected lag */
)
{
    int    i, j;
    FLOAT  *p, *p1;
    FLOAT  max, t0;
    int    p_max;
    
    max = FLT_MIN_G729;
    p_max = lagmax; /* to avoid visual warning */
    
    for (i = lagmax; i >= lagmin; i--) {
        p  = signal;
        p1 = &signal[-i];
        t0 = (F)0.0;
        
        for (j=0; j<l_frame; j++) {
            t0 += *p++ * *p1++;
        }
        
        if (t0 >= max) {
            max    = t0;
            p_max = i;
        }
    }
    
    /* compute energy */
    
    t0 = (F)0.01;                  /* to avoid division by zero */
    p = &signal[-p_max];
    for(i=0; i<l_frame; i++, p++) {
        t0 += *p * *p;
    }
    t0 = inv_sqrt(t0);          /* 1/sqrt(energy)    */
    
    *cor_max = max * t0;        /* max/sqrt(energy)  */
    
    return(p_max);
}


/*----------------------------------------------------------------------------
* pitch_fr3cp - find the pitch period  with 1/3 subsample resolution
*----------------------------------------------------------------------------
*/
int pitch_fr3cp(        /* output: integer part of pitch period        */
    FLOAT exc[],            /* input : excitation buffer                   */
    FLOAT xn[],             /* input : target vector                       */
    FLOAT h[],              /* input : impulse response of filters.        */
    int l_subfr,           /* input : Length of frame to compute pitch    */
    int t0_min,            /* input : minimum value in the searched range */
    int t0_max,            /* input : maximum value in the searched range */
    int i_subfr,           /* input : indicator for first subframe        */
    int *pit_frac,         /* output: chosen fraction                     */
    int rate
)
{
    int    i, frac;
    int    lag, t_min, t_max;
    FLOAT  max;
    FLOAT  corr_int;
    FLOAT  corr_v[10+2*L_INTER4];  /* size: 2*L_INTER4+t0_max-t0_min+1 */
    FLOAT  *corr;
    
    int midLag;
    
    /* Find interval to compute normalized correlation */
    t_min = t0_min - L_INTER4;
    t_max = t0_max + L_INTER4;
    
    corr = &corr_v[-t_min];    /* corr[t_min:t_max] */
    
    /* Compute normalized correlation between target and filtered excitation */
    norm_corr(exc, xn, h, l_subfr, t_min, t_max, corr);
    
    /* find integer pitch */
    max = corr[t0_min];
    lag  = t0_min;
    
    for(i= t0_min+1; i<=t0_max; i++)
    {
        if( corr[i] >= max)
        {
            max = corr[i];
            lag = i;
        }
    }
    
    /* If first subframe and lag > 84 do not search fractionnal pitch */
    
    if( (i_subfr == 0) && (lag > 84) )
    {
        *pit_frac = 0;
        return(lag);
    }
    
    /* test the fractions around lag and choose the one which maximizes
    the interpolated normalized correlation */
    
    if (rate == G729D) {    /* 6.4 kbps */
        if (i_subfr == 0) {
            max  = interpol_3(&corr[lag], -2);
            frac = -2;
            
            for (i = -1; i <= 2; i++) {
                corr_int = interpol_3(&corr[lag], i);
                if(corr_int > max) {
                    max = corr_int;
                    frac = i;
                }
            }
        }
        else {
            midLag = t0_max - 4;
            if ((lag == midLag - 1) || lag == midLag) {
                max  = interpol_3(&corr[lag], -2);
                frac = -2;
                
                for (i = -1; i <= 2; i++) {
                    corr_int = interpol_3(&corr[lag], i);
                    if(corr_int > max) {
                        max = corr_int;
                        frac = i;
                    }
                }
            }
            else if (lag == midLag - 2) {
                max  = interpol_3(&corr[lag], 0);
                frac = 0;
                
                for (i = 1; i <= 2; i++) {
                    corr_int = interpol_3(&corr[lag], i);
                    if(corr_int > max) {
                        max = corr_int;
                        frac = i;
                    }
                }
            }      
            else if (lag == midLag + 1) {
                max  = interpol_3(&corr[lag], -2);
                frac = -2;
                
                for (i = -1; i <= 0; i++) {
                    corr_int = interpol_3(&corr[lag], i);
                    if(corr_int > max) {
                        max = corr_int;
                        frac = i;
                    }
                }
            }
            else
                frac = 0;
        }
        
    }
    else {
        max  = interpol_3(&corr[lag], -2);
        frac = -2;

⌨️ 快捷键说明

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