fsk.c now matches fsk_horus closer
authorbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 24 Jan 2016 04:20:58 +0000 (04:20 +0000)
committerbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Sun, 24 Jan 2016 04:20:58 +0000 (04:20 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@2650 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/octave/tfsk.m
codec2-dev/src/fsk.c

index 9b9ec895735585eb3590bd8b5afc6771ca397560..31f42214abe859bab491a278d2aa5b0e62113f11 100644 (file)
@@ -75,13 +75,13 @@ endfunction
 
 
 %Compare 2 vectors, fail if they are not close enough
-function pass = vcompare(va,vb,vname,tname,tol)
+function pass = vcompare(vc,voct,vname,tname,tol)
     
     %Get delta of vectors
-    dvec = abs(abs(va)-abs(vb));     
+    dvec = abs(abs(vc)-abs(voct));     
     
     %Normalize difference
-    dvec = dvec ./ abs(max(abs(va)));
+    dvec = dvec ./ abs(max(abs(voct)));
     
     maxdvec = abs(max(dvec));
     pass = maxdvec<tol;
@@ -171,15 +171,17 @@ function test_stats = fsk_demod_xt(Fs,Rs,f1,f2,mod,tname)
         o_rx_timing = [o_rx_timing states.rx_timing];
     end
     
-    
+    % One part-per-thousand allowed on important parameters
     pass =         vcompare(o_f1_dc,      t_f1_dc,    'f1_dc',    tname,.001);
     pass = pass && vcompare(o_f2_dc,      t_f2_dc,    'f2_dc',    tname,.001);
-    % Note fx_int tolerances higher because FP error is accumulated here 
-    pass = pass && vcompare(o_f1_int,     t_f1_int,   'f1_int',   tname,.02);
-    pass = pass && vcompare(o_f2_int,     t_f2_int,   'f2_int',   tname,.02);
-    pass = pass && vcompare(o_rx_timing,  t_rx_timing,'rx_timing',tname,.011);
-    pass = pass && vcompare(o_EbNodB,     t_EbNodB,   'EbNodB',   tname,.15);
-    pass = pass && vcompare(o_ppm   ,     t_ppm,      'ppm',      tname,.011);
+    pass = pass && vcompare(o_f1_int,     t_f1_int,   'f1_int',   tname,.001);
+    pass = pass && vcompare(o_f2_int,     t_f2_int,   'f2_int',   tname,.001);
+    pass = pass && vcompare(o_rx_timing,  t_rx_timing,'rx_timing',tname,.001);
+    
+    % Much larger tolerances on unimportant statistics
+    pass = pass && vcompare(o_EbNodB,     t_EbNodB,   'EbNodB',   tname,.05);
+    pass = pass && vcompare(o_ppm   ,     t_ppm,      'ppm',      tname,.02);
+    
     diffpass = sum(xor(obits,bits'))<4;
     diffbits = sum(xor(obits,bits'));
     
@@ -410,7 +412,7 @@ endfunction
 
 function pass = ebno_battery_test(timing_offset,fading,df,dA)
     %Range of EbNodB over which to test
-    ebnodbrange = fliplr(5:13);
+    ebnodbrange = (5:13);
     ebnodbs = length(ebnodbrange);
     
     mode = 5;
index eb12d4e2f48b02ef044aa384b653462fb4253444..21a26c416a94f67b99e8bd663f86a967164c1496 100644 (file)
@@ -146,6 +146,8 @@ struct FSK * fsk_create(int Fs, int Rs, int tx_f1,int tx_f2)
     fsk->EbNodB = 0;
     fsk->f1_est = 0;
     fsk->f2_est = 0;
+    fsk->twist_est = 0;
+    fsk->ppm = 0;
     
     return fsk;
 }
@@ -341,10 +343,6 @@ void fsk_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
     /* Estimate tone frequencies */
     fsk_demod_freq_est(fsk,fsk_in,&f1,&f2,&twist);
     
-    fsk->f1_est = f1;
-    fsk->f2_est = f2;
-    fsk->twist_est = twist;
-    
     /* allocate memory for the integrated samples */
     /* Note: This must be kept after fsk_demod_freq_est for memory usage reasons */
     f1_int = (COMP*) alloca(sizeof(COMP)*(nsym+1)*P);
@@ -353,10 +351,19 @@ void fsk_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
     /* Allocate circular buffers for integration */
     f1_intbuf = (COMP*) alloca(sizeof(COMP)*Ts);
     f2_intbuf = (COMP*) alloca(sizeof(COMP)*Ts);
+    
+    /* If this is the first run and we haven't already estimated freq, save the new est */
+    if(fsk->f1_est<1 || fsk->f2_est<1){
+        fsk->f1_est = f1;
+        fsk->f2_est = f2;
+        fsk->twist_est = twist;
+    }
 
     /* Figure out how much to nudge each sample downmixer for every sample */
-    dphi1 = comp_exp_j(-2*M_PI*((float)(f1)/(float)(Fs)));
-    dphi2 = comp_exp_j(-2*M_PI*((float)(f2)/(float)(Fs)));
+    /* Use old freq. estimate here so that old samples will be converted at old 
+     * frequency, to match behaviour of fsk_horus */
+    dphi1 = comp_exp_j(-2*M_PI*((float)(fsk->f1_est)/(float)(Fs)));
+    dphi2 = comp_exp_j(-2*M_PI*((float)(fsk->f2_est)/(float)(Fs)));
 
     dc_i = 0;
     cbuf_i = 0;
@@ -370,6 +377,12 @@ void fsk_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
             sample_src = &fsk_in[0];
             dc_i = 0;
             using_old_samps = 0;
+            
+            /* Recalculate delta-phi after switching to new sample source */
+            phi1_c = comp_normalize(phi1_c);
+            phi2_c = comp_normalize(phi2_c);
+            dphi1 = comp_exp_j(-2*M_PI*((float)(f1)/(float)(Fs)));
+            dphi2 = comp_exp_j(-2*M_PI*((float)(f2)/(float)(Fs)));
         }
         /* Downconvert and place into integration buffer */
         f1_intbuf[dc_i]=fcmult(sample_src[dc_i],phi1_c);
@@ -392,6 +405,12 @@ void fsk_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
                 sample_src = &fsk_in[0];
                 dc_i = 0;
                 using_old_samps = 0;
+                
+                /* Recalculate delta-phi after switching to new sample source */
+                phi1_c = comp_normalize(phi1_c);
+                phi2_c = comp_normalize(phi2_c);
+                dphi1 = comp_exp_j(-2*M_PI*((float)(f1)/(float)(Fs)));
+                dphi2 = comp_exp_j(-2*M_PI*((float)(f2)/(float)(Fs)));
             }
             /* Downconvert and place into integration buffer */
             f1_intbuf[cbuf_i+j]=fcmult(sample_src[dc_i],phi1_c);
@@ -423,8 +442,12 @@ void fsk_demod(struct FSK *fsk, uint8_t rx_bits[], float fsk_in[]){
         
     }
 
-    fsk->phi1_c = comp_normalize(phi1_c);
-    fsk->phi2_c = comp_normalize(phi2_c);
+    fsk->phi1_c = phi1_c;
+    fsk->phi2_c = phi2_c;
+    
+    fsk->f1_est = f1;
+    fsk->f2_est = f2;
+    fsk->twist_est = twist;
 
     /* Stash samples away in the old sample buffer for the next round of bit getting */
     memcpy((void*)&(fsk->samp_old[0]),(void*)&(fsk_in[nin-nstash]),sizeof(float)*nstash);