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

📄 adapt.c

📁 手机加密通话软件
💻 C
📖 第 1 页 / 共 2 页
字号:
int     MinDelayPtr,
int     MaxDelayPtr,
int     *BestDelayPtr)
{
        int     i;
        fxpt_32 emax;

        *BestDelayPtr = MinDelayPtr;

        emax = match[*BestDelayPtr];

        for (i=MinDelayPtr; i<=MaxDelayPtr; i++) {
                if (match[i] > emax) {
                        *BestDelayPtr = i;
                        emax = match[*BestDelayPtr];
                }
        }
}

/**************************************************************************
*                                                                         *
* ROUTINE
*               SelectDelay
*
* FUNCTION
*               Select shortest delay of 2, 3, or 4 submultiples
*               if its match is within 1 dB of MSPE to favor
                smooth "pitch"
* SYNOPSIS
*               SelectDelay(BestDelayPtr, SubMult, match,
*                             MinDelayPtr, MaxDelayPtr)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       BestDelayPtr    int     i/o     Pointer to delay with top match score
*       SubMult         int      i      Sumbmultiples array
*       match           fxpt_32  i      Match score array for each delay
*       MinDelayPtr     int      i      Pointer to minimum delay to search
*       MaxDelayPtr     int      i      Pointer to maximum delay to search
*
**************************************************************************/
void SelectDelay(
int     *BestDelayPtr,
int     SubMult[256][4],
fxpt_32 match[MAX_NUM_ADAPT],           /* 30.1 format */
int     MinDelayPtr,
int     MaxDelayPtr)
{
        int     i;
        int     SubMultPtr, BigPtr;
        int     Best;

FXPT_PUSHSTATE("SelectDelay", -1.0, -1.0);
        /* Initialize */
        Best = *BestDelayPtr;

        /* For each submultiple (2, 3, & 4) */
        for (i=1; i<=SubMult[*BestDelayPtr][0]; i++) {

                /* Find best neighborhood match for given submultiple */
                BigPtr = SubMult[*BestDelayPtr][i];
                for (SubMultPtr = max(SubMult[*BestDelayPtr][i]-8, MinDelayPtr);
                    SubMultPtr <= min(SubMult[*BestDelayPtr][i]+8, MaxDelayPtr);
                    SubMultPtr++) {
                        if (match[SubMultPtr] > match[BigPtr])
                                BigPtr = SubMultPtr;
                }

                /* Select submultiple match if within 1 dB MSPE match */
                /*if (match[BigPtr] >= (0.88*match[*BestDelayPtr]))*/
                if (match[BigPtr] >=
                    fxpt_mult64_fix(28836, match[*BestDelayPtr], 15))
                        Best = BigPtr;
        }

        *BestDelayPtr = Best;
FXPT_POPSTATE();
}

/**************************************************************************
*                                                                         *
* ROUTINE
*               Neighborhood
*
* FUNCTION
*               Search BestDelayPtr's neighboring delays (to be used with
*               earlier stages of searching).  Find gain and match
*               score for neighboring delays and find best neighborhood
*               match (could use end-point correction on unity spaced delays)
* SYNOPSIS
*               Neighborhood(lp_imp, residual, match, MinDelayPtr, MaxDelayPtr
                                DelayTable, AdaptCB, BestDelayPtr, gain)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       lp_imp          fxpt_16  i      LP impulse response
*       residual        fxpt_16  i      Residual from LPC analysis
*       match           fxpt_32  i      Adaptive Codebook match score array
*       MinDelayPtr     int      i      Pointer to minimum delay to search
*       MaxDelayPtr     int      i      Pointer to maximum delay to search
*       DelayTable      fxpt_16  i      Table of integer and fractional delays
*       AdaptCB         fxpt_16  i      Adaptive codebook
*       BestDelayPtr    int      o      Pointer to best delay
*       gain            fxpt_16  o      Adaptive Codebook gain array
*
**************************************************************************/
void Neighborhood(
fxpt_16 lp_imp[SF_LEN],                 /* 15.0 format */
fxpt_16 residual[RES_LEN],              /* 2.13 format */
fxpt_32 match[MAX_NUM_ADAPT],           /* 30.1 format */
int     MinDelayPtr,
int     MaxDelayPtr,
fxpt_16 DelayTable[],                   /* 8.7 format */
fxpt_16 AdaptCB[MAX_ABUF_LEN],          /* 15.0 format */
int     *BestDelayPtr,
fxpt_32 gain[MAX_NUM_ADAPT])            /* 17.14 format */
{
        int     i;
        int     BigPtr;
        fxpt_16 IntDelay;
        fxpt_16 FracDelay;                      /* 8.7 format */
        fxpt_16 AdaptCBShift[MAX_A_LEN];        /* 15.0 format */

FXPT_PUSHSTATE("Neighborhood", -1.0, -1.0);
        BigPtr = *BestDelayPtr;

        for (i=max(*BestDelayPtr - NeighborhoodRange, MinDelayPtr);
            i<=min(*BestDelayPtr + NeighborhoodRange, MaxDelayPtr); i++) {
                if (i != *BestDelayPtr) {
                        IntDelay = fxpt_shr16_fast(DelayTable[i], 7);
                        FracDelay = fxpt_sub16(DelayTable[i],
                            fxpt_shl16_fast(IntDelay, 7));
                        if (FracDelay > 0) {
                                delay_short(&AdaptCB[START-1], FracDelay,
                                    IntDelay, &AdaptCBShift[0]);
                                CalcACBParms(AdaptCBShift, lp_imp,
                                    TRUE, 69, residual, &match[i],
                                    &gain[i]);
                        }
                        if (match[i] > match[*BestDelayPtr])
                                BigPtr = i;
                }
        }

        *BestDelayPtr = BigPtr;
FXPT_POPSTATE();
}

