polartest.c 14.7 KB
Newer Older
Guy De Souza's avatar
Guy De Souza committed
1 2 3 4 5 6 7 8 9 10
#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"
#include "PHY/CODING/nrPolar_tools/nr_polar_pbch_defs.h"
Florian Kaltenberger's avatar
WIP  
Florian Kaltenberger committed
11
#include "PHY/CODING/nrPolar_tools/nr_polar_uci_defs.h"
yilmazt's avatar
yilmazt committed
12
#include "PHY/CODING/coding_defs.h"
yilmazt's avatar
yilmazt committed
13
#include "SIMULATION/TOOLS/sim.h"
Guy De Souza's avatar
Guy De Souza committed
14

Florian Kaltenberger's avatar
Florian Kaltenberger committed
15
//#define DEBUG_DCI_POLAR_PARAMS
yilmazt's avatar
yilmazt committed
16
//#define DEBUG_POLAR_TIMING
17
//#define DEBUG_CRC
yilmazt's avatar
yilmazt committed
18

Guy De Souza's avatar
Guy De Souza committed
19 20 21 22 23
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;
24
	int decoder_int16=0;
Guy De Souza's avatar
Guy De Souza committed
25 26 27 28
	cpu_freq_GHz = get_cpu_freq_GHz();
	reset_meas(&timeEncoder);
	reset_meas(&timeDecoder);
	randominit(0);
yilmazt's avatar
yilmazt committed
29
	crcTableInit();
30

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

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

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

yilmazt's avatar
yilmazt committed
73 74 75
		case 'q':
			decoder_int16 = 1;
			break;
76

yilmazt's avatar
yilmazt committed
77 78 79 80 81 82
		case 'g':
			iterations = 1;
			SNRstart = -6.0;
			SNRstop = -6.0;
			decoder_int16 = 1;
			break;
83

yilmazt's avatar
yilmazt committed
84 85 86
		case 'h':
			printf("./polartest -s SNRstart -d SNRinc -f SNRstop -m [0=PBCH|1=DCI|2=UCI] -i iterations -l decoderListSize -a pathMetricAppr\n");
			exit(-1);
87

Guy De Souza's avatar
Guy De Souza committed
88 89
		default:
			perror("[polartest.c] Problem at argument parsing with getopt");
90
			exit(-1);
Guy De Souza's avatar
Guy De Souza committed
91 92
	}

yilmazt's avatar
yilmazt committed
93
	if (polarMessageType == 0) { //PBCH
94
	  testLength = 64;//NR_POLAR_PBCH_PAYLOAD_BITS;
Guy De Souza's avatar
Guy De Souza committed
95
		coderLength = NR_POLAR_PBCH_E;
96
		aggregation_level = NR_POLAR_PBCH_AGGREGATION_LEVEL;
yilmazt's avatar
yilmazt committed
97
	} else if (polarMessageType == 1) { //DCI
98
		//testLength = nr_get_dci_size(params_rel15->dci_format, params_rel15->rnti_type, &fp->initial_bwp_dl, cfg);
99 100
	  testLength = 41; //20;
	  coderLength = 108*8; //to be changed by aggregate level function.
yilmazt's avatar
yilmazt committed
101
	} else if (polarMessageType == -1) { //UCI
Guy De Souza's avatar
Guy De Souza committed
102 103 104
		//testLength = ;
		//coderLength = ;
	}
Florian Kaltenberger's avatar
WIP  
Florian Kaltenberger committed
105
	
Guy De Souza's avatar
Guy De Souza committed
106 107 108 109

	//Logging
	time_t currentTime;
	time (&currentTime);
110 111
	char fileName[512], currentTimeInfo[25];
	char folderName[] = ".";
Guy De Souza's avatar
Guy De Souza committed
112

113
	/*
yilmazt's avatar
yilmazt committed
114 115
	folderName=getenv("HOME");
	strcat(folderName,"/Desktop/polartestResults");
116 117
	*/
	
