nr_polar_init.c 8.67 KB
Newer Older
yilmazt's avatar
yilmazt committed
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/nr_polar_init.h
 * \brief
 * \author Turker Yilmaz
 * \date 2018
 * \version 0.1
 * \company EURECOM
 * \email turker.yilmaz@eurecom.fr
 * \note
 * \warning
*/

yilmazt's avatar
yilmazt committed
33
#include "nrPolar_tools/nr_polar_defs.h"
34 35 36
#include "PHY/CODING/nrPolar_tools/nr_polar_dci_defs.h"
#include "PHY/CODING/nrPolar_tools/nr_polar_uci_defs.h"
#include "PHY/CODING/nrPolar_tools/nr_polar_pbch_defs.h"
yilmazt's avatar
yilmazt committed
37 38 39 40
#include "PHY/NR_TRANSPORT/nr_dci.h"

void nr_polar_init(t_nrPolar_paramsPtr *polarParams,
				   int8_t messageType,
41 42 43
				   uint16_t messageLength,
				   uint8_t aggregation_level)
{
yilmazt's avatar
yilmazt committed
44
	t_nrPolar_paramsPtr currentPtr = *polarParams;
yilmazt's avatar
yilmazt committed
45
	uint16_t aggregation_prime = nr_polar_aggregation_prime(aggregation_level);
yilmazt's avatar
yilmazt committed
46 47 48

	//Parse the list. If the node is already created, return without initialization.
	while (currentPtr != NULL) {
yilmazt's avatar
yilmazt committed
49
		if (currentPtr->idx == (messageType * messageLength * aggregation_prime)) return;
yilmazt's avatar
yilmazt committed
50
		else currentPtr = currentPtr->nextPtr;
yilmazt's avatar
yilmazt committed
51 52 53 54 55 56 57
	}

	//Else, initialize and add node to the end of the linked list.
	t_nrPolar_paramsPtr newPolarInitNode = malloc(sizeof(t_nrPolar_params));

	if (newPolarInitNode != NULL) {

yilmazt's avatar
yilmazt committed
58
		newPolarInitNode->idx = (messageType * messageLength * aggregation_prime);
yilmazt's avatar
yilmazt committed
59 60 61 62 63 64 65 66 67
		newPolarInitNode->nextPtr = NULL;

		if (messageType == 0) { //PBCH
			newPolarInitNode->n_max = NR_POLAR_PBCH_N_MAX;
			newPolarInitNode->i_il = NR_POLAR_PBCH_I_IL;
			newPolarInitNode->i_seg = NR_POLAR_PBCH_I_SEG;
			newPolarInitNode->n_pc = NR_POLAR_PBCH_N_PC;
			newPolarInitNode->n_pc_wm = NR_POLAR_PBCH_N_PC_WM;
			newPolarInitNode->i_bil = NR_POLAR_PBCH_I_BIL;
68
			newPolarInitNode->crcParityBits = NR_POLAR_PBCH_CRC_PARITY_BITS;
yilmazt's avatar
yilmazt committed
69 70 71
			newPolarInitNode->payloadBits = NR_POLAR_PBCH_PAYLOAD_BITS;
			newPolarInitNode->encoderLength = NR_POLAR_PBCH_E;
			newPolarInitNode->crcCorrectionBits = NR_POLAR_PBCH_CRC_ERROR_CORRECTION_BITS;
72
			newPolarInitNode->crc_generator_matrix = crc24c_generator_matrix(newPolarInitNode->payloadBits);//G_P
yilmazt's avatar
yilmazt committed
73
		} else if (messageType == 1) { //DCI
74 75 76 77 78 79 80 81 82 83
			newPolarInitNode->n_max = NR_POLAR_DCI_N_MAX;
			newPolarInitNode->i_il = NR_POLAR_DCI_I_IL;
			newPolarInitNode->i_seg = NR_POLAR_DCI_I_SEG;
			newPolarInitNode->n_pc = NR_POLAR_DCI_N_PC;
			newPolarInitNode->n_pc_wm = NR_POLAR_DCI_N_PC_WM;
			newPolarInitNode->i_bil = NR_POLAR_DCI_I_BIL;
			newPolarInitNode->crcParityBits = NR_POLAR_DCI_CRC_PARITY_BITS;
			newPolarInitNode->payloadBits = messageLength;
			newPolarInitNode->encoderLength = aggregation_level*108;
			newPolarInitNode->crcCorrectionBits = NR_POLAR_DCI_CRC_ERROR_CORRECTION_BITS;
84
			newPolarInitNode->crc_generator_matrix=crc24c_generator_matrix(newPolarInitNode->payloadBits+newPolarInitNode->crcParityBits);//G_P
yilmazt's avatar
yilmazt committed
85 86 87 88 89 90
		} else if (messageType == -1) { //UCI

		} else {
			AssertFatal(1 == 0, "[nr_polar_init] Incorrect Message Type(%d)", messageType);
		}

91 92 93 94 95 96 97
		newPolarInitNode->K = newPolarInitNode->payloadBits + newPolarInitNode->crcParityBits; // Number of bits to encode.
		newPolarInitNode->N = nr_polar_output_length(newPolarInitNode->K, newPolarInitNode->encoderLength, newPolarInitNode->n_max);
		newPolarInitNode->n = log2(newPolarInitNode->N);
		newPolarInitNode->G_N = nr_polar_kronecker_power_matrices(newPolarInitNode->n);

		//polar_encoder vectors:
		newPolarInitNode->nr_polar_crc = malloc(sizeof(uint8_t) * newPolarInitNode->crcParityBits);
yilmazt's avatar
yilmazt committed
98
		newPolarInitNode->nr_polar_aPrime = malloc(sizeof(uint8_t) * ((ceil((newPolarInitNode->payloadBits)/32.0)*4)+3));
99
		newPolarInitNode->nr_polar_APrime = malloc(sizeof(uint8_t) * newPolarInitNode->K);
yilmazt's avatar
yilmazt committed
100 101
		newPolarInitNode->nr_polar_D = malloc(sizeof(uint8_t) * newPolarInitNode->N);
		newPolarInitNode->nr_polar_E = malloc(sizeof(uint8_t) * newPolarInitNode->encoderLength);
102 103

		//Polar Coding vectors
yilmazt's avatar
yilmazt committed
104 105 106 107
		newPolarInitNode->nr_polar_U = malloc(sizeof(uint8_t) * newPolarInitNode->N); //Decoder: nr_polar_uHat
		newPolarInitNode->nr_polar_CPrime = malloc(sizeof(uint8_t) * newPolarInitNode->K); //Decoder: nr_polar_cHat
		newPolarInitNode->nr_polar_B = malloc(sizeof(uint8_t) * newPolarInitNode->K); //Decoder: nr_polar_bHat
		newPolarInitNode->nr_polar_A = malloc(sizeof(uint8_t) * newPolarInitNode->payloadBits); //Decoder: nr_polar_aHat
108

109 110


yilmazt's avatar
yilmazt committed
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
		newPolarInitNode->Q_0_Nminus1 = nr_polar_sequence_pattern(newPolarInitNode->n);

		newPolarInitNode->interleaving_pattern = malloc(sizeof(uint16_t) * newPolarInitNode->K);
		nr_polar_interleaving_pattern(newPolarInitNode->K,
									  newPolarInitNode->i_il,
									  newPolarInitNode->interleaving_pattern);

		newPolarInitNode->rate_matching_pattern = malloc(sizeof(uint16_t) * newPolarInitNode->encoderLength);
		uint16_t *J = malloc(sizeof(uint16_t) * newPolarInitNode->N);
		nr_polar_rate_matching_pattern(newPolarInitNode->rate_matching_pattern,
									   J,
									   nr_polar_subblock_interleaver_pattern,
									   newPolarInitNode->K,
									   newPolarInitNode->N,
									   newPolarInitNode->encoderLength);

		newPolarInitNode->information_bit_pattern = malloc(sizeof(uint8_t) * newPolarInitNode->N);
		newPolarInitNode->Q_I_N = malloc(sizeof(int16_t) * (newPolarInitNode->K + newPolarInitNode->n_pc));
		newPolarInitNode->Q_F_N = malloc( sizeof(int16_t) * (newPolarInitNode->N + 1)); // Last element shows the final array index assigned a value.
		newPolarInitNode->Q_PC_N = malloc( sizeof(int16_t) * (newPolarInitNode->n_pc));
		for (int i = 0; i <= newPolarInitNode->N; i++)
			newPolarInitNode->Q_F_N[i] = -1; // Empty array.
		nr_polar_info_bit_pattern(newPolarInitNode->information_bit_pattern,
								  newPolarInitNode->Q_I_N,
								  newPolarInitNode->Q_F_N,
								  J,
								  newPolarInitNode->Q_0_Nminus1,
								  newPolarInitNode->K,
								  newPolarInitNode->N,
								  newPolarInitNode->encoderLength,
								  newPolarInitNode->n_pc);

		newPolarInitNode->channel_interleaver_pattern = malloc(sizeof(uint16_t) * newPolarInitNode->encoderLength);
		nr_polar_channel_interleaver_pattern(newPolarInitNode->channel_interleaver_pattern,
											 newPolarInitNode->i_bil,
											 newPolarInitNode->encoderLength);

		free(J);

knopp's avatar
knopp committed
150 151 152
		build_decoder_tree(newPolarInitNode);
		printf("decoder tree nodes %d\n",newPolarInitNode->tree.num_nodes);

yilmazt's avatar
yilmazt committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	} else {
		AssertFatal(1 == 0, "[nr_polar_init] New t_nrPolar_paramsPtr could not be created");
	}

	currentPtr = *polarParams;
	//If polarParams is empty:
	if (currentPtr == NULL)
	{
		*polarParams = newPolarInitNode;
		return;
	}
	//Else, add node to the end of the linked list.
	while (currentPtr->nextPtr != NULL) {
			currentPtr = currentPtr->nextPtr;
	}
	currentPtr->nextPtr= newPolarInitNode;
	return;
}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

