new 20ms interpolation working OK for hts1a
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Tue, 24 Aug 2010 02:32:16 +0000 (02:32 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Tue, 24 Aug 2010 02:32:16 +0000 (02:32 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@184 01035d8c-6547-0410-b346-abe4f91aad63

codec2/octave/plinterp.m [new file with mode: 0644]
codec2/src/c2sim.c
codec2/src/code.sh [deleted file]
codec2/src/interp.c
codec2/unittest/Makefile
codec2/unittest/tinterp.c

diff --git a/codec2/octave/plinterp.m b/codec2/octave/plinterp.m
new file mode 100644 (file)
index 0000000..794a085
--- /dev/null
@@ -0,0 +1,11 @@
+load ../unittest/tinterp_prev.txt;
+load ../unittest/tinterp_interp.txt;
+load ../unittest/tinterp_next.txt;
+
+clf;
+plot(tinterp_prev(:,1), 20.0*log10(tinterp_prev(:,2)),";prev;")
+hold on;
+plot(tinterp_interp(:,1), 20.0*log10(tinterp_interp(:,2)),'g+-;interp;')
+plot(tinterp_next(:,1), 20.0*log10(tinterp_next(:,2)),'ro-;next;')
+hold off;
+axis([0 pi 0 80])
index a84ea5914bebc884be1caaf0ecc6e320e69b1e55..4049f3b7cfd402789297538b0742f922ae473b86 100644 (file)
@@ -67,6 +67,8 @@ register char *argv[];  /* array of command line arguments in string form */
   return 0;
 }
 
+void synth_one_frame(short buf[], MODEL *model, float Sn_[], float Pn[]);
+
 /*---------------------------------------------------------------------------*\
                                                                           
                                MAIN                                        
@@ -89,6 +91,8 @@ int main(int argc, char *argv[])
   int   frames;
   float prev_Wo;
   float pitch;
+  int   voiced1;
+  float phi1[MAX_AMP];
 
   char  out_file[MAX_STR];
   int   arg;
@@ -97,7 +101,8 @@ int main(int argc, char *argv[])
 
   int lpc_model, order;
   int lsp, lsp_quantiser;
-  float ak[LPC_MAX+1];
+  float ak[LPC_MAX];
+  COMP  Sw_[FFT_ENC];
   
   int dump;
   
@@ -110,8 +115,10 @@ int main(int argc, char *argv[])
   int   hand_voicing;
   FILE *fvoicing;
 
-  MODEL model_1, model_2, model_3, model_synth, model_a, model_b;
-  int transition, decimate;
+  MODEL prev_model, interp_model, tmp_model;
+  int decimate;
+  float tmp_phi = 0;
+  float ak_phase[LPC_ORD+1];
 
   for(i=0; i<M; i++)
       Sn[i] = 1.0;
@@ -120,16 +127,15 @@ int main(int argc, char *argv[])
 
   prev_Wo = TWO_PI/P_MAX;
 
-  model_1.Wo = TWO_PI/P_MIN;
-  model_1.L = floor(PI/model_1.Wo);
-  for(i=1; i<=model_1.L; i++) {
-      model_1.A[i] = 0.0;
-      model_1.phi[i] = 0.0;
+  prev_model.Wo = TWO_PI/P_MIN;
+  prev_model.L = floor(PI/prev_model.Wo);
+  for(i=1; i<=prev_model.L; i++) {
+      prev_model.A[i] = 0.0;
+      prev_model.phi[i] = 0.0;
   }
   for(i=1; i<=MAX_AMP; i++) {
       ex_phase[i] = 0.0;
   }
-  model_synth = model_3 = model_2 = model_1;
 
   if (argc < 2) {
       printf("\nCodec2 - 2400 bit/s speech codec - Simulation Program\n");
@@ -198,7 +204,6 @@ int main(int argc, char *argv[])
   postfilt = switch_present("--postfilter",argc,argv);
 
   decimate = switch_present("--dec",argc,argv);
-  transition = 0;
 
   /* Initialise ------------------------------------------------------------*/
 
@@ -219,7 +224,6 @@ int main(int argc, char *argv[])
       Sn[i] = Sn[i+N];
     for(i=0; i<N; i++)
       Sn[i+M-N] = buf[i];
-    dump_Sn(Sn);
  
     /* Estimate pitch */
 
@@ -229,7 +233,7 @@ int main(int argc, char *argv[])
 
     /* estimate model parameters */
 
-    dft_speech(Sw, Sn, w); dump_Sw(Sw);   
+    dft_speech(Sw, Sn, w); 
     two_stage_pitch_refinement(&model, Sw);
     estimate_amplitudes(&model, Sw, W);
     dump_Sn(Sn); dump_Sw(Sw); dump_model(&model);
@@ -238,9 +242,7 @@ int main(int argc, char *argv[])
 
     if (phase0) {
        float Wn[M];                    /* windowed speech samples */
-       float ak_phase[LPC_ORD+1];      /* autocorrelation coeffs  */
        float Rk[LPC_ORD+1];            /* autocorrelation coeffs  */
-       COMP  Sw_[FFT_ENC];
        
        dump_phase(&model.phi[0], model.L);
 
@@ -272,13 +274,10 @@ int main(int argc, char *argv[])
 
        for(i=0; i<MAX_AMP; i++)
            model.phi[i] = 0;
+       
        if (hand_voicing) {
            fscanf(fvoicing,"%d\n",&model.voiced);
        }
-       phase_synth_zero_order(&model, ak_phase, ex_phase);
-       
-       if (postfilt)
-           postfilter(&model, &bg_est);
     }
  
     /* optional LPC model amplitudes */
@@ -291,64 +290,63 @@ int main(int argc, char *argv[])
 
     /* option decimation to 20ms rate, which enables interpolation
        routine to synthesise in between frame */
-#ifdef FIX_ME_FOR_NEW_INTERP_ROUTINE
+  
     if (decimate) {
-       if (frames%2) {
-
-           /* odd frames use the original model parameters */
-
-           model_synth = model_2;
-           transition = 0;
-       }
-       else {
-           interp(&model_3, &model_1, &model_synth, &model_a, &model_b, 
-                  &transition);
-
-           /* phase need to be supplied outside of this routine, e.g. via
-              a phase model */
-              
-           for(i=1; i<=model_synth.L; i++)
-               model_synth.phi[i] = model_2.phi[i];
+       if (!phase0) {
+           printf("needs --phase0 to resample phase for interpolated Wo\n");
+           exit(0);
        }
 
-       model_3 = model_2;
-       model_2 = model_1;
-       model_1 = model;
-       model = model_synth;
-    }
-#endif
-    /* 
-       Simulate Wo quantisation noise
-       model.Wo += 2.0*(PI/8000)*(1.0 - 2.0*(float)rand()/RAND_MAX);
-       if (model.Wo > TWO_PI/20.0) model.Wo = TWO_PI/20.0;
-       if (model.Wo < TWO_PI/160.0) model.Wo = TWO_PI/160.0;
-       model.L = floor(PI/model.Wo);   
-    */
-
-    /* Synthesise speech */
-
-    if (fout != NULL) {
-
-       if (transition) {
-           synthesise(Sn_,&model_a,Pn,1);
-           synthesise(Sn_,&model_b,Pn,0);
-       }
-       else {
-           synthesise(Sn_,&model,Pn,1);
-       }
+       /* odd frame - interpolate */
 
-       /* Save output speech to disk */
+       if (frames%2) {
 
-       for(i=0; i<N; i++) {
-           if (Sn_[i] > 32767.0)
-               buf[i] = 32767;
-           else if (Sn_[i] < -32767.0)
-               buf[i] = -32767;
+           #ifdef TEST
+           model.voiced = 1;
+           prev_model.voiced = 1;
+           if (fabs(prev_model.Wo - model.Wo) < 0.1*model.Wo) {
+               interp_model.voiced = 1;
+               interpolate(&interp_model, &prev_model, &model);
+               for(i=0; i<=interp_model.L; i++) {
+                   interp_model.phi[i] = phi1[i];
+               }
+               printf("interp\n");
+           }
            else
-               buf[i] = Sn_[i];
+               interp_model = tmp_model;
+           #endif
+
+           interp_model.voiced = voiced1;
+           interpolate(&interp_model, &prev_model, &model);
+           
+           if (phase0)
+               phase_synth_zero_order(&interp_model, ak_phase, ex_phase);      
+           if (postfilt)
+               postfilter(&interp_model, &bg_est);
+           synth_one_frame(buf, &interp_model, Sn_, Pn);
+           fwrite(buf,sizeof(short),N,fout);
+
+           if (phase0)
+               phase_synth_zero_order(&model, ak_phase, ex_phase);     
+           if (postfilt)
+               postfilter(&model, &bg_est);
+           synth_one_frame(buf, &model, Sn_, Pn);
+           fwrite(buf,sizeof(short),N,fout);
+
+           prev_model = model;
+       }
+       else {
+           voiced1 = model.voiced;
        }
+    }
+    else {
+       if (phase0)
+           phase_synth_zero_order(&model, ak_phase, ex_phase); 
+       if (postfilt)
+           postfilter(&model, &bg_est);
+       synth_one_frame(buf, &model, Sn_, Pn);
        fwrite(buf,sizeof(short),N,fout);
-    }    
+    }
   }
 
   if (fout != NULL)
@@ -366,3 +364,19 @@ int main(int argc, char *argv[])
   return 0;
 }
 