yilmazt's avatar
yilmazt committed
118 119 120
	#ifdef DEBUG_POLAR_TIMING
	sprintf(fileName,"%s/TIMING_ListSize_%d_pmAppr_%d_Payload_%d_Itr_%d",folderName,decoderListSize,pathMetricAppr,testLength,iterations);
	#else
yilmazt's avatar
yilmazt committed
121
	sprintf(fileName,"%s/_ListSize_%d_pmAppr_%d_Payload_%d_Itr_%d",folderName,decoderListSize,pathMetricAppr,testLength,iterations);
yilmazt's avatar
yilmazt committed
122
	#endif
Guy De Souza's avatar
Guy De Souza committed
123 124 125
	strftime(currentTimeInfo, 25, "_%Y-%m-%d-%H-%M-%S.csv", localtime(&currentTime));
	strcat(fileName,currentTimeInfo);

yilmazt's avatar
yilmazt committed
126
	//Create "~/Desktop/polartestResults" folder if it doesn't already exist.
127
	/*
Guy De Souza's avatar
Guy De Souza committed
128
	struct stat folder = {0};
yilmazt's avatar
yilmazt committed
129
	if (stat(folderName, &folder) == -1) mkdir(folderName, S_IRWXU | S_IRWXG | S_IRWXO);
130 131
	*/
	
yilmazt's avatar
yilmazt committed
132
	FILE* logFile;
133 134 135 136 137
	logFile = fopen(fileName, "w");
	if (logFile==NULL) {
	  fprintf(stderr,"[polartest.c] Problem creating file %s with fopen\n",fileName);
	  exit(-1);
	}
yilmazt's avatar
yilmazt committed
138 139 140 141

#ifdef DEBUG_POLAR_TIMING
    fprintf(logFile,",timeEncoderCRCByte[us],timeEncoderCRCBit[us],timeEncoderInterleaver[us],timeEncoderBitInsertion[us],timeEncoder1[us],timeEncoder2[us],timeEncoderRateMatching[us],timeEncoderByte2Bit[us]\n");
#else
Guy De Souza's avatar
Guy De Souza committed
142
    fprintf(logFile,",SNR,nBitError,blockErrorState,t_encoder[us],t_decoder[us]\n");
yilmazt's avatar
yilmazt committed
143
#endif
Guy De Souza's avatar
Guy De Souza committed
144

yilmazt's avatar
yilmazt committed
145 146
    uint8_t testArrayLength = ceil(testLength / 32.0);
    uint8_t coderArrayLength = ceil(coderLength / 32.0);
yilmazt's avatar
yilmazt committed
147

yilmazt's avatar
yilmazt committed
148 149 150
	uint32_t testInput[testArrayLength]; //generate randomly
	uint32_t encoderOutput[coderArrayLength];
	uint32_t estimatedOutput[testArrayLength]; //decoder output
yilmazt's avatar
yilmazt committed
151 152 153
	memset(testInput,0,sizeof(uint32_t) * testArrayLength);
	memset(encoderOutput,0,sizeof(uint32_t) * coderArrayLength);
	memset(estimatedOutput,0,sizeof(uint32_t) * testArrayLength);
Guy De Souza's avatar
Guy De Souza committed
154

yilmazt's avatar
yilmazt committed
155 156 157 158 159
	uint8_t encoderOutputByte[coderLength];
	double modulatedInput[coderLength]; //channel input
	double channelOutput[coderLength];  //add noise

	int16_t channelOutput_int16[coderLength];
knopp's avatar
knopp committed
160
 
yilmazt's avatar
yilmazt committed
161
	t_nrPolar_paramsPtr nrPolar_params = NULL, currentPtr = NULL;
yilmazt's avatar
yilmazt committed
162
	nr_polar_init(&nrPolar_params, polarMessageType, testLength, aggregation_level);
yilmazt's avatar
yilmazt committed
163 164
	currentPtr = nr_polar_params(nrPolar_params, polarMessageType, testLength, aggregation_level);

