polartest.c 9.45 KB
Newer Older
Guy De Souza's avatar
Guy De Souza committed
1 2 3 4 5 6 7 8 9
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>

#include "PHY/CODING/nrPolar_tools/nr_polar_defs.h"
yilmazt's avatar
yilmazt committed
10
#include "PHY/CODING/coding_defs.h"
yilmazt's avatar
yilmazt committed
11
#include "SIMULATION/TOOLS/sim.h"
Guy De Souza's avatar
Guy De Souza committed
12

yilmazt's avatar
yilmazt committed
13
//#define DEBUG_POLAR_PARAMS
14
//#define DEBUG_DCI_POLAR_PARAMS
yilmazt's avatar
yilmazt committed
15

Guy De Souza's avatar
Guy De Souza committed
16 17 18 19 20 21 22 23 24 25
int main(int argc, char *argv[]) {

	//Initiate timing. (Results depend on CPU Frequency. Therefore, might change due to performance variances during simulation.)
	time_stats_t timeEncoder,timeDecoder;
	opp_enabled=1;
	cpu_freq_GHz = get_cpu_freq_GHz();
	reset_meas(&timeEncoder);
	reset_meas(&timeDecoder);

	randominit(0);
yilmazt's avatar
yilmazt committed
26 27
	crcTableInit();
	uint32_t crc;
Guy De Souza's avatar
Guy De Souza committed
28
	//Default simulation values (Aim for iterations = 1000000.)
yilmazt's avatar
yilmazt committed
29
	int itr, iterations = 1000, arguments, polarMessageType = 0; //0=PBCH, 1=DCI, -1=UCI
yilmazt's avatar
yilmazt committed
30
	double SNRstart = -20.0, SNRstop = 0.0, SNRinc= 0.5; //dB
Guy De Souza's avatar
Guy De Souza committed
31 32 33 34

	double SNR, SNR_lin;
	int16_t nBitError = 0; // -1 = Decoding failed (All list entries have failed the CRC checks).
	int8_t decoderState=0, blockErrorState=0; //0 = Success, -1 = Decoding failed, 1 = Block Error.
yilmazt's avatar
yilmazt committed
35
	uint16_t testLength = 0, coderLength = 0, blockErrorCumulative=0, bitErrorCumulative=0;
Guy De Souza's avatar
Guy De Souza committed
36
	double timeEncoderCumulative = 0, timeDecoderCumulative = 0;
yilmazt's avatar
yilmazt committed
37
	uint8_t aggregation_level, decoderListSize, pathMetricAppr;
yilmazt's avatar
yilmazt committed
38

Guy De Souza's avatar
Guy De Souza committed
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
	while ((arguments = getopt (argc, argv, "s:d:f:m:i:l:a:")) != -1)
	switch (arguments)
	{
		case 's':
			SNRstart = atof(optarg);
			printf("SNRstart = %f\n", SNRstart);
			break;

		case 'd':
			SNRinc = atof(optarg);
			break;

		case 'f':
			SNRstop = atof(optarg);
			break;

		case 'm':
			polarMessageType = atoi(optarg);
			break;

		case 'i':
			iterations = atoi(optarg);
			break;

		case 'l':
			decoderListSize = (uint8_t) atoi(optarg);
			break;

		case 'a':
			pathMetricAppr = (uint8_t) atoi(optarg);
			break;

		default:
			perror("[polartest.c] Problem at argument parsing with getopt");
			abort ();
	}

yilmazt's avatar
yilmazt committed
76
	if (polarMessageType == 0) { //PBCH
Guy De Souza's avatar
Guy De Souza committed
77 78
		testLength = NR_POLAR_PBCH_PAYLOAD_BITS;
		coderLength = NR_POLAR_PBCH_E;
79
		aggregation_level = NR_POLAR_PBCH_AGGREGATION_LEVEL;
yilmazt's avatar
yilmazt committed
80
	} else if (polarMessageType == 1) { //DCI
81 82 83
		//testLength = nr_get_dci_size(params_rel15->dci_format, params_rel15->rnti_type, &fp->initial_bwp_dl, cfg);
		testLength = 20;
		coderLength = 108; //to be changed by aggregate level function.
yilmazt's avatar
yilmazt committed
84
	} else if (polarMessageType == -1) { //UCI
Guy De Souza's avatar
Guy De Souza committed
85 86 87 88 89 90 91
		//testLength = ;
		//coderLength = ;
	}

	//Logging
	time_t currentTime;
	time (&currentTime);
yilmazt's avatar
yilmazt committed
92
	char *folderName, fileName[512], currentTimeInfo[25];
Guy De Souza's avatar
Guy De Souza committed
93

yilmazt's avatar
yilmazt committed
94 95 96
	folderName=getenv("HOME");
	strcat(folderName,"/Desktop/polartestResults");
	sprintf(fileName,"%s/_ListSize_%d_pmAppr_%d_Payload_%d_Itr_%d",folderName,decoderListSize,pathMetricAppr,testLength,iterations);
Guy De Souza's avatar
Guy De Souza committed
97 98 99
	strftime(currentTimeInfo, 25, "_%Y-%m-%d-%H-%M-%S.csv", localtime(&currentTime));
	strcat(fileName,currentTimeInfo);

yilmazt's avatar
yilmazt committed
100
	//Create "~/Desktop/polartestResults" folder if it doesn't already exist.
Guy De Souza's avatar
Guy De Souza committed
101
	struct stat folder = {0};
yilmazt's avatar
yilmazt committed
102
	if (stat(folderName, &folder) == -1) mkdir(folderName, S_IRWXU | S_IRWXG | S_IRWXO);
Guy De Souza's avatar
Guy De Souza committed
103

yilmazt's avatar
yilmazt committed
104
	FILE* logFile;
Guy De Souza's avatar
Guy De Souza committed
105 106 107 108 109 110 111
    logFile = fopen(fileName, "w");
    if (logFile==NULL) {
        fprintf(stderr,"[polartest.c] Problem creating file %s with fopen\n",fileName);
        exit(-1);
      }
    fprintf(logFile,",SNR,nBitError,blockErrorState,t_encoder[us],t_decoder[us]\n");

yilmazt's avatar
yilmazt committed
112 113 114
	//uint8_t *testInput = malloc(sizeof(uint8_t) * testLength); //generate randomly
	//uint8_t *encoderOutput = malloc(sizeof(uint8_t) * coderLength);
	uint32_t testInput[4], encoderOutput[4];
yilmazt's avatar
yilmazt committed
115 116
	memset(testInput,0,sizeof(testInput));
	memset(encoderOutput,0,sizeof(encoderOutput));
yilmazt's avatar
yilmazt committed
117

Guy De Souza's avatar
Guy De Souza committed
118 119 120
	double *modulatedInput = malloc (sizeof(double) * coderLength); //channel input

	double *channelOutput  = malloc (sizeof(double) * coderLength); //add noise
yilmazt's avatar
yilmazt committed
121
	uint32_t *estimatedOutput = malloc(sizeof(uint8_t) * testLength); //decoder output
Guy De Souza's avatar
Guy De Souza committed
122

yilmazt's avatar
yilmazt committed
123
	t_nrPolar_paramsPtr nrPolar_params = NULL, currentPtr = NULL;
yilmazt's avatar
yilmazt committed
124
	nr_polar_init(&nrPolar_params, polarMessageType, testLength, aggregation_level);
yilmazt's avatar
yilmazt committed
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
#ifdef DEBUG_DCI_POLAR_PARAMS
	unsigned int poly24c = 0xb2b11700;
	printf("testInput: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
			testInput[0], testInput[1], testInput[2], testInput[3]);
	printf("encOutput: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
			  encoderOutput[0], encoderOutput[1], encoderOutput[2], encoderOutput[3]);
	testInput[0]=0x01189400;
	uint8_t testInput2[8];
	nr_crc_bit2bit_uint32_8_t(testInput, 32, testInput2);
	printf("testInput2: [0]->%x \t [1]->%x \t [2]->%x \t [3]->%x\n            [4]->%x \t [5]->%x \t [6]->%x \t [7]->%x \t\n",
				testInput2[0], testInput2[1], testInput2[2], testInput2[3],
				testInput2[4], testInput2[5], testInput2[6], testInput2[7]);
	printf("crc32: [0]->0x%08x\n",crc24c(testInput2, 32));
	printf("crc56: [0]->0x%08x\n",crc24c(testInput2, 56));
	return 0;
	uint8_t testInput8[4];
	/*testInput8[0]=0x00;
	testInput8[1]=0x49;
	testInput8[2]=0x81;
	testInput8[3]=0x10;
	testInput8[4]=0x00;*/
	testInput8[0]=0xff;
	testInput8[1]=0xd0;
	testInput8[2]=0xff;
	testInput8[3]=0x82;
	crc = crc24c(testInput8, 31);
	for (int i=0;i<24;i++) printf("[i]=%d\n",(crc>>i)&1);
	printf("crc: [0]->0x%08x\n",crc);
	printf("crcbit: %x\n",crcbit(testInput8, 3, poly24c));
	return 0;
	unsigned char test[] = "Thebigredfox";

	for (int i=0;i<8;i++) printf("[i]=%d\n",(test[0]>>i)&1);
	printf("test[0]=%x\n",test[0]);
	printf("%s -- sizeof=%d\n",test,sizeof(test));
	printf("%x\n", crcbit(test, sizeof(test) - 1, poly24c));
	printf("%x\n", crc24c(test, (sizeof(test) - 1)*8));
	polarMessageType = 1;
	testLength = 41;
	aggregation_level=1;
	coderLength = 108;
yilmazt's avatar
yilmazt committed
166 167
	nr_polar_init(&nrPolar_params, polarMessageType, testLength, aggregation_level);
	nr_polar_print_polarParams(nrPolar_params);
yilmazt's avatar
yilmazt committed
168

yilmazt's avatar
yilmazt committed
169 170 171 172 173 174 175 176 177
    crc = crc24c(testInput, testLength)>>8;
    for (int i=0;i<24;i++) printf("[i]=%d\n",(crc>>i)&1);
    printf("crc: [0]->0x%08x\n",crc);
    testInput[testLength>>3] = ((uint8_t*)&crc)[2];
    testInput[1+(testLength>>3)] = ((uint8_t*)&crc)[1];
    testInput[2+(testLength>>3)] = ((uint8_t*)&crc)[0];
    printf("testInput: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
    			testInput[0], testInput[1], testInput[2], testInput[3]);
	return (0);
yilmazt's avatar
yilmazt committed
178
	currentPtr = nr_polar_params(nrPolar_params, polarMessageType, testLength, aggregation_level);
yilmazt's avatar
yilmazt committed
179 180 181 182 183 184
	polar_encoder(testInput, encoderOutput, currentPtr);
	printf("AFTER POLAR ENCODING\n");
	printf("testInput: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
			testInput[0], testInput[1], testInput[2], testInput[3]);
	printf("encOutput: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
			encoderOutput[0], encoderOutput[1], encoderOutput[2], encoderOutput[3]);
yilmazt's avatar
yilmazt committed
185 186 187
	return (0);
#endif

yilmazt's avatar
yilmazt committed
188
	currentPtr = nr_polar_params(nrPolar_params, polarMessageType, testLength, aggregation_level);
Guy De Souza's avatar
Guy De Souza committed
189 190

	// We assume no a priori knowledge available about the payload.
yilmazt's avatar
yilmazt committed
191 192
	double aPrioriArray[currentPtr->payloadBits];
	for (int i=0; i<currentPtr->payloadBits; i++) aPrioriArray[i] = NAN;
Guy De Souza's avatar
Guy De Souza committed
193 194 195 196 197 198 199 200

	for (SNR = SNRstart; SNR <= SNRstop; SNR += SNRinc) {
		SNR_lin = pow(10, SNR/10);
		for (itr = 1; itr <= iterations; itr++) {

		for(int i=0; i<testLength; i++) testInput[i]=(uint8_t) (rand() % 2);

		start_meas(&timeEncoder);
yilmazt's avatar
yilmazt committed
201
		polar_encoder(testInput, encoderOutput, currentPtr);
Guy De Souza's avatar
Guy De Souza committed
202 203 204 205 206 207 208 209 210 211
		stop_meas(&timeEncoder);

		//BPSK modulation
		for(int i=0; i<coderLength; i++) {
			if (encoderOutput[i] == 0)
				modulatedInput[i]=1/sqrt(2);
			else
				modulatedInput[i]=(-1)/sqrt(2);

			channelOutput[i] = modulatedInput[i] + (gaussdouble(0.0,1.0) * (1/sqrt(2*SNR_lin)));
212
			//printf("%f\n",channelOutput[i]);
Guy De Souza's avatar
Guy De Souza committed
213 214 215
		}


216

Guy De Souza's avatar
Guy De Souza committed
217
		start_meas(&timeDecoder);
218
		/*decoderState = polar_decoder(channelOutput,
yilmazt's avatar
yilmazt committed
219 220
									 estimatedOutput,
									 currentPtr,
221
									 NR_POLAR_DECODER_LISTSIZE,
yilmazt's avatar
yilmazt committed
222
									 aPrioriArray,
223 224 225 226 227 228 229
									 NR_POLAR_DECODER_PATH_METRIC_APPROXIMATION);*/
		decoderState = polar_decoder_aPriori(channelOutput,
											 estimatedOutput,
											 currentPtr,
											 NR_POLAR_DECODER_LISTSIZE,
											 NR_POLAR_DECODER_PATH_METRIC_APPROXIMATION,
											 aPrioriArray);
Guy De Souza's avatar
Guy De Souza committed
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 266 267 268 269 270 271 272 273 274 275
		stop_meas(&timeDecoder);

		//calculate errors
		if (decoderState==-1) {
			blockErrorState=-1;
			nBitError=-1;
		} else {
			for(int i=0; i<testLength; i++){
				if (estimatedOutput[i]!=testInput[i]) nBitError++;
			}
			if (nBitError>0) blockErrorState=1;
		}

		//Iteration times are in microseconds.
		timeEncoderCumulative+=(timeEncoder.diff_now/(cpu_freq_GHz*1000.0));
		timeDecoderCumulative+=(timeDecoder.diff_now/(cpu_freq_GHz*1000.0));
		fprintf(logFile,",%f,%d,%d,%f,%f\n", SNR, nBitError, blockErrorState,
				(timeEncoder.diff_now/(cpu_freq_GHz*1000.0)), (timeDecoder.diff_now/(cpu_freq_GHz*1000.0)));

		if (nBitError<0) {
			blockErrorCumulative++;
			bitErrorCumulative+=testLength;
		} else {
			blockErrorCumulative+=blockErrorState;
			bitErrorCumulative+=nBitError;
		}

		decoderState=0;
		nBitError=0;
		blockErrorState=0;

		}
		//Calculate error statistics for the SNR.
		printf("[ListSize=%d, Appr=%d] SNR=%+8.3f, BLER=%9.6f, BER=%12.9f, t_Encoder=%9.3fus, t_Decoder=%9.3fus\n",
				decoderListSize, pathMetricAppr, SNR, ((double)blockErrorCumulative/iterations),
				((double)bitErrorCumulative / (iterations*testLength)),
				(timeEncoderCumulative/iterations),timeDecoderCumulative/iterations);

		blockErrorCumulative = 0; bitErrorCumulative = 0;
		timeEncoderCumulative = 0; timeDecoderCumulative = 0;
	}

	print_meas(&timeEncoder,"polar_encoder",NULL,NULL);
	print_meas(&timeDecoder,"polar_decoder",NULL,NULL);

	fclose(logFile);
276 277
	//free(testInput);
	//free(encoderOutput);
Guy De Souza's avatar
Guy De Souza committed
278 279 280 281 282 283
	free(modulatedInput);
	free(channelOutput);
	free(estimatedOutput);

	return (0);
}