tcohpsk.c not crashing, syncing up OK
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 17 May 2015 01:26:20 +0000 (01:26 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 17 May 2015 01:26:20 +0000 (01:26 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@2131 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/octave/cohpsk.m
codec2-dev/octave/tcohpsk.m
codec2-dev/src/cohpsk.c
codec2-dev/src/cohpsk_internal.h
codec2-dev/unittest/tcohpsk.c

index 78a1794afd174fd1f25007eab98dd7ab57bc8e38..6be9c59155aaa36ab18a6fd6e2f7b835b233a235 100644 (file)
@@ -643,7 +643,7 @@ function [ch_symb rx_timing rx_filt rx_baseband afdmdv f_est] = rate_Fs_rx_proce
 endfunction
 
 
-void update_ct_symb_buf(COMP ct_symb_buf, ch_symb, Nct_sym_buf, Nsymbrowpilot)
+function ct_symb_buf = update_ct_symb_buf(ct_symb_buf, ch_symb, Nct_sym_buf, Nsymbrowpilot)
 
   % update memory in symbol buffer
 
@@ -719,13 +719,16 @@ function [next_sync cohpsk] = frame_sync_fine_freq_est(cohpsk, ch_symb, sync, ne
 
   if sync == 1
     corr = 0; mag = 0;
+    f_fine_rect = exp(-j*cohpsk.f_fine_est*2*pi*sampling_points/Rs)';
     for c=1:Nc*Nd
+      f_corr_vec = f_fine_rect .* ct_symb_buf(cohpsk.ct+sampling_points,c);
       for p=1:length(sampling_points)
-        corr += pilot2(p, c-Nc*floor((c-1)/Nc)) * ct_symb_buf(cohpsk.ct + sampling_points,c);
+        corr += pilot2(p, c-Nc*floor((c-1)/Nc)) * f_corr_vec(p);
         mag  += abs(f_corr_vec(p));
       end
     end
     cohpsk.ratio = abs(corr)/mag;
+    %printf("f_fine_est: %f ratio: %f\n", cohpsk.f_fine_est, cohpsk.ratio);
   end
 
 endfunction
index 42954845570ed0e1faf6a53a124eeb89ea50087d..c529ec1aa465e9168462c282dee9e6fd658d9eeb 100644 (file)
@@ -26,13 +26,41 @@ randn('state',1);
 % [ ] set up various tests we use to characterise modem for easy 
 %     repeating when we change modem
 
-frames = 100;
-foff = -40;
-dfoff = -0.5/Fs;
-EsNodB = 12;
-fading_en = 1;
-hf_delay_ms = 2;
-compare_with_c = 0;
+test = 'compare to c';
+
+if strcmp(test, 'compare to c')
+  frames = 10;
+  foff =  0;
+  dfoff = 0;
+  EsNodB = 12;
+  fading_en = 0;
+  hf_delay_ms = 2;
+  compare_with_c = 1;
+end
+
+% should be BER around 0.015 to 0.02
+
+if strcmp(test, 'awgn')
+  frames = 100;
+  foff =  0;
+  dfoff = 0;
+  EsNodB = 8;
+  fading_en = 0;
+  hf_delay_ms = 2;
+  compare_with_c = 0;
+end
+
+% Similar to AWGN - should be BER around 0.015 to 0.02
+
+if strcmp(test, 'fading');
+  frames = 100;
+  foff = -40;
+  dfoff = -0.5/Fs;
+  EsNodB = 12;
+  fading_en = 1;
+  hf_delay_ms = 2;
+  compare_with_c = 0;
+end
 
 EsNo = 10^(EsNodB/10);
 
index e3e88bf89d511142f30012e7b70f9ccfb8e38f59..d158a0e7dc35ce61be2467512615a60e563d16d3 100644 (file)
@@ -431,16 +431,15 @@ void frame_sync_fine_freq_est(struct COHPSK *coh, COMP ch_symb[][COHPSK_NC*ND],
 
         coh->ff_rect.real = cosf(coh->f_fine_est*2.0*M_PI/RS);
         coh->ff_rect.imag = -sinf(coh->f_fine_est*2.0*M_PI/RS);
-        fprintf(stderr, "  fine freq f: %f max_corr: %f max_mag: %f ct: %d\n", coh->f_fine_est, max_corr, max_mag, coh->ct);
+        fprintf(stderr, "  [%d]   fine freq f: %6.2f max_coff: %f max_mag: %f ct: %d\n", coh->frame, coh->f_fine_est, max_corr, max_mag, coh->ct);
  
         if (max_corr/max_mag > 0.7) {
-            fprintf(stderr, "  [%d] encouraging sync word!\n", coh->frame);
+            fprintf(stderr, "  [%d]   encouraging sync word!\n", coh->frame);
             coh->sync_timer = 0;
             *next_sync = 1;
         }
         else {
             *next_sync = 0;
-            fprintf(stderr, "  [%d]  back to coarse freq offset est...\n", coh->frame);
         }
         coh->ratio = max_corr/max_mag;        
     }
@@ -458,7 +457,7 @@ void update_ct_symb_buf(COMP ct_symb_buf[][COHPSK_NC*ND], COMP ch_symb[][COHPSK_
             ct_symb_buf[r][c] = ct_symb_buf[r+NSYMROWPILOT][c];
     }
   
-    for(r=NCT_SYMB_BUF-NSYMROWPILOT, i=0; r<NSYMROWPILOT; r++, i++) {
+    for(r=NCT_SYMB_BUF-NSYMROWPILOT, i=0; r<NCT_SYMB_BUF; r++, i++) {
         for(c=0; c<COHPSK_NC*ND; c++)
             ct_symb_buf[r][c] = ch_symb[i][c];
     }
@@ -474,7 +473,7 @@ int sync_state_machine(struct COHPSK *coh, int sync, int next_sync)
 
         /* check that sync is still good, fall out of sync on consecutive bad frames */
 
-        corr_with_pilots(&corr, &mag, coh, coh->ct, 0.0);
+        corr_with_pilots(&corr, &mag, coh, coh->ct, coh->f_fine_est);
         // printf("%f\n", cabsolute(corr)/mag);
 
         if (cabsolute(corr)/mag < 0.5) 
@@ -589,11 +588,12 @@ void rate_Fs_rx_processing(struct COHPSK *coh, COMP ch_symb[NSYMROWPILOT][COHPSK
 
         if (coh->rx_baseband_log) {
             for(c=0; c<COHPSK_NC*ND; c++) {       
-            for(i=0; i<nin; i++) {
-              coh->rx_baseband_log[c*(M+M/P)*LOG_FRAMES*NSYMROWPILOT + coh->rx_baseband_log_col_index + i] = rx_baseband[c][i]; 
+                for(i=0; i<nin; i++) {
+                    coh->rx_baseband_log[c*(M+M/P)*LOG_FRAMES*NSYMROWPILOT + coh->rx_baseband_log_col_index + i] = rx_baseband[c][i]; 
+                }
             }
-          }
-          coh->rx_baseband_log_col_index += nin;        
+            coh->rx_baseband_log_col_index += nin;
+            assert(coh->rx_baseband_log_col_index <= coh->rx_baseband_log_col_sz);
         }
 
         if (coh->rx_filt_log) {
@@ -609,12 +609,13 @@ void rate_Fs_rx_processing(struct COHPSK *coh, COMP ch_symb[NSYMROWPILOT][COHPSK
     if (coh->ch_symb_log) {
        for(r=0; r<NSYMROWPILOT; r++, coh->ch_symb_log_r++) {
             for(c=0; c<COHPSK_NC*ND; c++) {
-               coh->ch_symb_log[coh->ch_symb_log_r*COHPSK_NC*ND* + c] = ch_symb[r][c]; 
+               coh->ch_symb_log[coh->ch_symb_log_r*COHPSK_NC*ND + c] = ch_symb[r][c]; 
             }
         }
     }
 }
 
+
 /*---------------------------------------------------------------------------*\
                                                        
   FUNCTION....: cohpsk_demod()      
@@ -646,8 +647,9 @@ void cohpsk_demod(struct COHPSK *coh, int rx_bits[], int *reliable_sync_bit, COM
 
     for (i=0; i<(NSW-1)*NSYMROWPILOT*M; i++)
         coh->ch_fdm_frame_buf[i] = coh->ch_fdm_frame_buf[i+NSYMROWPILOT*M];
-    for (j=0; i<NSYMROWPILOT*M; i++,j++)
+    for (j=0; i<NSW*NSYMROWPILOT*M; i++,j++)
         coh->ch_fdm_frame_buf[i] = rx_fdm[j];
+    //printf("i: %d j: %d rx_fdm[0]: %f %f\n", i,j, rx_fdm[0].real, rx_fdm[0].imag);
 
     next_sync = sync = coh->sync;
     nin = M;
@@ -697,7 +699,7 @@ void cohpsk_demod(struct COHPSK *coh, int rx_bits[], int *reliable_sync_bit, COM
             frame_sync_fine_freq_est(coh, &ch_symb[(NSW-1)*NSYMROWPILOT], sync, &next_sync);
 
             if (fabs(coh->f_fine_est) > 2.0) {
-                fprintf(stderr, "  [%d] Hmm %f is a bit big so back to coarse est ...\n", coh->frame, coh->f_fine_est);
+                fprintf(stderr, "  [%d] Hmm %f is a bit big :(\n", coh->frame, coh->f_fine_est);
                 next_sync = 0;
             }
         }
index 76d81525dd3db1f8e2cda44745ef2c37acd8f6d8..ddbb8084bd436808d00b2b8146dbe0f99d6c8189 100644 (file)
@@ -34,7 +34,7 @@
                                                         one copy */
 #define NSW              3                           /* number of sync window frames */
 
-#define LOG_FRAMES       35
+#define LOG_FRAMES       2                           /* todo: re-arrange logging and tcohpsk.c so this can be moved */
 
 #include "fdmdv_internal.h"
 #include "kiss_fft.h"
@@ -66,6 +66,7 @@ struct COHPSK {
 
     COMP           *rx_baseband_log;
     int            rx_baseband_log_col_index;
+    int            rx_baseband_log_col_sz;
     COMP           *rx_filt_log;
     int            rx_filt_log_col_index;
     COMP           *ch_symb_log;
index 74371dc402868a6a58d52f6a442af8ac50f40d0b..8afee7556b8183d86c6926269ec257ff1a3b7039 100644 (file)
 #include "comp_prim.h"
 #include "noise_samples.h"
 
-#define FRAMES LOG_FRAMES  /* #defined in cohpsk_internal.h */
-#define RS     50
-#define FOFF   10.5
-#define ESNODB 8.0
+#define FRAMES      LOG_FRAMES            /* LOG_FRAMES is #defined in cohpsk_internal.h                        */
+#define SYNC_FRAMES 12                    /* sync state uses up extra log storage as we reprocess several times */
+#define FRAMESL     (SYNC_FRAMES*FRAMES)  /* worst case is every frame is out of sync                           */
+
+#define RS          50
+#define FOFF        10.5
+#define ESNODB      8.0
 
 extern float pilots_coh[][PILOTS_NC];
 
@@ -98,10 +101,11 @@ int main(int argc, char *argv[])
     fdmdv = coh->fdmdv;
     assert(coh != NULL);
 
-    coh->rx_filt_log = (COMP *)malloc(sizeof(COMP)*COHPSK_NC*ND*(P+1)*NSYMROWPILOT*FRAMES);
-    coh->rx_baseband_log = (COMP *)malloc(sizeof(COMP)*COHPSK_NC*ND*(M+M/P)*NSYMROWPILOT*FRAMES);
-    coh->ch_symb_log = (COMP *)malloc(sizeof(COMP)*NSYMROWPILOT*FRAMES*COHPSK_NC*ND);
-
+    coh->rx_filt_log = (COMP *)malloc(sizeof(COMP)*COHPSK_NC*ND*(P+1)*NSYMROWPILOT*FRAMESL);
+    coh->rx_baseband_log_col_sz = M*NSYMROWPILOT*FRAMESL;
+    coh->rx_baseband_log = (COMP *)malloc(sizeof(COMP)*COHPSK_NC*ND*(M+M/P)*NSYMROWPILOT*FRAMESL);
+    coh->ch_symb_log = (COMP *)malloc(sizeof(COMP)*NSYMROWPILOT*FRAMESL*COHPSK_NC*ND);
+    
     log_r = log_data_r = noise_r = log_bits = 0;
     ptest_bits_coh = (int*)test_bits_coh;
     ptest_bits_coh_end = (int*)test_bits_coh + sizeof(test_bits_coh)/sizeof(int);
@@ -119,7 +123,8 @@ int main(int argc, char *argv[])
     /* Main Loop ---------------------------------------------------------------------*/
 
     for(f=0; f<FRAMES; f++) {
-        
+        coh->frame = f;
+
        /* --------------------------------------------------------*\
                                  Mod
        \*---------------------------------------------------------*/
@@ -205,7 +210,7 @@ int main(int argc, char *argv[])
     //octave_save_complex(fout, "rx_fdm_frame_bb_log_c", (COMP*)rx_fdm_frame_bb_log, 1, M*NSYMROWPILOT*FRAMES, M*NSYMROWPILOT*FRAMES);  
     octave_save_complex(fout, "rx_baseband_log_c", (COMP*)coh->rx_baseband_log, COHPSK_NC*ND, coh->rx_baseband_log_col_index, (M+M/P)*FRAMES*NSYMROWPILOT);  
     octave_save_complex(fout, "rx_filt_log_c", (COMP*)coh->rx_filt_log, COHPSK_NC*ND, coh->rx_filt_log_col_index, (P+1)*FRAMES*NSYMROWPILOT);  
-    octave_save_complex(fout, "ch_symb_log_c", (COMP*)coh->ch_symb_log, NSYMROWPILOT*FRAMES, COHPSK_NC*ND, COHPSK_NC*ND);  
+    octave_save_complex(fout, "ch_symb_log_c", (COMP*)coh->ch_symb_log, coh->ch_symb_log_r, COHPSK_NC*ND, COHPSK_NC*ND);  
     octave_save_complex(fout, "ct_symb_ff_log_c", (COMP*)ct_symb_ff_log, NSYMROWPILOT*FRAMES, COHPSK_NC*ND, COHPSK_NC*ND);  
     octave_save_float(fout, "rx_amp_log_c", (float*)rx_amp_log, log_data_r, COHPSK_NC*ND, COHPSK_NC*ND);  
     octave_save_float(fout, "rx_phi_log_c", (float*)rx_phi_log, log_data_r, COHPSK_NC*ND, COHPSK_NC*ND);