yilmazt's avatar
yilmazt committed
165
#ifdef DEBUG_DCI_POLAR_PARAMS
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
	uint32_t dci_pdu[4];
	memset(dci_pdu,0,sizeof(uint32_t)*4);
	dci_pdu[0]=0x01189400;
	printf("dci_pdu: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
			dci_pdu[0], dci_pdu[1], dci_pdu[2], dci_pdu[3]);
	uint32_t encoder_output[54];
	memset(encoder_output,0,sizeof(uint32_t)*54);
	uint16_t size=41;
	uint16_t rnti=3;
	aggregation_level=8;
	nr_polar_init(&nrPolar_params, 1, size, aggregation_level);
	t_nrPolar_paramsPtr currentPtrDCI=nr_polar_params(nrPolar_params, 1, size, aggregation_level);

	polar_encoder_dci(dci_pdu, encoder_output, currentPtrDCI, rnti);
	for (int i=0;i<54;i++)
		printf("encoder_output: [%2d]->0x%08x \n",i, encoder_output[i]);

	uint8_t *encoder_outputByte = malloc(sizeof(uint8_t) * currentPtrDCI->encoderLength);
	double *channel_output  = malloc (sizeof(double) * currentPtrDCI->encoderLength);
	uint32_t dci_estimation[4];
	memset(dci_estimation,0,sizeof(uint32_t)*4);
	printf("dci_estimation: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
			dci_estimation[0], dci_estimation[1], dci_estimation[2], dci_estimation[3]);
	nr_bit2byte_uint32_8_t(encoder_output, currentPtrDCI->encoderLength, encoder_outputByte);
	printf("[polartest] encoder_outputByte: ");
	for (int i = 0; i < currentPtrDCI->encoderLength; i++) printf("%d-", encoder_outputByte[i]);
	printf("\n");
	for(int i=0; i<currentPtrDCI->encoderLength; i++) {
		if (encoder_outputByte[i] == 0) {
			channel_output[i]=1/sqrt(2);
		}
		else {
			channel_output[i]=(-1)/sqrt(2);
		}
	}
knopp's avatar
knopp committed
201

202 203 204 205 206 207 208 209
	decoderState = polar_decoder_dci(channel_output,
									 dci_estimation,
									 currentPtrDCI,
									 NR_POLAR_DECODER_LISTSIZE,
									 NR_POLAR_DECODER_PATH_METRIC_APPROXIMATION,
									 rnti);
	printf("dci_estimation: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
			dci_estimation[0], dci_estimation[1], dci_estimation[2], dci_estimation[3]);
yilmazt's avatar
yilmazt committed
210 211
	free(encoder_outputByte);
	free(channel_output);
212 213 214 215
	return 0;
#endif

#ifdef DEBUG_CRC
yilmazt's avatar
yilmazt committed
216
	uint32_t crc;
yilmazt's avatar
yilmazt committed
217
	unsigned int poly24c = 0xb2b11700;
218 219 220 221 222 223 224 225 226 227 228 229 230
	uint32_t testInputCRC[4];
	testInputCRC[0]=0x00291880;
	//testInputCRC[0]=0x01189400;
	testInputCRC[1]=0x00000000;
	testInputCRC[2]=0x00000000;
	testInputCRC[3]=0x00000000;

	uint32_t testInputcrc=0x01189400;
	uint32_t testInputcrc2=0x00291880;

	uint8_t testInputCRC2[8];
	nr_crc_bit2bit_uint32_8_t(testInputCRC, 32, testInputCRC2);
	printf("testInputCRC2: [0]->%x \t [1]->%x \t [2]->%x \t [3]->%x\n"
yilmazt's avatar
yilmazt committed
231
		   "            [4]->%x \t [5]->%x \t [6]->%x \t [7]->%x\n",
232 233 234 235 236 237 238 239
				testInputCRC2[0], testInputCRC2[1], testInputCRC2[2], testInputCRC2[3],
				testInputCRC2[4], testInputCRC2[5], testInputCRC2[6], testInputCRC2[7]);
	unsigned int crc41 = crc24c(testInputCRC, 32);
	unsigned int crc65 = crc24c(testInputCRC, 56);
	printf("crc41: [0]->0x%08x\tcrc65: [0]->0x%08x\n",crc41, crc65);
	for (int i=0;i<32;i++) printf("crc41[%d]=%d\tcrc65[%d]=%d\n",i,(crc41>>i)&1,i,(crc65>>i)&1);

    crc = crc24c(testInputCRC, testLength)>>8;
