sine.c converted to kiss_fft, phase & quantise left
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 10 Jun 2012 11:29:29 +0000 (11:29 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 10 Jun 2012 11:29:29 +0000 (11:29 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@539 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/c2sim.c
codec2-dev/src/codec2.c
codec2-dev/src/codec2_internal.h
codec2-dev/src/sine.c
codec2-dev/src/sine.h

index 740c63ebc8fd61ec9a44efd4e81457cbce5d840c..354c3452555a79b4945c98db4e5e7099e26c20dc 100644 (file)
@@ -47,7 +47,7 @@
 #include "postfilter.h"
 #include "interp.h"
 
-void synth_one_frame(short buf[], MODEL *model, float Sn_[], float Pn[]);
+void synth_one_frame(kiss_fft_cfg fft_dec_cfg, short buf[], MODEL *model, float Sn_[], float Pn[]);
 void print_help(const struct option *long_options, int num_opts, char* argv[]);
 
 /*---------------------------------------------------------------------------*\
@@ -64,6 +64,7 @@ int main(int argc, char *argv[])
     float Sn[M];       /* float input speech samples            */
     COMP  Sw[FFT_ENC]; /* DFT of Sn[]                           */
     kiss_fft_cfg  fft_enc_cfg;
+    kiss_fft_cfg  fft_dec_cfg;
     float w[M];                /* time domain hamming window            */
     COMP  W[FFT_ENC];  /* DFT of w[]                            */
     MODEL model;
@@ -309,6 +310,7 @@ int main(int argc, char *argv[])
     /* Initialise ------------------------------------------------------------*/
 
     fft_enc_cfg = kiss_fft_alloc(FFT_ENC, 1, NULL, NULL);
+    fft_dec_cfg = kiss_fft_alloc(FFT_DEC, 0, NULL, NULL);
     make_analysis_window(fft_enc_cfg, w,W);
     make_synthesis_window(Pn);
     quantise_init();
@@ -662,7 +664,7 @@ int main(int argc, char *argv[])
                                           order);      
                if (postfilt)
                    postfilter(&interp_model, &bg_est);
-               synth_one_frame(buf, &interp_model, Sn_, Pn);
+               synth_one_frame(fft_dec_cfg, buf, &interp_model, Sn_, Pn);
                //printf("  buf[0] %d\n", buf[0]);
                if (fout != NULL) 
                    fwrite(buf,sizeof(short),N,fout);
@@ -673,7 +675,7 @@ int main(int argc, char *argv[])
                    phase_synth_zero_order(&model, ak, ex_phase, order);        
                if (postfilt)
                    postfilter(&model, &bg_est);
-               synth_one_frame(buf, &model, Sn_, Pn);
+               synth_one_frame(fft_dec_cfg, buf, &model, Sn_, Pn);
                //printf("  buf[0] %d\n", buf[0]);
                if (fout != NULL) 
                    fwrite(buf,sizeof(short),N,fout);
@@ -696,7 +698,7 @@ int main(int argc, char *argv[])
                phase_synth_zero_order(&model, ak, ex_phase, order);    
            if (postfilt)
                postfilter(&model, &bg_est);
-           synth_one_frame(buf, &model, Sn_, Pn);
+           synth_one_frame(fft_dec_cfg, buf, &model, Sn_, Pn);
            if (fout != NULL) fwrite(buf,sizeof(short),N,fout);
        }
 
@@ -735,11 +737,11 @@ int main(int argc, char *argv[])
     return 0;
 }
 