+void synth_one_frame(short buf[], MODEL *model, float Sn_[], float Pn[])
+{
+    int     i;
+
+    synthesise(Sn_, model, Pn, 1);
+
+    for(i=0; i<N; i++) {
+       if (Sn_[i] > 32767.0)
+           buf[i] = 32767;
+       else if (Sn_[i] < -32767.0)
+           buf[i] = -32767;
+       else
+           buf[i] = Sn_[i];
+    }
+
+}
diff --git a/codec2/src/code.sh b/codec2/src/code.sh
deleted file mode 100755 (executable)
index adeddc4..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/sh
-# code.sh
-# David Rowe 10 Sep 2009
-#
-# Run steps to encode a speech sample
-
-../unittest/tnlp ../raw/$1.raw ../unittest/$1_nlp.p
-../src/sinenc ../raw/$1.raw $1.mdl 300 ../unittest/$1_nlp.p
-../src/sinedec ../raw/$1.raw $1.mdl -o $1_uq.raw
-../src/sinedec ../raw/$1.raw $1.mdl --phase0 -o $1_phase0.raw --postfilter
-../src/sinedec ../raw/$1.raw $1.mdl --lpc 10 -o $1_lpc10.raw
-../src/sinedec ../raw/$1.raw $1.mdl --lpc 10 --lsp -o $1_lsp.raw
-../src/sinedec ../raw/$1.raw $1.mdl --phase0 --lpc 10 -o $1_phase0_lpc10.raw --postfilter
-../src/sinedec ../raw/$1.raw $1.mdl --phase0 --lpc 10 --lsp -o $1_phase0_lsp.raw --postfilter
-
index 2bdf38c22f220a012a16470414eda5cf915879e6..5ccee79434fb0c284287d8e9024e8512c66a59c2 100644 (file)
@@ -103,8 +103,8 @@ float sample_log_amp(MODEL *model, float w)
 
     assert(w > 0.0); assert (w <= PI);
 
