modified several files to make more robust to zero input vectors
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Thu, 1 Nov 2012 02:45:49 +0000 (02:45 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Thu, 1 Nov 2012 02:45:49 +0000 (02:45 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@913 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/c2sim.c
codec2-dev/src/codec2.c
codec2-dev/src/codec2_internal.h
codec2-dev/src/listensim.sh
codec2-dev/src/nlp.c
codec2-dev/src/postfilter.c
codec2-dev/src/quantise.c
codec2-dev/src/sine.c

index f9403ccffdb9ecd34aa6168a95e01a1059b7ed30..6aef4ef2e1d69d07faef7320decd9a858dfd43d1 100644 (file)
@@ -363,7 +363,7 @@ int main(int argc, char *argv[])
        //printf("frame: %d ", frames);
 
        /* Read input speech */
-
+       
        for(i=0; i<M-N; i++) {
            Sn[i] = Sn[i+N];
            Sn_pre[i] = Sn_pre[i+N];
index 7130d67b9a0de7e00705ce4a95aa92fc94ba4584..89de671d5739a58c34839db0a210de5a70b248a4 100644 (file)
@@ -612,7 +612,7 @@ void codec2_encode_1400(struct CODEC2 *c2, unsigned char * bits, short speech[])
     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
        pack(bits, &nbit, lsp_indexes[i], lsp_bits(i));
     }
+
     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
 }
 
index df5b8e21b9f6f01fca5b125ccb178c0e5dc0d4a5..b006d03515287f6059856eb8755bdd8ef781ea41 100644 (file)
@@ -1,6 +1,6 @@
 /*---------------------------------------------------------------------------*\
                                                                              
-  FILE........: fdmdv_internal.h
+  FILE........: codec2_internal.h
   AUTHOR......: David Rowe                                                          
   DATE CREATED: April 16 2012
                                                                              
index 72964d4f3eeda5f5d01c1a4ce0e210691cf36e25..b296cac58867d39b4a91fd5e3564c3380e1f5b65 100755 (executable)
@@ -4,6 +4,6 @@
 #
 # Listen to files processed with sim.sh
 
-../script/menu.sh $1_lpcpf.raw $1_3200.raw $1_2400.raw $2 $3
+../script/menu.sh $1_uq.raw $1_lpc10.raw $1_lpcpf.raw $1_phase0.raw $1_phase0_lpcpf.raw $2 $3 $4 $5
 
 
index 4b089610fdf4ef8c5a38d6a0d3aab7a53ecd7ed9..3214578e84ac721a6068b0a6b61787dbba3b84e7 100644 (file)
@@ -224,11 +224,12 @@ float nlp(
     float best_f0;
 
     assert(nlp_state != NULL);
+    assert(m <= PMAX_M);
     nlp = (NLP*)nlp_state;
 
     /* Square, notch filter at DC, and LP filter vector */
 
-    for(i=m-n; i<M; i++)           /* square latest speech samples */
+    for(i=m-n; i<m; i++)           /* square latest speech samples */
        nlp->sq[i] = Sn[i]*Sn[i];
 
     for(i=m-n; i<m; i++) {     /* notch filter at DC */
@@ -236,7 +237,14 @@ float nlp(
        notch += COEFF*nlp->mem_y;
        nlp->mem_x = nlp->sq[i];
        nlp->mem_y = notch;
-       nlp->sq[i] = notch;
+       nlp->sq[i] = notch + 1.0;  /* With 0 input vectors to codec,
+                                     kiss_fft() would take a long
+                                     time to execute when running in
+                                     real time.  Problem was traced
+                                     to kiss_fft function call in
+                                     this function. Adding this small
+                                     constant fixed problem.  Not
+                                     exactly sure why. */
     }
 
     for(i=m-n; i<m; i++) {     /* FIR filter vector */
@@ -282,7 +290,7 @@ float nlp(
            gmax_bin = i;
        }
     }
-
+    
     //#define POST_PROCESS_MBE
     #ifdef POST_PROCESS_MBE
     best_f0 = post_process_mbe(Fw, pmin, pmax, gmax, Sw, W, prev_Wo);
@@ -336,10 +344,10 @@ float post_process_sub_multiples(COMP Fw[],
     /* post process estimate by searching submultiples */
 
     mult = 2;
-    min_bin = PE_FFT_SIZE*DEC/pmax;
+    min_bin = PE_FFT_SIZE*DEC/pmax; 
     cmax_bin = gmax_bin;
     prev_f0_bin = *prev_Wo*(4000.0/PI)*(PE_FFT_SIZE*DEC)/SAMPLE_RATE;
-
+    
     while(gmax_bin/mult >= min_bin) {
 
        b = gmax_bin/mult;                      /* determine search interval */
index 077a3d27ec8d3b76d12854f88883fdece5329490..c78f495bedf849d5c8f4add2c7e2fbb60cffff25 100644 (file)
@@ -27,6 +27,7 @@
   along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <assert.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <math.h>
@@ -110,6 +111,7 @@ void postfilter(
   for(m=1; m<=model->L; m++)
       e += model->A[m]*model->A[m];
 
+  assert(e > 0.0);
   e = 10.0*log10(e/model->L);
 
   /* If beneath threhold, update bg estimate.  The idea
index 4a5fefee48779fbe83996651834b1aa77cb746c0..1c98afd42325567aed10a6a8c988ceda88a3d3c7 100644 (file)
@@ -972,7 +972,7 @@ void lpc_post_filter(kiss_fft_cfg fft_fwd_cfg, MODEL *model, COMP Pw[], float ak
     
     /* measure energy before post filtering */
 
-    e_before = 0.0;
+    e_before = 1E-4;
     for(i=0; i<FFT_ENC/2; i++)
        e_before += Pw[i].real;
 
@@ -983,7 +983,7 @@ void lpc_post_filter(kiss_fft_cfg fft_fwd_cfg, MODEL *model, COMP Pw[], float ak
        dump_Pwb(Pw);
     #endif
 
-    e_after = 0.0;
+    e_after = 1E-4;
     for(i=0; i<FFT_ENC/2; i++) {
        Pfw[i] = pow(Rw[i], LPCPF_BETA);
        Pw[i].real *= Pfw[i] * Pfw[i];
@@ -1251,17 +1251,29 @@ float speech_to_uq_lsps(float lsp[],
     int   i, roots;
     float Wn[M];
     float R[LPC_MAX+1];
-    float E;
+    float e, E;
 
-    for(i=0; i<M; i++)
+    e = 0.0;
+    for(i=0; i<M; i++) {
        Wn[i] = Sn[i]*w[i];
+       e += Wn[i]*Wn[i];
+    }
+
+    /* trap 0 energy case as LPC analysis will fail */
+    
+    if (e == 0.0) {
+       for(i=0; i<order; i++)
+           lsp[i] = (PI/order)*(float)i;
+       return 0.0;
+    }
+    
     autocorrelate(Wn, R, M, order);
     levinson_durbin(R, ak, order);
   
     E = 0.0;
     for(i=0; i<=order; i++)
        E += ak[i]*R[i];
+    
     /* 15 Hz BW expansion as I can't hear the difference and it may help
        help occasional fails in the LSP root finding.  Important to do this
        after energy calculation to avoid -ve energy values.
@@ -1955,6 +1967,7 @@ int encode_WoE(MODEL *model, float e, float xq[])
   int          ndim = ge_cb[0].k;
 
   assert((1<<WO_E_BITS) == nb_entries);
+  assert(e >= 0.0);
 
   x[0] = log10((model->Wo/PI)*4000.0/50.0)/log10(2);
   x[1] = 10.0*log10(1e-4 + e);
@@ -1970,6 +1983,7 @@ int encode_WoE(MODEL *model, float e, float xq[])
     err[i] -= codebook1[ndim*n1+i];
   }
 
+  //printf("enc: %f %f (%f)(%f) \n", xq[0], xq[1], e, 10.0*log10(1e-4 + e));
   return n1;
 }
 
@@ -2001,6 +2015,7 @@ void decode_WoE(MODEL *model, float *e, float xq[], int n1)
     err[i] -= codebook1[ndim*n1+i];
   }
 
+  //printf("dec: %f %f\n", xq[0], xq[1]);
   model->Wo = pow(2.0, xq[0])*(PI*50.0)/4000.0;
 
   /* bit errors can make us go out of range leading to all sorts of
index 4ada8d80c3386ab2fd1cf3ce9bd8a895fd84cedd..67cbeaeb7236950afe47163739143cb814f64c1d 100644 (file)
@@ -400,7 +400,7 @@ float est_voicing_mbe(
     float elow, ehigh, eratio;
     float dF0, sixty;
 
-    sig = 0.0;
+    sig = 1E-4;
     for(l=1; l<=model->L/4; l++) {
        sig += model->A[l]*model->A[l];
     }
@@ -412,7 +412,7 @@ float est_voicing_mbe(
     }
 
     Wo = model->Wo;
-    error = 0.0;
+    error = 1E-4;
 
     /* Just test across the harmonics in the first 1000 Hz (L/4) */
 
@@ -463,7 +463,7 @@ float est_voicing_mbe(
        determine if we have made any gross errors.
     */
 
-    elow = ehigh = 0.0;
+    elow = ehigh = 1E-4;
     for(l=1; l<=model->L/2; l++) {
        elow += model->A[l]*model->A[l];
     }