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

📄 sp_frm.c

📁 GSM半数率源代码(VSELP) GSM半数率源代码(VSELP)
💻 C
📖 第 1 页 / 共 5 页
字号:
 *
 *   DESCRIPTION:
 *
 *
 *   REFERENCE:  Sub-clause 4.1.6 of GSM Recommendation 06.20
 *
 *   Keywords: openlooplagsearch, openloop, lag, pitch
 *
 **************************************************************************/



short  compResidEnergy(Shortword pswSpeech[],
                              Shortword ppswInterpCoef[][NP],
                              Shortword pswPreviousCoef[],
                              Shortword pswCurrentCoef[],
                              struct NormSw psnsSqrtRs[])
{

/*_________________________________________________________________________
 |                                                                         |
 |                            Automatic Variables                          |
 |_________________________________________________________________________|
*/

  short  i,
         j,
         siOverflowPossible,
         siInterpDecision;
  Shortword swMinShift,
         swShiftFactor,
         swSample,
        *pswCoef;
  Shortword pswTempState[NP];
  Shortword pswResidual[S_LEN];
  Longword L_ResidualEng;

/*_________________________________________________________________________
 |                                                                         |
 |                            Executable Code                              |
 |_________________________________________________________________________|
*/

  /* Find minimum shift count of the square-root of residual energy */
  /* estimates over the four subframes.  According to this minimum, */
  /* find a shift count for the residual signal which will be used  */
  /* to avoid overflow when the actual residual energies are        */
  /* calculated over the frame                                      */
  /*----------------------------------------------------------------*/

  swMinShift = SW_MAX;
  for (i = 0; i < N_SUB; i++)
  {

    if (sub(psnsSqrtRs[i].sh, swMinShift) < 0 && psnsSqrtRs[i].man > 0)
      swMinShift = psnsSqrtRs[i].sh;
  }

  if (sub(swMinShift, 1) >= 0)
  {

    siOverflowPossible = 0;
  }

  else if (swMinShift == 0)
  {
    siOverflowPossible = 1;
    swShiftFactor = ONE_HALF;
  }

  else if (sub(swMinShift, -1) == 0)
  {
    siOverflowPossible = 1;
    swShiftFactor = ONE_QUARTER;
  }

  else
  {
    siOverflowPossible = 1;
    swShiftFactor = ONE_EIGHTH;
  }

  /* Copy analysis filter state into temporary buffer */
  /*--------------------------------------------------*/

  for (i = 0; i < NP; i++)
    pswTempState[i] = pswAnalysisState[i];

  /* Send the speech frame, one subframe at a time, through the analysis */
  /* filter which is based on interpolated coefficients.  After each     */
  /* subframe, accumulate the energy in the residual signal, scaling to  */
  /* avoid overflow if necessary.                                        */
  /*---------------------------------------------------------------------*/

  L_ResidualEng = 0;

  for (i = 0; i < N_SUB; i++)
  {

    lpcFir(&pswSpeech[i * S_LEN], ppswInterpCoef[i], pswTempState,
           pswResidual);

    if (siOverflowPossible)
    {

      for (j = 0; j < S_LEN; j++)
      {

        swSample = mult_r(swShiftFactor, pswResidual[j]);
        L_ResidualEng = L_mac(L_ResidualEng, swSample, swSample);
      }
    }

    else
    {

      for (j = 0; j < S_LEN; j++)
      {

        L_ResidualEng = L_mac(L_ResidualEng, pswResidual[j], pswResidual[j]);
      }
    }
  }

  /* Send the speech frame, one subframe at a time, through the analysis */
  /* filter which is based on un-interpolated coefficients.  After each  */
  /* subframe, subtract the energy in the residual signal from the       */
  /* accumulated residual energy due to the interpolated coefficient     */
  /* analysis filter, again scaling to avoid overflow if necessary.      */
  /* Note that the analysis filter state is updated during these         */
  /* filtering operations.                                               */
  /*---------------------------------------------------------------------*/

