From f4631f2186dc33b09835411656f034d8c60821d7 Mon Sep 17 00:00:00 2001
From: Parminder Singh <parminder.singh@iis.fraunhofer.de>
Date: Fri, 14 Aug 2020 10:20:36 +0200
Subject: [PATCH] Bug-fix in Up-link with respect to PUSCH RE calculation when
 PTRS is enabled.

Problem STATEMENT:
- When enabling PTRS flag in ulsim, there are multiple scrambling and decoding errors
  are observed even with high SNR values
  SNR 60dB   errors_scrambling 5546/13800
  Channel BLER 1.000000e+00, Channel BER 4.018841e-01

ANALYSIS:
- While extracting PTRS RE's, the PUSCH RE count was not updated accordingly for PTRS symbols.
- This results in 0 value LLR's on same positions which cause offset issue in later processing.

IMPLEMENTATION:
* nr_ulsch_demodulation.c
- In case of PTRS symbol, respective total number of PTRS RE's are subtracted from available
  PUSCH RE's in each PUSCH Symbol processing.

TESTING
* ulsim.c
- PTRS are enabled for testing and are not enabled by default by this commit.
- Available bit count is adjusted if PTRS is enabled.

VERIFICATION
- Total Scrambling errors after gNb processing shall be only 250 bits (in K=2)
  which were replaced with PTRS at UE side while transmission.
  SNR 60.000000 errors_scrambling 250/13800
  Channel BLER 0.000000e+00, Channel BER 1.811594e-02
- After adjusting available bits in ulsim.c for error calculation
  SNR 60.000000: n_errors errors_scrambling 0/13550
  Channel BLER 0.000000e+00, Channel BER 0.000000e+00

EXTRA
- Removed white-spaces from all above mentioned files
- Added Global/vim tags to .gitignore file
---
 .gitignore                                        |  6 ++++++
 openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c | 13 +++++++++++--
 openair1/PHY/NR_TRANSPORT/pucch_rx.c              |  3 +++
 openair1/SIMULATION/NR_PHY/ulsim.c                | 12 +++++++++++-
 4 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
index 8f8ff1f4548..ff947966a2b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,9 @@ targets/bin/
 
 # vscode
 .vscode
