nr_polar_encoder.c 5.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * 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 23 24 25 26 27 28 29 30 31 32
/*!\file PHY/CODING/nrPolar_tools/nr_polar_encoder.c
 * \brief
 * \author Turker Yilmaz
 * \date 2018
 * \version 0.1
 * \company EURECOM
 * \email turker.yilmaz@eurecom.fr
 * \note
 * \warning
*/

yilmazt's avatar
yilmazt committed
33 34
#define DEBUG_POLAR_ENCODER_DCI

Guy De Souza's avatar
Guy De Souza committed
35 36
#include "PHY/CODING/nrPolar_tools/nr_polar_defs.h"

37 38 39
void polar_encoder(uint32_t *in,
				   uint32_t *out,
				   t_nrPolar_paramsPtr polarParams)
40
{
yilmazt's avatar
yilmazt committed
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
	if (polarParams->idx == 0){//PBCH
		nr_bit2byte_uint32_8_t(in, polarParams->payloadBits, polarParams->nr_polar_A);
		/*
		 * Bytewise operations
		 */
		//Calculate CRC.
		nr_matrix_multiplication_uint8_t_1D_uint8_t_2D(polarParams->nr_polar_A,
													   polarParams->crc_generator_matrix,
													   polarParams->nr_polar_crc,
													   polarParams->payloadBits,
													   polarParams->crcParityBits);
		for (uint8_t i = 0; i < polarParams->crcParityBits; i++)
			polarParams->nr_polar_crc[i] = (polarParams->nr_polar_crc[i] % 2);

		//Attach CRC to the Transport Block. (a to b)
		for (uint16_t i = 0; i < polarParams->payloadBits; i++)
			polarParams->nr_polar_B[i] = polarParams->nr_polar_A[i];
		for (uint16_t i = polarParams->payloadBits; i < polarParams->K; i++)
			polarParams->nr_polar_B[i]= polarParams->nr_polar_crc[i-(polarParams->payloadBits)];
	} else { //UCI

	}

	//Interleaving (c to c')
	nr_polar_interleaver(polarParams->nr_polar_B,
						 polarParams->nr_polar_CPrime,
						 polarParams->interleaving_pattern,
						 polarParams->K);

	//Bit insertion (c' to u)
	nr_polar_bit_insertion(polarParams->nr_polar_CPrime,
						   polarParams->nr_polar_U,
						   polarParams->N,
						   polarParams->K,
						   polarParams->Q_I_N,
						   polarParams->Q_PC_N,
						   polarParams->n_pc);

	//Encoding (u to d)
	nr_matrix_multiplication_uint8_t_1D_uint8_t_2D(polarParams->nr_polar_U,
												   polarParams->G_N,
												   polarParams->nr_polar_D,
												   polarParams->N,
												   polarParams->N);
	for (uint16_t i = 0; i < polarParams->N; i++)
		polarParams->nr_polar_D[i] = (polarParams->nr_polar_D[i] % 2);

	//Rate matching
	//Sub-block interleaving (d to y) and Bit selection (y to e)
yilmazt's avatar
yilmazt committed
90 91 92 93
	nr_polar_interleaver(polarParams->nr_polar_D,
						 polarParams->nr_polar_E,
						 polarParams->rate_matching_pattern,
						 polarParams->encoderLength);
Guy De Souza's avatar
Guy De Souza committed
94 95

	/*
yilmazt's avatar
yilmazt committed
96
	 * Return bits.
Guy De Souza's avatar
Guy De Souza committed
97
	 */
yilmazt's avatar
yilmazt committed
98 99 100 101 102 103
	nr_byte2bit_uint8_32_t(polarParams->nr_polar_E, polarParams->encoderLength, out);
}