  for (i = 0; i < N_SUB; i++)
  {

    switch (i)
    {

      case 0:

        pswCoef = pswPreviousCoef;
        break;

      case 1:
      case 2:
      case 3:

        pswCoef = pswCurrentCoef;
        break;
    }

    lpcFir(&pswSpeech[i * S_LEN], pswCoef, pswAnalysisState,
           pswResidual);

    if (siOverflowPossible)
    {

      for (j = 0; j < S_LEN; j++)
      {

        swSample = mult_r(swShiftFactor, pswResidual[j]);
        L_ResidualEng = L_msu(L_ResidualEng, swSample, swSample);
      }
    }

    else
    {

      for (j = 0; j < S_LEN; j++)
      {

        L_ResidualEng = L_msu(L_ResidualEng, pswResidual[j], pswResidual[j]);
      }
    }
  }

  /* Make soft-interpolation decision based on the difference in residual */
  /* energies                                                             */
  /*----------------------------------------------------------------------*/

  if (L_ResidualEng < 0)
    siInterpDecision = 1;

  else
    siInterpDecision = 0;

  return siInterpDecision;
}

/***************************************************************************
 *
 *    FUNCTION NAME: cov32
 *
 *    PURPOSE: Calculates B, F, and C correlation matrices from which
 *             the reflection coefficients are computed using the FLAT
 *             algorithm. The Spectral Smoothing Technique (SST) is applied
 *             to the correlations. End point correction is employed
 *             in computing the correlations to minimize computation.
 *
 *    INPUT:
 *
 *       pswIn[0:169]
 *                     A sampled speech vector used to compute
 *                     correlations need for generating the optimal
 *                     reflection coefficients via the FLAT algorithm.
 *
 *       CVSHIFT       The number of right shifts by which the normalized
 *                     correlations are to be shifted down prior to being
 *                     rounded into the Shortword output correlation arrays
 *                     B, F, and C.
 *
 *       pL_rFlatSstCoefs[NP]
 *
 *                     A table stored in Rom containing the spectral
 *                     smoothing function coefficients.
 *
 *    OUTPUTS:
 *
 *       pppL_B[0:NP-1][0:NP-1][0:1]
 *                     An output correlation array containing the backward
 *                     correlations of the input signal. It is a square
 *                     matrix symmetric about the diagonal. Only the upper
 *                     right hand triangular region of this matrix is
 *                     initialized, but two dimensional indexing is retained
 *                     to enhance clarity. The third array dimension is used
 *                     by function flat to swap the current and the past
 *                     values of B array, eliminating the need to copy
 *                     the updated B values onto the old B values at the
 *                     end of a given lattice stage. The third dimension
 *                     is similarily employed in arrays F and C.
 *
 *       pppL_F[0:NP-1][0:NP-1][0:1]
 *                     An output correlation array containing the forward
 *                     correlations of the input signal. It is a square
 *                     matrix symmetric about the diagonal. Only the upper
 *                     right hand triangular region of this matrix is
 *                     initialized.
 *
 *       pppL_C[0:NP-1][0:NP-1][0:1]
 *                     An output correlation array containing the cross
 *                     correlations of the input signal. It is a square
 *                     matrix which is not symmetric. All its elements
 *                     are initialized, for the third dimension index = 0.
 *
 *       pL_R0         Average normalized signal power over F_LEN
 *                     samples, given by 0.5*(Phi(0,0)+Phi(NP,NP)), where
 *                     Phi(0,0) and Phi(NP,NP) are normalized signal
 *                     autocorrelations.  The average unnormalized signal
 *                     power over the frame is given by adjusting L_R0 by
 *                     the shift count which is returned. pL_R0 along
 *                     with the returned shift count are the inputs to
 *                     the frame energy quantizer.
 *
 *        Longword pL_VadAcf[4]
 *                     An array with the autocorrelation coefficients to be
 *                     used by the VAD.
 *
 *        Shortword *pswVadScalAuto
 *                     Input scaling factor used by the VAD.
 *
 *    RETURN:
 *
 *       swNormPwr     The shift count to be applied to pL_R0 for
 *                     reconstructing the average unnormalized
 *                     signal power over the frame.
 *                     Negative shift count means that a left shift was
 *                     applied to the correlations to achieve a normalized
 *                     value of pL_R0.
 *
 *   DESCRIPTION:
 *
 *
 *      The input energy of the signal is assumed unknown.  It maximum
 *      can be F_LEN*0.5. The 0.5 factor accounts for scaling down of the
 *      input signal in the high-pass filter.  Therefore the signal is
 *      shifted down by 3 shifts producing an energy reduction of 2^(2*3)=64.
 *      The resulting energy is then normalized.  Based on the shift count,
 *      the correlations F, B, and C are computed using as few shifts as
 *      possible, so a precise result is attained.
 *      This is an implementation of equations: 2.1 through 2.11.
 *
 *   REFERENCE:  Sub-clause 4.1.3 of GSM Recommendation 06.20
 *
 *   keywords: energy, autocorrelation, correlation, cross-correlation
 *   keywords: spectral smoothing, SST, LPC, FLAT, flat
 *
 *************************************************************************/

