ccoding_byte.c 6.25 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22
/* file: ccoding_byte.c
23
   purpose: Encoding routines for implementing 802.11 convolutionally-coded waveforms
24
   author: raymond.knopp@eurecom.fr, based on similar code for 3GPP convolutional code (UMTS) by P. Humblet (2000)
25
   date: 10.2004
26
*/
27
#include "coding_defs.h"
28 29 30


unsigned short gdot11[] = { 0133, 0171 }; // {A,B}
31
unsigned short gdot11_rev[] = { 0155, 0117 }; // {A,B}
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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
47 48 49 50
ccodedot11_encode (unsigned int numbytes,
                   unsigned char *inPtr,
                   unsigned char *outPtr,
                   unsigned char puncturing)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
{
  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;
66

67 68 69 70 71 72
#ifdef DEBUG_CCODE
  dummy = 0;
#endif //DEBUG_CCODE

  /* Do not increment inPtr until we read the next octet */
  bit_index=0;
73

74 75 76 77 78 79 80
  while (numbytes-- > 0) {
    c = *inPtr++;
#ifdef DEBUG_CCODE
    printf("** %d **\n",c);
#endif //DEBUG_CCODE

    switch (puncturing) {
81 82 83 84 85 86 87 88 89 90 91 92 93 94
    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;

95
#ifdef DEBUG_CCODE
96 97
        printf("%d: %d -> %d (%d)\n",dummy,state,out,ccodedot11_table[state]);
        dummy+=2;
98
#endif //DEBUG_CCODE      
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

      }

      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;

121
#ifdef DEBUG_CCODE
122 123
        printf("%d: %d -> %d (%d)\n",dummy,state,out,ccodedot11_table[state]);
        dummy+=2;
124
#endif //DEBUG_CCODE      
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

        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;

147
#ifdef DEBUG_CCODE
148 149
        printf("%d: %d -> %d (%d)\n",dummy,state,out,ccodedot11_table[state]);
        dummy+=2;
150
#endif //DEBUG_CCODE      
151 152 153 154 155 156 157 158 159

        bit_index=(bit_index==0)?1:0;

      }

      break;

    default:
      break;
160 161
    }
  }
162

163
  /*
164 165
  // Termination - Add one NULL byte to terminate trellis
  #ifdef DEBUG_CCODE
166
      printf("Termination\n");
167
  #endif //DEBUG_CCODE
168 169 170
  for (shiftbit = 0; shiftbit<8;shiftbit++) {
    state >>= 1;
    out = ccodedot11_table[state];
171

172 173
    *outPtr++ = out  & 1;
    *outPtr++ = (out>>1)&1;
174 175

  #ifdef DEBUG_CCODE
176 177
    printf("%d: %d -> %d (%d)\n",dummy,state,out,ccodedot11_table[state]);
    dummy+=2;
178
  #endif //DEBUG_CCODE
179
    printf("%d -> %d (%d)\n",state,out,ccodedot11_table[state]);
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
  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;
206

207 208 209
    /* Compute R output bits */
    for (j = 0; j < 2; j++) {
      sum = 0;
210

211 212 213
      for (k = 0; k < 7; k++)
        if ((i & gdot11[j]) & (1 << k))
          sum++;
214

215 216 217 218 219 220 221 222 223 224 225 226 227
      /* 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;
228

229 230 231
    /* Compute R output bits */
    for (j = 0; j < 2; j++) {
      sum = 0;
232

233 234 235
      for (k = 0; k < 7; k++)
        if ((i & gdot11_rev[j]) & (1 << k))
          sum++;
236

237 238 239 240 241 242 243 244 245 246
      /* Write the sum modulo 2 in bit j */
      ccodedot11_table_rev[i] |= (sum & 1) << j;
    }
  }
}



/*****************************************************************/
/**
247
  Test program
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

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

#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();
265

266 267 268 269
  inPtr = test;
  outPtr = output;

  ccodedot11_encode(16, inPtr, outPtr,0);
270

271 272 273 274 275 276 277
  for (i = 0; i < 32; i++) printf("%x ", output[i]);

  printf("\n");
}
#endif

/** @}*/