inital framework for phase experiments
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 20 Jul 2012 03:31:05 +0000 (03:31 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 20 Jul 2012 03:31:05 +0000 (03:31 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@594 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/c2sim.c
codec2-dev/src/dump.c
codec2-dev/src/dump.h
codec2-dev/src/phase.c
codec2-dev/src/phase.h

index 5a412ea2ad0fe08991b2353c6a65d5161fdda363..62b79098be98812840b29fb698c67646d233d45d 100644 (file)
@@ -50,6 +50,7 @@
 void synth_one_frame(kiss_fft_cfg fft_inv_cfg, short buf[], MODEL *model, float Sn_[], float Pn[]);
 void print_help(const struct option *long_options, int num_opts, char* argv[]);
 
+
 /*---------------------------------------------------------------------------*\
                                                                           
                                MAIN                                        
@@ -94,7 +95,7 @@ int main(int argc, char *argv[])
     int   postfilt;
     float bg_est;
 
-    int   hand_voicing = 0, phasetest = 0;
+    int   hand_voicing = 0, phaseexp = 0;
     FILE *fvoicing = 0;
 
     MODEL prev_model, interp_model;
@@ -118,6 +119,7 @@ int main(int argc, char *argv[])
     #ifdef DUMP
     int   dump;
     #endif
+    struct PEXP *pexp = NULL;
 
     char* opt_string = "ho:";
     struct option long_options[] = {
@@ -131,7 +133,7 @@ int main(int argc, char *argv[])
         { "lspdt_mode", required_argument, NULL, 0 },
         { "lspjvm", no_argument, &lspjvm, 1 },
         { "phase0", no_argument, &phase0, 1 },
-        { "phasetest", no_argument, &phasetest, 1 },
+        { "phaseexp", no_argument, &phaseexp, 1 },
         { "postfilter", no_argument, &postfilt, 1 },
         { "hand_voicing", required_argument, &hand_voicing, 1 },
         { "dec", no_argument, &decimate, 1 },
@@ -314,6 +316,8 @@ int main(int argc, char *argv[])
     make_analysis_window(fft_fwd_cfg, w, W);
     make_synthesis_window(Pn);
     quantise_init();
+    if (phaseexp)
+       pexp = phase_experiment_create();
 
     /*----------------------------------------------------------------*\
 
@@ -352,6 +356,9 @@ int main(int argc, char *argv[])
        dump_Sn(Sn); dump_Sw(Sw); dump_model(&model);
         #endif
 
+       if (phaseexp)
+           phase_experiment(pexp, &model);
+
        /*------------------------------------------------------------*\
 
                             Zero-phase modelling
@@ -724,6 +731,8 @@ int main(int argc, char *argv[])
     if (lpc_model)
        printf("SNR av = %5.2f dB\n", sum_snr/frames);
 
+    if (phaseexp)
+       phase_experiment_destroy(pexp);
     #ifdef DUMP
     if (dump)
        dump_off();
index ba406a34992b3255f5838591b7d5aaff663db098..aca0b29c321770007bed7e2d6aad8ce376c51f2d 100644 (file)
@@ -55,6 +55,7 @@ static FILE *fak = NULL;
 static FILE *fbg = NULL;
 static FILE *fE = NULL;
 static FILE *frk = NULL;
+static FILE *fhephase = NULL;
 
 static char  prefix[MAX_STR];
 
@@ -104,6 +105,8 @@ void dump_off(){
        fclose(fE);
     if (frk != NULL)
        fclose(frk);
+    if (fhephase != NULL)
+       fclose(fhephase);
 }
 
 void dump_Sn(float Sn[]) {
@@ -262,6 +265,25 @@ void dump_phase_(float phase_[], int L) {
     fprintf(fphase_,"\n");    
 }
 
+
+void dump_hephase(int ind[], int dim) {
+    int m;
+    char s[MAX_STR];
+
+    if (!dumpon) return;
+
+    if (fhephase == NULL) {
+       sprintf(s,"%s_hephase.txt", prefix);
+       fhephase = fopen(s, "wt");
+       assert(fhephase != NULL);
+    }
+
+    for(m=0; m<dim; m++)
+       fprintf(fhephase,"%d\t",ind[m]);
+    fprintf(fhephase,"\n");    
+}
+
+
 void dump_snr(float snr) {
     char s[MAX_STR];
 
index 547d3cf606a705115135975399590894b336048e..79afe057532fe8e1b95dda3d5995fc6d9b59ff79 100644 (file)
@@ -51,6 +51,7 @@ void dump_lpc_snr(float snr);
 void dump_snr(float snr);
 void dump_phase(float phase[], int L);
 void dump_phase_(float phase[], int L);
+void dump_hephase(int ind[], int dim);
 
 /* NLP states */
 
index 9c838cd0a94f71f69ee91cd72a1caa03d0fbbd2b..4ac7893118f3d650c0f5400784e5b69f732e8e71 100644 (file)
@@ -263,3 +263,285 @@ void phase_synth_zero_order(
   }
 
 }
+
+/* states for phase experiments */
+
+struct PEXP {
+    float phi_prev[MAX_AMP];
+    float Wo_prev;
+};
+
+
+/*---------------------------------------------------------------------------* \
+
+  phase_experiment_create()
+
+  Inits states for phase quantisation experiments.
+
+\*---------------------------------------------------------------------------*/
+
+struct PEXP * phase_experiment_create() {
+    struct PEXP *pexp;
+    int i;
+
+    pexp = (struct PEXP *)malloc(sizeof(struct PEXP));
+    assert (pexp != NULL);
+
+    for(i=0; i<MAX_AMP; i++)
+       pexp->phi_prev[i] = 0.0;
+    pexp->Wo_prev = 0.0;
+
+    return pexp;
+}
+
+
+/*---------------------------------------------------------------------------* \
+
+  phase_experiment_destroy()
+
+\*---------------------------------------------------------------------------*/
+
+void phase_experiment_destroy(struct PEXP *pexp) {
+    assert(pexp != NULL);
+    free(pexp);
+}
+
+
+struct AMPINDEX {
+    float amp;
+    int   index;
+};
+
+static void bubbleSort(struct AMPINDEX numbers[], int array_size)
+{
+    int i, j;
+    struct AMPINDEX temp;
+  for (i = (array_size - 1); i > 0; i--)
+  {
+    for (j = 1; j <= i; j++)
+    {
+      if (numbers[j-1].amp < numbers[j].amp)
+      {
+        temp = numbers[j-1];
+        numbers[j-1] = numbers[j];
+        numbers[j] = temp;
+      }
+    }
+  }
+}
+
+/*---------------------------------------------------------------------------* \
+
+  phase_experiment()
+
+  Phase quantisation experiments.
+
+\*---------------------------------------------------------------------------*/
+
+void phase_experiment(struct PEXP *pexp, MODEL *model) {
+    int i;
+
+    assert(pexp != NULL);
+
+    //#define AMP
+    #ifdef AMP
+    {
+       int r = 0;
+       float max = 0;
+       for(i=1; i<model.L/4; i++)
+           if (model.A[i] > max) max = model.A[i];
+       for(i=1; i<model.L/4; i++) {
+           if (model.A[i] < 0.25*max) {
+               model.phi[i] += (PI/2)*(1.0 - 2.0*(float)rand()/RAND_MAX);
+               r++;
+           }
+       }
+       printf("r %d L/4 %d\n", r, model.L/4);
+    }
+    #endif
+
+    //#define AMP1
+    #ifdef AMP1
+    {
+       int r = 0, nr = 0, st, dim;
+       int top, j, max_harm, n_harm;
+       struct AMPINDEX sorted[MAX_AMP];
+       int ind[MAX_AMP];
+
+       st = 1;
+       dim = model.L/4;
+
+       for(i=st,j=1; i<=st+dim; i++,j++) {
+           sorted[j].amp = model.A[i];
+           sorted[j].index = i;
+       }
+       bubbleSort(&sorted[1], dim);
+       n_harm = max_harm = 0;
+       if (max_harm > dim)
+           n_harm = dim;
+
+       for(i=st; i<=st+dim; i++) {             
+           top = 0;
+           for(j=1; j<=n_harm; j++)
+               if (model.A[i] == sorted[j].amp)
+                   top = 1;
+               
+           if (!top) {
+               model.phi[i] = 0.0; /* make sure */
+               //model.phi[i] = PI*(1.0 - 2.0*(float)rand()/RAND_MAX);
+               model.phi[i] = phi_prev[i] + i*N*model.Wo;
+               r++;
+           }
+           else
+               nr++;
+       }
+       printf("r %d nr %d dim %d\n", r, nr, dim);
+           
+       for(i=0; i<n_harm; i++)
+           ind[i] = sorted[i+1].index;
+       for(i=n_harm; i<max_harm; i++)
+           ind[i] = 0;
+       dump_hephase(ind, max_harm);
+    }
+
+    #ifdef UPPER
+    for(i=3*model.L/4; i<=model.L; i++) {
+       //model.phi[i] = phi_prev[i] + i*N*model.Wo;
+       model.phi[i] = PI*(1.0 - 2.0*(float)rand()/RAND_MAX);
+    }
+    #endif
+    for(i=1; i<=model.L; i++)
+       phi_prev[i] = model.phi[i];         
+    #endif
+
+    //#define HF
+    #ifdef HF
+    for(i=1; i<model.L/4; i++)
+       model.phi[i] += (PI/8)*(1.0 - 2.0*(float)rand()/RAND_MAX);
+       
+    for(i=model.L/4; i<3*model.L/4; i++)
+       model.phi[i] += (PI/4)*(1.0 - 2.0*(float)rand()/RAND_MAX);
+       
+    for(i=3*model.L/4; i<=model.L; i++)
+       model.phi[i] += (PI/2)*(1.0 - 2.0*(float)rand()/RAND_MAX);      
+    #endif
+
+    //#define QUANT
+    #ifdef QUANT
+    for(i=1; i<=MAX_AMP; i++)
+       model.phi[i] += (PI/4)*(1.0 - 2.0*(float)rand()/RAND_MAX);
+    #endif
+
+    //#define PRED
+    #ifdef PRED
+    for(i=1; i<=MAX_AMP; i++) {
+       if ((frames % 2) != 0) {
+           /* predict on even frames */
+           model.phi[i] = phi_prev[i] + N*i*(model.Wo);
+       }
+       else {          
+           /* 2 bit quantise on odd */
+           model.phi[i] += (PI/8)*(1.0 - 2.0*(float)rand()/RAND_MAX);
+       }
+       phi_prev[i] = model.phi[i];         
+       Wo_prev = model.Wo;
+    }  
+    //if ((frames % 2) != 0)
+    //    printf("frame: %d\n", frames);
+    #endif
+
+    #define PRED_ERR
+    #ifdef PRED_ERR
+    for(i=model->L/4+1; i<=model->L/2; i++) {
+       float pred = pexp->phi_prev[i] + N*i*(model->Wo);
+       float err = pred - model->phi[i];
+       err = atan2(sin(err),cos(err));
+       printf("%f\n",err);
+    }
+    #endif
+
+    //#define PRED2
+    #ifdef PRED2
+    /*
+      if (fabs(model.Wo - Wo_prev) > 0.1*model.Wo) {
+      for(i=1; i<=model.L/2; i++)
+      phi_prev[i] = (PI/8)*(1.0 - 2.0*(float)rand()/RAND_MAX);
+      }
+      else
+      printf("%d\n", frames);
+    */
+           
+    for(i=1; i<=model.L/4; i++) {
+       model.phi[i] = phi_prev[i] + N*i*(model.Wo);
+    }
+    #ifdef OLD
+    if ((frames % 2) != 0) {
+       /* predict on even frames */
+       for(i=model.L/4+1; i<=model.L; i++)
+           model.phi[i] = phi_prev[i] + N*i*(model.Wo);
+    }
+    else {             
+       /* 3 bit quantise on odd */
+       for(i=model.L/4+1; i<=model.L; i++) {
+           float pred = phi_prev[i] + N*i*(model.Wo);
+           if (pred > model.phi[i]) 
+               model.phi[i] = pred - PI/8;
+           else
+               model.phi[i] = pred + PI/8;
+                   
+           //model.phi[i] += (PI/8)*(1.0 - 2.0*(float)rand()/RAND_MAX);
+       }
+    }
+    #endif
+
+   #ifdef QUANT
+   for(i=model.L/4+1; i<=model.L/2; i++) {
+       float pred = phi_prev[i] + N*i*(model.Wo);
+       float err = pred - model.phi[i];
+       err = atan2(sin(err),cos(err));
+       float interval = 0.25;
+       int ind = floor(err/(interval*PI)+0.5);
+       float qerr = ind*interval*PI;
+       //printf("%f %d %f\n", err, ind, ind*0.25*PI);
+       if (pred > model.phi[i]) 
+           model.phi[i] = pred - qerr;
+       else
+           model.phi[i] = pred + qerr;
+                   
+    }
+
+    for(i=model.L/2+1; i<=7*model.L/8; i++) {
+       float pred = phi_prev[i] + N*i*(model.Wo);
+       float err = pred - model.phi[i];
+       err = atan2(sin(err),cos(err));
+       float interval = 0.5;
+       int ind = floor(err/(interval*PI)+0.5);
+       float qerr = ind*interval*PI;
+       //printf("%f %d %f\n", err, ind, ind*0.25*PI);
+       if (pred > model.phi[i]) 
+           model.phi[i] = pred - qerr;
+       else
+           model.phi[i] = pred + qerr;
+                   
+    }
+    #endif
+
+    /*
+      for(i=1; i<=model.L/4; i++) {
+      model.A[i] = 0.0;
+      }
+      for(i=model.L/2+1; i<=model.L; i++) {
+      model.A[i] = 0.0;
+      }
+    */
+           
+    #endif
+
+    for(i=1; i<model->L; i++)
+       pexp->phi_prev[i] = model->phi[i];          
+    pexp->Wo_prev = model->Wo;
+
+
+}
+
index 725698d90530d483866172dfdd4008a0f8132dbf..d849f6b372438b5721f70b2739b51d8569fac7e6 100644 (file)
 void phase_synth_zero_order(kiss_fft_cfg fft_dec_cfg, MODEL *model, float aks[], 
                             float *ex_phase, int order);
 
+struct PEXP;
+
+struct PEXP * phase_experiment_create();
+void phase_experiment_destroy(struct PEXP *pexp);
+void phase_experiment(struct PEXP *pexp, MODEL *model);
+
 #endif