From 9e083707624636bf3f5e58c6a5baf74d5655dedb Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Fri, 18 Mar 2016 19:05:16 +0100
Subject: [PATCH] add pucch energy plot

---
 T_messages.txt |  4 ++++
 tracer/defs.h  |  4 +++-
 tracer/main.c  | 50 ++++++++++++++++++++++++++++++++++++++++----------
 tracer/plot.c  | 33 ++++++++++++++++++++++++++++++---
 4 files changed, 77 insertions(+), 14 deletions(-)

diff --git a/T_messages.txt b/T_messages.txt
index f954dd46ec..dd5dd8eec1 100644
--- a/T_messages.txt
+++ b/T_messages.txt
@@ -15,6 +15,10 @@ ID = PUCCH_1AB_IQ
     DESC = eNodeB PUCCH received IQ data
     GROUP = PHY:GRAPHIC:HEAVY
     FORMAT = int,eNB_ID : int,UE_ID : int,frame : int,subframe : int,I : int,Q
+ID = PUCCH_1_ENERGY
+    DESC = eNodeB PUCCH 1 energy and threshold
+    GROUP = PHY:GRAPHIC:HEAVY
+    FORMAT = int,eNB_ID : int,UE_ID : int,frame : int,subframe : int,energy : int,threshold
 
 #legacy logs
 ID = LEGACY_MAC_INFO
diff --git a/tracer/defs.h b/tracer/defs.h
index 483e42ae90..231255b46d 100644
--- a/tracer/defs.h
+++ b/tracer/defs.h
@@ -4,13 +4,15 @@
 /* types of plots */
 #define PLOT_VS_TIME   0
 #define PLOT_IQ_POINTS 1
+#define PLOT_MINMAX    2
 
 /* ... is { int count; int type; char *color; } for 'nplots' plots */
 void *make_plot(int width, int height, char *title, int nplots, ...);
 void plot_set(void *plot, float *data, int len, int pos, int pp);
 void iq_plot_set(void *plot, short *data, int len, int pos, int pp);
 void iq_plot_set_sized(void *_plot, short *data, int len, int pp);
-void iq_plot_add_point_loop(void *_plot, short i, short q, int pp);
+void iq_plot_add_iq_point_loop(void *_plot, short i, short q, int pp);
+void iq_plot_add_energy_point_loop(void *_plot, int e, int pp);
 
 /* returns an opaque pointer - truly a 'database *', see t_data.c */
 void *parse_database(char *filename);
diff --git a/tracer/main.c b/tracer/main.c
index 0b529b132b..cedad0079d 100644
--- a/tracer/main.c
+++ b/tracer/main.c
@@ -7,6 +7,7 @@
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <math.h>
 
 #include "defs.h"
 
@@ -14,10 +15,14 @@
 #include "../T_IDs.h"
 #include "../T_defs.h"
 
+#define BLUE "#0c0c72"
+#define RED "#d72828"
+
 void *ul_plot;
 void *chest_plot;
 void *pusch_iq_plot;
 void *pucch_iq_plot;
+void *pucch_plot;
 
 #ifdef T_USE_SHARED_MEMORY
 
@@ -213,8 +218,12 @@ void get_message(int s)
     GET(s, &size, sizeof(int));
     GET(s, buf, size);
 #if 0
-    printf("got T_ENB_INPUT_SIGNAL eNB %d frame %d subframe %d antenna %d size %d %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
-           eNB, frame, subframe, antenna, size, buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],buf[10],buf[11],buf[12],buf[13],buf[14],buf[15]);
+    printf("got T_ENB_INPUT_SIGNAL eNB %d frame %d subframe %d antenna "
+           "%d size %d %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x "
+           "%2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
+           eNB, frame, subframe, antenna, size, buf[0],buf[1],buf[2],
+           buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],buf[10],
+           buf[11],buf[12],buf[13],buf[14],buf[15]);
 #endif
     if (size != 4 * 7680)
       {printf("bad T_ENB_INPUT_SIGNAL, only 7680 samples allowed\n");abort();}
@@ -272,8 +281,22 @@ void get_message(int s)
     GET(s, &subframe, sizeof(int));
     GET(s, &I, sizeof(int));
     GET(s, &Q, sizeof(int));
-printf("receiving %d %d\n", I, Q);
-    if (pucch_iq_plot) iq_plot_add_point_loop(pucch_iq_plot, I*10, Q*10, 0);
+    if (pucch_iq_plot) iq_plot_add_iq_point_loop(pucch_iq_plot,I*10,Q*10, 0);
+    break;
+  }
+  case T_PUCCH_1_ENERGY: {
+    int eNB, UE, frame, subframe, e, t;
+    GET(s, &eNB, sizeof(int));
+    GET(s, &UE, sizeof(int));
+    GET(s, &frame, sizeof(int));
+    GET(s, &subframe, sizeof(int));
+    GET(s, &e, sizeof(int));
+    GET(s, &t, sizeof(int));
+//printf("t %d e %d\n", t, (int)(10*log10(e)));
+    if (pucch_plot) {
+      iq_plot_add_energy_point_loop(pucch_plot, t, 0);
+      iq_plot_add_energy_point_loop(pucch_plot, 10*log10(e), 1);
+    }
     break;
   }
   case T_buf_test: {
@@ -281,8 +304,10 @@ printf("receiving %d %d\n", I, Q);
     int size;
     GET(s, &size, sizeof(int));
     GET(s, buf, size);
-    printf("got buffer size %d %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
-           size, buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],buf[8],buf[9],buf[10],buf[11],buf[12],buf[13],buf[14],buf[15]);
+    printf("got buffer size %d %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x"
+           " %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
+           size, buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],
+           buf[8],buf[9],buf[10],buf[11],buf[12],buf[13],buf[14],buf[15]);
     break;
   }
   default: printf("unkown message type %d\n", m); abort();
