From 6fa9f76a7575449a1960c9b24dfee74ff95620f5 Mon Sep 17 00:00:00 2001
From: Nico Palermo/IV3NWV <nicopal@microtelecom.it>
Date: Mon, 23 Nov 2020 06:03:59 +0100
Subject: [PATCH] Interface to q65_intrinsics_fastfading changed to support
 B90Ts instead of B90 and then supporting correctly modes with any T/R
 interval

---
 lib/q65_decode.f90        |  6 +++++-
 lib/qra/q65/q65.c         | 24 ++++++++++++------------
 lib/qra/q65/q65.h         |  2 +-
 lib/qra/q65/q65_loops.f90 |  5 +++--
 lib/qra/q65/q65_subs.c    |  4 ++--
 5 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/lib/q65_decode.f90 b/lib/q65_decode.f90
index 347621026..76b8faec9 100644
--- a/lib/q65_decode.f90
+++ b/lib/q65_decode.f90
@@ -137,9 +137,13 @@ contains
           read(c78,1060) apsymbols
        endif
        call timer('q65loops',0)
+!       call q65_loops(c00,nutc,npts/2,nsps/2,nmode,mode65,nsubmode,         &
+!            nFadingModel,ndepth,jpk0,xdt,f0,width,iaptype,apmask,apsymbols, &
+!            snr1,xdt1,f1,snr2,irc,dat4)
+!      baud rate required to compute B90TS later
        call q65_loops(c00,nutc,npts/2,nsps/2,nmode,mode65,nsubmode,         &
             nFadingModel,ndepth,jpk0,xdt,f0,width,iaptype,apmask,apsymbols, &
-            snr1,xdt1,f1,snr2,irc,dat4)
+            snr1,xdt1,f1,snr2,irc,dat4,baud) 
        call timer('q65loops',1)
        snr2=snr2 + db(6912.0/nsps)
        if(irc.ge.0) exit
