Add C/I computation

Change-Id: Ib4ceec553f2e5f77bf3f6777724968456a180f5e
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
diff --git a/Transceiver52M/sigProcLib.cpp b/Transceiver52M/sigProcLib.cpp
index fcda5fa..1ea1c54 100644
--- a/Transceiver52M/sigProcLib.cpp
+++ b/Transceiver52M/sigProcLib.cpp
@@ -1455,6 +1455,28 @@
   return out;
 };
 
+static float computeCI(const signalVector *burst, CorrelationSequence *sync,
+                       float toa, int start, complex xcorr)
+{
+  float S, C;
+  int ps;
+
+  /* Integer position where the sequence starts */
+  ps = start + 1 - sync->sequence->size() + (int)roundf(toa);
+
+  /* Estimate Signal power */
+  S = 0.0f;
+  for (int i=0, j=ps; i<(int)sync->sequence->size(); i++,j++)
+    S += (*burst)[j].norm2();
+  S /= sync->sequence->size();
+
+  /* Esimate Carrier power */
+  C = xcorr.norm2() / ((sync->sequence->size() - 1) * sync->gain.abs());
+
+  /* Interference = Signal - Carrier, so C/I = C / (S - C) */
+  return 3.0103f * log2f(C / (S - C));
+}
+
 /*
  * Detect a burst based on correlation and peak-to-average ratio
  *
@@ -1470,6 +1492,7 @@
 {
   const signalVector *corr_in;
   signalVector *dec = NULL;
+  complex xcorr;
 
   if (sps == 4) {
     dec = downsampleBurst(burst);
@@ -1497,15 +1520,18 @@
   if ((*toa < 3 * sps) || (*toa > len - 3 * sps))
     return 0;
 
-  /* Peak -to-average ratio */
+  /* Peak-to-average ratio */
   if (computePeakRatio(&corr, sps, *toa, *amp) < thresh)
     return 0;
 
-  /* Compute peak-to-average ratio. Reject if we don't have enough values */
-  *amp = peakDetect(corr, toa, NULL);
+  /* Refine TOA and correlation value */
+  xcorr = peakDetect(corr, toa, NULL);
+
+  /* Compute C/I */
+  float CI = computeCI(corr_in, sync, *toa, start, xcorr);
 
   /* Normalize our channel gain */
-  *amp = *amp / sync->gain;
+  *amp = xcorr / sync->gain;
 
   /* Compensate for residuate time lag */
   *toa = *toa - sync->toa;