[xiph-commits] r13608 - trunk/ghost/libghost

jm at svn.xiph.org jm at svn.xiph.org
Fri Aug 24 03:39:15 PDT 2007


Author: jm
Date: 2007-08-24 03:39:14 -0700 (Fri, 24 Aug 2007)
New Revision: 13608

Modified:
   trunk/ghost/libghost/ceft.c
   trunk/ghost/libghost/ceft.h
   trunk/ghost/libghost/ghost.c
   trunk/ghost/libghost/ghost.h
Log:
Trying (without much success so far) to use pitch within CEFT


Modified: trunk/ghost/libghost/ceft.c
===================================================================
--- trunk/ghost/libghost/ceft.c	2007-08-24 00:52:13 UTC (rev 13607)
+++ trunk/ghost/libghost/ceft.c	2007-08-24 10:39:14 UTC (rev 13608)
@@ -267,6 +267,13 @@
 
 #define NBANDS 23 /*or 22 if we discard the small last band*/
 int qbank[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 20, 24, 28, 36, 44, 52, 68, 84, 116, 128};
+#if 0
+#define PBANDS 6
+int pbank[] = {1, 5, 9, 20, 44, 84, 128};
+#else
+#define PBANDS 1
+int pbank[] = {1, 128};
+#endif
 
 #if 0
 void compute_bank(float *ps, float *bank)
@@ -360,7 +367,7 @@
 {
    int i;
    float q=32;
-   X[0] = (1.f/q)*floor(.5+q*X[0]);
+   //X[0] = (1.f/q)*floor(.5+q*X[0]);
    for (i=1;i<NBANDS;i++)
    {
       int j;
@@ -373,12 +380,83 @@
          q = 4;
       else
          q = 4;
+      //q = 1;
+      q/=2;
       alg_quant3(X+qbank[i]*2-1, 2*(qbank[i+1]-qbank[i]), q);
    }
    //FIXME: This is a kludge, even though I don't think it really matters much
    X[255] = 0;
 }
 
+
+void pitch_quant_bank(float *X, float *P)
+{
+   int i;
+   for (i=0;i<PBANDS;i++)
+   {
+      float Sxy=0;
+      int j;
+      float gain;
+      for (j=pbank[i];j<pbank[i+1];j++)
+      {
+         Sxy += X[j*2-1]*P[j*2-1];
+         Sxy += X[j*2]*P[j*2];
+      }
+      gain = Sxy/(2*(pbank[i+1]-pbank[i]));
+      for (j=pbank[i];j<pbank[i+1];j++)
+      {
+         P[j*2-1] *= gain;
+         P[j*2] *= gain;
+      }
+      //printf ("%f ", gain);
+   }
+   P[255] = 0;
+   //printf ("\n");
+}
+
+void pitch_renormalise_bank(float *X, float *P)
+{
+   int i;
+   for (i=1;i<NBANDS;i++)
+   {
+      int j;
+      float Rpp=0;
+      float Rxp=0;
+      float Rxx=0;
+      float gain1, gain2;
+      for (j=qbank[i];j<qbank[i+1];j++)
+      {
+         Rxp += X[j*2-1]*P[j*2-1];
+         Rxp += X[j*2  ]*P[j*2  ];
+         Rpp += P[j*2-1]*P[j*2-1];
+         Rpp += P[j*2  ]*P[j*2  ];
+         Rxx += X[j*2-1]*X[j*2-1];
+         Rxx += X[j*2  ]*X[j*2  ];
+      }
+      Rxx *= .5/(qbank[i+1]-qbank[i]);
+      Rxp *= .5/(qbank[i+1]-qbank[i]);
+      Rpp *= .5/(qbank[i+1]-qbank[i]);
+      gain1 = sqrt(Rxp*Rxp + 1 - Rpp)-Rxp;
+      //gain2 = -sqrt(Rxp*Rxp + 1 - Rpp)-Rxp;
+      //if (fabs(gain2)<fabs(gain1))
+      //   gain1 = gain2;
+      //printf ("%f ", Rxx, Rxp, Rpp, gain);
+      //printf ("%f ", gain1);
+      Rxx = 0;
+      for (j=qbank[i];j<qbank[i+1];j++)
+      {
+         X[j*2-1] = P[j*2-1]+gain1*X[j*2-1];
+         X[j*2  ] = P[j*2  ]+gain1*X[j*2  ];
+         Rxx += X[j*2-1]*X[j*2-1];
+         Rxx += X[j*2  ]*X[j*2  ];
+      }
+      //printf ("%f ", gain1);
+   }
+   //printf ("\n");
+   //FIXME: Kludge
+   X[255] = 0;
+}
+
 #define BARK_BANDS 20
 
 struct CEFTState_ {
@@ -396,97 +474,60 @@
    return st;
 }
 