yilmazt's avatar
yilmazt committed
240 241
    for (int i=0;i<24;i++) printf("[i]=%d\n",(crc>>i)&1);
    printf("crc: [0]->0x%08x\n",crc);
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
    //crcbit(testInputCRC, sizeof(test) - 1, poly24c));

    testInputCRC[testLength>>3] = ((uint8_t*)&crc)[2];
    testInputCRC[1+(testLength>>3)] = ((uint8_t*)&crc)[1];
    testInputCRC[2+(testLength>>3)] = ((uint8_t*)&crc)[0];
    printf("testInputCRC: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
    			testInputCRC[0], testInputCRC[1], testInputCRC[2], testInputCRC[3]);


    //uint32_t trial32 = 0xffffffff;
    uint32_t trial32 = 0xf10fffff;
    uint8_t a[4];
	//memcpy(a, &trial32, sizeof(trial32));
	*(uint32_t *)a = trial32;
    unsigned char trial[4];
    trial[0]=0xff; trial[1]=0xff;  trial[2]=0x0f; trial[3]=0xf1;
    uint32_t trialcrc = crc24c(trial, 32);
    uint32_t trialcrc32 = crc24c((uint8_t *)&trial32, 32);
    //uint32_t trialcrc32 = crc24c(a, 32);
    printf("crcbit(trial = %x\n", crcbit(trial, 4, poly24c));
    printf("trialcrc = %x\n", trialcrc);
    printf("trialcrc32 = %x\n", trialcrc32);
    for (int i=0;i<32;i++) printf("trialcrc[%2d]=%d\ttrialcrc32[%2d]=%d\n",i,(trialcrc>>i)&1,i,(trialcrc32>>i)&1);


    //uint8_t nr_polar_A[32] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    uint8_t nr_polar_A[32] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,1};
    uint8_t nr_polar_crc[24];
    uint8_t **crc_generator_matrix = crc24c_generator_matrix(32);
yilmazt's avatar
yilmazt committed
271
	nr_matrix_multiplication_uint8_1D_uint8_2D(nr_polar_A,
272 273 274 275 276 277 278 279 280
												   crc_generator_matrix,
												   nr_polar_crc,
												   32,
												   24);
	for (uint8_t i = 0; i < 24; i++){
		nr_polar_crc[i] = (nr_polar_crc[i] % 2);
		printf("nr_polar_crc[%d]=%d\n",i,nr_polar_crc[i]);
	}
    return 0;
yilmazt's avatar
yilmazt committed
281
#endif
Guy De Souza's avatar
Guy De Souza committed
282

yilmazt's avatar
yilmazt committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
#ifdef DEBUG_POLAR_TIMING
	for (SNR = SNRstart; SNR <= SNRstop; SNR += SNRinc) {
		SNR_lin = pow(10, SNR / 10);
		for (itr = 1; itr <= iterations; itr++) {
			for (int j=0; j<ceil(testLength / 32.0); j++) {
				for(int i=0; i<32; i++) {
					testInput[j] |= ( ((uint32_t) (rand()%2)) &1);
					testInput[j]<<=1;
				}
			}
			printf("testInput: [0]->0x%08x \n", testInput[0]);
			polar_encoder_timing(testInput, encoderOutput, currentPtr, cpu_freq_GHz, logFile);
		}
	}
	fclose(logFile);
	free(testInput);
	free(encoderOutput);
	free(modulatedInput);
	free(channelOutput);
	free(estimatedOutput);
	return (0);
#endif
Guy De Souza's avatar
Guy De Souza committed
305 306

	// We assume no a priori knowledge available about the payload.