/**************************************************************************
*
* ROUTINE
*               CalcIntAdaptParms
*
* FUNCTION
*               Calculate gain and match score for integer adaptive delays
* SYNOPSIS
*               CalcIntAdaptParms(AdaptCB, pc_imp, MinDelayPtr, MaxDelayPtr,
*                   DelayTable, residual, gain, match)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       AdaptCB         fxpt_16  i      Adaptive Codebook
*       pc_imp          fxpt_16  i      Impulse response of PCs
*       MinDelayPtr     int      i      Pointer to minimum delay to search
*       MaxDelayPtr     int      i      Pointer to maximum delay to search
*       DelayTable      fxpt_16  i      Adaptive delay coding table
*       residual        fxpt_16  i      Residual from LPC analysis
*       gain            fxpt_32  o      Gain array
*       match           fxpt_32  o      Match score array
*
**************************************************************************
*
*       Uses end-point correction on unity spaced delays
*
**************************************************************************/

void CalcIntAdaptParms(
fxpt_16 AdaptCB[MAX_ABUF_LEN],          /* 15.0 format */
fxpt_16 pc_imp[SF_LEN],                 /* 2.13 format */
int     MinDelayPtr,
int     MaxDelayPtr,
fxpt_16 DelayTable[MAX_NUM_ADAPT],      /* 8.7 format */
fxpt_16 residual[RES_LEN],              /* 15.0 format */
fxpt_32 gain[MAX_NUM_ADAPT],            /* 17.14 format */
fxpt_32 match[MAX_NUM_ADAPT])           /* 30.1 format */
{
        int     first = TRUE;
        fxpt_16 IntDelay;
        fxpt_16 FracDelay;              /* 8.7 format */
        int     i;

FXPT_PUSHSTATE("CalcIntAdaptParms", -1.0, -1.0);
        for (i=MinDelayPtr; i<=MaxDelayPtr; i++) {
                IntDelay = (int) fxpt_shr16_fast(DelayTable[i], 7);

                /*FracDelay = DelayTable[i] - IntDelay;*/
                FracDelay = fxpt_sub16(DelayTable[i],
                    fxpt_shl16_fast(IntDelay, 7));
                if (FracDelay == 0) {
                        CalcACBParms(&AdaptCB[START - IntDelay-1],
                                     &pc_imp[0], first, IntDelay,
                                     &residual[0], &match[i], &gain[i]);
                        first = FALSE;
                }
                else
                        match[i] = 0;
        }
FXPT_POPSTATE();
}


