static FILE *fsnr = NULL;
 static FILE *fak = NULL;
 static FILE *fbg = NULL;
+static FILE *fE = NULL;
 
 static char  prefix[MAX_STR];
 
        fclose(fak);
     if (fbg != NULL)
        fclose(fbg);
+    if (fE != NULL)
+       fclose(fE);
 }
 
 void dump_Sn(float Sn[]) {
     fprintf(fbg,"%f\t%f\t%f\n", e, bg_est, percent_uv);    
 }
 
+void dump_E(float E) {
+    char s[MAX_STR];
 
+    if (!dumpon) return;
 
+    if (fE == NULL) {
+       sprintf(s,"%s_E.txt", prefix);
+       fE = fopen(s, "wt");
+       assert(fE != NULL);
+    }
 
+    fprintf(fE,"%f\n", 10.0*log10(E));
+}
 
 void dump_Pw(COMP Pw[]);
 void dump_lsp(float lsp[]);
 void dump_ak(float ak[], int order);
+void dump_E(float E);
 
 /* phase modelling */
 
 
--- /dev/null
+#!/bin/sh
+# fq20.shsh
+# David Rowe 27 July 2010
+# 
+# Decode a file with fully quantised codec at 20ms frame rate
+
+../src/sinedec ../raw/$1.raw $1.mdl -o $1_phase0_lsp_20_EWo2.raw --phase 0 --lpc 10 --lsp --postfilter --dec
+
 
 
 # compare to other codecs
 
-../script/menu.sh ../raw/$1.raw $1_phase0_lsp.raw ../raw/$1_g729a.raw ../raw/$1_gsm13k.raw ../raw/$1_speex_8k.raw $2 $3
+../script/menu.sh ../raw/$1.raw $1_phase0_lsp.raw $1_phase0_lsp_20.raw $1_phase0_lsp_20_EWo.raw ../raw/$1_g729a.raw ../raw/$1_gsm13k.raw ../raw/$1_speex_8k.raw $2 $3
 
 
 #
 # Run menu with common sample file options, headphone version
 
-../script/menu.sh ../raw/$1.raw $1_uq.raw $1_phase0.raw $1_lpc10.raw $1_lsp.raw $1_phase0_lpc10.raw $1_phase0_lsp.raw ../raw/$1_g729a.raw $2 $3 -d /dev/dsp1
+#../script/menu.sh ../raw/$1.raw $1_uq.raw $1_phase0.raw $1_lpc10.raw $1_lsp.raw $1_phase0_lpc10.raw $1_phase0_lsp.raw ../raw/$1_g729a.raw $2 $3 -d /dev/dsp1
+
+# compare to other codecs
+
+#../script/menu.sh ../raw/$1.raw $1_phase0_lsp.raw $1_phase0_lsp_20.raw ../raw/$1_g729a.raw ../raw/$1_gsm13k.raw ../raw/$1_speex_8k.raw $2 $3 -d /dev/dsp1
+
+../script/menu.sh ../raw/$1.raw $1_uq.raw $1_phase0.raw $1_test.raw ../raw/$1_g729a.raw $2 $3 -d /dev/dsp1
 
 
 
 #endif    
     /* used during development: copy remaining LSPs from orig if we haven't
        quantised all of them */
-    for(j=l; j<order; j++)
+    for(j=l; j<order; j++) 
        lsp_[j] = lsp[j];
 
     lsp_to_lpc(lsp_, &ak[1], order, NULL);
     dump_lsp(lsp);
   }
 
+  dump_E(E);
+  #ifdef SIM_QUANT
+  /* simulated LPC energy quantisation */
+  {
+      float e = 10.0*log10(E);
+      e += 2.0*(1.0 - 2.0*(float)rand()/RAND_MAX);
+      E = pow(10.0,e/10.0);
+  }
+  #endif
+
   aks_to_M2(ak,order,model,E,&snr);   /* {ak} -> {Am} LPC decode */
 
   #ifdef CLICKY
 
   int dump;
   
   int phase, phase_model;
-  float ex_phase[1];
+  float ex_phase[MAX_AMP+1];
   int voiced, voiced_1, voiced_2;
 
   int   postfilt;
       model_1.A[i] = 0.0;
       model_1.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 < 3) {
 
         //dump_phase_(&model.phi[0]);
     }
-
-    /* optional LPC model amplitudes */
+ 
+   /* optional LPC model amplitudes */
 
     if (lpc_model) {
        snr = lpc_model_amplitudes(Sn, &model, order, lsp, ak);
         dump_quantised_model(&model);
     }
 
-
     /* option decimation to 20ms rate, which enables interpolation
        routine to synthesise in between frame */
 
 
            model_synth = model_2;
            transition = 0;
-
        }
        else {
            interp(&model_3, &model_1, &model_synth, &model_a, &model_b, 
        model = model_synth;
     }
 
+    /* 
+       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) {