fine freq tracking loop working
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Thu, 26 Apr 2012 05:46:53 +0000 (05:46 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Thu, 26 Apr 2012 05:46:53 +0000 (05:46 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@383 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/octave/tfdmdv.m
codec2-dev/src/fdmdv.c
codec2-dev/src/fdmdv_internal.h
codec2-dev/unittest/tfdmdv.c

index a1f8cf06111b6adc61b091bb57ef65a541e18e15..34216ee323f2900086949580f1c76e73b4b312a7 100644 (file)
@@ -20,7 +20,7 @@ frames = 25;
 prev_tx_symbols = ones(Nc+1,1);
 prev_rx_symbols = ones(Nc+1,1);
 foff_phase_rect = 1;
-track = 0;
+coarse_fine = 0;
 fest_state = 0;
 
 % Octave outputs we want to collect for comparison to C version
@@ -45,7 +45,7 @@ rx_timing_log = [];
 rx_symbols_log = [];
 rx_bits_log = []; 
 sync_bit_log = [];  
-track_log = [];
+coarse_fine_log = [];
 
 for f=1:frames
 
@@ -68,10 +68,9 @@ for f=1:frames
   [pilot prev_pilot pilot_lut_index prev_pilot_lut_index] = get_pilot(pilot_lut_index, prev_pilot_lut_index, M);
 
   [foff_coarse S1 S2] = rx_est_freq_offset(rx_fdm, pilot, prev_pilot, M);
-  if track == 0
+  if coarse_fine == 0
     foff = foff_coarse;
   end
-  foff_log = [foff_log foff];
   foff_coarse_log = [foff_coarse_log foff_coarse];
 
   pilot_baseband1_log = [pilot_baseband1_log pilot_baseband1];
@@ -105,11 +104,13 @@ for f=1:frames
   rx_bits_log = [rx_bits_log rx_bits]; 
   foff_fine_log = [foff_fine_log foff_fine];
   sync_bit_log = [sync_bit_log sync_bit];  
+  foff -= 0.5*foff_fine;
+  foff_log = [foff_log foff];
 
   % freq est state machine
 
-  [track fest_state] = freq_state(sync_bit, fest_state);
-  track_log = [track_log track];
+  [coarse_fine fest_state] = freq_state(sync_bit, fest_state);
+  coarse_fine_log = [coarse_fine_log coarse_fine];
 end
 
 % Compare to the output from the C version
@@ -189,7 +190,7 @@ plot_sig_and_error(9, 211, foff_coarse_log, foff_coarse_log - foff_coarse_log_c,
 plot_sig_and_error(9, 212, foff_fine_log, foff_fine_log - foff_fine_log_c, 'Fine Freq Offset' )
 
 plot_sig_and_error(10, 211, foff_log, foff_log - foff_log_c, 'Freq Offset' )
-plot_sig_and_error(10, 212, track_log, track_log - track_log_c, 'Freq Track' )
+plot_sig_and_error(10, 212, coarse_fine_log, coarse_fine_log - coarse_fine_log_c, 'Freq Est Coarse(0) Fine(1)', [1 frames -0.5 1.5] )
 
 c=15;
 plot_sig_and_error(11, 211, real(rx_baseband_log(c,:)), real(rx_baseband_log(c,:) - rx_baseband_log_c(c,:)), 'Rx baseband real' )
@@ -199,7 +200,7 @@ plot_sig_and_error(12, 211, real(rx_filt_log(c,:)), real(rx_filt_log(c,:) - rx_f
 plot_sig_and_error(12, 212, imag(rx_filt_log(c,:)), imag(rx_filt_log(c,:) - rx_filt_log_c(c,:)), 'Rx filt imag' )
 
 plot_sig_and_error(13, 211, env_log, env_log - env_log_c, 'env' )
-plot_sig_and_error(13, 212, real(rx_symbols_log(c,:)), real(rx_symbols_log(c,:) - rx_symbols_log_c(c,:)), 'rx symbols' )
+stem_sig_and_error(13, 212, real(rx_symbols_log(c,:)), real(rx_symbols_log(c,:) - rx_symbols_log_c(c,:)), 'rx symbols' )
 
 st=10*28;
 en = 12*28;
@@ -251,5 +252,6 @@ check(rx_timing_log, rx_timing_log_c, 'rx_timing');
 check(rx_symbols_log, rx_symbols_log_c, 'rx_symbols');
 check(rx_bits_log, rx_bits_log_c, 'rx bits');
 check(sync_bit_log, sync_bit_log_c, 'sync bit');
+check(coarse_fine_log, coarse_fine_log_c, 'coarse_fine');
 
 printf("\npasses: %d fails: %d\n", passes, fails);
index abf6b94af354c54b2645157deabb7c80e9a80aae..b59c438eefd91473b5f3be4a2da45c6c82826b5f 100644 (file)
@@ -222,13 +222,14 @@ struct FDMDV *fdmdv_create(void)
        f->pilot_lpf1[i].imag = f->pilot_lpf2[i].imag = 0.0;
     }
 
+    f->foff = 0.0;
     f->foff_rect.real = 1.0;
     f->foff_rect.imag = 0.0;
     f->foff_phase_rect.real = 1.0;
     f->foff_phase_rect.imag = 0.0;
 
     f->fest_state = 0;
-    f->track = 0;
+    f->coarse_fine = COARSE;
 
     return f;
 }
@@ -1020,7 +1021,7 @@ void fdmdv_put_test_bits(struct FDMDV *f, int *sync, int *bit_errors, int rx_bit
 
 int freq_state(int sync_bit, int *state)
 {
-    int next_state, track;
+    int next_state, coarse_fine;
 
     /* acquire state, look for 6 symbol 010101 sequence from sync bit */
 
@@ -1081,11 +1082,11 @@ int freq_state(int sync_bit, int *state)
 
     *state = next_state;
     if (*state >= 6)
-       track = 1;
+       coarse_fine = FINE;
     else
-       track = 0;
+       coarse_fine = COARSE;
  
-    return track;
+    return coarse_fine;
 }
 
 /*---------------------------------------------------------------------------*\
@@ -1107,7 +1108,7 @@ int freq_state(int sync_bit, int *state)
 
 void fdmdv_demod(struct FDMDV *fdmdv, int rx_bits[], int *sync_bit, float rx_fdm[], int *nin)
 {
-    float         foff;
+    float         foff_coarse, foff_fine;
     COMP          rx_fdm_fcorr[M+M/P];
     COMP          rx_baseband[NC+1][M+M/P];
     COMP          rx_filt[NC+1][P+1];
@@ -1118,15 +1119,20 @@ void fdmdv_demod(struct FDMDV *fdmdv, int rx_bits[], int *sync_bit, float rx_fdm
 
     /* freq offset estimation and correction */
 
-    foff = rx_est_freq_offset(fdmdv, rx_fdm, *nin);
-    freq_shift(rx_fdm_fcorr, rx_fdm, foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, *nin);
+    foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm, *nin);
+    if (fdmdv->coarse_fine == COARSE)
+       fdmdv->foff = foff_coarse;
+    freq_shift(rx_fdm_fcorr, rx_fdm, fdmdv->foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, *nin);
        
     /* baseband processing */
 
     fdm_downconvert(rx_baseband, rx_fdm_fcorr, fdmdv->phase_rx, fdmdv->freq, *nin);
     rx_filter(rx_filt, rx_baseband, fdmdv->rx_filter_memory, *nin);
     rx_timing = rx_est_timing(rx_symbols, rx_filt, rx_baseband, fdmdv->rx_filter_mem_timing, env, fdmdv->rx_baseband_mem_timing, *nin);         
-    ferr = qpsk_to_bits(rx_bits, sync_bit, fdmdv->prev_rx_symbols, rx_symbols);
+    foff_fine = qpsk_to_bits(rx_bits, sync_bit, fdmdv->prev_rx_symbols, rx_symbols);
     memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(NC+1));
+
+    fdmdv->coarse_fine = freq_state(*sync_bit, &fdmdv->fest_state);
+    fdmdv->foff  -= 0.5*foff_fine;
 }
 
index 7982ff6b01f9c93ea5cf244511ecfbce2ca3b136..b9bd89348d2de082e41ccb4fb576830b59d66eb7 100644 (file)
 #define NPILOTLPF                  (4*M)    /* number of samples we DFT pilot over, pilot est window */
 #define MPILOTFFT                    256
 
+/* freq offset sestimation states */
+
+#define COARSE                   0
+#define FINE                     1
+
 /*---------------------------------------------------------------------------*\
                                                                              
                                STRUCT for States
@@ -90,6 +95,7 @@ struct FDMDV {
     COMP S1[MPILOTFFT];
     COMP S2[MPILOTFFT];
 
+    float foff;
     COMP foff_rect;
     COMP foff_phase_rect;
     
@@ -100,7 +106,7 @@ struct FDMDV {
     COMP prev_rx_symbols[NC+1];
     
     int  fest_state;
-    int  track;
+    int  coarse_fine;
 };
 
 /*---------------------------------------------------------------------------*\
index 3b8277e68f6632fd5483367fdb46ec58a92d3979..68024d0c5edbcf6c57bd0b49ee8a9bb0814cb7f8 100644 (file)
@@ -53,7 +53,6 @@ int main(int argc, char *argv[])
     COMP          tx_fdm[M];
     float         rx_fdm[M+M/P];
     float         foff_coarse;
-    float         foff;
     int           nin;
     COMP          rx_fdm_fcorr[M+M/P];
     COMP          rx_baseband[NC+1][M+M/P];
@@ -88,7 +87,7 @@ int main(int argc, char *argv[])
     int           rx_bits_log[FDMDV_BITS_PER_FRAME*FRAMES];
     float         foff_fine_log[FRAMES];
     int           sync_bit_log[FRAMES];
-    int           track_log[FRAMES];
+    int           coarse_fine_log[FRAMES];
 
     FILE         *fout;
     int           f,c,i;
@@ -121,9 +120,9 @@ int main(int argc, char *argv[])
        /* freq offset estimation and correction */
 
        foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm, nin);
