rate_matching.c 6.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2014 Eurecom

  This program is free software; you can redistribute it and/or modify it under 
  the terms of the GNU General Public License as published by the Free Software 
  Foundation; either version 2 of the License, or (at your option) any later version

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fsr/openairinterface
  Address      : Eurecom, 2229, route des crtes, 06560 Valbonne Sophia Antipolis, France

*******************************************************************************/

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
#ifdef DEBUG
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#endif

#include "PHY/defs.h"

//#define DEBUG_PHY 1

unsigned int ps0, ps1, ps2, pb;

//----------------------------------------------
//
/*!\brief Tausworthe Uniform Random Generator.  This is based on the hardware implementation described in 
  Lee et al, "A Hardware Gaussian Noise Generator Usign the Box-Muller Method and its Error Analysis," IEEE Trans. on Computers, 2006.
*/
//

inline void pset_taus_seed(unsigned int off) {


  ps0 = (unsigned int)0x1e23d852 + (off<<4);
  ps1 = (unsigned int)0x81f38a1c + (off<<4);
  ps2 = (unsigned int)0xfe1a133e + (off<<4);

}

inline unsigned int ptaus(void) {

  pb = (((ps0 << 13) ^ ps0) >> 19);
  ps0 = (((ps0 & 0xFFFFFFFE) << 12)^  pb);
  pb = (((ps1 << 2) ^ ps1) >> 25);
  ps1 = (((ps1 & 0xFFFFFFF8) << 4)^  pb);
  pb = (((ps2 << 3) ^ ps2) >> 11);
  ps2 = (((ps2 & 0xFFFFFFF0) << 17)^  pb);
  return ps0 ^ ps1 ^ ps2;
}


int rate_matching(unsigned int N_coded, 
		  unsigned int N_input, 
		  unsigned char *inPtr,
		  unsigned char N_bps,
		  unsigned int off){


  unsigned int i,j,U,Umod,rep_flag=0;
  unsigned int N_coded2,N_input2,Mod_input2;
  int N_punctured;

  if ((9*N_coded<(N_input<<2)) || (N_input<(N_coded>>1))) {
    // check validity of parameters
    // the first condition represents the case where the rate is greater than 2/3
    // the second condition represents the case where the rate is less than 1/4
    msg("[PHY][CODING] Rate matching parameter error (N_coded %d, N_input %d, N_bps %d), exiting\n",N_coded,N_input,N_bps);
#ifdef USER_MODE

    exit(-1);
#else
    return(-1);
#endif
  }

  //initialize all bits as transmitted
  for (i=0;i<N_input;i++)
    inPtr[i] |= 0x80;

  // No go puncture the bits that should be removed
  i=0;
  
  N_coded2 = N_coded/N_bps;
  pset_taus_seed(off);
  N_input2 = N_input/N_bps;

  N_punctured = N_input2-N_coded2;

  if (N_punctured < 0) {  // Repeat bits instead of puncturing
    N_punctured = -N_punctured;
    rep_flag = 1;
  }

#ifdef USER_MODE
#ifdef DEBUG_PHY
  printf("rate_matching : N_coded %d, N_input %d, N_bps %d\n",N_coded,N_input,N_bps);
  printf("rate_matching : N_punctured = %d (%d)\n",N_bps*N_punctured,rep_flag);
  printf("rate_matching : code rate = %f\n",(double)N_input/(double)N_coded/2.0);
#endif
#endif

  Mod_input2 = (1<<(1+log2_approx(N_input2)))-1;
  
  while (i < N_punctured) {
    // generate N_punctured random positions in the input vector
    do { // generate a modulo-N_input2 random variable
      U = ptaus();
      Umod = U&Mod_input2;
      //      printf("i %d, N_punctured %d U %u Umod %d Iptr[Umod] %x\n",i,N_punctured,U,Umod,inPtr[Umod]);
    // check if the bit is already punctured/repeated, if so skip it and generate another RV 
      if ((Umod < N_input2) && (((inPtr[Umod]&0x80) == 0)||((inPtr[Umod]&0xc0) == 0xc0)))
	Umod=N_input2;

    } while (Umod>=N_input2);
      
    for (j=0;j<N_bps;j++) {            // loop over symbol bits
      if (rep_flag == 0)
	inPtr[Umod + (j*N_input2)] &= 0x7f; // clear MSB to indicate bit is punctured
      else
	inPtr[Umod + (j*N_input2)] |= 0xc0; // set MSB-1 to indicate bit is repeated

    }

    /*    
#ifdef USER_MODE
#ifdef DEBUG_PHY
    printf("rate_matching : i= %d, U = %d\n",i,Umod);
#endif
#endif
    */
    i++;
  }
  return(0);
}

