[xiph-cvs] cvs commit: speex/libspeex filters.c filters.h modes.c nb_celp.c nb_celp.h

Jean-Marc Valin jm at xiph.org
Thu Dec 19 23:16:10 PST 2002



jm          02/12/20 02:16:10

  Modified:    libspeex filters.c filters.h modes.c nb_celp.c nb_celp.h
  Log:
  Improvements to the perceptual enhancement code: smoother pitch comb
  filter, better tuning, and a stupid bugfix (gain hard-coded to .5).

Revision  Changes    Path
1.31      +46 -5     speex/libspeex/filters.c

Index: filters.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/filters.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- filters.c	27 Oct 2002 06:01:30 -0000	1.30
+++ filters.c	20 Dec 2002 07:16:10 -0000	1.31
@@ -211,6 +211,13 @@
 }
 
 
+void comp_filter_mem_init (CombFilterMem *mem)
+{
+   mem->last_pitch=0;
+   mem->last_pitch_gain[0]=mem->last_pitch_gain[1]=mem->last_pitch_gain[2]=0;
+   mem->smooth_gain=1;
+}
+
 void comb_filter(
 float *exc,          /*decoded excitation*/
 float *new_exc,      /*enhanced excitation*/
@@ -219,33 +226,67 @@
 int nsf,             /*sub-frame size*/
 int pitch,           /*pitch period*/
 float *pitch_gain,   /*pitch gain (3-tap)*/
-float  comb_gain     /*gain of comb filter*/
+float  comb_gain,    /*gain of comb filter*/
+CombFilterMem *mem
 )
 {
    int i;
    float exc_energy=0, new_exc_energy=0;
    float gain;
-
+   float step;
+   float fact;
    /*Compute excitation energy prior to enhancement*/
    for (i=0;i<nsf;i++)
       exc_energy+=exc[i]*exc[i];
 
+   /*Some gain adjustment is pitch is too high or if unvoiced*/
+   {
+      float g=0;
+      g = .5*fabs(pitch_gain[0]+pitch_gain[1]+pitch_gain[2] +
+      mem->last_pitch_gain[0] + mem->last_pitch_gain[1] + mem->last_pitch_gain[2]);
+      if (g>1.3)
+         comb_gain*=1.3/g;
+      if (g<.5)
+         comb_gain*=2*g;
+   }
+   step = 1.0/nsf;
+   fact=0;
    /*Apply pitch comb-filter (filter out noise between pitch harmonics)*/
    for (i=0;i<nsf;i++)
    {
-      new_exc[i] = exc[i] + comb_gain * (
+      fact += step;
+
+      new_exc[i] = exc[i] + comb_gain * fact * (
                                          pitch_gain[0]*exc[i-pitch+1] +
                                          pitch_gain[1]*exc[i-pitch] +
                                          pitch_gain[2]*exc[i-pitch-1]
+                                         )
+      + comb_gain * (1-fact) * (
+                                         mem->last_pitch_gain[0]*exc[i-mem->last_pitch+1] +
+                                         mem->last_pitch_gain[1]*exc[i-mem->last_pitch] +
+                                         mem->last_pitch_gain[2]*exc[i-mem->last_pitch-1]
                                          );
    }
-   
+
+   mem->last_pitch_gain[0] = pitch_gain[0];
+   mem->last_pitch_gain[1] = pitch_gain[1];
+   mem->last_pitch_gain[2] = pitch_gain[2];
+   mem->last_pitch = pitch;
+
    /*Gain after enhancement*/
    for (i=0;i<nsf;i++)
       new_exc_energy+=new_exc[i]*new_exc[i];
 
    /*Compute scaling factor and normalize energy*/
    gain = sqrt(exc_energy)/sqrt(.1+new_exc_energy);
+   if (gain < .5)
+      gain=.5;
+   if (gain>1)
+      gain=1;
+
    for (i=0;i<nsf;i++)
-      new_exc[i] *= gain;
+   {
+      mem->smooth_gain = .96*mem->smooth_gain + .04*gain;
+      new_exc[i] *= mem->smooth_gain;
+   }
 }

<p><p>1.22      +12 -1     speex/libspeex/filters.h

Index: filters.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/filters.h,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- filters.h	27 Oct 2002 06:01:30 -0000	1.21
+++ filters.h	20 Dec 2002 07:16:10 -0000	1.22
@@ -33,6 +33,14 @@
 #ifndef FILTERS_H
 #define FILTERS_H
 
+
+typedef struct CombFilterMem {
+   int   last_pitch;
+   float last_pitch_gain[3];
+   float smooth_gain;
+} CombFilterMem;
+
+
 void qmf_decomp(float *xx, float *aa, float *y1, float *y2, int N, int M, float *mem, void *stack);
 void fir_mem_up(float *x, float *a, float *y, int N, int M, float *mem, void *stack);
 
@@ -53,6 +61,8 @@
 
 void residue_percep_zero(float *xx, float *ak, float *awk1, float *awk2, float *y, int N, int ord, void *stack);
 
+void comp_filter_mem_init (CombFilterMem *mem);
+
 void comb_filter(
 float *exc,          /*decoded excitation*/
 float *new_exc,      /*enhanced excitation*/
@@ -61,7 +71,8 @@
 int nsf,             /*sub-frame size*/
 int pitch,           /*pitch period*/
 float *pitch_gain,   /*pitch gain (3-tap)*/
-float  comb_gain     /*gain of comb filter*/
+float  comb_gain,    /*gain of comb filter*/
+CombFilterMem *mem
 );
 
 

<p><p>1.92      +6 -6      speex/libspeex/modes.c

Index: modes.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/modes.c,v
retrieving revision 1.91
retrieving revision 1.92
diff -u -r1.91 -r1.92
--- modes.c	20 Dec 2002 05:20:28 -0000	1.91
+++ modes.c	20 Dec 2002 07:16:10 -0000	1.92
@@ -168,7 +168,7 @@
    noise_codebook_quant,
    noise_codebook_unquant,
    NULL,
-   0, 0, -1,
+   .7, .7, -1,
    43
 };
 