-void synth_one_frame(short buf[], MODEL *model, float Sn_[], float Pn[])
+void synth_one_frame(kiss_fft_cfg fft_dec_cfg, short buf[], MODEL *model, float Sn_[], float Pn[])
 {
     int     i;
 
-    synthesise(Sn_, model, Pn, 1);
+    synthesise(fft_dec_cfg, Sn_, model, Pn, 1);
 
     for(i=0; i<N; i++) {
        if (Sn_[i] > 32767.0)
index 79fb6b96db36faa2a40b471fff0e41662e0b75b3..f55e33dc212903048c392a942c87ec4814d37d5e 100644 (file)
@@ -104,6 +104,7 @@ struct CODEC2 * CODEC2_WIN32SUPPORT codec2_create(int mode)
     c2->fft_enc_cfg = kiss_fft_alloc(FFT_ENC, 1, NULL, NULL);
     make_analysis_window(c2->fft_enc_cfg, c2->w,c2->W);
     make_synthesis_window(c2->Pn);
+    c2->fft_dec_cfg = kiss_fft_alloc(FFT_DEC, 0, NULL, NULL);
     quantise_init();
     c2->prev_Wo_enc = 0.0;
     c2->bg_est = 0.0;
@@ -147,6 +148,7 @@ void CODEC2_WIN32SUPPORT codec2_destroy(struct CODEC2 *c2)
     assert(c2 != NULL);
     nlp_destroy(c2->nlp);
     KISS_FFT_FREE(c2->fft_enc_cfg);
+    KISS_FFT_FREE(c2->fft_dec_cfg);
     free(c2);
 }
 
@@ -737,7 +739,7 @@ void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model, float
 
     phase_synth_zero_order(model, ak, &c2->ex_phase, LPC_ORD);
     postfilter(model, &c2->bg_est);
-    synthesise(c2->Sn_, model, c2->Pn, 1);
+    synthesise(c2->fft_dec_cfg, c2->Sn_, model, c2->Pn, 1);
 
     for(i=0; i<N; i++) {
        if (c2->Sn_[i] > 32767.0)
index 88cb7d1f14cec87ba1697d637a31166b72837233..2060a97a99e8ef8f3401716d8c8817188c7a3fa0 100644 (file)
@@ -39,6 +39,7 @@ struct CODEC2 {
     float         hpf_states[2];           /* high pass filter states                   */
     void         *nlp;                     /* pitch predictor states                    */
 
+    kiss_fft_cfg  fft_dec_cfg;             /* FFT config for decoder                    */
     float         Sn_[2*N];               /* synthesised output speech                 */
     float         ex_phase;                /* excitation model phase track              */
     float         bg_est;                  /* background noise estimate for post filter */
index 9f46b981e32997477a334d2f21a82240cb2bdd56..392554c9b5c9495154b9018b5dd776e6cc3e1ac2 100644 (file)
@@ -134,8 +134,6 @@ void make_analysis_window(kiss_fft_cfg fft_enc_cfg, float w[], COMP W[])
 
   kiss_fft(fft_enc_cfg, (kiss_fft_cpx *)wshift, (kiss_fft_cpx *)W);
 
-    // fft(&W[0].real,FFT_ENC,-1);         /* "Numerical Recipes in C" FFT */
-
   /* 
       Re-arrange W[] to be symmetrical about FFT_ENC/2.  Makes later 
       analysis convenient.
@@ -203,11 +201,12 @@ float hpf(float x, float states[])
 
 void dft_speech(kiss_fft_cfg fft_enc_cfg, COMP Sw[], float Sn[], float w[])
 {
-  int i;
-  
+  int  i;
+  COMP sw[FFT_ENC];
+
   for(i=0; i<FFT_ENC; i++) {
-    Sw[i].real = 0.0;
-    Sw[i].imag = 0.0;
+    sw[i].real = 0.0;
+    sw[i].imag = 0.0;
   }
 
   /* Centre analysis window on time axis, we need to arrange input
@@ -216,14 +215,14 @@ void dft_speech(kiss_fft_cfg fft_enc_cfg, COMP Sw[], float Sn[], float w[])
   /* move 2nd half to start of FFT input vector */
 
   for(i=0; i<NW/2; i++)
-    Sw[i].real = Sn[i+M/2]*w[i+M/2];
+    sw[i].real = Sn[i+M/2]*w[i+M/2];
 
   /* move 1st half to end of FFT input vector */
 
   for(i=0; i<NW/2; i++)
-    Sw[FFT_ENC-NW/2+i].real = Sn[i+M/2-NW/2]*w[i+M/2-NW/2];
+    sw[FFT_ENC-NW/2+i].real = Sn[i+M/2-NW/2]*w[i+M/2-NW/2];
 
-  fft(&Sw[0].real,FFT_ENC,-1);
+  kiss_fft(fft_enc_cfg, (kiss_fft_cpx *)sw, (kiss_fft_cpx *)Sw);
 }
 
 /*---------------------------------------------------------------------------*\
@@ -367,7 +366,7 @@ void estimate_amplitudes(MODEL *model, COMP Sw[], COMP W[])
 
     /* Estimate phase of harmonic */
 
-    model->phi[m] = atan2(Sw[b].imag,Sw[b].real);
+    model->phi[m] = atan2(-Sw[b].imag,Sw[b].real);
   }
 }
 