-    m = floor(w/model->Wo);
-    f = w - m*model->Wo;
+    m = floor(w/model->Wo + 0.5);
+    f = (w - m*model->Wo)/w;
     assert(f <= 1.0);
 
     if (m < 1) {
index 35143c20a6c29666a070d443f8e9a65ce75dc265..56657d618c8ae67bb3d55bade3eca95de1ffc477 100644 (file)
@@ -12,7 +12,7 @@ TNLP_OBJ     = tnlp.o ../src/sine.o ../src/nlp.o ../src/four1.o ../src/dump.o
 TCONTPHASE_OBJ = tcontphase.o ../src/globals.o ../src/dump.o ../src/synth.o \
                  ../src/four1.c ../src/initdec.o ../src/phase.o
 
-TINTERP_OBJ    = tinterp.o ../src/interp.o
+TINTERP_OBJ    = tinterp.o ../src/sine.o ../src/four1.o ../src/interp.o
 
 TQUANT_OBJ     = tquant.o ../src/quantise.o ../src/lpc.o ../src/lsp.o \
                  ../src/dump.o ../src/four1.o
index f0cf8539d557bbc42dd51957969ea3d71f77ca7f..dcd69b70eadc7873653b8e516fc59161c4c63502 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
+#include <ctype.h>
 
 #include "defines.h"
-#include "dump.h"
+#include "sine.h"
 #include "interp.h"
 
-int main() {
-    MODEL  prev, next, interp;
-    int    l;
+void make_amp(MODEL *model, float f0, float cdB, float mdBHz)
+{
+    int   i;
+    float mdBrad = mdBHz*FS/TWO_PI;
+
+    model->Wo = f0*TWO_PI/FS;
+    model->L  = PI/model->Wo;
+    for(i=0; i<=model->L; i++)
+       model->A[i] = pow(10.0,(cdB + (float)i*model->Wo*mdBrad)/20.0);
+    model->voiced = 1;
+}
+
+void write_amp(char file[], MODEL *model)
+{
     FILE  *f;
+    int    i;
 
-    f = fopen("interp.txt","wt");
+    f = fopen(file,"wt");
+    for(i=1; i<=model->L; i++)
+       fprintf(f, "%f\t%f\n", model->Wo*i, model->A[i]);
+    fclose(f);
+}
 
-    prev.Wo = PI/20;
-    prev.L = PI/prev.Wo;
-    prev.voiced = 1;
-    for(l=1;l<=prev.L; l++) 
-       prev.A[l] = 1000.0;
+char *get_next_float(char *s, float *num)
+{
+    char *p = s;
+    char  tmp[MAX_STR];
 
-    next.Wo = PI/30;
-    next.L = PI/next.Wo;
-    next.voiced = 1;
-    for(l=1;l<=next.L; l++) 
-       next.A[l] = 2000.0;
+    while(*p && !isspace(*p)) 
+       p++;
+    memcpy(tmp, s, p-s);
+    tmp[p-s] = 0;
+    *num = atof(tmp);
 
-    interp.voiced = 1.0;
-    interpolate(&interp, &prev, &next);
-    printf("Wo = %f\n", interp.Wo);
-    for(l=1; l<=interp.L; l++)
-       fprintf(f, "%f\n", interp.A[l]);
+    return p+1;
+}
+
+char *get_next_int(char *s, int *num)
+{
+    char *p = s;
+    char  tmp[MAX_STR];
+
+    while(*p && !isspace(*p)) 
+       p++;
+    memcpy(tmp, s, p-s);
+    tmp[p-s] = 0;
+    *num = atoi(tmp);
+
+    return p+1;
+}
 
+void load_amp(MODEL *model, char file[], int frame)
+{
+    FILE *f;
+    int   i;
+    char  s[1024];
+    char *ps;
+
+    f = fopen(file,"rt");
+
+    for(i=0; i<frame; i++)
+       fgets(s, 1023, f);
+
+    ps = s;
+    ps = get_next_float(ps, &model->Wo);
+    ps = get_next_int(ps, &model->L);
+    for(i=1; i<=model->L; i++)
+       ps = get_next_float(ps, &model->A[i]);
+       
     fclose(f);
+}
+
+int main() {
+    MODEL  prev, next, interp;
+
+    //make_amp(&prev, 50.0, 60.0, 6E-3);
+    //make_amp(&next, 50.0, 40.0, 6E-3);
+    load_amp(&prev, "../src/hts1a_model.txt", 32);
+    load_amp(&next, "../src/hts1a_model.txt", 34);
+
+    interp.voiced = 1;
+    interpolate(&interp, &prev, &next);
+
+    write_amp("tinterp_prev.txt", &prev);
+    write_amp("tinterp_interp.txt", &interp);
+    write_amp("tinterp_next.txt", &next);
+
     return 0;
 }