@@ -386,13 +411,18 @@ int main(int n, char **v)
 
   if (do_xforms) {
     ul_plot = make_plot(512, 100, "UL Input Signal", 1,
-                        7680*10, PLOT_VS_TIME, "blue");
+                        7680*10, PLOT_VS_TIME, BLUE);
     chest_plot = make_plot(512, 100, "UL Channel Estimate UE 0", 1,
-                           512, PLOT_VS_TIME, "blue");
+                           512, PLOT_VS_TIME, BLUE);
     pusch_iq_plot = make_plot(100, 100, "PUSCH IQ", 1,
-                              12*25*14, PLOT_IQ_POINTS, "blue");
+                              12*25*14, PLOT_IQ_POINTS, BLUE);
     pucch_iq_plot = make_plot(100, 100, "PUCCH IQ", 1,
-                              1000, PLOT_IQ_POINTS, "blue");
+                              1000, PLOT_IQ_POINTS, BLUE);
+    pucch_plot = make_plot(512, 100, "PUCCH 1 energy (SR)", 2,
+                           /* threshold */
+                           10240, PLOT_MINMAX, RED,
+                           /* pucch 1 */
+                           10240, PLOT_MINMAX, BLUE);
   }
 
   for (i = 0; i < on_off_n; i++)
diff --git a/tracer/plot.c b/tracer/plot.c
index e9aa57a5d2..58f25fd41c 100644
--- a/tracer/plot.c
+++ b/tracer/plot.c
@@ -84,9 +84,21 @@ static void *plot_thread(void *_p)
 
       if (pthread_mutex_lock(&p->lock)) abort();
 
+      XFillRectangle(p->d, p->px, p->bg, 0, 0, p->width, p->height);
+
       for (pp = 0; pp < p->nplots; pp++) {
-        XFillRectangle(p->d, p->px, p->bg, 0, 0, p->width, p->height);
-        if (p->p[pp].type == PLOT_VS_TIME) {
+        if (p->p[pp].type == PLOT_MINMAX) {
+          s = p->p[pp].buf;
+          for (i = 0; i < 512; i++) {
+            int min = *s;
+            int max = *s;
+            for (j = 0; j < p->p[pp].count/512; j++, s++) {
+              if (*s < min) min = *s;
+              if (*s > max) max = *s;
+            }
+            XDrawLine(p->d, p->px, p->p[pp].g, i, 100-min, i, 100-max);
+          }
+        } else if (p->p[pp].type == PLOT_VS_TIME) {
           for (i = 0; i < p->p[pp].count; i++)
             p->p[pp].buf[i] =
                 10*log10(1.0+(float)(p->p[pp].iqbuf[2*i]*p->p[pp].iqbuf[2*i]+
@@ -215,6 +227,10 @@ void *make_plot(int width, int height, char *title, int nplots, ...)
       if (p->p[i].buf == NULL) abort();
       p->p[i].iqbuf = malloc(sizeof(short) * count * 2);
       if(p->p[i].iqbuf==NULL)abort();
+    } else if (type == PLOT_MINMAX) {
+      p->p[i].buf = malloc(sizeof(float) * count);
+      if (p->p[i].buf == NULL) abort();
+      p->p[i].iqbuf = NULL;
     } else {
       p->p[i].buf = NULL;
       p->p[i].iqbuf = malloc(sizeof(short) * count * 2);
@@ -269,7 +285,7 @@ void iq_plot_set_sized(void *_plot, short *data, int count, int pp)
   if (pthread_mutex_unlock(&p->lock)) abort();
 }
 
-void iq_plot_add_point_loop(void *_plot, short i, short q, int pp)
+void iq_plot_add_iq_point_loop(void *_plot, short i, short q, int pp)
 {
   plot *p = _plot;
   if (pthread_mutex_lock(&p->lock)) abort();
@@ -280,3 +296,14 @@ void iq_plot_add_point_loop(void *_plot, short i, short q, int pp)
   if (p->p[pp].iq_insert_pos == p->p[pp].count) p->p[pp].iq_insert_pos = 0;
   if (pthread_mutex_unlock(&p->lock)) abort();
 }
+
+void iq_plot_add_energy_point_loop(void *_plot, int e, int pp)
+{
+  plot *p = _plot;
+  if (pthread_mutex_lock(&p->lock)) abort();
+  p->p[pp].buf[p->p[pp].iq_insert_pos] = e;
+  if (p->p[pp].iq_count != p->p[pp].count) p->p[pp].iq_count++;
+  p->p[pp].iq_insert_pos++;
+  if (p->p[pp].iq_insert_pos == p->p[pp].count) p->p[pp].iq_insert_pos = 0;
+  if (pthread_mutex_unlock(&p->lock)) abort();
+}
-- 
2.26.2