ccoding_byte.c 6.29 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 254 255 256 257 258 259 260 261 262 263 264 265
/* file: ccoding_byte.c
   purpose: Encoding routines for implementing 802.11 convolutionally-coded waveforms 
   author: raymond.knopp@eurecom.fr, based on similar code for 3GPP convolutional code (UMTS) by P. Humblet (2000)
   date: 10.2004 
*/
#include "defs.h"


unsigned short gdot11[] = { 0133, 0171 }; // {A,B}
unsigned short gdot11_rev[] = { 0155, 0117 }; // {A,B}   
unsigned char  ccodedot11_table[128];
unsigned char  ccodedot11_table_rev[128];


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

  Encodes for an arbitrary convolutional code of rate 1/2
  with a constraint length of 7 bits.
  The inputs are bit packed in octets (from MSB to LSB).
  Trellis termination is included here
*************************************************************************/



void
ccodedot11_encode (unsigned int numbytes, 
		   unsigned char *inPtr, 
		   unsigned char *outPtr,
		   unsigned char puncturing)
{
  unsigned int             state;

  unsigned char              c, out, shiftbit =0;

  //  printf("In ccodedot11_encode (%d,%p,%p,%d)\n",numbytes,inPtr,outPtr,puncturing);

#ifdef DEBUG_CCODE
  unsigned int  dummy;
#endif //DEBUG_CCODE
  int bit_index;

  /* The input bit is shifted in position 8 of the state.
     Shiftbit will take values between 1 and 8 */
  state = 0;
  
#ifdef DEBUG_CCODE
  dummy = 0;
#endif //DEBUG_CCODE

  /* Do not increment inPtr until we read the next octet */
  bit_index=0;
  while (numbytes-- > 0) {
    c = *inPtr++;
#ifdef DEBUG_CCODE
    printf("** %d **\n",c);
#endif //DEBUG_CCODE

    switch (puncturing) {
      case 0:  //rate 1/2
	for (shiftbit = 0; shiftbit<8;shiftbit++) {
	  
	  state >>= 1;
	  if ((c&(1<<shiftbit)) != 0){
	    state |= 64;
	  }
	  
	  out = ccodedot11_table[state];
	  
	  *outPtr++ = out  & 1;
	  *outPtr++ = (out>>1)&1;
	  
#ifdef DEBUG_CCODE
	  printf("%d: %d -> %d (%d)\n",dummy,state,out,ccodedot11_table[state]);
	  dummy+=2;
#endif //DEBUG_CCODE      
	  
	}
	break;
      case 1: // rate 3/4
	for (shiftbit = 0; shiftbit<8;shiftbit++) {
	  
	  state >>= 1;
	  if ((c&(1<<shiftbit)) != 0){
	    state |= 64;
	  }
	  
	  out = ccodedot11_table[state];
	  
	  if (bit_index<2)
	    *outPtr++ = out  & 1;
	  if (bit_index!=1)
	    *outPtr++ = (out>>1)&1;
	  
#ifdef DEBUG_CCODE
	  printf("%d: %d -> %d (%d)\n",dummy,state,out,ccodedot11_table[state]);
	  dummy+=2;
#endif //DEBUG_CCODE      
	  
	  bit_index=(bit_index==2)?0:(bit_index+1);
	}
	break;
      case 2: // rate 2/3
	for (shiftbit = 0; shiftbit<8;shiftbit++) {
	  
	  state >>= 1;
	  if ((c&(1<<shiftbit)) != 0){
	    state |= 64;
	  }
	  
	  out = ccodedot11_table[state];
	  
	  *outPtr++ = out  & 1;
	  if (bit_index==0)
	    *outPtr++ = (out>>1)&1;
	  
#ifdef DEBUG_CCODE
	  printf("%d: %d -> %d (%d)\n",dummy,state,out,ccodedot11_table[state]);
	  dummy+=2;
#endif //DEBUG_CCODE      
	  
	  bit_index=(bit_index==0)?1:0;
	  
	}
	break;
      default:
	break;
    }
  }
  /*
  // Termination - Add one NULL byte to terminate trellis     
#ifdef DEBUG_CCODE
      printf("Termination\n");
#endif //DEBUG_CCODE  
  for (shiftbit = 0; shiftbit<8;shiftbit++) {
    state >>= 1;
    out = ccodedot11_table[state];
    
    *outPtr++ = out  & 1;
    *outPtr++ = (out>>1)&1;
    
#ifdef DEBUG_CCODE
    printf("%d: %d -> %d (%d)\n",dummy,state,out,ccodedot11_table[state]);
    dummy+=2;
#endif //DEBUG_CCODE      
    printf("%d -> %d (%d)\n",state,out,ccodedot11_table[state]);
    
  }

  */


}



/*************************************************************************
  
  Functions to initialize the code tables

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


/* Basic code table initialization for constraint length 7 */
/* Input in MSB, followed by state in 6 LSBs */

void ccodedot11_init(void)
{
  unsigned int  i, j, k, sum;

  for (i = 0; i < 128; i++) {
    ccodedot11_table[i] = 0;
    /* Compute R output bits */
    for (j = 0; j < 2; j++) {
      sum = 0;
      for (k = 0; k < 7; k++)
        if ((i & gdot11[j]) & (1 << k))
          sum++;
      /* Write the sum modulo 2 in bit j */
      ccodedot11_table[i] |= (sum & 1) << j;
    }
  }
}

/* Input in LSB, followed by state in 6 MSBs */
void ccodedot11_init_inv(void)
{
  unsigned int  i, j, k, sum;

  for (i = 0; i < 128; i++) {
    ccodedot11_table_rev[i] = 0;
    /* Compute R output bits */
    for (j = 0; j < 2; j++) {
      sum = 0;
      for (k = 0; k < 7; k++)
        if ((i & gdot11_rev[j]) & (1 << k))
          sum++;
      /* Write the sum modulo 2 in bit j */
      ccodedot11_table_rev[i] |= (sum & 1) << j;
    }
  }
}



/*****************************************************************/
/**
  Test program 

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

#ifdef DEBUG_CCODE
#include <stdio.h>

main()
{
  unsigned char test[] = "0Thebigredfox";
  unsigned char output[512], *inPtr, *outPtr;
  unsigned int i;

  test[0] = 128;
  test[1] = 0;


  ccodedot11_init();
  
  inPtr = test;
  outPtr = output;

  ccodedot11_encode(16, inPtr, outPtr,0);
  for (i = 0; i < 32; i++) printf("%x ", output[i]);

  printf("\n");
}
#endif

/** @}*/