From 353f153353863b97fad69d3a0c59494ffac0f448 Mon Sep 17 00:00:00 2001
From: Francesco Mani <francesco.mani@orange.com>
Date: Tue, 26 Feb 2019 18:06:43 +0100
Subject: [PATCH] slot_fep for ssb done once before sss and pbch channel
 estimation moved out of slot_fep

---
 openair1/PHY/INIT/nr_init.c                   |  2 +-
 openair1/PHY/MODULATION/modulation_UE.h       |  3 +-
 openair1/PHY/MODULATION/slot_fep_nr.c         | 49 ++---------------
 .../nr_dl_channel_estimation.c                |  8 +--
 openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h |  4 --
 .../PHY/NR_UE_TRANSPORT/nr_initial_sync.c     | 52 +++++++++----------
 openair1/PHY/NR_UE_TRANSPORT/pss_nr.c         |  3 +-
 openair1/PHY/NR_UE_TRANSPORT/sss_nr.c         | 18 -------
 openair1/SCHED_NR_UE/phy_procedures_nr_ue.c   |  3 --
 openair1/SIMULATION/NR_PHY/pbchsim.c          | 40 +++++---------
 targets/RT/USER/nr-ue.c                       |  2 +-
 11 files changed, 48 insertions(+), 136 deletions(-)