-void ceft_encode(CEFTState *st, float *in, float *out)
+void ceft_encode(CEFTState *st, float *in, float *out, float *pitch, float *window)
 {
    //float bark[BARK_BANDS];
-   float Xps[st->length>>1];
    float X[st->length];
+   float Xp[st->length];
    int i;
-
+   float bank[NBANDS];
+   float pitch_bank[NBANDS];
+   float p[st->length];
+   
+   for (i=0;i<st->length;i++)
+      p[i] = pitch[i]*window[i];
+   
    spx_fft_float(st->frame_fft, in, X);
+   spx_fft_float(st->frame_fft, p, Xp);
    
-   Xps[0] = .1+X[0]*X[0];
-   for (i=1;i<st->length>>1;i++)
-      Xps[i] = .1+X[2*i-1]*X[2*i-1] + X[2*i]*X[2*i];
-
-#if 1
-   float bank[NBANDS];
+   /* Bands for the input signal */
    compute_bank(X, bank);
    normalise_bank(X, bank);
-#else
-   filterbank_compute_bank(st->bank, Xps, bark);
-   filterbank_compute_psd(st->bank, bark, Xps);
    
-   for(i=0;i<st->length>>1;i++)
-      Xps[i] = sqrt(Xps[i]);
-   X[0] /= Xps[0];
-   for (i=1;i<st->length>>1;i++)
-   {
-      X[2*i-1] /= Xps[i];
-      X[2*i] /= Xps[i];
-   }
-   X[st->length-1] /= Xps[(st->length>>1)-1];
-#endif
+   /* Bands for the pitch signal */
+   compute_bank(Xp, pitch_bank);
+   normalise_bank(Xp, pitch_bank);
    
    /*for(i=0;i<st->length;i++)
       printf ("%f ", X[i]);
    printf ("\n");
-*/
+   */
+   pitch_quant_bank(X, Xp);
    
-#if 1
+   if (1) {
+      for (i=1;i<st->length;i++)
+         X[i] -= Xp[i];
+      float tmp[NBANDS];
+      compute_bank(X, tmp);
+      normalise_bank(X, tmp);
+      
+   }
+   //Quantise input
    quant_bank2(X);
-#else
-   for(i=0;i<st->length;i++)
-   {
-      float q = 4;
-      if (i<10)
-         q = 8;
-      else if (i<20)
-         q = 4;
-      else if (i<30)
-         q = 2;
-      else if (i<50)
-         q = 1;
-      else
-         q = .5;
-      //q=1;
-      int sq = floor(.5+q*X[i]);
-      printf ("%d ", sq);
-      X[i] = (1.f/q)*(sq);
-   }
-   printf ("\n");
-#endif
 
-#if 0
-   X[0]  *= Xps[0];
-   for (i=1;i<st->length>>1;i++)
-   {
-      X[2*i-1] *= Xps[i];
-      X[2*i] *= Xps[i];
-   }
-   X[st->length-1] *= Xps[(st->length>>1)-1];
-#else
+   //Renormalise the quantised signal back to unity
    float bank2[NBANDS];
    compute_bank(X, bank2);
    normalise_bank(X, bank2);
 
+   if (1) {
+      pitch_renormalise_bank(X, Xp);
+   }
+   
+   /* Denormalise back to real power */
    denormalise_bank(X, bank);
-#endif
    
-   
-#if 0
-   for(i=0;i<BARK_BANDS;i++)
-   {
-      printf("%f ", bark[i]);
-   }
-   printf ("\n");
-#endif 
-   /*
-   for(i=0;i<st->length;i++)
-      printf ("%f ", X[i]);
-   printf ("\n");
-   */
-
+   //Synthesis
    spx_ifft_float(st->frame_fft, X, out);
 
 }

Modified: trunk/ghost/libghost/ceft.h
===================================================================
--- trunk/ghost/libghost/ceft.h	2007-08-24 00:52:13 UTC (rev 13607)
+++ trunk/ghost/libghost/ceft.h	2007-08-24 10:39:14 UTC (rev 13608)
@@ -21,5 +21,5 @@
 
 CEFTState *ceft_init(int len);
 