/**************************************************************************
*                                                                         *
* ROUTINE
*               CalcFracAdaptParms
*
* FUNCTION
*               Calculate gain and match score for fractional adaptive delays
* SYNOPSIS
*               CalcFracAdaptParms(AdaptCB, pc_imp, MinDelayPtr, MaxDelayPtr,
*                       DelayTable, residual, gain, match)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       AdaptCB         fxpt_16  i      Adaptive Codebook
*       pc_imp          fxpt_16  i      Impulse response of PCs
*       MinDelayPtr     int      i      Minimum delay pointer to search
*       MaxDelayPtr     int      i      Maximum delay pointer to search
*       DelayTable      fxpt_16  i      Adaptive delay coding table
*       residual        fxpt_16  i      Residual from LPC analysis
*       gain            fxpt_32  o      Gain array
*       match           fxpt_32  o      Match score array
*
**************************************************************************
*
*       Uses end-point correction on unity spaced delays
*
**************************************************************************/
void CalcFracAdaptParms(
fxpt_16 AdaptCB[MAX_ABUF_LEN],                  /* 15.0 format */
fxpt_16 pc_imp[SF_LEN],                         /* 2.13 format */
int     MinDelayPtr,
int     MaxDelayPtr,
fxpt_16 DelayTable[MAX_NUM_ADAPT],              /* 8.7 format */
fxpt_16 residual[RES_LEN],                      /* 15.0 format */
fxpt_32 gain[MAX_NUM_ADAPT],                    /* 17.14 format */
fxpt_32 match[MAX_NUM_ADAPT])                   /* 30.1 format */
{
        fxpt_16 IntDelay;
        fxpt_16 FracDelay;                      /* 8.7 format */
        int     i;
        fxpt_16 AdaptCBShift[MAX_A_LEN];        /* 15.0 format */

FXPT_PUSHSTATE("CalcFracAdaptParms", -1.0, -1.0);
        for (i=MinDelayPtr; i<=MaxDelayPtr; i++) {
                IntDelay = fxpt_shr16_fast(DelayTable[i], 7);
                FracDelay = fxpt_sub16(DelayTable[i],
                    fxpt_shl16_fast(IntDelay, 7));
                if (FracDelay > 0) {
                        delay_short(&AdaptCB[START-1], FracDelay,
                            IntDelay, AdaptCBShift);
                        CalcACBParms(AdaptCBShift, pc_imp, 1, 69,
                                     residual, &match[i], &gain[i]);
                }
        }
FXPT_POPSTATE();
}

/**************************************************************************
*                                                                         *
* ROUTINE
*               FindAdaptResidual
*
* FUNCTION
*               Find residual after adaptive analysis
* SYNOPSIS
*               FindAdaptResidual(speech, AdaptCB, pc, AdaptGain, AdaptDelay,
*                       residual)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       speech          fxpt_16  i      Subframe of input speech
*       AdaptCB         fxpt_16  i      Adaptive Codebook vector
*       pc              fxpt_16  i      Subframe of interpolated predictor
*                                       coefficients
*       AdaptGain       fxpt_16  i      Adaptive codebook gain
*       AdaptDelay      fxpt_16  i      Adaptive codebook delay
*       residual        fxpt_16  o      Adaptive residual
*
**************************************************************************/

void FindAdaptResidual(
fxpt_16 speech[SF_LEN],                 /* 15.0 format */
fxpt_16 AdaptCB[MAX_ABUF_LEN],          /* 15.0 format */
fxpt_16 pc[ORDER+1],                    /* 2.13 format */
fxpt_16 AdaptGain,                      /* 1.14 format */
fxpt_16 AdaptDelay,                     /* 8.7 format */
fxpt_16 residual[RES_LEN])              /* 15.0 format */
{
        int     i;
        fxpt_16 pcexp[ORDER+1];         /* 2.13 format */

FXPT_PUSHSTATE("FindAdaptResidual", -1.0, -1.0);
        /* Initialize residual */
        ZeroArray16(residual, RES_LEN);

        /* Adaptive Codebook Synthesis */
        ConstructAdaptCW(residual, AdaptCB, AdaptGain, AdaptDelay);

        /* LP Filter */
        do_pfilt_dynamic(&Adapt_ResP, pc, residual);

        /* Calculate Residual */
        for (i=0; i<RES_LEN; i++) {
                residual[i] = fxpt_sub16(speech[i], residual[i]);
        }

        /* Perceptual Weighting of residual */
        BWExpand(GAMMA, pc, pcexp);
        do_zpfilt_dynamic(&Adapt_ResZ, &Adapt_ResP2, pc, pcexp, residual);
FXPT_POPSTATE();
}

⌨️ 快捷键说明

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