+
+# Tags for vim/global
+GPATH
+GRTAGS
+GTAGS
+tags
diff --git a/openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c b/openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c
index a966db5844a..824d933d5c8 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c
@@ -1018,7 +1018,7 @@ int nr_rx_pusch(PHY_VARS_gNB *gNB,
                 unsigned char harq_pid)
 {
 
-  uint8_t aarx, aatx, dmrs_symbol_flag; // dmrs_symbol_flag, a flag to indicate DMRS REs in current symbol
+  uint8_t aarx, aatx, dmrs_symbol_flag, ptrs_symbol_flag; // dmrs_symbol_flag, a flag to indicate DMRS REs in current symbol
   uint32_t nb_re_pusch, bwp_start_subcarrier;
   uint8_t L_ptrs = 0; // PTRS parameter
   int avgs;
@@ -1027,7 +1027,7 @@ int nr_rx_pusch(PHY_VARS_gNB *gNB,
   nfapi_nr_pusch_pdu_t *rel15_ul = &gNB->ulsch[ulsch_id][0]->harq_processes[harq_pid]->ulsch_pdu;
 
   dmrs_symbol_flag = 0;
-  gNB->pusch_vars[ulsch_id]->ptrs_sc_per_ofdm_symbol = 0;
+  ptrs_symbol_flag = 0;
 
   if(symbol == rel15_ul->start_symbol_index){
     gNB->pusch_vars[ulsch_id]->rxdataF_ext_offset = 0;
@@ -1069,7 +1069,10 @@ int nr_rx_pusch(PHY_VARS_gNB *gNB,
 
   if (rel15_ul->pdu_bit_map & PUSCH_PDU_BITMAP_PUSCH_PTRS) {  // if there is ptrs pdu
     if(is_ptrs_symbol(symbol, gNB->pusch_vars[ulsch_id]->ptrs_symbols))
+    {
+      ptrs_symbol_flag = 1;
       gNB->pusch_vars[ulsch_id]->ptrs_symbol_index = symbol;
+    }
   }
 
 
@@ -1100,6 +1103,12 @@ int nr_rx_pusch(PHY_VARS_gNB *gNB,
                                 frame_parms);
     stop_meas(&gNB->ulsch_rbs_extraction_stats);
 
+    if(ptrs_symbol_flag == 1)
+    {
+      /*  Subtract total PTRS RE's in the smybol from PUSCH RE's  */
+      nb_re_pusch -= gNB->pusch_vars[ulsch_id]->ptrs_sc_per_ofdm_symbol;
+    }
+
     nr_ulsch_scale_channel(gNB->pusch_vars[ulsch_id]->ul_ch_estimates_ext,
                            frame_parms,
                            gNB->ulsch[ulsch_id],
diff --git a/openair1/PHY/NR_TRANSPORT/pucch_rx.c b/openair1/PHY/NR_TRANSPORT/pucch_rx.c
index 501df8bb357..c41752cffb0 100644
--- a/openair1/PHY/NR_TRANSPORT/pucch_rx.c
+++ b/openair1/PHY/NR_TRANSPORT/pucch_rx.c
@@ -1047,6 +1047,8 @@ void init_pucch2_luts() {
     bit = (i&0x80) > 0 ? 0 : 1;
    *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,7);
    *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,7);
+
+#ifdef DEBUG_NR_PUCCH_RX
    printf("i %d, lut_num (%d,%d,%d,%d,%d,%d,%d,%d)\n",i,
 	  ((int16_t *)lut_num_i)[0],
 	  ((int16_t *)lut_num_i)[1],
@@ -1056,6 +1058,7 @@ void init_pucch2_luts() {
 	  ((int16_t *)lut_num_i)[5],
 	  ((int16_t *)lut_num_i)[6],
 	  ((int16_t *)lut_num_i)[7]);
+#endif 
   }
 }
 
diff --git a/openair1/SIMULATION/NR_PHY/ulsim.c b/openair1/SIMULATION/NR_PHY/ulsim.c
index bcaa967a30c..e3cd504c3b7 100644
--- a/openair1/SIMULATION/NR_PHY/ulsim.c
+++ b/openair1/SIMULATION/NR_PHY/ulsim.c
@@ -561,6 +561,7 @@ int main(int argc, char **argv)
   uint16_t l_prime_mask = get_l_prime(nb_symb_sch, typeB, pusch_dmrs_pos0, length_dmrs);  // [hna] remove dmrs struct
   uint8_t ptrs_time_density = get_L_ptrs(ptrs_mcs1, ptrs_mcs2, ptrs_mcs3, Imcs, mcs_table);
   uint8_t ptrs_freq_density = get_K_ptrs(n_rb0, n_rb1, nb_rb);
+  int ptrs_symbols = 0; // to calculate total PTRS RE's in a slot
 
   if(1<<ptrs_time_density >= nb_symb_sch)
     pdu_bit_map &= ~PUSCH_PDU_BITMAP_PUSCH_PTRS; // disable PUSCH PTRS
@@ -838,6 +839,15 @@ int main(int argc, char **argv)
         //----------------- count and print errors -----------------
         //----------------------------------------------------------
 
+        if (pusch_pdu->pdu_bit_map & PUSCH_PDU_BITMAP_PUSCH_PTRS) {
+            for (int i = pusch_pdu->start_symbol_index; i < pusch_pdu->start_symbol_index + pusch_pdu->nr_of_symbols; i++){
+               ptrs_symbols += ((gNB->pusch_vars[UE_id]->ptrs_symbols) >> i) & 1;
+            }
+            /*  2*5*(50/2), for RB = 50,K = 2 for 5 OFDM PTRS symbols */
+            available_bits = available_bits - (2 * ptrs_symbols * (nb_rb/((ptrs_freq_density)?4:2)));
+            printf("After PTRS subtraction available_bits are : %d \n", available_bits);
+        }
+
         for (i = 0; i < available_bits; i++) {
 
           if(((ulsch_ue[0]->g[i] == 0) && (gNB->pusch_vars[UE_id]->llr[i] <= 0)) ||
@@ -865,7 +875,7 @@ int main(int argc, char **argv)
             errors_decoding++;
           }
         }
-	if (n_trials == 1) {
+	if (n_trials == 1 && errors_decoding > 0) {
 	  for (int r=0;r<ulsch_ue[0]->harq_processes[harq_pid]->C;r++) 
 	    for (int i=0;i<ulsch_ue[0]->harq_processes[harq_pid]->K>>3;i++) {
 	      if ((ulsch_ue[0]->harq_processes[harq_pid]->c[r][i]^ulsch_gNB->harq_processes[harq_pid]->c[r][i]) != 0) printf("************");
-- 
GitLab