-void ceft_encode(CEFTState *st, float *in, float *out);
+void ceft_encode(CEFTState *st, float *in, float *out, float *pitch, float *window);
 

Modified: trunk/ghost/libghost/ghost.c
===================================================================
--- trunk/ghost/libghost/ghost.c	2007-08-24 00:52:13 UTC (rev 13607)
+++ trunk/ghost/libghost/ghost.c	2007-08-24 10:39:14 UTC (rev 13608)
@@ -32,10 +32,10 @@
 #include "filterbank.h"
 
 #define PCM_BUF_SIZE 2048
+#define PITCH_BUF_SIZE 1024
 
 #define SINUSOIDS 30
 #define MASK_LPC_ORDER 10
-
 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)
 {
    int i,j;
@@ -93,8 +93,10 @@
    st->new_pcm = st->pcm_buf + PCM_BUF_SIZE - st->advance;
    
    st->noise_buf = calloc(PCM_BUF_SIZE,sizeof(float));
-   st->new_noise = st->noise_buf + PCM_BUF_SIZE/2 - st->length/2;
+   st->new_noise = st->noise_buf + PCM_BUF_SIZE - st->length;
    
+   st->pitch_buf = calloc(PITCH_BUF_SIZE,sizeof(float));
+   
    st->psy = vorbis_psy_init(44100, PCM_BUF_SIZE);
    
    st->analysis_window = calloc(st->length,sizeof(float));
@@ -215,12 +217,39 @@
       printf ("\n");*/
       /*for (i=0;i<st->length;i++)
       y[i] *= st->synthesis_window[i];*/
-
 #if 0
+      float max_score=-1;
+      int pitch_index=0;
+      {
+         int lag, i;
+         for (lag=0;lag<1024-st->length;lag++)
+         {
+            float score;
+            float Sxx=1;
+            float Sxy=0, Syy=1;
+            for (i=0;i<st->length;i++)
+            {
+               Sxx += x[i]*x[i];
+               Sxy += x[i]*st->pitch_buf[i+lag];
+               Syy += st->pitch_buf[i+lag]*st->pitch_buf[i+lag];
+            }
+            score = sqrt(Sxy*Sxy/(Sxx*Syy));
+            if (Sxy<0)
+               score = 0;
+            if (score > max_score)
+            {
+               max_score = score;
+               pitch_index = lag;
+            }
+            //printf ("%f ", score);
+         }
+         //printf ("\n");
+      }
+      //printf ("%f %d ", max_score, pitch_index);
       float z[st->length];
       for (i=0;i<st->length;i++)
          z[i] = x[i]-y[i];
-      ceft_encode(st->ceft, z, z);
+      ceft_encode(st->ceft, z, z, st->pitch_buf+pitch_index, st->analysis_window);
       for (i=0;i<st->length;i++)
          y[i] = y[i]+z[i];
       
@@ -240,6 +269,11 @@
          st->new_noise[i] = x[i]-y[i];
       }
       
+      for (i=0;i<PITCH_BUF_SIZE-st->advance;i++)
+         st->pitch_buf[i] = st->pitch_buf[i+st->advance];
+      for (i=0;i<st->advance;i++)
+         st->pitch_buf[PITCH_BUF_SIZE+i-st->advance] = st->current_frame[i]-st->new_noise[i];
+               
       /*for (i=0;i<st->overlap;i++)
          pcm[i] = st->syn_memory[i]+y[i];
       for (i=st->overlap;i<st->advance;i++)
@@ -305,7 +339,7 @@
          printf ("\n");*/
          exit(1);
       }
-#if 1
+#if 0
       float noise[st->advance];
       //for (i=0;i<MASK_LPC_ORDER;i++)
       //   awk1[i] = 0;

Modified: trunk/ghost/libghost/ghost.h
===================================================================
--- trunk/ghost/libghost/ghost.h	2007-08-24 00:52:13 UTC (rev 13607)
+++ trunk/ghost/libghost/ghost.h	2007-08-24 10:39:14 UTC (rev 13608)
@@ -46,6 +46,7 @@
    
    float *noise_buf;
    float *new_noise;
+   float *pitch_buf;
    //float *current_noise;
    
    int length;



More information about the commits mailing list