@@ -190,7 +190,7 @@
    split_cb_shape_sign_unquant,
    &split_cb_nb_vlbr,
 
-   0.75, 0.6, .6,
+   0.7, 0.5, .55,
    119
 };
 
@@ -212,7 +212,7 @@
    split_cb_shape_sign_unquant,
    &split_cb_nb_lbr,
 
-   0.75, 0.6, .5,
+   0.7, 0.55, .45,
    160
 };
 
@@ -234,7 +234,7 @@
    split_cb_shape_sign_unquant,
    &split_cb_nb_med,
 
-   0.72, 0.65, .3,
+   0.7, 0.63, .35,
    220
 };
 
@@ -256,7 +256,7 @@
    split_cb_shape_sign_unquant,
    &split_cb_nb,
 
-   0.7, 0.65, .2,
+   0.7, 0.65, .25,
    300
 };
 
@@ -300,7 +300,7 @@
    split_cb_shape_sign_unquant,
    &split_cb_nb,
 
-   0.65, 0.65, 0,
+   0.65, 0.65, -1,
    492
 };
 

<p><p>1.99      +5 -3      speex/libspeex/nb_celp.c

Index: nb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.c,v
retrieving revision 1.98
retrieving revision 1.99
diff -u -r1.98 -r1.99
--- nb_celp.c	20 Dec 2002 05:20:28 -0000	1.98
+++ nb_celp.c	20 Dec 2002 07:16:10 -0000	1.99
@@ -873,6 +873,8 @@
    st->old_qlsp = (float*)speex_alloc(st->lpcSize*sizeof(float));
    st->interp_qlsp = (float*)speex_alloc(st->lpcSize*sizeof(float));
    st->mem_sp = (float*)speex_alloc(5*st->lpcSize*sizeof(float));
+   st->comb_mem = (CombFilterMem *) speex_alloc(sizeof(CombFilterMem));
+   comp_filter_mem_init (st->comb_mem);
 
    st->pi_gain = (float*)speex_alloc(st->nbSubframes*sizeof(float));
    st->last_pitch = 40;
@@ -908,6 +910,7 @@
    speex_free(st->stack);
    speex_free(st->mem_sp);
    speex_free(st->pi_gain);
+   speex_free(st->comb_mem);
    
    speex_free(state);
 }
@@ -1221,7 +1224,6 @@
          k1=SUBMODE(lpc_enh_k1);
          k2=SUBMODE(lpc_enh_k2);
          k3=(1-(1-r*k1)/(1-r*k2))/r;
-         k3=k1-k2;
          if (!st->lpc_enh_enabled)
          {
             k1=k2;
@@ -1408,9 +1410,9 @@
          sp[i]=exc[i];
 
       /* Signal synthesis */
-      if (st->lpc_enh_enabled && SUBMODE(comb_gain>0))
+      if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0)
          comb_filter(exc, sp, st->interp_qlpc, st->lpcSize, st->subframeSize,
-                              pitch, pitch_gain, .5);
+                              pitch, pitch_gain, SUBMODE(comb_gain), st->comb_mem);
       if (st->lpc_enh_enabled)
       {
          /* Use enhanced LPC filter */

<p><p>1.43      +2 -1      speex/libspeex/nb_celp.h

Index: nb_celp.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.h,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -r1.42 -r1.43
--- nb_celp.h	19 Dec 2002 08:21:11 -0000	1.42
+++ nb_celp.h	20 Dec 2002 07:16:10 -0000	1.43
@@ -40,6 +40,7 @@
 #include "speex_bits.h"
 #include "speex_callbacks.h"
 #include "vbr.h"
+#include "filters.h"
 
 /**Structure representing the full state of the narrowband encoder*/
 typedef struct EncState {
@@ -157,7 +158,7 @@
    SpeexSubmode **submodes; /**< Sub-mode data */
    int    submodeID;      /**< Activated sub-mode */
    int    lpc_enh_enabled; /**< 1 when LPC enhancer is on, 0 otherwise */
-
+   CombFilterMem *comb_mem;
    SpeexCallback speex_callbacks[SPEEX_MAX_CALLBACKS];
 
    SpeexCallback user_callback;

<p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list