diff --git a/lib/qra/q65/q65.c b/lib/qra/q65/q65.c
index 03b5219f5..c91571ba6 100644
--- a/lib/qra/q65/q65.c
+++ b/lib/qra/q65/q65.c
@@ -277,9 +277,9 @@ int q65_esnodb(const q65_codec_ds *pCodec, float *pEsNodB, const int *ydec, cons
 
 // Symbol time interval in seconds
 #define TS_QRA64 0.576
-#define TS_Q65   0.640
+// #define TS_Q65   0.640 // T/R = 60 s
 // The tables are computed assuming that the bin spacing is that of QRA64, that's to say
-// 1/Ts = 12000/6912 Hz, but in q65 Ts is longer (0.640 s) and the table index
+// 1/Ts = 12000/6912 Hz, but in Q65 Ts depends on the T/R interval and the table index
 // corresponding to a given B90 must be scaled appropriately.
 // See below.
 
@@ -287,7 +287,7 @@ int q65_intrinsics_fastfading(q65_codec_ds *pCodec,
 								float *pIntrinsics,				// intrinsic symbol probabilities output
 								const float *pInputEnergies,	// received energies input
 								const int submode,				// submode idx (0=A ... 4=E)
-								const float B90,				// spread bandwidth (90% fractional energy)
+								const float B90Ts,				// spread bandwidth (90% fractional energy)
 								const int fadingModel)			// 0=Gaussian 1=Lorentzian fade model
 {
 	int n, k, j;
@@ -295,26 +295,26 @@ int q65_intrinsics_fastfading(q65_codec_ds *pCodec,
 	int hidx, hlen, hhsz, hlast;
 	const float *hptr;
 	float fTemp, fNoiseVar, sumix, maxlogp;
-	float EsNoMetric;
+	float EsNoMetric,B90;
 	float *weight;
 	const float *pCurSym, *pCurBin;
 	float *pCurIx;
 
+//	printf("pcodec=%08x submode=%d fadingmodel=%d B90Ts=%f\n",pcodec, submode,fadingModel, B90Ts);
+	
 	if (pCodec==NULL)
 		return Q65_DECODE_INVPARAMS;	// invalid pCodec pointer
 
+	
 	if (submode<0 || submode>4)
 		return Q65_DECODE_INVPARAMS;	// invalid submode
 
-	// As the symbol duration in q65 is longer than in QRA64 the fading tables continue
-	// to be valid if the B90 parameter is scaled by the actual symbol rate
+	// As the symbol duration in q65 is different than in QRA64,
+	// the fading tables continue to be valid if the B90Ts parameter 
+	// is properly scaled to the QRA64 symbol interval
 	// Compute index to most appropriate weighting function coefficients
-    hidx = (int)(logf(B90*TS_Q65/TS_QRA64)/logf(1.09f) - 0.499f);
-
-//	if (hidx<0 || hidx > 64) 
-//		// index of weighting function out of range
-//		// B90 out of range
-//		return q65_DECODE_INVPARAMS;	
+	B90 = B90Ts/TS_QRA64;
+    hidx = (int)(logf(B90)/logf(1.09f) - 0.499f);
 
 	// Unlike in QRA64 we accept any B90, anyway limiting it to
 	// the extreme cases (0.9 to 210 Hz approx.)
diff --git a/lib/qra/q65/q65.h b/lib/qra/q65/q65.h
index bd80a934c..2e764a32b 100644
--- a/lib/qra/q65/q65.h
+++ b/lib/qra/q65/q65.h
@@ -69,7 +69,7 @@ int		q65_intrinsics_fastfading(q65_codec_ds *pCodec,
 					float *pIntrinsics,				// intrinsic symbol probabilities output
 					const float *pInputEnergies,	// received energies input
 					const int submode,				// submode idx (0=A ... 4=E)
-					const float B90,				// spread bandwidth (90% fractional energy)
+					const float B90Ts,				// normalized spread bandwidth (90% fractional energy)
 					const int fadingModel);			// 0=Gaussian 1=Lorentzian fade model
 
 
diff --git a/lib/qra/q65/q65_loops.f90 b/lib/qra/q65/q65_loops.f90
index 1e1576556..1a976e26b 100644
--- a/lib/qra/q65/q65_loops.f90
+++ b/lib/qra/q65/q65_loops.f90
@@ -1,6 +1,6 @@
 subroutine q65_loops(c00,nutc,npts2,nsps,mode,mode64,nsubmode,nFadingModel,   &
      ndepth,jpk0,xdt0,f0,width,iaptype,APmask,APsymbols,snr1,xdt1,f1,    &
-     snr2,irc,dat4)
+     snr2,irc,dat4,baud)
 
   use packjt77
   use timer_module, only: timer
@@ -84,7 +84,8 @@ subroutine q65_loops(c00,nutc,npts2,nsps,mode,mode64,nsubmode,nFadingModel,   &
               if(b90.gt.230.0) cycle
 !              if(b90.lt.0.15*width) exit
               call timer('q65_intr',0)
-              call q65_intrinsics_ff(s3,nsubmode,b90,nFadingModel,s3prob)
+			  b90ts = b90/baud	
+              call q65_intrinsics_ff(s3,nsubmode,b90ts,nFadingModel,s3prob)
               call timer('q65_intr',1)
 
               call timer('q65_dec ',0)
diff --git a/lib/qra/q65/q65_subs.c b/lib/qra/q65/q65_subs.c
index b36adc540..9d765c86b 100644
--- a/lib/qra/q65/q65_subs.c
+++ b/lib/qra/q65/q65_subs.c
@@ -46,7 +46,7 @@ void q65_enc_(int x[], int y[])
   q65_encode(&codec,y,x);
 }
 
-void q65_intrinsics_ff_(float s3[], int* submode, float* B90,
+void q65_intrinsics_ff_(float s3[], int* submode, float* B90Ts,
 			int* fadingModel, float s3prob[])
 {
 
@@ -69,7 +69,7 @@ void q65_intrinsics_ff_(float s3[], int* submode, float* B90,
     }
     first=0;
   }
-  rc = q65_intrinsics_fastfading(&codec,s3prob,s3,*submode,*B90,*fadingModel);
+  rc = q65_intrinsics_fastfading(&codec,s3prob,s3,*submode,*B90Ts,*fadingModel);
   if(rc<0) {
     printf("error in q65_intrinsics()\n");
     exit(0);