added plphase.m to explore zero phase model some more
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Thu, 10 Sep 2009 04:11:10 +0000 (04:11 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Thu, 10 Sep 2009 04:11:10 +0000 (04:11 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@53 01035d8c-6547-0410-b346-abe4f91aad63

codec2/TODO.txt
codec2/octave/plphase.m [new file with mode: 0644]
codec2/src/dump.c
codec2/src/dump.h
codec2/src/phase.c
codec2/src/sinedec.c

index c8faad82c3fbf31f13030a9102d58058d4df3b83..098a927827d0d3930f87dbe4adf502b1183bffb6 100644 (file)
@@ -1,6 +1,8 @@
 TODO for codec2
 ---------------
 
+Planned tasks and half formed ideas for codec2.....
+
 [X] Convert files from DOS to Unix
 [X] Get sinenc and sinedec to build and run under gcc
 [ ] refactor
@@ -18,3 +20,19 @@ TODO for codec2
 [ ] Go through papers referenced in thesis and credit various
     techniques to papers.
       + sure there was somthing about zero phase synthesis is those papers
+
+[ ] voicing errors can be clearly seen in synthesised speech using pl2.m
+
+[ ] Voicing improvement
+    + voicing tracker, if enery about the same in frame n-1,n,n+1, and n-1, n+1
+      are voiced then make frame n voiced.
+
+[ ] mmt1 with zero phase model
+    + male with lots of background noise
+    + zero order synth model sounds more "clicky" on mmt1, this suggests less
+      dispersion
+    + could be due to IRS filtering or background noise
+    + exploring this could lead to better understanding on phase, e.g. add
+      noise to clean samples and explore effect on zero phase model
+    + wrote plphase.m to start analysing this
+
diff --git a/codec2/octave/plphase.m b/codec2/octave/plphase.m
new file mode 100644 (file)
index 0000000..4af9577
--- /dev/null
@@ -0,0 +1,182 @@
+% Copyright David Rowe 2009
+% This program is distributed under the terms of the GNU General Public License 
+% Version 2
+%
+% Plot phase modelling information from dump files.
+
+function plphase(samname, f)
+  
+  sn_name = strcat(samname,"_sn.txt");
+  Sn = load(sn_name);
+
+  sw_name = strcat(samname,"_sw.txt");
+  Sw = load(sw_name);
+
+  model_name = strcat(samname,"_model.txt");
+  model = load(model_name);
+
+  pw_name = strcat(samname,"_pw.txt");
+  if (file_in_path(".",pw_name))
+    Pw = load(pw_name);
+  endif
+
+  ak_name = strcat(samname,"_ak.txt");
+  if (file_in_path(".",ak_name))
+    ak = load(ak_name);
+  endif
+
+  phase_name = strcat(samname,"_phase.txt");
+  if (file_in_path(".",phase_name))
+    phase = load(phase_name);
+  endif
+
+  phase_name_ = strcat(samname,"_phase_.txt");
+  if (file_in_path(".",phase_name_))
+    phase_ = load(phase_name_);
+  endif
+
+  snr_name = strcat(samname,"_snr.txt");
+  if (file_in_path(".",snr_name))
+    snr = load(snr_name);
+  endif
+
+  sn_name_ = strcat(samname,".raw");
+  if (file_in_path(".",sn_name_))
+    fs_ = fopen(sn_name_,"rb");
+    sn_  = fread(fs_,Inf,"short");
+  endif
+
+  do 
+    figure(1);
+    clg;
+    s = [ Sn(2*f-1,:) Sn(2*f,:) ];
+    plot(s);
+    grid;
+    axis([1 length(s) -20000 20000]);
+
+    figure(2);
+    Wo = model(f,1);
+    L = model(f,2);
+    Am = model(f,3:(L+2));
+    plot((1:L)*Wo*4000/pi, 20*log10(Am),";Am;");
+    axis([1 4000 -10 80]);
+    hold on;
+    plot((0:255)*4000/256, Sw(f,:),";Sw;");
+    grid;
+
+    if (file_in_path(".",pw_name))
+       plot((0:255)*4000/256, 10*log10(Pw(f,:)),";Pw;");
+    endif      
+
+    if (file_in_path(".",snr_name))
+      snr_label = sprintf(";phase SNR %4.2f dB;",snr(f));
+      plot(1,1,snr_label);
+    endif
+
+    % phase model - determine SNR and error spectrum for phase model 1
+
+    if (file_in_path(".",phase_name_))
+      orig  = Am.*exp(j*phase(f,1:L));
+      synth = Am.*exp(j*phase_(f,1:L));
+      signal = orig * orig';
+      noise = (orig-synth) * (orig-synth)';
+      snr_phase = 10*log10(signal/noise);
+
+      phase_err_label = sprintf(";phase_err SNR %4.2f dB;",snr_phase);
+      plot((1:L)*Wo*4000/pi, 20*log10(orig-synth), phase_err_label);
+      hold off;
+    endif
+
+    if (file_in_path(".",phase_name))
+      figure(3);
+      plot((1:L)*Wo*4000/pi, phase(f,1:L)*180/pi, "-o;phase;");
+      axis;
+      if (file_in_path(".", phase_name_))
+        hold on;
+        plot((1:L)*Wo*4000/pi, phase_(f,1:L)*180/pi, ";phase_;");
+       grid
+       hold off;
+      endif
+    endif
+
+    % synthesised speech 
+
+    if (file_in_path(".",sn_name_))
+      figure(4);
+      s_ = sn_((f-3)*80+1:(f+1)*80);
+      plot(s_);
+      axis([1 length(s_) -20000 20000]);
+    endif
+
+    if (file_in_path(".",ak_name))
+      figure(5);
+      axis;
+      akw = ak(f,:);
+      weight = 1.0 .^ (0:length(akw)-1);
+      akw = akw .* weight;
+      H = 1./fft(akw,8000);
+      subplot(211);
+      plot(20*log10(abs(H(1:4000))),";LPC mag spec;");
+      grid;    
+      subplot(212);
+      plot(angle(H(1:4000))*180/pi,";LPC phase spec;");
+      grid;
+    endif
+
+    hold off;
+
+    % autocorrelation function to research voicing est
+    
+    %M = length(s);
+    %sw = s .* hanning(M)';
+    %for k=0:159
+    %  R(k+1) = sw(1:320-k) * sw(1+k:320)';
+    %endfor
+    %figure(4);
+    %R_label = sprintf(";R(k) %3.2f;",max(R(20:159))/R(1));
+    %plot(R/R(1),R_label);
+    %grid
+
+    figure(2);
+
+    % interactive menu
+
+    printf("\rframe: %d  menu: n-next  b-back  p-png  q-quit ", f);
+    fflush(stdout);
+    k = kbhit();
+    if (k == 'n')
+      f = f + 1;
+    endif
+    if (k == 'b')
+      f = f - 1;
+    endif
+
+    % optional print to PNG
+
+    if (k == 'p')
+    
+      pngname = sprintf("%s_%d",samname,f);
+
+      % small image
+
+      __gnuplot_set__ terminal png size 420,300
+      ss = sprintf("__gnuplot_set__ output \"%s.png\"", pngname);
+      eval(ss)
+      replot;
+
+      % larger image
+
+      __gnuplot_set__ terminal png size 800,600
+      ss = sprintf("__gnuplot_set__ output \"%s_large.png\"", pngname);
+      eval(ss)
+      replot;
+
+      % for some reason I need this to stop large plot getting wiped
+      __gnuplot_set__ output "/dev/null"
+
+    endif
+
+  until (k == 'q')
+  printf("\n");
+
+endfunction
index c52d574b955537b9bc47d7c20a9e2aeea8c2a735..9e5ad14c7ae0bf8638b8b069e518fc645ee03bb0 100644 (file)
@@ -46,6 +46,7 @@ static FILE *fe = NULL;
 static FILE *fsq = NULL;
 static FILE *fdec = NULL;
 static FILE *fsnr = NULL;
+static FILE *fak = NULL;
 
 static char  prefix[MAX_STR];
 
@@ -83,6 +84,8 @@ void dump_off(){
        fclose(fdec);
     if (fsnr != NULL)
        fclose(fsnr);
+    if (fak != NULL)
+       fclose(fak);
 }
 
 void dump_Sn(float Sn[]) {
@@ -231,7 +234,7 @@ void dump_snr(float snr) {
 
     if (!dumpon) return;
 
-    if (fphase_ == NULL) {
+    if (fsnr == NULL) {
        sprintf(s,"%s_snr.txt", prefix);
        fsnr = fopen(s, "wt");
        assert(fsnr != NULL);
@@ -274,6 +277,23 @@ void dump_lsp(float lsp[]) {
     fprintf(flsp,"\n");    
 }
 
+void dump_ak(float ak[], int order) {
+    int i;
+    char s[MAX_STR];
+
+    if (!dumpon) return;
+
+    if (fak == NULL) {
+       sprintf(s,"%s_ak.txt", prefix);
+       fak = fopen(s, "wt");
+       assert(fak != NULL);
+    }
+
+    for(i=0; i<=order; i++)
+       fprintf(fak,"%f\t",ak[i]);
+    fprintf(fak,"\n");    
+}
+
 void dump_Fw(COMP Fw[]) {
     int i;
     char s[MAX_STR];
index 2d3bbc25b20539e4ab440745c5ce0fc566e4ef7a..59ccb3482fe3c37d487ad4fb9dc65fd9375aa270 100644 (file)
@@ -38,6 +38,7 @@ void dump_model(MODEL *m);
 void dump_quantised_model(MODEL *m);
 void dump_Pw(COMP Pw[]);
 void dump_lsp(float lsp[]);
+void dump_ak(float ak[], int order);
 
 /* phase modelling */
 
index ffb132af76b501648f50aad44a25c6d34fbd05ea..f455fd1e7bfb934a5add9756b66b4cda8437ac50 100644 (file)
@@ -32,8 +32,8 @@
 #include <assert.h>
 #include <string.h>
 
-#define VTHRESH1 3.0
-#define VTHRESH2 3.0
+#define VTHRESH1 2.0
+#define VTHRESH2 2.0
 
 /*---------------------------------------------------------------------------*\
 
@@ -133,7 +133,7 @@ float phase_model_first_order(
 ) 
 {
   float G;                     /* LPC gain */
-  int i,m;
+  int   m;
 
   float E,Emin;                        /* current and minimum error so far */
   int P;                       /* current pitch period */
@@ -162,8 +162,8 @@ float phase_model_first_order(
 
   #ifdef NO_LPC_PHASE
   /* useful for testing with Sn[] an impulse train */
-  for(i=1; i<=PHASE_LPC_ORD; i++)
-     aks[i] = 0;
+  for(m=1; m<=PHASE_LPC_ORD; m++)
+     aks[m] = 0;
   #endif
   G = 1.0;
   aks_to_H(&model,aks,G,H,PHASE_LPC_ORD);
index 9d72a274a18759084df6a197052dd5709a3fce34..d2dee22f095218f08d722a5d65c9d2ba335f7474 100644 (file)
@@ -217,7 +217,9 @@ int main(int argc, char *argv[])
        else
            assert(order == PHASE_LPC_ORD);
 
+       dump_ak(ak, PHASE_LPC_ORD);
        snr = phase_model_first_order(ak, H, &n_min, &min_Am);
+
        dump_snr(snr);
        if (phase_model == 0) {
            phase_synth_zero_order(snr, H, &prev_Wo, &ex_phase);