yilmazt's avatar
yilmazt committed
307 308
	double aPrioriArray[currentPtr->payloadBits];
	for (int i=0; i<currentPtr->payloadBits; i++) aPrioriArray[i] = NAN;
knopp's avatar
knopp committed
309

Guy De Souza's avatar
Guy De Souza committed
310
	for (SNR = SNRstart; SNR <= SNRstop; SNR += SNRinc) {
knopp's avatar
knopp committed
311
	  printf("SNR %f\n",SNR);
Guy De Souza's avatar
Guy De Souza committed
312 313 314
		SNR_lin = pow(10, SNR/10);
		for (itr = 1; itr <= iterations; itr++) {

yilmazt's avatar
yilmazt committed
315 316 317 318 319 320 321
			for (int i = 0; i < testArrayLength; i++) {
				for (int j = 0; j < (sizeof(testInput[0])*8)-1; j++) {
					testInput[i] |= ( ((uint32_t) (rand()%2)) &1);
					testInput[i]<<=1;
				}
				testInput[i] |= ( ((uint32_t) (rand()%2)) &1);
			}
Guy De Souza's avatar
Guy De Souza committed
322

yilmazt's avatar
yilmazt committed
323 324 325
			/*printf("testInput: [0]->0x%08x\n", testInput[0]);
			for (int i=0; i<32; i++)
				printf("%d\n",(testInput[0]>>i)&1);*/
Guy De Souza's avatar
Guy De Souza committed
326

327

328

329 330
			int len_mod64=currentPtr->payloadBits&63;
			((uint64_t*)testInput)[currentPtr->payloadBits/64]&=((((uint64_t)1)<<len_mod64)-1);
331

yilmazt's avatar
yilmazt committed
332
			start_meas(&timeEncoder);
333 334 335
			if (decoder_int16==0)
			  polar_encoder(testInput, encoderOutput, currentPtr);
			else
yilmazt's avatar
yilmazt committed
336 337
				polar_encoder_fast((uint64_t*)testInput, encoderOutput,0, currentPtr);
			  //polar_encoder_fast((uint64_t*)testInput, (uint64_t*)encoderOutput,0, currentPtr);
yilmazt's avatar
yilmazt committed
338 339
			stop_meas(&timeEncoder);
			/*printf("encoderOutput: [0]->0x%08x\n", encoderOutput[0]);
340
			printf("encoderOutput: [1]->0x%08x\n", encoderOutput[1]);*/
knopp's avatar
knopp committed
341

yilmazt's avatar
yilmazt committed
342 343
			//Bit-to-byte:
			nr_bit2byte_uint32_8_t(encoderOutput, coderLength, encoderOutputByte);
Guy De Souza's avatar
Guy De Souza committed
344

yilmazt's avatar
yilmazt committed
345 346 347 348 349 350
			//BPSK modulation
			for(int i=0; i<coderLength; i++) {
				if (encoderOutputByte[i] == 0)
					modulatedInput[i]=1/sqrt(2);
				else
					modulatedInput[i]=(-1)/sqrt(2);
Guy De Souza's avatar
Guy De Souza committed
351

yilmazt's avatar
yilmazt committed
352
				channelOutput[i] = modulatedInput[i] + (gaussdouble(0.0,1.0) * (1/sqrt(2*SNR_lin)));
knopp's avatar
knopp committed
353 354 355 356 357 358 359 360 361

				
				if (decoder_int16==1) {
				  if (channelOutput[i] > 15) channelOutput_int16[i] = 127;
				  else if (channelOutput[i] < -16) channelOutput_int16[i] = -128;
				  else channelOutput_int16[i] = (int16_t) (8*channelOutput[i]);
				}


yilmazt's avatar
yilmazt committed
362
			}
Guy De Souza's avatar
Guy De Souza committed
363

yilmazt's avatar
yilmazt committed
364 365 366 367 368 369 370
			start_meas(&timeDecoder);
			/*decoderState = polar_decoder(channelOutput,
									 	 estimatedOutput,
									 	 currentPtr,
									 	 NR_POLAR_DECODER_LISTSIZE,
									 	 aPrioriArray,
									 	 NR_POLAR_DECODER_PATH_METRIC_APPROXIMATION);*/
knopp's avatar
knopp committed
371 372 373 374 375 376 377 378 379
			if (decoder_int16==0)
			  decoderState = polar_decoder_aPriori(channelOutput,
							       estimatedOutput,
							       currentPtr,
							       NR_POLAR_DECODER_LISTSIZE,
							       NR_POLAR_DECODER_PATH_METRIC_APPROXIMATION,
							       aPrioriArray);
			else 
			  decoderState = polar_decoder_int16(channelOutput_int16,
380
							     (uint64_t*)estimatedOutput,
knopp's avatar
knopp committed
381 382 383
							     currentPtr);

			  
yilmazt's avatar
yilmazt committed
384 385
			stop_meas(&timeDecoder);
			/*printf("testInput: [0]->0x%08x\n", testInput[0]);
386 387
			printf("estimatedOutput: [0]->0x%08x\n", estimatedOutput[0]);*/

yilmazt's avatar
yilmazt committed
388 389

			//calculate errors
390
			if (decoderState!=0) {
yilmazt's avatar
yilmazt committed
391 392 393
				blockErrorState=-1;
				nBitError=-1;
			} else {
394 395
			  for (int j = 0; j < currentPtr->payloadBits; j++) {
			    if (((estimatedOutput[0]>>j) & 1) != ((testInput[0]>>j) & 1)) nBitError++;
396
			    //			    printf("bit %d: %d => %d\n",j,(testInput[0]>>j)&1,(estimatedOutput[0]>>j)&1);
397 398 399 400 401
			  }
			

			  if (nBitError>0) { 
			    blockErrorState=1; 
402
			    //			    printf("Error: Input %x, Output %x\n",testInput[0],estimatedOutput[0]);
403
			  }
Guy De Souza's avatar
Guy De Souza committed
404 405
			}

yilmazt's avatar
yilmazt committed
406
			//Iteration times are in microseconds.
407 408
			timeEncoderCumulative+=(timeEncoder.diff/(cpu_freq_GHz*1000.0));
			timeDecoderCumulative+=(timeDecoder.diff/(cpu_freq_GHz*1000.0));
yilmazt's avatar
yilmazt committed
409
			fprintf(logFile,",%f,%d,%d,%f,%f\n", SNR, nBitError, blockErrorState,
410
					(timeEncoder.diff/(cpu_freq_GHz*1000.0)), (timeDecoder.diff/(cpu_freq_GHz*1000.0)));
yilmazt's avatar
yilmazt committed
411 412 413 414 415 416 417 418

			if (nBitError<0) {
				blockErrorCumulative++;
				bitErrorCumulative+=testLength;
			} else {
				blockErrorCumulative+=blockErrorState;
				bitErrorCumulative+=nBitError;
			}
Guy De Souza's avatar
Guy De Souza committed
419

yilmazt's avatar
yilmazt committed
420 421 422
			decoderState=0;
			nBitError=0;
			blockErrorState=0;
Guy De Souza's avatar
Guy De Souza committed
423 424 425 426 427 428

		}
		//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)),
429 430
		       (double)timeEncoder.diff/timeEncoder.trials/(cpu_freq_GHz*1000.0),(double)timeDecoder.diff/timeDecoder.trials/(cpu_freq_GHz*1000.0));
		       //(timeEncoderCumulative/iterations),timeDecoderCumulative/iterations);
431

Florian Kaltenberger's avatar
Florian Kaltenberger committed
432 433 434
		if (blockErrorCumulative==0 && bitErrorCumulative==0)
		  break;

Guy De Souza's avatar
Guy De Souza committed
435 436 437 438 439 440 441 442 443 444
		blockErrorCumulative = 0; bitErrorCumulative = 0;
		timeEncoderCumulative = 0; timeDecoderCumulative = 0;
	}

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

	fclose(logFile);
	return (0);
}