check(rx_baseband_log, rx_baseband_log_c, 'rx_baseband',0.01);
 check(rx_filt_log, rx_filt_log_c, 'rx_filt');
 check(ch_symb_log, ch_symb_log_c, 'ch_symb',0.01);
-check(rx_amp_log, rx_amp_log_c, 'rx_amp_log');
+check(rx_amp_log, rx_amp_log_c, 'rx_amp_log',0.01);
 check(rx_phi_log, rx_phi_log_c, 'rx_phi_log');
-check(rx_symb_log, rx_symb_log_c, 'rx_symb');
+check(rx_symb_log, rx_symb_log_c, 'rx_symb',0.01);
 check(rx_bits_log, rx_bits_log_c, 'rx_bits');
 
 % Determine bit error rate
 
 sz = length(tx_bits_log_c);
-Nerrs_c = sum(xor(tx_bits_log_c(framesize+1:sz-framesize), rx_bits_log_c(2*framesize+1:sz)));
+Nerrs_c = sum(xor(tx_bits_log_c(framesize+1:sz-2*framesize), rx_bits_log_c(3*framesize+1:sz)));
 Tbits_c = sz - 2*framesize;
 ber_c = Nerrs_c/Tbits_c;
 ber = Nerrs/Tbits;
 
 #define RS     50
 #define FOFF   1
 
+extern float pilots_coh[][PILOTS_NC];
+
 int main(int argc, char *argv[])
 {
     struct COHPSK *coh;
     COMP           tx_fdm[M*NSYMROWPILOT];
     COMP           rx_fdm[M*NSYMROWPILOT];
     COMP           ch_symb[NSYMROWPILOT][PILOTS_NC];
+    COMP           ct_symb_buf[2*NSYMROWPILOT][COHPSK_NC];
     int            rx_bits[COHPSK_BITS_PER_FRAME];
     
     int            tx_bits_log[COHPSK_BITS_PER_FRAME*FRAMES];
     COMP           rx_onesym[PILOTS_NC];
     int            rx_baseband_log_col_index = 0;
     COMP           rx_baseband_log[PILOTS_NC][(M+M/P)*NSYMROWPILOT*FRAMES];
+    
+    int            t,ct,p;
+    float          max_corr;
+    COMP           corr;
 
     coh = cohpsk_create();
     assert(coh != NULL);
          }
          rx_baseband_log_col_index += nin;        
 
-         //if (f == 3)
-          //    exit(0);
          for(c=0; c<PILOTS_NC; c++) {       
             for(i=0; i<P; i++) {
               rx_filt_log[c][rx_filt_log_col_index + i] = rx_filt[c][i]; 
          rx_filt_log_col_index += P;        
 
         }
-        qpsk_symbols_to_bits(coh, rx_bits, ch_symb);
 
+        /* coarse frame sync */
+
+        for(r=0; r<NSYMROWPILOT; r++)
+            for(c=0; c<PILOTS_NC; c++) {
+                ct_symb_buf[r][c] = ct_symb_buf[r+NSYMROWPILOT][c];
+                ct_symb_buf[r+NSYMROWPILOT][c] = ch_symb[r][c];
+            }
+        
+        max_corr = 0;
+        for(t=0; t<NSYMROWPILOT; t++) {
+            
+            corr.real = corr.imag = 0.0;
+            for(p=0; p<NPILOTSFRAME; p++) {
+                for(c=0; c<PILOTS_NC; c++) {
+                    corr = cadd(corr,fcmult(pilots_coh[p][c], ct_symb_buf[t+p*(NS+1)][c]));
+                }
+            }
+        
+            if (cabsolute(corr) > max_corr) {
+                max_corr = cabsolute(corr);
+                ct = t;
+            }
+        }
+        //printf("max_corr: %f ct: %d\n", max_corr, ct);
+
+        /* Coherent phase estimation and correction */
+
+        qpsk_symbols_to_bits(coh, rx_bits, ct_symb_buf + ct);
+        
        /* --------------------------------------------------------*\
                               Log each vector 
        \*---------------------------------------------------------*/