filters.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:15k
源码类别:

Windows CE

开发平台:

C/C++

  1. /* Copyright (C) 2002 Jean-Marc Valin 
  2.    File: filters.c
  3.    Various analysis/synthesis filters
  4.    Redistribution and use in source and binary forms, with or without
  5.    modification, are permitted provided that the following conditions
  6.    are met:
  7.    
  8.    - Redistributions of source code must retain the above copyright
  9.    notice, this list of conditions and the following disclaimer.
  10.    
  11.    - Redistributions in binary form must reproduce the above copyright
  12.    notice, this list of conditions and the following disclaimer in the
  13.    documentation and/or other materials provided with the distribution.
  14.    
  15.    - Neither the name of the Xiph.org Foundation nor the names of its
  16.    contributors may be used to endorse or promote products derived from
  17.    this software without specific prior written permission.
  18.    
  19.    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22.    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
  23.    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. #include "config.h"
  33. #endif
  34. #include "filters.h"
  35. #include "stack_alloc.h"
  36. #include "misc.h"
  37. #include "math_approx.h"
  38. #include "ltp.h"
  39. #include <math.h>
  40. #ifdef _USE_SSE
  41. #include "filters_sse.h"
  42. #elif defined (ARM4_ASM) || defined(ARM5E_ASM)
  43. #include "filters_arm4.h"
  44. #elif defined (BFIN_ASM)
  45. #include "filters_bfin.h"
  46. #endif
  47. void bw_lpc(spx_word16_t gamma, const spx_coef_t *lpc_in, spx_coef_t *lpc_out, int order)
  48. {
  49.    int i;
  50.    spx_word16_t tmp=gamma;
  51.    for (i=0;i<order;i++)
  52.    {
  53.       lpc_out[i] = MULT16_16_P15(tmp,lpc_in[i]);
  54.       tmp = MULT16_16_P15(tmp, gamma);
  55.    }
  56. }
  57. #ifdef FIXED_POINT
  58. /* FIXME: These functions are ugly and probably introduce too much error */
  59. void signal_mul(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
  60. {
  61.    int i;
  62.    for (i=0;i<len;i++)
  63.    {
  64.       y[i] = SHL32(MULT16_32_Q14(EXTRACT16(SHR32(x[i],7)),scale),7);
  65.    }
  66. }
  67. void signal_div(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
  68. {
  69.    int i;
  70.    if (scale > SHL32(EXTEND32(SIG_SCALING), 8))
  71.    {
  72.       spx_word16_t scale_1;
  73.       scale = PSHR32(scale, SIG_SHIFT);
  74.       scale_1 = EXTRACT16(DIV32_16(SHL32(EXTEND32(SIG_SCALING),7),scale));
  75.       for (i=0;i<len;i++)
  76.       {
  77.          y[i] = SHR32(MULT16_16(scale_1, EXTRACT16(SHR32(x[i],SIG_SHIFT))),7);
  78.       }
  79.    } else {
  80.       spx_word16_t scale_1;
  81.       scale = PSHR32(scale, SIG_SHIFT-5);
  82.       scale_1 = DIV32_16(SHL32(EXTEND32(SIG_SCALING),3),scale);
  83.       for (i=0;i<len;i++)
  84.       {
  85.          y[i] = MULT16_16(scale_1, EXTRACT16(SHR32(x[i],SIG_SHIFT-2)));
  86.       }
  87.    }
  88. }
  89. #else
  90. void signal_mul(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
  91. {
  92.    int i;
  93.    for (i=0;i<len;i++)
  94.       y[i] = scale*x[i];
  95. }
  96. void signal_div(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
  97. {
  98.    int i;
  99.    float scale_1 = 1/scale;
  100.    for (i=0;i<len;i++)
  101.       y[i] = scale_1*x[i];
  102. }
  103. #endif
  104. #ifdef FIXED_POINT
  105. spx_word16_t compute_rms(const spx_sig_t *x, int len)
  106. {
  107.    int i;
  108.    spx_word32_t sum=0;
  109.    spx_sig_t max_val=1;
  110.    int sig_shift;
  111.    for (i=0;i<len;i++)
  112.    {
  113.       spx_sig_t tmp = x[i];
  114.       if (tmp<0)
  115.          tmp = -tmp;
  116.       if (tmp > max_val)
  117.          max_val = tmp;
  118.    }
  119.    sig_shift=0;
  120.    while (max_val>16383)
  121.    {
  122.       sig_shift++;
  123.       max_val >>= 1;
  124.    }
  125.    for (i=0;i<len;i+=4)
  126.    {
  127.       spx_word32_t sum2=0;
  128.       spx_word16_t tmp;
  129.       tmp = EXTRACT16(SHR32(x[i],sig_shift));
  130.       sum2 = MAC16_16(sum2,tmp,tmp);
  131.       tmp = EXTRACT16(SHR32(x[i+1],sig_shift));
  132.       sum2 = MAC16_16(sum2,tmp,tmp);
  133.       tmp = EXTRACT16(SHR32(x[i+2],sig_shift));
  134.       sum2 = MAC16_16(sum2,tmp,tmp);
  135.       tmp = EXTRACT16(SHR32(x[i+3],sig_shift));
  136.       sum2 = MAC16_16(sum2,tmp,tmp);
  137.       sum = ADD32(sum,SHR32(sum2,6));
  138.    }
  139.    
  140.    return EXTRACT16(SHR32(SHL32(EXTEND32(spx_sqrt(1+DIV32(sum,len))),(sig_shift+3)),SIG_SHIFT));
  141. }
  142. #ifndef OVERRIDE_NORMALIZE16
  143. int normalize16(const spx_sig_t *x, spx_word16_t *y, spx_sig_t max_scale, int len)
  144. {
  145.    int i;
  146.    spx_sig_t max_val=1;
  147.    int sig_shift;
  148.    
  149.    for (i=0;i<len;i++)
  150.    {
  151.       spx_sig_t tmp = x[i];
  152.       if (tmp<0)
  153.          tmp = NEG32(tmp);
  154.       if (tmp >= max_val)
  155.          max_val = tmp;
  156.    }
  157.    sig_shift=0;
  158.    while (max_val>max_scale)
  159.    {
  160.       sig_shift++;
  161.       max_val >>= 1;
  162.    }
  163.    for (i=0;i<len;i++)
  164.       y[i] = EXTRACT16(SHR32(x[i], sig_shift));
  165.    
  166.    return sig_shift;
  167. }
  168. #endif
  169. #else
  170. spx_word16_t compute_rms(const spx_sig_t *x, int len)
  171. {
  172.    int i;
  173.    float sum=0;
  174.    for (i=0;i<len;i++)
  175.    {
  176.       sum += x[i]*x[i];
  177.    }
  178.    return sqrt(.1+sum/len);
  179. }
  180. #endif
  181. #ifndef OVERRIDE_FILTER_MEM2
  182. #ifdef PRECISION16
  183. void filter_mem2(const spx_sig_t *x, const spx_coef_t *num, const spx_coef_t *den, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
  184. {
  185.    int i,j;
  186.    spx_word16_t xi,yi,nyi;
  187.    for (i=0;i<N;i++)
  188.    {
  189.       xi= EXTRACT16(PSHR32(SATURATE(x[i],536870911),SIG_SHIFT));
  190.       yi = EXTRACT16(PSHR32(SATURATE(ADD32(x[i], SHL32(mem[0],1)),536870911),SIG_SHIFT));
  191.       nyi = NEG16(yi);
  192.       for (j=0;j<ord-1;j++)
  193.       {
  194.          mem[j] = MAC16_16(MAC16_16(mem[j+1], num[j],xi), den[j],nyi);
  195.       }
  196.       mem[ord-1] = ADD32(MULT16_16(num[ord-1],xi), MULT16_16(den[ord-1],nyi));
  197.       y[i] = SHL32(EXTEND32(yi),SIG_SHIFT);
  198.    }
  199. }
  200. #else
  201. void filter_mem2(const spx_sig_t *x, const spx_coef_t *num, const spx_coef_t *den, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
  202. {
  203.    int i,j;
  204.    spx_sig_t xi,yi,nyi;
  205.    for (i=0;i<N;i++)
  206.    {
  207.       xi=SATURATE(x[i],805306368);
  208.       yi = SATURATE(ADD32(xi, SHL32(mem[0],2)),805306368);
  209.       nyi = NEG32(yi);
  210.       for (j=0;j<ord-1;j++)
  211.       {
  212.          mem[j] = MAC16_32_Q15(MAC16_32_Q15(mem[j+1], num[j],xi), den[j],nyi);
  213.       }
  214.       mem[ord-1] = SUB32(MULT16_32_Q15(num[ord-1],xi), MULT16_32_Q15(den[ord-1],yi));
  215.       y[i] = yi;
  216.    }
  217. }
  218. #endif
  219. #endif
  220. #ifndef OVERRIDE_IIR_MEM2
  221. #ifdef PRECISION16
  222. void iir_mem2(const spx_sig_t *x, const spx_coef_t *den, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
  223. {
  224.    int i,j;
  225.    spx_word16_t yi,nyi;
  226.    for (i=0;i<N;i++)
  227.    {
  228.       yi = EXTRACT16(PSHR32(SATURATE(x[i] + SHL32(mem[0],1),536870911),SIG_SHIFT));
  229.       nyi = NEG16(yi);
  230.       for (j=0;j<ord-1;j++)
  231.       {
  232.          mem[j] = MAC16_16(mem[j+1],den[j],nyi);
  233.       }
  234.       mem[ord-1] = MULT16_16(den[ord-1],nyi);
  235.       y[i] = SHL32(EXTEND32(yi),SIG_SHIFT);
  236.    }
  237. }
  238. #else
  239. void iir_mem2(const spx_sig_t *x, const spx_coef_t *den, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
  240. {
  241.    int i,j;
  242.    spx_word32_t xi,yi,nyi;
  243.    for (i=0;i<N;i++)
  244.    {
  245.       xi=SATURATE(x[i],805306368);
  246.       yi = SATURATE(xi + SHL32(mem[0],2),805306368);
  247.       nyi = NEG32(yi);
  248.       for (j=0;j<ord-1;j++)
  249.       {
  250.          mem[j] = MAC16_32_Q15(mem[j+1],den[j],nyi);
  251.       }
  252.       mem[ord-1] = MULT16_32_Q15(den[ord-1],nyi);
  253.       y[i] = yi;
  254.    }
  255. }
  256. #endif
  257. #endif
  258. #ifndef OVERRIDE_FIR_MEM2
  259. #ifdef PRECISION16
  260. void fir_mem2(const spx_sig_t *x, const spx_coef_t *num, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
  261. {
  262.    int i,j;
  263.    spx_word16_t xi,yi;
  264.    for (i=0;i<N;i++)
  265.    {
  266.       xi= EXTRACT16(PSHR32(SATURATE(x[i],536870911),SIG_SHIFT));
  267.       yi = EXTRACT16(PSHR32(SATURATE(x[i] + SHL32(mem[0],1),536870911),SIG_SHIFT));
  268.       for (j=0;j<ord-1;j++)
  269.       {
  270.          mem[j] = MAC16_16(mem[j+1], num[j],xi);
  271.       }
  272.       mem[ord-1] = MULT16_16(num[ord-1],xi);
  273.       y[i] = SHL32(EXTEND32(yi),SIG_SHIFT);
  274.    }
  275. }
  276. #else
  277. void fir_mem2(const spx_sig_t *x, const spx_coef_t *num, spx_sig_t *y, int N, int ord, spx_mem_t *mem)
  278. {
  279.    int i,j;
  280.    spx_word32_t xi,yi;
  281.    for (i=0;i<N;i++)
  282.    {
  283.       xi=SATURATE(x[i],805306368);
  284.       yi = xi + SHL32(mem[0],2);
  285.       for (j=0;j<ord-1;j++)
  286.       {
  287.          mem[j] = MAC16_32_Q15(mem[j+1], num[j],xi);
  288.       }
  289.       mem[ord-1] = MULT16_32_Q15(num[ord-1],xi);
  290.       y[i] = SATURATE(yi,805306368);
  291.    }
  292. }
  293. #endif
  294. #endif
  295. void syn_percep_zero(const spx_sig_t *xx, const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_sig_t *y, int N, int ord, char *stack)
  296. {
  297.    int i;
  298.    VARDECL(spx_mem_t *mem);
  299.    ALLOC(mem, ord, spx_mem_t);
  300.    for (i=0;i<ord;i++)
  301.      mem[i]=0;
  302.    iir_mem2(xx, ak, y, N, ord, mem);
  303.    for (i=0;i<ord;i++)
  304.       mem[i]=0;
  305.    filter_mem2(y, awk1, awk2, y, N, ord, mem);
  306. }
  307. void residue_percep_zero(const spx_sig_t *xx, const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_sig_t *y, int N, int ord, char *stack)
  308. {
  309.    int i;
  310.    VARDECL(spx_mem_t *mem);
  311.    ALLOC(mem, ord, spx_mem_t);
  312.    for (i=0;i<ord;i++)
  313.       mem[i]=0;
  314.    filter_mem2(xx, ak, awk1, y, N, ord, mem);
  315.    for (i=0;i<ord;i++)
  316.      mem[i]=0;
  317.    fir_mem2(y, awk2, y, N, ord, mem);
  318. }
  319. #ifndef OVERRIDE_COMPUTE_IMPULSE_RESPONSE
  320. void compute_impulse_response(const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack)
  321. {
  322.    int i,j;
  323.    spx_word16_t y1, ny1i, ny2i;
  324.    VARDECL(spx_mem_t *mem1);
  325.    VARDECL(spx_mem_t *mem2);
  326.    ALLOC(mem1, ord, spx_mem_t);
  327.    ALLOC(mem2, ord, spx_mem_t);
  328.    
  329.    y[0] = LPC_SCALING;
  330.    for (i=0;i<ord;i++)
  331.       y[i+1] = awk1[i];
  332.    i++;
  333.    for (;i<N;i++)
  334.       y[i] = VERY_SMALL;
  335.    
  336.    for (i=0;i<ord;i++)
  337.       mem1[i] = mem2[i] = 0;
  338.    for (i=0;i<N;i++)
  339.    {
  340.       y1 = ADD16(y[i], EXTRACT16(PSHR32(mem1[0],LPC_SHIFT)));
  341.       ny1i = NEG16(y1);
  342.       y[i] = ADD16(SHL16(y1,1), EXTRACT16(PSHR32(mem2[0],LPC_SHIFT)));
  343.       ny2i = NEG16(y[i]);
  344.       for (j=0;j<ord-1;j++)
  345.       {
  346.          mem1[j] = MAC16_16(mem1[j+1], awk2[j],ny1i);
  347.          mem2[j] = MAC16_16(mem2[j+1], ak[j],ny2i);
  348.       }
  349.       mem1[ord-1] = MULT16_16(awk2[ord-1],ny1i);
  350.       mem2[ord-1] = MULT16_16(ak[ord-1],ny2i);
  351.    }
  352. }
  353. #endif
  354. void qmf_decomp(const spx_word16_t *xx, const spx_word16_t *aa, spx_sig_t *y1, spx_sig_t *y2, int N, int M, spx_word16_t *mem, char *stack)
  355. {
  356.    int i,j,k,M2;
  357.    VARDECL(spx_word16_t *a);
  358.    VARDECL(spx_word16_t *x);
  359.    spx_word16_t *x2;
  360.    
  361.    ALLOC(a, M, spx_word16_t);
  362.    ALLOC(x, N+M-1, spx_word16_t);
  363.    x2=x+M-1;
  364.    M2=M>>1;
  365.    for (i=0;i<M;i++)
  366.       a[M-i-1]= aa[i];
  367.    for (i=0;i<M-1;i++)
  368.       x[i]=mem[M-i-2];
  369.    for (i=0;i<N;i++)
  370.       x[i+M-1]=SATURATE(PSHR(xx[i],1),16383);
  371.    for (i=0,k=0;i<N;i+=2,k++)
  372.    {
  373.       y1[k]=0;
  374.       y2[k]=0;
  375.       for (j=0;j<M2;j++)
  376.       {
  377.          y1[k]=ADD32(y1[k],SHR(MULT16_16(a[j],ADD16(x[i+j],x2[i-j])),1));
  378.          y2[k]=SUB32(y2[k],SHR(MULT16_16(a[j],SUB16(x[i+j],x2[i-j])),1));
  379.          j++;
  380.          y1[k]=ADD32(y1[k],SHR(MULT16_16(a[j],ADD16(x[i+j],x2[i-j])),1));
  381.          y2[k]=ADD32(y2[k],SHR(MULT16_16(a[j],SUB16(x[i+j],x2[i-j])),1));
  382.       }
  383.    }
  384.    for (i=0;i<M-1;i++)
  385.      mem[i]=SATURATE(PSHR(xx[N-i-1],1),16383);
  386. }
  387. /* By segher */
  388. void fir_mem_up(const spx_sig_t *x, const spx_word16_t *a, spx_sig_t *y, int N, int M, spx_word32_t *mem, char *stack)
  389.    /* assumptions:
  390.       all odd x[i] are zero -- well, actually they are left out of the array now
  391.       N and M are multiples of 4 */
  392. {
  393.    int i, j;
  394.    VARDECL(spx_word16_t *xx);
  395.    
  396.    ALLOC(xx, M+N-1, spx_word16_t);
  397.    for (i = 0; i < N/2; i++)
  398.       xx[2*i] = SHR(x[N/2-1-i],SIG_SHIFT+1);
  399.    for (i = 0; i < M - 1; i += 2)
  400.       xx[N+i] = mem[i+1];
  401.    for (i = 0; i < N; i += 4) {
  402.       spx_sig_t y0, y1, y2, y3;
  403.       spx_word16_t x0;
  404.       y0 = y1 = y2 = y3 = 0;
  405.       x0 = xx[N-4-i];
  406.       for (j = 0; j < M; j += 4) {
  407.          spx_word16_t x1;
  408.          spx_word16_t a0, a1;
  409.          a0 = a[j];
  410.          a1 = a[j+1];
  411.          x1 = xx[N-2+j-i];
  412.          y0 = ADD32(y0,SHR(MULT16_16(a0, x1),1));
  413.          y1 = ADD32(y1,SHR(MULT16_16(a1, x1),1));
  414.          y2 = ADD32(y2,SHR(MULT16_16(a0, x0),1));
  415.          y3 = ADD32(y3,SHR(MULT16_16(a1, x0),1));
  416.          a0 = a[j+2];
  417.          a1 = a[j+3];
  418.          x0 = xx[N+j-i];
  419.          y0 = ADD32(y0,SHR(MULT16_16(a0, x0),1));
  420.          y1 = ADD32(y1,SHR(MULT16_16(a1, x0),1));
  421.          y2 = ADD32(y2,SHR(MULT16_16(a0, x1),1));
  422.          y3 = ADD32(y3,SHR(MULT16_16(a1, x1),1));
  423.       }
  424.       y[i] = y0;
  425.       y[i+1] = y1;
  426.       y[i+2] = y2;
  427.       y[i+3] = y3;
  428.    }
  429.    for (i = 0; i < M - 1; i += 2)
  430.       mem[i+1] = xx[i];
  431. }
  432. void comb_filter_mem_init (CombFilterMem *mem)
  433. {
  434.    mem->last_pitch=0;
  435.    mem->last_pitch_gain[0]=mem->last_pitch_gain[1]=mem->last_pitch_gain[2]=0;
  436.    mem->smooth_gain=1;
  437. }
  438. #ifdef FIXED_POINT
  439. #define COMB_STEP 32767
  440. #else
  441. #define COMB_STEP 1.0
  442. #endif
  443. void comb_filter(
  444. spx_sig_t *exc,          /*decoded excitation*/
  445. spx_sig_t *new_exc,      /*enhanced excitation*/
  446. spx_coef_t *ak,           /*LPC filter coefs*/
  447. int p,               /*LPC order*/
  448. int nsf,             /*sub-frame size*/
  449. int pitch,           /*pitch period*/
  450. spx_word16_t *pitch_gain,   /*pitch gain (3-tap)*/
  451. spx_word16_t  comb_gain,    /*gain of comb filter*/
  452. CombFilterMem *mem
  453. )
  454. {
  455.    int i;
  456.    spx_word16_t exc_energy=0, new_exc_energy=0;
  457.    spx_word16_t gain;
  458.    spx_word16_t step;
  459.    spx_word16_t fact;
  460.    /*Compute excitation amplitude prior to enhancement*/
  461.    exc_energy = compute_rms(exc, nsf);
  462.    /*for (i=0;i<nsf;i++)
  463.      exc_energy+=((float)exc[i])*exc[i];*/
  464.    /*Some gain adjustment if pitch is too high or if unvoiced*/
  465. #ifdef FIXED_POINT
  466.    {
  467.       spx_word16_t g = gain_3tap_to_1tap(pitch_gain)+gain_3tap_to_1tap(mem->last_pitch_gain);
  468.       if (g > 166)
  469.          comb_gain = MULT16_16_Q15(DIV32_16(SHL32(EXTEND32(165),15),g), comb_gain);
  470.       if (g < 64)
  471.          comb_gain = MULT16_16_Q15(SHL16(g, 9), comb_gain);
  472.    }
  473. #else
  474.    {
  475.       float g=0;
  476.       g = GAIN_SCALING_1*.5*(gain_3tap_to_1tap(pitch_gain)+gain_3tap_to_1tap(mem->last_pitch_gain));
  477.       if (g>1.3)
  478.          comb_gain*=1.3/g;
  479.       if (g<.5)
  480.          comb_gain*=2.*g;
  481.    }
  482. #endif
  483.    step = DIV32(COMB_STEP, nsf);
  484.    fact=0;
  485.    /*Apply pitch comb-filter (filter out noise between pitch harmonics)*/
  486.    for (i=0;i<nsf;i++)
  487.    {
  488.       spx_word32_t exc1, exc2;
  489.       fact = ADD16(fact,step);
  490.       
  491.       exc1 = SHL32(MULT16_32_Q15(SHL16(pitch_gain[0],7),exc[i-pitch+1]) +
  492.                  MULT16_32_Q15(SHL16(pitch_gain[1],7),exc[i-pitch]) +
  493.                  MULT16_32_Q15(SHL16(pitch_gain[2],7),exc[i-pitch-1]) , 2);
  494.       exc2 = SHL32(MULT16_32_Q15(SHL16(mem->last_pitch_gain[0],7),exc[i-mem->last_pitch+1]) +
  495.                  MULT16_32_Q15(SHL16(mem->last_pitch_gain[1],7),exc[i-mem->last_pitch]) +
  496.                  MULT16_32_Q15(SHL16(mem->last_pitch_gain[2],7),exc[i-mem->last_pitch-1]),2);
  497.       new_exc[i] = exc[i] + MULT16_32_Q15(comb_gain, ADD32(MULT16_32_Q15(fact,exc1), MULT16_32_Q15(SUB16(COMB_STEP,fact), exc2)));
  498.    }
  499.    mem->last_pitch_gain[0] = pitch_gain[0];
  500.    mem->last_pitch_gain[1] = pitch_gain[1];
  501.    mem->last_pitch_gain[2] = pitch_gain[2];
  502.    mem->last_pitch = pitch;
  503.    /*Amplitude after enhancement*/
  504.    new_exc_energy = compute_rms(new_exc, nsf);
  505.    if (exc_energy > new_exc_energy)
  506.       exc_energy = new_exc_energy;
  507.    
  508.    gain = DIV32_16(SHL32(EXTEND32(exc_energy),15),ADD16(1,new_exc_energy));
  509. #ifdef FIXED_POINT
  510.    if (gain < 16384)
  511.       gain = 16384;
  512. #else
  513.    if (gain < .5)
  514.       gain=.5;
  515. #endif
  516. #ifdef FIXED_POINT
  517.    for (i=0;i<nsf;i++)
  518.    {
  519.       mem->smooth_gain = ADD16(MULT16_16_Q15(31457,mem->smooth_gain), MULT16_16_Q15(1311,gain));
  520.       new_exc[i] = MULT16_32_Q15(mem->smooth_gain, new_exc[i]);
  521.    }
  522. #else
  523.    for (i=0;i<nsf;i++)
  524.    {
  525.       mem->smooth_gain = .96*mem->smooth_gain + .04*gain;
  526.       new_exc[i] *= mem->smooth_gain;
  527.    }
  528. #endif
  529. }