Shortword cov32(Shortword pswIn[],
                       Longword pppL_B[NP][NP][2],
                       Longword pppL_F[NP][NP][2],
                       Longword pppL_C[NP][NP][2],
                       Longword *pL_R0,
                       Longword pL_VadAcf[],
                       Shortword *pswVadScalAuto)
{

/*_________________________________________________________________________
 |                                                                         |
 |                            Automatic Variables                          |
 |_________________________________________________________________________|
*/

  Longword L_max,
         L_Pwr0,
         L_Pwr,
         L_temp,
         pL_Phi[NP + 1];
  Shortword swTemp,
         swNorm,
         swNormSig,
         swNormPwr,
         pswInScale[A_LEN],
         swPhiNorm;
  short int i,
         k,
         kk,
         n;

/*_________________________________________________________________________
 |                                                                         |
 |                              Executable Code                            |
 |_________________________________________________________________________|
*/

  /* Calculate energy in the frame vector (160 samples) for each   */
  /* of NP frame placements. The energy is reduced by 64. This is  */
  /* accomplished by shifting the input right by 3 bits. An offset */
  /* of 0x117f0b is placed into the accumulator to account for     */
  /* the worst case power gain due to the 3 LSB's of the input     */
  /* signal which were right shifted. The worst case is that the   */
  /* 3 LSB's were all set to 1 for each of the samples. Scaling of */
  /* the input by a half is assumed here.                          */
  /*---------------------------------------------------------------*/

  L_max = 0;
  for (L_Pwr = 0x117f0b, i = 0; i < F_LEN; i++)
  {
    swTemp = shr(pswIn[i], 3);
    L_Pwr = L_mac(L_Pwr, swTemp, swTemp);
  }
  L_max |= L_Pwr;

  /* L_max tracks the maximum power over NP window placements */
  /*----------------------------------------------------------*/

  for (i = 1; i <= NP; i++)
  {

    /* Subtract the power due to 1-st sample from previous window
     * placement. */
    /*-----------------------------------------------------------*/

    swTemp = shr(pswIn[i - 1], 3);
    L_Pwr = L_msu(L_Pwr, swTemp, swTemp);

    /* Add the power due to new sample at the current window placement. */
    /*------------------------------------------------------------------*/

    swTemp = shr(pswIn[F_LEN + i - 1], 3);
    L_Pwr = L_mac(L_Pwr, swTemp, swTemp);

    L_max |= L_Pwr;

  }

  /* Compute the shift count needed to achieve normalized value */
  /* of

⌨️ 快捷键说明

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