void polar_encoder_dci(uint32_t *in,
					   uint32_t *out,
					   t_nrPolar_paramsPtr polarParams,
yilmazt's avatar
yilmazt committed
104 105 106 107 108 109
					   uint16_t n_RNTI)
{
#ifdef DEBUG_POLAR_ENCODER_DCI
	printf("[polar_encoder_dci] in: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n", in[0], in[1], in[2], in[3]);
#endif
	//(a to a')
yilmazt's avatar
yilmazt committed
110 111 112 113
	nr_crc_bit2bit_uint32_8_t(in, polarParams->payloadBits, polarParams->nr_polar_aPrime);
	//Parity bits computation (p)
	polarParams->crcBit = crc24c(polarParams->nr_polar_aPrime,
								 (polarParams->payloadBits+polarParams->crcParityBits));
yilmazt's avatar
yilmazt committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#ifdef DEBUG_POLAR_ENCODER_DCI
	printf("[polar_encoder_dci] crc: 0x%08x\n", polarParams->crcBit);
#endif
	//(a to b)
	//Using "nr_polar_aPrime" to hold "nr_polar_b".
	uint8_t arrayInd = ceil(polarParams->payloadBits / 32.0);
	for (int j=0; j<arrayInd-1; j++) {
		for (int i=0; i<32; i++) {
			polarParams->nr_polar_B[i+j*32] = (in[j]>>i)&1;
		}
	}
	for (int i=0; i<((polarParams->payloadBits)%32); i++) {
		polarParams->nr_polar_B[i+(arrayInd-1)*32] = (in[(arrayInd-1)]>>i)&1;
	}
	for (int i=0; i<8; i++) {
		polarParams->nr_polar_B[polarParams->payloadBits+i] = ((polarParams->crcBit)>>(31-i))&1;
	}
	//Scrambling
	for (int i=0; i<16; i++) {
		polarParams->nr_polar_B[polarParams->payloadBits+8+i] =
				( (((polarParams->crcBit)>>(23-i))&1) + ((n_RNTI>>(15-i))&1) ) % 2;
	}
Guy De Souza's avatar
Guy De Souza committed
136 137

	//Interleaving (c to c')
yilmazt's avatar
yilmazt committed
138 139
	nr_polar_interleaver(polarParams->nr_polar_B,
						 polarParams->nr_polar_CPrime,
140 141
						 polarParams->interleaving_pattern,
						 polarParams->K);
Guy De Souza's avatar
Guy De Souza committed
142 143

	//Bit insertion (c' to u)
yilmazt's avatar
yilmazt committed
144 145
	nr_polar_bit_insertion(polarParams->nr_polar_CPrime,
						   polarParams->nr_polar_U,
146 147 148 149 150
						   polarParams->N,
						   polarParams->K,
						   polarParams->Q_I_N,
						   polarParams->Q_PC_N,
						   polarParams->n_pc);
Guy De Souza's avatar
Guy De Souza committed
151 152

	//Encoding (u to d)
yilmazt's avatar
yilmazt committed
153
	nr_matrix_multiplication_uint8_t_1D_uint8_t_2D(polarParams->nr_polar_U,
154
												   polarParams->G_N,
yilmazt's avatar
yilmazt committed
155
												   polarParams->nr_polar_D,
156 157 158
												   polarParams->N,
												   polarParams->N);
	for (uint16_t i = 0; i < polarParams->N; i++)
yilmazt's avatar
yilmazt committed
159
		polarParams->nr_polar_D[i] = (polarParams->nr_polar_D[i] % 2);
Guy De Souza's avatar
Guy De Souza committed
160 161 162

	//Rate matching
	//Sub-block interleaving (d to y) and Bit selection (y to e)
yilmazt's avatar
yilmazt committed
163 164 165 166
	nr_polar_interleaver(polarParams->nr_polar_D,
						 polarParams->nr_polar_E,
						 polarParams->rate_matching_pattern,
						 polarParams->encoderLength);
167 168 169 170

	/*
	 * Return bits.
	 */
yilmazt's avatar
yilmazt committed
171
	nr_byte2bit_uint8_32_t(polarParams->nr_polar_E, polarParams->encoderLength, out);
yilmazt's avatar
yilmazt committed
172 173 174 175 176 177 178
#ifdef DEBUG_POLAR_ENCODER_DCI
	uint8_t outputInd = ceil(polarParams->encoderLength / 32.0);
	printf("[polar_encoder_dci] out: ");
	for (int i = 0; i < outputInd; i++) {
		printf("[%d]->0x%08x\t", i, out[i]);
	}
#endif
Guy De Souza's avatar
Guy De Souza committed
179
}