-       if (fdmdv->track == 0)
-           foff = foff_coarse;
-       freq_shift(rx_fdm_fcorr, rx_fdm, foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, nin);
+       if (fdmdv->coarse_fine == COARSE)
+           fdmdv->foff = foff_coarse;
+       freq_shift(rx_fdm_fcorr, rx_fdm, fdmdv->foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, nin);
        
        /* baseband processing */
 
@@ -132,8 +131,9 @@ int main(int argc, char *argv[])
        rx_timing = rx_est_timing(rx_symbols, rx_filt, rx_baseband, fdmdv->rx_filter_mem_timing, env, fdmdv->rx_baseband_mem_timing, nin);       
        foff_fine = qpsk_to_bits(rx_bits, &sync_bit, fdmdv->prev_rx_symbols, rx_symbols);
        memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(NC+1));
-       fdmdv->track = freq_state(sync_bit, &fdmdv->fest_state);
-       
+       fdmdv->coarse_fine = freq_state(sync_bit, &fdmdv->fest_state);
+       fdmdv->foff  -= 0.5*foff_fine;
+
        /* --------------------------------------------------------*\
                            Log each vector 
        \*---------------------------------------------------------*/
@@ -154,7 +154,7 @@ int main(int argc, char *argv[])
        memcpy(&S1_log[f*MPILOTFFT], fdmdv->S1, sizeof(COMP)*MPILOTFFT);
        memcpy(&S2_log[f*MPILOTFFT], fdmdv->S2, sizeof(COMP)*MPILOTFFT);
        foff_coarse_log[f] = foff_coarse;
-       foff_log[f] = foff;
+       foff_log[f] = fdmdv->foff;
 
        /* rx down conversion */
 
@@ -185,7 +185,7 @@ int main(int argc, char *argv[])
        foff_fine_log[f] = foff_fine;
        sync_bit_log[f] = sync_bit;
 
-       track_log[f] = fdmdv->track;
+       coarse_fine_log[f] = fdmdv->coarse_fine;
     }
 
 
@@ -218,7 +218,7 @@ int main(int argc, char *argv[])
     octave_save_int(fout, "rx_bits_log_c", rx_bits_log, 1, FDMDV_BITS_PER_FRAME*FRAMES);
     octave_save_float(fout, "foff_fine_log_c", foff_fine_log, 1, FRAMES);  
     octave_save_int(fout, "sync_bit_log_c", sync_bit_log, 1, FRAMES);  
-    octave_save_int(fout, "track_log_c", track_log, 1, FRAMES);  
+    octave_save_int(fout, "coarse_fine_log_c", coarse_fine_log, 1, FRAMES);  
     fclose(fout);
 
     codec2_destroy(fdmdv);