Working demod with coarse frame sync in C; still need wide freq. estimation and octav...
authorbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 11 Feb 2018 22:48:31 +0000 (22:48 +0000)
committerbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 11 Feb 2018 22:48:31 +0000 (22:48 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@3399 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/octave/ofdm_lib.m
codec2-dev/octave/tofdm.m
codec2-dev/src/ofdm.c
codec2-dev/unittest/tofdm.c

index b04c9a3065a56fba27384d8fc58547a5f1f5d76b..b68399935705874d17b3d224f6bae6989c4b22ba 100644 (file)
@@ -63,7 +63,7 @@ endfunction
         + more suitable for real time implementation
 #}
 
-function [t_est foff_est] = coarse_sync(states, rx, rate_fs_pilot_samples)
+function [t_est foff_est ratio] = coarse_sync(states, rx, rate_fs_pilot_samples)
     Nsamperframe = states.Nsamperframe; Fs = states.Fs;
     Npsam = length(rate_fs_pilot_samples);
     verbose  = states.verbose;
@@ -71,13 +71,18 @@ function [t_est foff_est] = coarse_sync(states, rx, rate_fs_pilot_samples)
     Ncorr = length(rx) - (Nsamperframe+Npsam) + 1;
     assert(Ncorr > 0);
     corr = zeros(1,Ncorr);
+    mag_max = 0;
     for i=1:Ncorr
       corr(i)   = abs(rx(i:i+Npsam-1) * rate_fs_pilot_samples');
       corr(i)  += abs(rx(i+Nsamperframe:i+Nsamperframe+Npsam-1) * rate_fs_pilot_samples');
+      mag_l = max( abs(rx(i)) , abs(rx(i+Nsamperframe)) );
+      mag_max = max(mag_max, mag_l);
     end
 
     [mx t_est] = max(abs(corr));
 
+    ratio = (mx + 1e-9) / (mag_max + 1e-9);
+
     C  = abs(fft(rx(t_est:t_est+Npsam-1) .* conj(rate_fs_pilot_samples), Fs));
     C += abs(fft(rx(t_est+Nsamperframe:t_est+Nsamperframe+Npsam-1) .* conj(rate_fs_pilot_samples), Fs));
 
@@ -433,16 +438,31 @@ function [rx_bits states aphase_est_pilot_log rx_np rx_amp] = ofdm_demod2(states
   ofdm_load_const;
   [rx_bits states aphase_est_pilot_log rx_np rx_amp] =ofdm_demod(states,rxbuf_in);
 
+
+  [t_est foff_est ratio] = coarse_sync(states,states.rxbuf,rate_fs_pilot_samples);
+  t_est = mod(t_est,Nsamperframe) - (M+Ncp);
+  printf("t_est is %d, foff_est is %d, ofdm_foff_est is %f, ratio %f\n",t_est,foff_est,states.foff_est_hz,ratio)
   %If out of sync, try a coarse sync on RX buffer
   if states.sync <= 0
-    [t_est foff_est] = coarse_sync(states,states.rxbuf,rate_fs_pilot_samples);
+    %[t_est foff_est ratio] = coarse_sync(states,states.rxbuf,rate_fs_pilot_samples);
     %Correct to be within one frame and at head of frame instead of after first symbol
-    t_est = mod(t_est,Nsamperframe) - (M+Ncp);
-    if(t_est > 30)
-      states.nin = t_est;
+    if(ratio > .5)
+      if(t_est > 12)
+        states.nin = t_est;
+      end
+      if(abs(foff_est - states.foff_est_hz) > 3)
+        states.foff_est_hz = foff_est;
+      end
+      printf("Syncing\n")
+      states.sync = 3;
+    end
+  else
+    if(ratio < .5)
+      states.sync = states.sync-1;
+    end
+    if(abs(foff_est - states.foff_est_hz) > 3)
+        states.foff_est_hz = foff_est;
     end
-
-    printf("t_est is %d, foff_est is %d\n",t_est,foff_est)
   end
 endfunction
 
index c0177a061b40fad5f31f24da4b4dbf0f9a027eb2..2302958a39dae7d8001ec0aea0aec1492f1b1e8f 100644 (file)
@@ -22,6 +22,19 @@ function nums = im_re_interleave(nim)
 
 end
 
+% Trim two input arrays down to the shortest common length
+function [a b] = trim1(ain, bin)
+  com = min(length(ain),length(bin));
+  a = ain(1:com);
+  b = bin(1:com);
+endfunction
+
+function [a b] = trim2(ain, bin)
+  com = min( size(ain)(1), size(bin)(1) );
+  a = ain(1:com,:);
+  b = bin(1:com,:);
+endfunction
+
 function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 100,nheadsamp = 0)
   %Nframes = 30;
   %sample_clock_offset_ppm = 100;
@@ -72,8 +85,8 @@ function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 1
   Nsam = length(rx_log);
   prx = 1;
   nin = Nsamperframe+2*(M+Ncp);
-  states.rxbuf(Nrxbuf-nin+1:Nrxbuf) = rx_log(prx:nin);
-  prx += nin;
+  %states.rxbuf(Nrxbuf-nin+1:Nrxbuf) = rx_log(prx:nin);
+  %prx += nin;
 
   rxbuf_log = []; rxbuf_in_log = []; rx_sym_log = []; foff_hz_log = []; 
   timing_est_log = []; sample_point_log = []; 
@@ -89,9 +102,8 @@ function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 1
     states.sample_point = Ncp;
   end
 
-  %states.foff_est_hz = 10;
-  for f=1:Nframes
-
+  have_frame = true;
+  while have_frame
     % insert samples at end of buffer, set to zero if no samples
     % available to disable phase estimation on future pilots on last
     % frame of simulation
@@ -103,12 +115,15 @@ function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 1
     if lnew
       rxbuf_in(1:lnew) = rx_log(prx:prx+lnew-1);
     end
-    prx += lnew;
+    prx += nin;
+    if prx > Nsam
+      have_frame = false;
+    end
 
     [rx_bits states aphase_est_pilot_log arx_np arx_amp] = ofdm_demod2(states, rxbuf_in);
 
     % log some states for comparison to C
-
+  
     rxbuf_in_log = [rxbuf_in_log rxbuf_in];
     rxbuf_log = [rxbuf_log states.rxbuf];
     rx_sym_log = [rx_sym_log; states.rx_sym];
@@ -122,6 +137,40 @@ function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 1
     
   end
 
+  %states.foff_est_hz = 10;
+  % for f=1:Nframes
+
+  %   % insert samples at end of buffer, set to zero if no samples
+  %   % available to disable phase estimation on future pilots on last
+  %   % frame of simulation
+  
+  %   nin = states.nin;
+  %   lnew = min(Nsam-prx+1,nin);
+  %   rxbuf_in = zeros(1,nin);
+  %   %printf("nin: %d prx: %d lnew: %d\n", nin, prx, lnew);
+  %   if lnew
+  %     rxbuf_in(1:lnew) = rx_log(prx:prx+lnew-1);
+  %   end
+  %   prx += lnew;
+
+  %   [rx_bits states aphase_est_pilot_log arx_np arx_amp] = ofdm_demod2(states, rxbuf_in);
+
+  %   % log some states for comparison to C
+
+  %   rxbuf_in_log = [rxbuf_in_log rxbuf_in];
+  %   rxbuf_log = [rxbuf_log states.rxbuf];
+  %   rx_sym_log = [rx_sym_log; states.rx_sym];
+  %   phase_est_pilot_log = [phase_est_pilot_log; aphase_est_pilot_log];
+  %   rx_amp_log = [rx_amp_log arx_amp];
+  %   foff_hz_log = [foff_hz_log; states.foff_est_hz];
+  %   timing_est_log = [timing_est_log; states.timing_est];
+  %   sample_point_log = [sample_point_log; states.sample_point];
+  %   rx_np_log = [rx_np_log arx_np];
+  %   rx_bits_log = [rx_bits_log rx_bits];
+    
+  % end
+
+
   % ---------------------------------------------------------------------
   % Run C version and plot Octave and C states and differences 
   % ---------------------------------------------------------------------
@@ -141,6 +190,9 @@ function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 1
   figure(fg++); clf; plot(rx_np_log,'+'); title('Octave Scatter Diagram'); axis([-1.5 1.5 -1.5 1.5]);
   figure(fg++); clf; plot(rx_np_log_c,'+'); title('C Scatter Diagram'); axis([-1.5 1.5 -1.5 1.5]);
 
+  length(rxbuf_in_log_c)
+  length(rxbuf_in_log)
+
   stem_sig_and_error(fg++, 111, tx_bits_log_c, tx_bits_log - tx_bits_log_c, 'tx bits', [1 length(tx_bits_log) -1.5 1.5])
 
   stem_sig_and_error(fg, 211, real(tx_log_c), real(tx_log - tx_log_c), 'tx re', [1 length(tx_log_c) -0.1 0.1])
@@ -149,19 +201,22 @@ function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 1
   stem_sig_and_error(fg, 211, real(rx_log_c), real(rx_log - rx_log_c), 'rx re', [1 length(rx_log_c) -0.1 0.1])
   stem_sig_and_error(fg++, 212, imag(rx_log_c), imag(rx_log - rx_log_c), 'rx im', [1 length(rx_log_c) -0.1 0.1])
 
-  stem_sig_and_error(fg, 211, real(rxbuf_in_log_c), real(rxbuf_in_log - rxbuf_in_log_c), 'rxbuf in re', [1 length(rxbuf_in_log_c) -0.1 0.1])
-  stem_sig_and_error(fg++, 212, imag(rxbuf_in_log_c), imag(rxbuf_in_log - rxbuf_in_log_c), 'rxbuf in im', [1 length(rxbuf_in_log_c) -0.1 0.1])
+  %stem_sig_and_error(fg, 211, real(rxbuf_in_log_c), real(rxbuf_in_log - rxbuf_in_log_c), 'rxbuf in re', [1 length(rxbuf_in_log_c) -0.1 0.1])
+  %stem_sig_and_error(fg++, 212, imag(rxbuf_in_log_c), imag(rxbuf_in_log - rxbuf_in_log_c), 'rxbuf in im', [1 length(rxbuf_in_log_c) -0.1 0.1])
 
+  [rxbuf_log_c rxbuf_log] = trim1(rxbuf_log_c,rxbuf_log);
   stem_sig_and_error(fg, 211, real(rxbuf_log_c), real(rxbuf_log - rxbuf_log_c), 'rxbuf re', [1 length(rxbuf_log_c) -0.1 0.1])
   stem_sig_and_error(fg++, 212, imag(rxbuf_log_c), imag(rxbuf_log - rxbuf_log_c), 'rxbuf im', [1 length(rxbuf_log_c) -0.1 0.1])
 
+  [rx_sym_log_c rx_sym_log] = trim2(rx_sym_log_c,rx_sym_log);
   stem_sig_and_error(fg, 211, real(rx_sym_log_c), real(rx_sym_log - rx_sym_log_c), 'rx sym re', [1 length(rx_sym_log_c) -1.5 1.5])
   stem_sig_and_error(fg++, 212, imag(rx_sym_log_c), imag(rx_sym_log - rx_sym_log_c), 'rx sym im', [1 length(rx_sym_log_c) -1.5 1.5])
 
   % for angles pi and -pi are the same
-
+  
+  [phase_est_pilot_log phase_est_pilot_log_c] = trim2(phase_est_pilot_log, phase_est_pilot_log_c);
   d = phase_est_pilot_log - phase_est_pilot_log_c; d = angle(exp(j*d));
-
+  [rx_amp_log rx_amp_log_c] = trim1(rx_amp_log,rx_amp_log_c);
   stem_sig_and_error(fg, 211, phase_est_pilot_log_c, d, 'phase est pilot', [1 length(phase_est_pilot_log_c) -1.5 1.5])
   stem_sig_and_error(fg++, 212, rx_amp_log_c, rx_amp_log - rx_amp_log_c, 'rx amp', [1 length(rx_amp_log_c) -1.5 1.5])
 
@@ -170,8 +225,14 @@ function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 1
   stem_sig_and_error(fg,   211, timing_est_log_c, (timing_est_log - timing_est_log_c), 'timing est', [1 length(timing_est_log_c) -1.5 1.5])
   stem_sig_and_error(fg++, 212, sample_point_log_c, (sample_point_log - sample_point_log_c), 'sample point', [1 length(sample_point_log_c) -1.5 1.5])
 
+  [rx_bits_log rx_bits_log_c] = trim1(rx_bits_log,rx_bits_log_c);
   stem_sig_and_error(fg++, 111, rx_bits_log_c, rx_bits_log - rx_bits_log_c, 'rx bits', [1 length(rx_bits_log) -1.5 1.5])
 
+  [rxbuf_in_log rxbuf_in_log_c] = trim1(rxbuf_in_log,rxbuf_in_log_c);
+  [timing_est_log timing_est_log_c] = trim1(timing_est_log,timing_est_log_c');
+  [sample_point_log,sample_point_log_c] = trim1(sample_point_log,sample_point_log_c);
+  [foff_hz_log,foff_hz_log_c] = trim1(foff_hz_log,foff_hz_log_c');
+
   % Run through checklist -----------------------------
   pass = true;
   pass = check_no_abs(W, W_c, 'W') && pass;
@@ -183,13 +244,13 @@ function pass = run_ofdm_test(Nframes,sample_clock_offset_ppm,foff_hz,EbNodB = 1
   pass = check(rx_sym_log, rx_sym_log_c, 'rx_sym') && pass;
   pass = check(phase_est_pilot_log, phase_est_pilot_log_c, 'phase_est_pilot', tol=1E-3, its_an_angle=1) && pass;
   pass = check(rx_amp_log, rx_amp_log_c, 'rx_amp') && pass;
-  pass = check(timing_est_log, timing_est_log_c', 'timing_est') && pass;
+  pass = check(timing_est_log, timing_est_log_c, 'timing_est') && pass;
   pass = check(sample_point_log, sample_point_log_c, 'sample_point') && pass;
-  pass = check(foff_hz_log, foff_hz_log_c', 'foff_est_hz') && pass;
+  pass = check(foff_hz_log, foff_hz_log_c, 'foff_est_hz') && pass;
   pass = check(rx_bits_log, rx_bits_log_c, 'rx_bits') && pass;
   
   figure(fg++)
-  stem(timing_est_log-timing_est_log_c')
+  stem(timing_est_log-timing_est_log_c)
   figure(fg++)
   stem(phase_est_pilot_log-phase_est_pilot_log_c)
 
@@ -198,4 +259,4 @@ end
 % This works best on my machine -- Brady
 graphics_toolkit('fltk')
 
-run_ofdm_test(60,20,0.5,40,100)
\ No newline at end of file
+run_ofdm_test(60,100,5,8,10000)
index 99734d27c31b233b186da456fcc93c5f27bda4d2..f3bd188d586fca1d4c30039127aec9226f0d5332 100644 (file)
@@ -84,7 +84,7 @@ static complex float qpsk_mod(int *);
 static void qpsk_demod(complex float, int *);
 static void ofdm_txframe(struct OFDM *, complex float [], complex float *);
 //static int coarse_sync(struct OFDM *, complex float *, int);
-static int coarse_sync(struct OFDM *ofdm, complex float *rx, int length, float *foff_out);
+static int coarse_sync(struct OFDM *ofdm, complex float *rx, int length, float *foff_out, float *ratio_out);
 
 /* Defines */
 
@@ -291,7 +291,7 @@ static int coarser_sync(struct OFDM *ofdm, complex float *rx, int length, float
  * frames pilots so we need at least Nsamperframe+M+Ncp samples in rx.
  */
 
-static int coarse_sync(struct OFDM *ofdm, complex float *rx, int length, float *foff_out) {
+static int coarse_sync(struct OFDM *ofdm, complex float *rx, int length, float *foff_out, float *ratio_out) {
     complex float csam;
     const int Fs                  = ofdm->config.Fs;
     const int M                   = ofdm->config.M;
@@ -388,9 +388,12 @@ static int coarse_sync(struct OFDM *ofdm, complex float *rx, int length, float *
     }
     foff_est = (pmax > nmax) ? pmax_i : (nmax_i-Fs+1); 
     //foff_est = pmax_i;
-    fprintf(stderr,"foff_est is %f, tpos is %d, ratio is %f\n",foff_est,t_est%1280,max_corr/max_mag);
+    //fprintf(stderr,"foff_est is %f, tpos is %d, ratio is %f\n",foff_est,t_est%1280,max_corr/max_mag);
 
     *foff_out = foff_est;
+    if(ratio_out != NULL){
+        *ratio_out = (max_corr + 1e-9)/(max_mag + 1e-9);
+    }
 
     return t_est;
 }
@@ -783,6 +786,8 @@ void ofdm_demod_coarse(struct OFDM *ofdm, int *rx_bits, COMP *rxbuf_in){
     const int SampsPerFrame       = ofdm->config.SampsPerFrame;
     const int RxBufSize       = ofdm->config.RxBufSize;
     const int Fs = ofdm->config.Fs;
+    const int M = ofdm->config.M;
+    const int Ncp = ofdm->config.Ncp;
 
     int sync = ofdm->sync_count;
     int frame_point = ofdm->frame_point;
@@ -795,6 +800,8 @@ void ofdm_demod_coarse(struct OFDM *ofdm, int *rx_bits, COMP *rxbuf_in){
     memmove(&coarse_rxbuf[0],&coarse_rxbuf[nin], (RxBufSize - nin) * sizeof(coarse_rxbuf[0]));
     memcpy (&coarse_rxbuf[RxBufSize - nin],rxbuf_in,    nin * sizeof(coarse_rxbuf[0]));
 
+    ofdm_demod(ofdm,rx_bits,&coarse_rxbuf[RxBufSize - 2*SampsPerFrame + frame_point]);
+
     int frame_pos_est;
     float fshift;
     float corr_ratio_max = 0;
@@ -806,9 +813,10 @@ void ofdm_demod_coarse(struct OFDM *ofdm, int *rx_bits, COMP *rxbuf_in){
     complex float shift_nco = 1;
     complex float shift_nco_dph = 0;
 
-    if(sync <= 0 ) {
+    if(sync <= 0 || 1 ) {
         /* Do coarse search over frequency range in 20hz slices*/
-        for(fshift = -100; fshift < 100; fshift+=40){
+        //for(fshift = -100; fshift < 100; fshift+=40){
+        fshift = 0;{
             /* Shift block of samples by fshift */
             shift_nco_dph = cexpf(2*M_PI*(fshift/(float)Fs)*I);
             shift_nco = 1;
@@ -818,17 +826,74 @@ void ofdm_demod_coarse(struct OFDM *ofdm, int *rx_bits, COMP *rxbuf_in){
             }
 
             /* Do a coarse search for the pilot */
-            frame_pos_est = coarser_sync(ofdm,rxbuf_temp,RxBufSize,&foff_out,&corr_ratio);
+            frame_pos_est = coarse_sync(ofdm,rxbuf_temp,RxBufSize,&foff_out,&corr_ratio);
             if(corr_ratio > corr_ratio_max){
                 corr_ratio_max = corr_ratio;
                 fshift_max = -fshift + foff_out;
                 frame_pos_max = frame_pos_est;          
             }
         }
-        fprintf(stderr,"Best ratio %f freq %f offset %d\n",corr_ratio_max,fshift_max,frame_pos_max);
+        frame_pos_est = (frame_pos_max % SampsPerFrame) - (M+Ncp) + 1;
+        if(corr_ratio_max > 0.5 && sync <=0){
+            ofdm->sync_count = 3;
+            ofdm->frame_point = frame_pos_est;
+            ofdm->foff_est_hz = fshift_max;
+            fprintf(stderr,"syncing\n");
+        }
+        fprintf(stderr,"Best ratio %f freq %f offset %d\n",corr_ratio_max,fshift_max,frame_pos_est);
     }
 }
 
+// void ofdm_demod_coarse(struct OFDM *ofdm, int *rx_bits, COMP *rxbuf_in){
+//     const int SampsPerFrame       = ofdm->config.SampsPerFrame;
+//     const int RxBufSize       = ofdm->config.RxBufSize;
+//     const int Fs = ofdm->config.Fs;
+//     const int M = ofdm->config.M;
+//     const int Ncp = ofdm->config.Ncp;
+
+//     int sync = ofdm->sync_count;
+//     int frame_point = ofdm->frame_point;
+//     int nin = ofdm->nin;
+//     int frame_pos_est;
+//     float fshift;
+//     float corr_ratio_max = 0;
+//     float corr_ratio;
+//     float foff_out;
+//     float fshift_max;
+//     int frame_pos_max = 0;
+
+//     /* NCO for freq. shifting */
+//     complex float shift_nco = 1;
+//     complex float shift_nco_dph = 0;
+
+//     ofdm_demod(ofdm,rx_bits,rxbuf_in);
+//     frame_pos_est = coarse_sync(ofdm,ofdm->rxbuf,RxBufSize,&foff_out,&corr_ratio);
+//     frame_pos_est = (frame_pos_est % SampsPerFrame) - (M+Ncp) + 1;
+//         //fprintf(stderr,"frame_pos_est is %d\n",frame_pos_est);
+//     fprintf(stderr,"t_est is %d, foff_est is %d, ofdm_foff_est is %f\n",frame_pos_est,(int)foff_out,ofdm->foff_est_hz);
+    
+//     if(sync <= 0 ) {
+//         if(corr_ratio > 0.5){
+//             if(frame_pos_est > 12){
+//                 ofdm->nin = frame_pos_est;
+//             }
+//             float delta_f = fabsf((float)foff_out - ofdm->foff_est_hz);
+//             if(delta_f > 3){
+//                 ofdm->foff_est_hz = (float)foff_out;
+//             }
+//             ofdm->sync_count = 3;
+//         }
+//     }else{
+//         if(corr_ratio < 0.5){
+//             ofdm->sync_count--;
+//         }
+//         float delta_f = fabsf((float)foff_out - ofdm->foff_est_hz);
+//         if(delta_f > 3){
+//             ofdm->foff_est_hz = (float)foff_out;
+//        }
+//     }
+// }
+
 /*
  * ------------------------------------------
  * ofdm_demod - Demodulates one frame of bits
@@ -882,7 +947,7 @@ void ofdm_demod(struct OFDM *ofdm, int *rx_bits, COMP *rxbuf_in) {
 
     float woff_est = TAU * ofdm->foff_est_hz / (float)(Fs);
     float freq_offset = -1;
-
+    float ratio_out;
     /* update timing estimate -------------------------------------------------- */
 
     if (ofdm->timing_en == true) {
@@ -903,12 +968,10 @@ void ofdm_demod(struct OFDM *ofdm, int *rx_bits, COMP *rxbuf_in) {
             work[j] = ofdm->rxbuf[i] * cexpf(-I * woff_est * i);
         }
 
-        fprintf(stderr,"n>");
-        ft_est = coarse_sync(ofdm, work, (en - st), &freq_offset);
-        fprintf(stderr,"\nx>");
-        coarse_sync(ofdm,ofdm->rxbuf,RxBufSize,&freq_offset);
-        fprintf(stderr,"\n");
-        //coarser_sync(ofdm, work, (en - st), &freq_offset, NULL);
+        // fprintf(stderr,"n>");
+        ft_est = coarse_sync(ofdm, work, (en - st), &freq_offset,&ratio_out);
+        fprintf(stderr,"good ratio %f\n",ratio_out);
+        // fprintf(stderr,"\nx>");
 
         //ofdm->foff_est_hz += (freq_offset/2);
         ofdm->timing_est += (ft_est - ceilf(FtWindowWidth / 2));
@@ -923,6 +986,16 @@ void ofdm_demod(struct OFDM *ofdm, int *rx_bits, COMP *rxbuf_in) {
 
         ofdm->sample_point = max(ofdm->timing_est + (Ncp / 4), ofdm->sample_point);
         ofdm->sample_point = min(ofdm->timing_est + Ncp,       ofdm->sample_point);
+
+
+        int ft_est_other = coarse_sync(ofdm,ofdm->rxbuf,RxBufSize,&freq_offset,&ratio_out);
+        ft_est_other = (ft_est_other % SampsPerFrame) - (M+Ncp) + 1;
+        // fprintf(stderr,"\n");
+        //coarser_sync(ofdm, work, (en - st), &freq_offset, NULL);
+        if(ratio_out<.5){
+            ofdm->sync_count--;
+        }
+        fprintf(stderr,"ratio is %f t_offset is %d\n",ratio_out,ft_est_other);
     }
 
     /*
index b4ad25043f8b7e70dc6242317003909a0f4e1c61..b886fef9f53f462b68df1dbdf211e067184f3f6f 100644 (file)
@@ -166,7 +166,8 @@ int main(int argc, char *argv[])
 
     int            tx_bits_log[OFDM_BITSPERFRAME*NFRAMES];
     COMP           tx_log[samples_per_frame*NFRAMES];
-    COMP           rx_log[samples_per_frame*NFRAMES];
+    //COMP           rx_log[samples_per_frame*NFRAMES];
+    COMP *         rx_log;
     COMP           rxbuf_in_log[max_samples_per_frame*NFRAMES];
     COMP           rxbuf_log[OFDM_RXBUF*NFRAMES];
     COMP           rx_sym_log[(OFDM_NS + 3)*NFRAMES][OFDM_NC + 2];
@@ -210,16 +211,21 @@ int main(int argc, char *argv[])
                                Channel
     \*---------------------------------------------------------*/
 
-    fs_offset(rx_log, tx_log, samples_per_frame*NFRAMES, sample_clock_offset_ppm);
+    /*fs_offset(rx_log, tx_log, samples_per_frame*NFRAMES, sample_clock_offset_ppm);
 
     COMP foff_phase_rect = {1.0f, 0.0f};
 
-    freq_shift(rx_log, rx_log, foff_hz, &foff_phase_rect, samples_per_frame * NFRAMES);
+    freq_shift(rx_log, rx_log, foff_hz, &foff_phase_rect, samples_per_frame * NFRAMES);*/
 
     FILE *cplin = fopen("tofdm_rx_vec","r");
-    size_t s = fread(rx_log,sizeof(COMP),samples_per_frame*NFRAMES,cplin);
+    fseek(cplin,0,SEEK_END);
+    size_t file_len = ftell(cplin);
+    rewind(cplin);
+    size_t rx_samps_in = file_len/sizeof(COMP);
+    rx_log = malloc(rx_samps_in*sizeof(COMP));
+    fread(rx_log,sizeof(COMP),rx_samps_in,cplin);
     fclose(cplin);
-    fprintf(stderr,"Read %d\n",s);
+    //fprintf(stderr,"Read %d\n",s);
 
 
     /* --------------------------------------------------------*\
@@ -229,15 +235,16 @@ int main(int argc, char *argv[])
     /* Init rx with ideal timing so we can test with timing estimation disabled */
 
     int  Nsam = samples_per_frame*NFRAMES;
+    Nsam = rx_samps_in;
     int  prx = 0;
-    int  nin = samples_per_frame + 2*(OFDM_M+OFDM_NCP);
-
+    //int  nin = samples_per_frame + 2*(OFDM_M+OFDM_NCP);
+    int nin = ofdm_get_nin(ofdm);
     int  lnew;
     COMP rxbuf_in[max_samples_per_frame];
 
-    for (i=0; i<nin; i++,prx++) {
+    /*for (i=0; i<nin; i++,prx++) {
          ofdm->rxbuf[OFDM_RXBUF-nin+i] = rx_log[prx].real + I*rx_log[prx].imag;
-    }
+    }*/
 
     int nin_tot = 0;
 
@@ -251,80 +258,85 @@ int main(int argc, char *argv[])
     ofdm_set_phase_est_enable(ofdm, true);
     //ofdm->foff_est_hz = 10;
 
-    for(f=0; f<NFRAMES; f++) {
-        /* For initial testng, timing est is off, so nin is always
-           fixed.  TODO: we need a constant for rxbuf_in[] size that
-           is the maximum possible nin */
-
+    bool cont = true;
+    int ptr = 0;
+    for(f=0; cont; f++) {
+        // /* For initial testng, timing est is off, so nin is always
+        //    fixed.  TODO: we need a constant for rxbuf_in[] size that
+        //    is the maximum possible nin */
+
+        // nin = ofdm_get_nin(ofdm);
+        
+        // nin_tot += nin;
+
+        // assert(nin <= max_samples_per_frame);
+
+        // /* Insert samples at end of buffer, set to zero if no samples
+        //    available to disable phase estimation on future pilots on
+        //    last frame of simulation. */
+
+        // if ((Nsam-prx) < nin) {
+        //     lnew = Nsam-prx;
+        // } else {
+        //     lnew = nin;
+        // }
+        // //printf("nin: %d prx: %d lnew: %d\n", nin, prx, lnew);
+        // for(i=0; i<nin; i++) {
+        //     rxbuf_in[i].real = 0.0;
+        //     rxbuf_in[i].imag = 0.0;
+        // }
+
+        // if (lnew) {
+        //     for(i=0; i<lnew; i++, prx++) {
+        //         rxbuf_in[i] = rx_log[prx];
+        //     }
+        // }
+
+        // if(prx > Nsam){
+        //     cont = false;
+        //     break;
+        // }
+        //assert(prx <= max_samples_per_frame*NFRAMES);
+
+        //fprintf(stderr,"NIN:%d\n",nin);
         nin = ofdm_get_nin(ofdm);
-        assert(nin <= max_samples_per_frame);
-
-        /* Insert samples at end of buffer, set to zero if no samples
-           available to disable phase estimation on future pilots on
-           last frame of simulation. */
-
-        if ((Nsam-prx) < nin) {
-            lnew = Nsam-prx;
-        } else {
-            lnew = nin;
-        }
-        //printf("nin: %d prx: %d lnew: %d\n", nin, prx, lnew);
-        for(i=0; i<nin; i++) {
-            rxbuf_in[i].real = 0.0;
-            rxbuf_in[i].imag = 0.0;
+        if(ptr + nin >= Nsam){
+            break;
         }
 
-        if (lnew) {
-            for(i=0; i<lnew; i++, prx++) {
-                rxbuf_in[i] = rx_log[prx];
-            }
+        memset(rxbuf_in,0,sizeof(COMP) * nin);
+
+        for(i=0; i<nin && ptr < Nsam; i++,ptr++){
+            rxbuf_in[i] = rx_log[ptr];
         }
-        assert(prx <= max_samples_per_frame*NFRAMES);
+        ofdm_demod_coarse(ofdm, rx_bits, rxbuf_in);
 
-        ofdm_demod(ofdm, rx_bits, rxbuf_in);
 
-        /* rx vector logging -----------------------------------*/
+        if(ptr >= Nsam){
+            cont = false;
+            //break;
+        }
 
-        assert(nin_tot < samples_per_frame*NFRAMES);
-           //memcpy(&rxbuf_in_log[nin_tot], rxbuf_in, sizeof(COMP)*nin);
         modem_probe_samp_cft("rxbuf_in_log_c",rxbuf_in,nin);
 
-        nin_tot += nin;
-
-        /*for(i=0; i<OFDM_RXBUF; i++) {
-            rxbuf_log[OFDM_RXBUF*f+i].real = crealf(ofdm->rxbuf[i]);
-            rxbuf_log[OFDM_RXBUF*f+i].imag = cimagf(ofdm->rxbuf[i]);
-       }*/
-
-        for (i = 0; i < (OFDM_NS + 3); i++) {
-            for (j = 0; j < (OFDM_NC + 2); j++) {
-                rx_sym_log[(OFDM_NS + 3)*f+i][j].real = crealf(ofdm->rx_sym[i][j]);
-                rx_sym_log[(OFDM_NS + 3)*f+i][j].imag = cimagf(ofdm->rx_sym[i][j]);
+        if(f<NFRAMES){
+            for (i = 0; i < (OFDM_NS + 3); i++) {
+                for (j = 0; j < (OFDM_NC + 2); j++) {
+                    rx_sym_log[(OFDM_NS + 3)*f+i][j].real = crealf(ofdm->rx_sym[i][j]);
+                    rx_sym_log[(OFDM_NS + 3)*f+i][j].imag = cimagf(ofdm->rx_sym[i][j]);
+                }
             }
         }
-
-        /* note corrected phase (rx no phase) is one big linear array for frame */
-
-        /*for (i = 0; i < OFDM_ROWSPERFRAME*OFDM_NC; i++) {
-            rx_np_log[OFDM_ROWSPERFRAME*OFDM_NC*f + i].real = crealf(ofdm->rx_np[i]);
-            rx_np_log[OFDM_ROWSPERFRAME*OFDM_NC*f + i].imag = cimagf(ofdm->rx_np[i]);
-        }*/
-
         /* note phase/amp ests the same for each col, but check them all anyway */
-
-        for (i = 0; i < OFDM_ROWSPERFRAME; i++) {
-            for (j = 0; j < OFDM_NC; j++) {
-                phase_est_pilot_log[OFDM_ROWSPERFRAME*f+i][j] = ofdm->aphase_est_pilot_log[OFDM_NC*i+j];
-                rx_amp_log[OFDM_ROWSPERFRAME*OFDM_NC*f+OFDM_NC*i+j] = ofdm->rx_amp[OFDM_NC*i+j];
+        if(f < NFRAMES){
+            for (i = 0; i < OFDM_ROWSPERFRAME; i++) {
+                for (j = 0; j < OFDM_NC; j++) {
+                    phase_est_pilot_log[OFDM_ROWSPERFRAME*f+i][j] = ofdm->aphase_est_pilot_log[OFDM_NC*i+j];
+                    rx_amp_log[OFDM_ROWSPERFRAME*OFDM_NC*f+OFDM_NC*i+j] = ofdm->rx_amp[OFDM_NC*i+j];
+                }
             }
         }
-
-        //foff_hz_log[f] = ofdm->foff_est_hz;
-        //timing_est_log[f] = ofdm->timing_est + 1;      /* offset by 1 to match Octave */
-        //sample_point_log[f] = ofdm->sample_point + 1; /* offset by 1 to match Octave */
-     
         modem_probe_samp_i("rx_bits_log_c",rx_bits,OFDM_BITSPERFRAME);
-        //memcpy(&rx_bits_log[OFDM_BITSPERFRAME*f], rx_bits, sizeof(rx_bits));
     }
 
     /*---------------------------------------------------------*\
@@ -342,16 +354,10 @@ int main(int argc, char *argv[])
     octave_save_int(fout, "tx_bits_log_c", tx_bits_log, 1, OFDM_BITSPERFRAME*NFRAMES);
     octave_save_complex(fout, "tx_log_c", (COMP*)tx_log, 1, samples_per_frame*NFRAMES,  samples_per_frame*NFRAMES);
     octave_save_complex(fout, "rx_log_c", (COMP*)rx_log, 1, samples_per_frame*NFRAMES,  samples_per_frame*NFRAMES);
-    //octave_save_complex(fout, "rxbuf_in_log_c", (COMP*)rxbuf_in_log, 1, nin_tot, nin_tot);
-    //octave_save_complex(fout, "rxbuf_log_c_x", (COMP*)rxbuf_log, 1, OFDM_RXBUF*NFRAMES,  OFDM_RXBUF*NFRAMES);
     octave_save_complex(fout, "rx_sym_log_c", (COMP*)rx_sym_log, (OFDM_NS + 3)*NFRAMES, OFDM_NC + 2, OFDM_NC + 2);
     octave_save_float(fout, "phase_est_pilot_log_c", (float*)phase_est_pilot_log, OFDM_ROWSPERFRAME*NFRAMES, OFDM_NC, OFDM_NC);
     octave_save_float(fout, "rx_amp_log_c", (float*)rx_amp_log, 1, OFDM_ROWSPERFRAME*OFDM_NC*NFRAMES, OFDM_ROWSPERFRAME*OFDM_NC*NFRAMES);
-    //octave_save_float(fout, "foff_hz_log_c", foff_hz_log, NFRAMES, 1, 1);
-    //octave_save_int(fout, "timing_est_log_c", timing_est_log, NFRAMES, 1);
-    //ctave_save_int(fout, "sample_point_log_c", sample_point_log, NFRAMES, 1);
-    //octave_save_complex(fout, "rx_np_log_c", (COMP*)rx_np_log, 1, OFDM_ROWSPERFRAME*OFDM_NC*NFRAMES, OFDM_ROWSPERFRAME*OFDM_NC*NFRAMES);
-    //octave_save_int(fout, "rx_bits_log_cx", rx_bits_log, 1, OFDM_BITSPERFRAME*NFRAMES);
+
     fclose(fout);
 
     ofdm_destroy(ofdm);