added BPF option to c2sim
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 12 Apr 2015 01:49:41 +0000 (01:49 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 12 Apr 2015 01:49:41 +0000 (01:49 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@2114 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/octave/bpf.m [new file with mode: 0644]
codec2-dev/src/bpf.h [new file with mode: 0644]
codec2-dev/src/c2sim.c
codec2-dev/src/cohpsk.c

diff --git a/codec2-dev/octave/bpf.m b/codec2-dev/octave/bpf.m
new file mode 100644 (file)
index 0000000..5649a5c
--- /dev/null
@@ -0,0 +1,31 @@
+% bpf.m
+% David Rowe April 2015
+%
+% Design 400-2600 Hz BPF and save coeffs 
+
+1;
+
+function write_c_array(filename, arrayname, vec)
+
+  m = length(vec);
+
+  f=fopen(filename,"wt");
+  fprintf(f,"#define %s_N %d\n\n", toupper(arrayname), m);
+  fprintf(f,"float %s[]={\n", arrayname);
+  for r=1:m
+    if r < m
+      fprintf(f, "  %f,\n", vec(r));
+    else
+      fprintf(f, "  %f\n};", vec(r));
+    end
+  end
+
+  fclose(f);
+endfunction
+
+b=firls(100,[0 400 500 2400 2600 4000]/4000,[0.01 0.01 1 1 0.01 0.01]);
+freqz(b)
+write_c_array("../src/bpf.h", "bpf", b)
+
+% C header file of noise samples so C version gives extacly the same results
+
diff --git a/codec2-dev/src/bpf.h b/codec2-dev/src/bpf.h
new file mode 100644 (file)
index 0000000..9cc916f
--- /dev/null
@@ -0,0 +1,105 @@
+#define BPF_N 101
+
+float bpf[]={
+  0.002174,
+  0.003245,
+  0.002147,
+  0.001866,
+  0.002764,
+  0.000567,
+  -0.001641,
+  -0.000565,
+  -0.002415,
+  -0.005837,
+  -0.003620,
+  -0.002828,
+  -0.006268,
+  -0.002787,
+  0.001963,
+  -0.001234,
+  0.001446,
+  0.009200,
+  0.005331,
+  0.003521,
+  0.011821,
+  0.006951,
+  -0.002015,
+  0.005137,
+  0.001828,
+  -0.013390,
+  -0.007058,
+  -0.003273,
+  -0.020458,
+  -0.014321,
+  0.001751,
+  -0.012891,
+  -0.009730,
+  0.018993,
+  0.008544,
+  0.000534,
+  0.035755,
+  0.029074,
+  -0.001192,
+  0.030852,
+  0.030983,
+  -0.029834,
+  -0.009550,
+  0.011945,
+  -0.081971,
+  -0.082875,
+  0.000423,
+  -0.133526,
+  -0.211778,
+  0.182628,
+  0.514906,
+  0.182628,
+  -0.211778,
+  -0.133526,
+  0.000423,
+  -0.082875,
+  -0.081971,
+  0.011945,
+  -0.009550,
+  -0.029834,
+  0.030983,
+  0.030852,
+  -0.001192,
+  0.029074,
+  0.035755,
+  0.000534,
+  0.008544,
+  0.018993,
+  -0.009730,
+  -0.012891,
+  0.001751,
+  -0.014321,
+  -0.020458,
+  -0.003273,
+  -0.007058,
+  -0.013390,
+  0.001828,
+  0.005137,
+  -0.002015,
+  0.006951,
+  0.011821,
+  0.003521,
+  0.005331,
+  0.009200,
+  0.001446,
+  -0.001234,
+  0.001963,
+  -0.002787,
+  -0.006268,
+  -0.002828,
+  -0.003620,
+  -0.005837,
+  -0.002415,
+  -0.000565,
+  -0.001641,
+  0.000567,
+  0.002764,
+  0.001866,
+  0.002147,
+  0.003245,
+  0.002174
+};
\ No newline at end of file
index 1e294a7b13342161e65935b537cc4d35dea573b6..c80f9646a098c24c7a7b0ca58f5416d36a0a567d 100644 (file)
@@ -48,6 +48,7 @@
 #include "interp.h"
 #include "ampexp.h"
 #include "phaseexp.h"
+#include "bpf.h"
 
 void synth_one_frame(kiss_fft_cfg fft_inv_cfg, short buf[], MODEL *model, float Sn_[], float Pn[], int prede, float *de_mem, float gain);
 void print_help(const struct option *long_options, int num_opts, char* argv[]);
@@ -64,8 +65,9 @@ int main(int argc, char *argv[])
     FILE *fout = NULL; /* output speech file                    */
     FILE *fin;         /* input speech file                     */
     short buf[N];      /* input/output buffer                   */
+    float buf_float[N];
     float Sn[M];       /* float input speech samples            */
-    float Sn_pre[M];   /* pre-emphasised input speech samples   */
+    float Sn_pre[N];   /* pre-emphasised input speech samples   */
     COMP  Sw[FFT_ENC]; /* DFT of Sn[]                           */
     kiss_fft_cfg  fft_fwd_cfg;
     kiss_fft_cfg  fft_inv_cfg;
@@ -130,6 +132,8 @@ int main(int argc, char *argv[])
     struct PEXP *pexp = NULL;
     struct AEXP *aexp = NULL;
     float gain = 1.0;
+    int   bpf_en = 0;
+    float bpf_buf[BPF_N+N];
 
     char* opt_string = "ho:";
     struct option long_options[] = {
@@ -164,6 +168,7 @@ int main(int argc, char *argv[])
         { "vq_pitch_e", no_argument, &vector_quant_Wo_e, 1 },
         { "rate", required_argument, NULL, 0 },
         { "gain", required_argument, NULL, 0 },
+        { "bpf", no_argument, &bpf_en, 1 },
         #ifdef DUMP
         { "dump", required_argument, &dump, 1 },
         #endif
@@ -356,6 +361,11 @@ int main(int argc, char *argv[])
     if (ampexp)
        aexp = amp_experiment_create();
 
+    if (bpf_en) {
+        for(i=0; i<BPF_N; i++)
+            bpf_buf[i] = 0.0;
+    }
+
     /*----------------------------------------------------------------*\
 
                             Main Loop
@@ -366,19 +376,33 @@ int main(int argc, char *argv[])
     sum_snr = 0;
     while(fread(buf,sizeof(short),N,fin)) {
        frames++;
-       //printf("frame: %d ", frames);
 
-       /* Read input speech */
+       for(i=0; i<N; i++)
+           buf_float[i] = buf[i];
+
+       /* optionally filter input speech */
        
+        if (prede) {
+           pre_emp(Sn_pre, buf_float, &pre_mem, N);
+           for(i=0; i<N; i++)
+                buf_float[i] = Sn_pre[i];
+        }
+         
+        if (bpf_en) {
+            for(i=0; i<BPF_N; i++)
+                bpf_buf[i] =  bpf_buf[N+i];
+            for(i=0; i<N; i++)
+                bpf_buf[BPF_N+i] = buf_float[i];
+            inverse_filter(&bpf_buf[BPF_N], bpf, N, buf_float, BPF_N);
+       }
+
+        /* shift buffer of input samples, and insert new samples */
+
        for(i=0; i<M-N; i++) {
            Sn[i] = Sn[i+N];
-           Sn_pre[i] = Sn_pre[i+N];
        }
        for(i=0; i<N; i++)
-           Sn[i+M-N] = buf[i];
-
-       pre_emp(&Sn_pre[M-N], &Sn[M-N], &pre_mem, N);
-       
+           Sn[i+M-N] = buf_float[i];
 
        /*------------------------------------------------------------*\
 
@@ -435,15 +459,8 @@ int main(int argc, char *argv[])
 
            /* find aks here, these are overwritten if LPC modelling is enabled */
 
-           if (prede) {
-               for(i=0; i<M; i++)
-                   Wn[i] = Sn_pre[i]*w[i];
-           }
-           else {
-           
-               for(i=0; i<M; i++)
-                   Wn[i] = Sn[i]*w[i];
-           }
+            for(i=0; i<M; i++)
+                Wn[i] = Sn[i]*w[i];
            autocorrelate(Wn,Rk,M,order);
            levinson_durbin(Rk,ak,order);
 
@@ -480,10 +497,7 @@ int main(int argc, char *argv[])
 
        if (lpc_model) {
            
-           if (prede)
-               e = speech_to_uq_lsps(lsps, ak, Sn_pre, w, order);
-           else
-               e = speech_to_uq_lsps(lsps, ak, Sn, w, order);
+            e = speech_to_uq_lsps(lsps, ak, Sn, w, order);
 
             #ifdef DUMP
            dump_ak(ak, order);
index e1a624967b84bbfca25bbcf71a482cce3e348e5e..9c8ee662f31058bb393dfdb37990a7b22ffc0f23 100644 (file)
@@ -8,13 +8,14 @@
                  
   TODO:
 
-  [ ] Code to plot EB/No v BER curves for
+  [ ] Code to plot EB/No v BER curves to char perf for
       [ ] AWGN channel
       [ ] freq offset
       [ ] fading channel
       [ ] freq drift
       [ ] timing drift
   [ ] tune perf/impl loss to get closer to ideal
+      [X] linear interp of phase for better fading perf
   [ ] freq offset/drift feedback loop 
   [ ] smaller freq est block size to min ram req
                                                       
@@ -253,7 +254,8 @@ void qpsk_symbols_to_bits(struct COHPSK *coh, int rx_bits[], COMP ct_symb_buf[][
        end */
 
     for(c=0; c<PILOTS_NC; c++) {
-#ifdef OLD
+//#define AVERAGE
+#ifdef AVERAGE
         corr.real = 0.0; corr.imag = 0.0; mag = 0.0;
         for(p=0; p<NPILOTSFRAME+2; p++) {
             corr = cadd(corr, fcmult(coh->pilot2[p][c], ct_symb_buf[sampling_points[p]][c]));
@@ -266,7 +268,7 @@ void qpsk_symbols_to_bits(struct COHPSK *coh, int rx_bits[], COMP ct_symb_buf[][
             coh->phi_[r][c] = phi_;
             coh->amp_[r][c] = amp_;
         }
-#endif
+#else
 
         /* set up lin reg model and interpolate phase */
 
@@ -291,6 +293,7 @@ void qpsk_symbols_to_bits(struct COHPSK *coh, int rx_bits[], COMP ct_symb_buf[][
         for(r=0; r<NSYMROW; r++) {
              coh->amp_[r][c] = amp_;
         }
+#endif
     }
 
     /* now correct phase of data symbols and make decn on bits */