void nr_polar_print_polarParams(t_nrPolar_paramsPtr polarParams)
{
	uint8_t i = 0;
	if (polarParams == NULL) {
		printf("polarParams is empty.\n");
	} else {
		while (polarParams != NULL){
			printf("polarParams[%d] = %d\n", i, polarParams->idx);
			polarParams = polarParams->nextPtr;
			i++;
		}
	}
	return;
}

t_nrPolar_paramsPtr nr_polar_params (t_nrPolar_paramsPtr polarParams,
									 int8_t messageType,
yilmazt's avatar
yilmazt committed
189 190
									 uint16_t messageLength,
									 uint8_t aggregation_level)
191
{
yilmazt's avatar
yilmazt committed
192
	t_nrPolar_paramsPtr currentPtr = NULL;
193 194

	while (polarParams != NULL) {
yilmazt's avatar
yilmazt committed
195 196
		if (polarParams->idx ==
				(messageType * messageLength * (nr_polar_aggregation_prime(aggregation_level)) )) {
197 198 199 200 201 202 203 204
			currentPtr = polarParams;
			break;
		} else {
			polarParams = polarParams->nextPtr;
		}
	}
	return currentPtr;
}
yilmazt's avatar
yilmazt committed
205 206 207

uint16_t nr_polar_aggregation_prime (uint8_t aggregation_level)
{
yilmazt's avatar
yilmazt committed
208 209
	if (aggregation_level == 0) return 0;
	else if (aggregation_level == 1) return NR_POLAR_AGGREGATION_LEVEL_1_PRIME;
yilmazt's avatar
yilmazt committed
210 211 212
	else if (aggregation_level == 2) return NR_POLAR_AGGREGATION_LEVEL_2_PRIME;
	else if (aggregation_level == 4) return NR_POLAR_AGGREGATION_LEVEL_4_PRIME;
	else if (aggregation_level == 8) return NR_POLAR_AGGREGATION_LEVEL_8_PRIME;
yilmazt's avatar
yilmazt committed
213
	else return NR_POLAR_AGGREGATION_LEVEL_16_PRIME; //aggregation_level == 16
yilmazt's avatar
yilmazt committed
214
}