int rate_matching_lte(unsigned int N_coded, 
		      unsigned int N_input, 
		      unsigned char *inPtr,
		      unsigned int off){


  unsigned int i,U,Umod,rep_flag=0;
  unsigned int Mod_input;
  int N_punctured;

  if ((9*N_coded<(N_input<<2)) || (N_input<(N_coded>>1))) {
    // check validity of parameters
    // the first condition represents the case where the rate is greater than 2/3
    // the second condition represents the case where the rate is less than 1/4
    msg("[PHY][CODING] Rate matching parameter error (N_coded %d, N_input %d), exiting\n",N_coded,N_input);
#ifdef USER_MODE

    exit(-1);
#else
    return(-1);
#endif
  }

  //initialize all bits as transmitted
  for (i=0;i<N_input;i++)
    inPtr[i] |= 0x80;

  // No go puncture the bits that should be removed
  i=0;
  

  pset_taus_seed(off);

  N_punctured = N_input-N_coded;

  if (N_punctured < 0) {  // Repeat bits instead of puncturing
    N_punctured = -N_punctured;
    rep_flag = 1;
  }

#ifdef USER_MODE
#ifdef DEBUG_PHY
  printf("rate_matching : N_coded %d, N_input %d\n",N_coded,N_input);
  if (rep_flag)
    printf("rate_matching : N_repeated = %d\n",N_punctured);
  else
    printf("rate_matching : N_punctured = %d\n",N_punctured);
  printf("rate_matching : code rate = %f\n",(double)N_input/(double)N_coded/3.0);
#endif
#endif

  Mod_input = (1<<(1+log2_approx(N_input)))-1;
  
  while (i < N_punctured) {
    // generate N_punctured random positions in the input vector
    do { // generate a modulo-N_input2 random variable
      U = ptaus();
      Umod = U&Mod_input;
      //      printf("i %d, N_punctured %d U %u Umod %d Iptr[Umod] %x\n",i,N_punctured,U,Umod,inPtr[Umod]);
    // check if the bit is already punctured/repeated, if so skip it and generate another RV 
      if ((Umod < N_input) && (((inPtr[Umod]&0x80) == 0)||((inPtr[Umod]&0xc0) == 0xc0)))
	Umod=N_input;

    } while (Umod>=N_input);
      
    if (rep_flag == 0)
      inPtr[Umod] &= 0x7f; // clear MSB to indicate bit is punctured
    else
      inPtr[Umod] |= 0xc0; // set MSB-1 to indicate bit is repeated

  
    /*
#ifdef USER_MODE
#ifdef DEBUG_PHY
    printf("rate_matching : i= %d, U = %d\n",i,Umod);
#endif
#endif
    */
    i++;
  }
  return(0);
}



#ifdef MAIN
void main(int argc,char **argv) {

  unsigned char input[1024];

  if (argc == 1) {
    printf("Provide an input\n");
    exit(-1);
  }
  memset(input,0,1024);

  rate_matching(4*157,4*256,input,atoi(argv[1]),0);
}
#endif