@@ -558,6 +557,7 @@ void make_synthesis_window(float Pn[])
 \*---------------------------------------------------------------------------*/
 
 void synthesise(
+  kiss_fft_cfg fft_dec_cfg, 
   float  Sn_[],                /* time domain synthesised signal              */
   MODEL *model,                /* ptr to model parameters for this frame      */
   float  Pn[],         /* time domain Parzen window                   */
@@ -566,6 +566,7 @@ void synthesise(
 {
     int   i,l,j,b;     /* loop variables */
     COMP  Sw_[FFT_DEC];        /* DFT of synthesised signal */
+    COMP  sw_[FFT_DEC];        /* synthesised signal */
 
     if (shift) {
        /* Update memories */
@@ -606,14 +607,14 @@ void synthesise(
                b = (FFT_DEC/2)-1;
        }
        Sw_[b].real = model->A[l]*cos(model->phi[l]);
-       Sw_[b].imag = model->A[l]*sin(model->phi[l]);
+       Sw_[b].imag = -model->A[l]*sin(model->phi[l]);
        Sw_[FFT_DEC-b].real = Sw_[b].real;
        Sw_[FFT_DEC-b].imag = -Sw_[b].imag;
     }
 
     /* Perform inverse DFT */
 
-    fft(&Sw_[0].real,FFT_DEC,1);
+    kiss_fft(fft_dec_cfg, (kiss_fft_cpx *)Sw_, (kiss_fft_cpx *)sw_);
 #else
     /*
        Direct time domain synthesis using the cos() function.  Works
@@ -634,14 +635,14 @@ void synthesise(
     /* Overlap add to previous samples */
 
     for(i=0; i<N-1; i++) {
-       Sn_[i] += Sw_[FFT_DEC-N+1+i].real*Pn[i];
+       Sn_[i] += sw_[FFT_DEC-N+1+i].real*Pn[i];
     }
 
     if (shift)
        for(i=N-1,j=0; i<2*N; i++,j++)
-           Sn_[i] = Sw_[j].real*Pn[i];
+           Sn_[i] = sw_[j].real*Pn[i];
     else
        for(i=N-1,j=0; i<2*N; i++,j++)
-           Sn_[i] += Sw_[j].real*Pn[i];
+           Sn_[i] += sw_[j].real*Pn[i];
 }
 
index 64b7940182f667b60586e3abf9cce2f2c4203398..0a173a900daa7e8b468c0dfa61f405ac53c0bc25 100644 (file)
@@ -40,6 +40,6 @@ void estimate_amplitudes(MODEL *model, COMP Sw[], COMP W[]);
 float est_voicing_mbe(MODEL *model, COMP Sw[], COMP W[], COMP Sw_[],COMP Ew[], 
                      float prev_Wo);
 void make_synthesis_window(float Pn[]);
-void synthesise(float Sn_[], MODEL *model, float Pn[], int shift);
+void synthesise(kiss_fft_cfg fft_dec_cfg, float Sn_[], MODEL *model, float Pn[], int shift);
 
 #endif