diff --git a/openair1/PHY/INIT/nr_init.c b/openair1/PHY/INIT/nr_init.c
index 568070df93..2f613152dd 100644
--- a/openair1/PHY/INIT/nr_init.c
+++ b/openair1/PHY/INIT/nr_init.c
@@ -363,7 +363,7 @@ void nr_phy_config_request_sim(PHY_VARS_gNB *gNB,int N_RB_DL,int N_RB_UL,int mu,
   NR_DL_FRAME_PARMS *fp = &gNB->frame_parms;
   nfapi_nr_config_request_t *gNB_config = &gNB->gNB_config;
   //overwrite for new NR parameters
-  gNB_config->nfapi_config.rf_bands.rf_band[0] = 78;
+  gNB_config->nfapi_config.rf_bands.rf_band[0] = 5;
   gNB_config->nfapi_config.nrarfcn.value = 620000;
   gNB_config->subframe_config.numerology_index_mu.value = mu;
   gNB_config->subframe_config.duplex_mode.value = TDD;
diff --git a/openair1/PHY/MODULATION/modulation_UE.h b/openair1/PHY/MODULATION/modulation_UE.h
index 9734c02907..63c3637886 100644
--- a/openair1/PHY/MODULATION/modulation_UE.h
+++ b/openair1/PHY/MODULATION/modulation_UE.h
@@ -53,8 +53,7 @@ int nr_slot_fep(PHY_VARS_NR_UE *phy_vars_ue,
              unsigned char Ns,
              int sample_offset,
              int no_prefix,
-	     int reset_freq_est,
-			 NR_CHANNEL_EST_t channel);
+             NR_CHANNEL_EST_t channel);
 
 int slot_fep_mbsfn(PHY_VARS_UE *phy_vars_ue,
                    unsigned char l,
diff --git a/openair1/PHY/MODULATION/slot_fep_nr.c b/openair1/PHY/MODULATION/slot_fep_nr.c
index 1e1751fe24..f41212e543 100644
--- a/openair1/PHY/MODULATION/slot_fep_nr.c
+++ b/openair1/PHY/MODULATION/slot_fep_nr.c
@@ -39,12 +39,10 @@ int nr_slot_fep(PHY_VARS_NR_UE *ue,
 		unsigned char Ns,
 		int sample_offset,
 		int no_prefix,
-		int reset_freq_est,
 		NR_CHANNEL_EST_t channel)
 {
   NR_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
   NR_UE_COMMON *common_vars   = &ue->common_vars;
-  uint8_t eNB_id = 0;//ue_common_vars->eNb_id;
   unsigned char aa;
   unsigned char symbol = l;//+((7-frame_parms->Ncp)*(Ns&1)); ///symbol within sub-frame
   unsigned int nb_prefix_samples = (no_prefix ? 0 : frame_parms->nb_prefix_samples);
@@ -204,56 +202,17 @@ int nr_slot_fep(PHY_VARS_NR_UE *ue,
 
   switch(channel){
   case NR_PBCH_EST:
-
-#ifdef DEBUG_FEP
-    printf("Channel estimation eNB %d, slot %d, symbol %d\n",eNB_id,Ns,l);
-#endif
-#if UE_TIMING_TRACE
-    start_meas(&ue->dlsch_channel_estimation_stats);
-#endif
-    nr_pbch_channel_estimation(ue,eNB_id,0,
-			       Ns,
-			       l,
-			       symbol);
-      //}
-#if UE_TIMING_TRACE
-        stop_meas(&ue->dlsch_channel_estimation_stats);
-#endif
-
-      // do frequency offset estimation here!
-      // use channel estimates from current symbol (=ch_t) and last symbol (ch_{t-1})
-#ifdef DEBUG_FEP
-      printf("Frequency offset estimation\n");
-#endif
-
-      if (l==(4-frame_parms->Ncp)) {
-
-#if UE_TIMING_TRACE
-          start_meas(&ue->dlsch_freq_offset_estimation_stats);
-#endif
-
-        /*lte_est_freq_offset(common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].dl_ch_estimates[0],
-                            frame_parms,
-                            l,
-                            &common_vars->freq_offset,
-			    reset_freq_est);*/
-#if UE_TIMING_TRACE
-        stop_meas(&ue->dlsch_freq_offset_estimation_stats);
-#endif
-
-      }
-
   break;
 
   case NR_PDCCH_EST:
 
 #ifdef DEBUG_FEP
-    printf("PDCCH Channel estimation eNB %d, aatx %d, slot %d, symbol %d start_sc %d\n",eNB_id,aa,Ns,l,coreset_start_subcarrier);
+    printf("PDCCH Channel estimation aatx %d, slot %d, symbol %d start_sc %d\n",aa,Ns,l,coreset_start_subcarrier);
 #endif
 #if UE_TIMING_TRACE
     start_meas(&ue->dlsch_channel_estimation_stats);
 #endif
-    nr_pdcch_channel_estimation(ue,eNB_id,0,
+    nr_pdcch_channel_estimation(ue,0,
 				Ns,
 				l,
 				symbol,
@@ -267,7 +226,7 @@ int nr_slot_fep(PHY_VARS_NR_UE *ue,
     
   case NR_PDSCH_EST:
 #ifdef DEBUG_FEP
-    printf("Channel estimation eNB %d, aatx %d, slot %d, symbol %d\n",eNB_id,aa,Ns,l);
+    printf("Channel estimation aatx %d, slot %d, symbol %d\n",aa,Ns,l);
 #endif
 #if UE_TIMING_TRACE
     start_meas(&ue->dlsch_channel_estimation_stats);
@@ -276,7 +235,7 @@ int nr_slot_fep(PHY_VARS_NR_UE *ue,
   ue->frame_parms.nushift =  (p>>1)&1;;
 
     if (symbol ==l0)
-    nr_pdsch_channel_estimation(ue,eNB_id,0,
+    nr_pdsch_channel_estimation(ue,0,
 				Ns,
 				p,
 				l,
diff --git a/openair1/PHY/NR_UE_ESTIMATION/nr_dl_channel_estimation.c b/openair1/PHY/NR_UE_ESTIMATION/nr_dl_channel_estimation.c
index a90450b0a5..05ee82ae29 100644
--- a/openair1/PHY/NR_UE_ESTIMATION/nr_dl_channel_estimation.c
+++ b/openair1/PHY/NR_UE_ESTIMATION/nr_dl_channel_estimation.c
@@ -33,10 +33,8 @@
 
 
 int nr_pbch_channel_estimation(PHY_VARS_NR_UE *ue,
-			       uint8_t eNB_id,
 			       uint8_t eNB_offset,
 			       unsigned char Ns,
-			       unsigned char l,
 			       unsigned char symbol)
 {
   int pilot[200] __attribute__((aligned(16)));
@@ -78,8 +76,8 @@ int nr_pbch_channel_estimation(PHY_VARS_NR_UE *ue,
   k = nushift;
 
 #ifdef DEBUG_CH
-  printf("PBCH Channel Estimation : ThreadId %d, eNB_offset %d ch_offset %d, OFDM size %d, Ncp=%d, l=%d, Ns=%d, k=%d symbol %d\n",ue->current_thread_id[Ns>>1], eNB_offset,ch_offset,ue->frame_parms.ofdm_symbol_size,
-         ue->frame_parms.Ncp,l,Ns,k, symbol);
+  printf("PBCH Channel Estimation : ThreadId %d, eNB_offset %d ch_offset %d, OFDM size %d, Ncp=%d, Ns=%d, k=%d symbol %d\n",ue->current_thread_id[Ns>>1], eNB_offset,ch_offset,ue->frame_parms.ofdm_symbol_size,
+         ue->frame_parms.Ncp,Ns,k, symbol);
 #endif
 
   switch (k) {
@@ -254,7 +252,6 @@ int nr_pbch_channel_estimation(PHY_VARS_NR_UE *ue,
 }
 
 int nr_pdcch_channel_estimation(PHY_VARS_NR_UE *ue,
-				uint8_t eNB_id,
 				uint8_t eNB_offset,
 				unsigned char Ns,
 				unsigned char l,
@@ -478,7 +475,6 @@ int nr_pdcch_channel_estimation(PHY_VARS_NR_UE *ue,
 }
 
 int nr_pdsch_channel_estimation(PHY_VARS_NR_UE *ue,
-                              uint8_t eNB_id,
                               uint8_t eNB_offset,
                               unsigned char Ns,
                               unsigned short p,
diff --git a/openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h b/openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h
index ae4295a99b..a6ede43504 100644
--- a/openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h
+++ b/openair1/PHY/NR_UE_ESTIMATION/nr_estimation.h
@@ -43,7 +43,6 @@
 \param symbol symbol within frame
 */
 int nr_pdcch_channel_estimation(PHY_VARS_NR_UE *ue,
-				uint8_t eNB_id,
 				uint8_t eNB_offset,
 				unsigned char Ns,
 				unsigned char l,
@@ -52,14 +51,11 @@ int nr_pdcch_channel_estimation(PHY_VARS_NR_UE *ue,
 				unsigned short nb_rb_coreset);
 
 int nr_pbch_channel_estimation(PHY_VARS_NR_UE *ue,
-                              uint8_t eNB_id,
                               uint8_t eNB_offset,
                               unsigned char Ns,
-                              unsigned char l,
                               unsigned char symbol);
 
 int nr_pdsch_channel_estimation(PHY_VARS_NR_UE *ue,
-                              uint8_t eNB_id,
                               uint8_t eNB_offset,
                               unsigned char Ns,
                               unsigned short p,
diff --git a/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c b/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c
index e5f079ebc3..ea4745fe23 100644
--- a/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c
+++ b/openair1/PHY/NR_UE_TRANSPORT/nr_initial_sync.c
@@ -35,6 +35,7 @@
 #include "PHY/INIT/phy_init.h"
 #include "PHY/MODULATION/modulation_UE.h"
 #include "nr_transport_proto_ue.h"
+#include "PHY/NR_UE_ESTIMATION/nr_estimation.h"
 //#include "SCHED/defs.h"
 //#include "SCHED/extern.h"
 
@@ -68,32 +69,18 @@ int nr_pbch_detection(PHY_VARS_NR_UE *ue, runmode_t mode)
   frame_parms->nb_prefix_samples0 = frame_parms->nb_prefix_samples;
 
 
-  //symbol 1
-  nr_slot_fep(ue,
-	      1,
-	      0,
-	      ue->ssb_offset,
-	      0,
-	      1,
-	      NR_PBCH_EST);
-  
-  //symbol 2
-  nr_slot_fep(ue,
-	      2,
-	      0,
-	      ue->ssb_offset,
-	      0,
-	      1,
-	      NR_PBCH_EST);
-
-  //symbol 3
-  nr_slot_fep(ue,
-	      3,
-	      0,
-	      ue->ssb_offset,
-	      0,
-	      1,
-	      NR_PBCH_EST);
+  for(int i=1; i<4;i++) {
+ 
+#if UE_TIMING_TRACE
+    start_meas(&ue->dlsch_channel_estimation_stats);
+#endif
+    nr_pbch_channel_estimation(ue,0,
+			       0,
+			       i);
+#if UE_TIMING_TRACE
+    stop_meas(&ue->dlsch_channel_estimation_stats);
+#endif
+  }
 
   //put back nb_prefix_samples0
   frame_parms->nb_prefix_samples0 = nb_prefix_samples0;
@@ -224,6 +211,15 @@ int nr_initial_sync(PHY_VARS_NR_UE *ue, runmode_t mode)
   /* check that SSS/PBCH block is continuous inside the received buffer */
   if (sync_pos < (NR_NUMBER_OF_SUBFRAMES_PER_FRAME*fp->samples_per_subframe - (NB_SYMBOLS_PBCH * fp->ofdm_symbol_size))) {
 
+
+    for(int i=0; i<4;i++)
+      nr_slot_fep(ue,
+	          i,
+	          0,
+	          ue->ssb_offset,
+	          0,
+	          NR_PBCH_EST);
+
 #ifdef DEBUG_INITIAL_SYNCH
     LOG_I(PHY,"Calling sss detection (normal CP)\n");
 #endif
@@ -241,8 +237,8 @@ int nr_initial_sync(PHY_VARS_NR_UE *ue, runmode_t mode)
     int nb_prefix_samples0 = fp->nb_prefix_samples0;
     fp->nb_prefix_samples0 = fp->nb_prefix_samples;
 	  
-    nr_slot_fep(ue,0, 0, ue->ssb_offset, 0, 1, NR_PDCCH_EST);
-    nr_slot_fep(ue,1, 0, ue->ssb_offset, 0, 1, NR_PDCCH_EST);
+    nr_slot_fep(ue,0, 0, ue->ssb_offset, 0, NR_PDCCH_EST);
+    nr_slot_fep(ue,1, 0, ue->ssb_offset, 0, NR_PDCCH_EST);
     fp->nb_prefix_samples0 = nb_prefix_samples0;	
 
     LOG_I(PHY,"[UE  %d] AUTOTEST Cell Sync : frame = %d, rx_offset %d, freq_offset %d \n",
diff --git a/openair1/PHY/NR_UE_TRANSPORT/pss_nr.c b/openair1/PHY/NR_UE_TRANSPORT/pss_nr.c
index 9b09a846bb..e4495a5526 100644
--- a/openair1/PHY/NR_UE_TRANSPORT/pss_nr.c
+++ b/openair1/PHY/NR_UE_TRANSPORT/pss_nr.c
@@ -898,6 +898,7 @@ int pss_search_time_nr(int **rxdata, ///rx data in time domain
   }
 
   if (fo_flag){
+
 	  // fractional frequency offser computation according to Cross-correlation Synchronization Algorithm Using PSS
 	  // Shoujun Huang, Yongtao Su, Ying He and Shan Tang, "Joint time and frequency offset estimation in LTE downlink," 7th International Conference on Communications and Networking in China, 2012.
 
@@ -922,7 +923,7 @@ int pss_search_time_nr(int **rxdata, ///rx data in time domain
 
  	  // estimation of fractional frequency offset: angle[(result1)'*(result2)]/pi
 	  ffo_est=atan2(re1*im2-re2*im1,re1*re2+im1*im2)/M_PI;
-  
+
 #ifdef DBG_PSS_NR
 	  printf("ffo %lf\n",ffo_est);
 #endif
diff --git a/openair1/PHY/NR_UE_TRANSPORT/sss_nr.c b/openair1/PHY/NR_UE_TRANSPORT/sss_nr.c
index 26639a5134..493672f890 100644
--- a/openair1/PHY/NR_UE_TRANSPORT/sss_nr.c
+++ b/openair1/PHY/NR_UE_TRANSPORT/sss_nr.c
@@ -443,24 +443,6 @@ int rx_sss_nr(PHY_VARS_NR_UE *ue, int32_t *tot_metric,uint8_t *phase_max)
   // For now, symbol 0 = PSS/PBCH and it is never in symbol 0 or 7*2^mu (i.e. always shorter prefix)
   frame_parms->nb_prefix_samples0 = frame_parms->nb_prefix_samples;
 
-  // Do FFTs for SSS/PSS
-  // SSS
-  nr_slot_fep(ue,
-	      SSS_SYMBOL_NB-PSS_SYMBOL_NB,      // symbol number w.r.t. PSS
-	      0,                  // Ns slot number
-	      ue->ssb_offset,      // sample_offset of int16_t
-	      0,                  // no_prefix
-	      1,                  // reset frequency estimation
-	      NR_SSS_EST);
-
-  // PSS
-  nr_slot_fep(ue,
-	      0,
-	      0,
-	      ue->ssb_offset,
-	      0,
-	      1,
-	      NR_SSS_EST);
 
   frame_parms->nb_prefix_samples0 = nb_prefix_samples0;
 
diff --git a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
index 1a631000f5..a2388def23 100644
--- a/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
+++ b/openair1/SCHED_NR_UE/phy_procedures_nr_ue.c
@@ -4991,7 +4991,6 @@ int phy_procedures_nrUE_RX(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t eN
 		nr_tti_rx,
 		0,
 		0,
-		1,
 		NR_PDCCH_EST);
     VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SLOT_FEP, VCD_FUNCTION_OUT);
 #if UE_TIMING_TRACE
@@ -5022,7 +5021,6 @@ int phy_procedures_nrUE_RX(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t eN
 		  nr_tti_rx,
 		  0,
 		  0,
-		  1,
 		  NR_PDSCH_EST);
       
       //printf("phy procedure pdsch start measurement\n"); 
@@ -5106,7 +5104,6 @@ int phy_procedures_nrUE_RX(PHY_VARS_NR_UE *ue,UE_nr_rxtx_proc_t *proc,uint8_t eN
 		    nr_tti_rx,
 		    0,
 		    0,
-		    1,
 		    NR_PBCH_EST);
       
       nr_ue_pbch_procedures(eNB_id,ue,proc,0);
diff --git a/openair1/SIMULATION/NR_PHY/pbchsim.c b/openair1/SIMULATION/NR_PHY/pbchsim.c
index a52c6ecf0d..74850d2509 100644
--- a/openair1/SIMULATION/NR_PHY/pbchsim.c
+++ b/openair1/SIMULATION/NR_PHY/pbchsim.c
@@ -43,6 +43,7 @@
 #include "PHY/INIT/phy_init.h"
 #include "PHY/NR_TRANSPORT/nr_transport.h"
 #include "PHY/NR_UE_TRANSPORT/nr_transport_proto_ue.h"
+#include "PHY/NR_UE_ESTIMATION/nr_estimation.h"
 
 #include "SCHED_NR/sched_nr.h"
 
@@ -371,6 +372,7 @@ int main(int argc, char **argv)
   frame_parms->N_RB_DL = N_RB_DL;
   frame_parms->N_RB_UL = N_RB_DL;
   frame_parms->Nid_cell = Nid_cell;
+  frame_parms->ssb_type = nr_ssb_type_B;
 
   nr_phy_config_request_sim(gNB,N_RB_DL,N_RB_DL,mu,Nid_cell,SSB_positions);
   phy_init_nr_gNB(gNB,0,0);
@@ -545,7 +547,6 @@ int main(int argc, char **argv)
     n_errors_payload = 0;
 
     for (trial=0; trial<n_trials; trial++) {
-
       // multipath channel
       //multipath_channel(gNB2UE,s_re,s_im,r_re,r_im,frame_length_complex_samples,0);
       
@@ -596,32 +597,17 @@ int main(int argc, char **argv)
       }
       else {
 	UE->rx_offset=0;
-	//symbol 1
-	nr_slot_fep(UE,
-		    5,
-		    0,
-		    0,
-		    0,
-		    1,
-		    NR_PBCH_EST);
-	
-	//symbol 2
-	nr_slot_fep(UE,
-		    6,
-		    0,
-		    0,
-		    0,
-		    1,
-		    NR_PBCH_EST);
-	
-	//symbol 3
-	nr_slot_fep(UE,
-		    7,
-		    0,
-		    0,
-		    0,
-		    1,
-		    NR_PBCH_EST);
+
+	for (int i=5; i<8; i++) {
+	  nr_slot_fep(UE,
+	  	      i,
+		      0,
+		      0,
+		      0,
+		      NR_PBCH_EST);
+
+          nr_pbch_channel_estimation(UE,0,0,i);
+        }
 	 
 	ret = nr_rx_pbch(UE,
 			 &UE->proc.proc_rxtx[0],
diff --git a/targets/RT/USER/nr-ue.c b/targets/RT/USER/nr-ue.c
index 1dc3085edf..020dd9adbf 100644
--- a/targets/RT/USER/nr-ue.c
+++ b/targets/RT/USER/nr-ue.c
@@ -965,7 +965,7 @@ void *UE_thread(void *arg) {
                                                 UE->frame_parms.ofdm_symbol_size+UE->frame_parms.nb_prefix_samples0,
                                                 UE->frame_parms.nb_antennas_rx),"");
         //write_output("txdata_sym.m", "txdata_sym", UE->common_vars.rxdata[0], (UE->frame_parms.ofdm_symbol_size+UE->frame_parms.nb_prefix_samples0), 1, 1);
-        //nr_slot_fep(UE,0, 0, 0, 1, 1, NR_PDCCH_EST);
+        //nr_slot_fep(UE,0, 0, 0, 1, NR_PDCCH_EST);
       } //UE->mode != loop_through_memory
       else
         rt_sleep_ns(1000*1000);
-- 
2.26.2