polartest.c 7.38 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"
10
#include "PHY/NR_TRANSPORT/nr_dci.h"
yilmazt's avatar
yilmazt committed
11
#include "PHY/defs_gNB.h"
yilmazt's avatar
yilmazt committed
12
#include "SIMULATION/TOOLS/sim.h"
Guy De Souza's avatar
Guy De Souza committed
13

yilmazt's avatar
yilmazt committed
14
//#define DEBUG_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
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);

25 26 27 28 29 30 31 32 33 34
	//gNB scheduler
	/*PHY_VARS_gNB *gNB = RC.gNB[0][0];
	NR_DL_FRAME_PARMS *fp = &gNB->frame_parms;
	nfapi_nr_config_request_t *cfg = &gNB->gNB_config;

	nfapi_nr_dl_config_request_pdu_t *pdu;
	nfapi_nr_dl_config_pdcch_parameters_rel15_t *params_rel15 = &pdu->dci_dl_pdu.pdcch_params_rel15;
	params_rel15->rnti_type = NFAPI_NR_RNTI_RA;
	params_rel15->dci_format = NFAPI_NR_DL_DCI_FORMAT_1_0;*/

Guy De Souza's avatar
Guy De Souza committed
35 36
	randominit(0);
	//Default simulation values (Aim for iterations = 1000000.)
yilmazt's avatar
yilmazt committed
37
	int itr, iterations = 1000, arguments, polarMessageType = 0; //0=PBCH, 1=DCI, -1=UCI
yilmazt's avatar
yilmazt committed
38
	double SNRstart = -20.0, SNRstop = 0.0, SNRinc= 0.5; //dB
Guy De Souza's avatar
Guy De Souza committed
39 40 41 42

	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
43
	uint16_t testLength = 0, coderLength = 0, blockErrorCumulative=0, bitErrorCumulative=0;
Guy De Souza's avatar
Guy De Souza committed
44 45
	double timeEncoderCumulative = 0, timeDecoderCumulative = 0;

46
	uint8_t aggregation_level, decoderListSize = 8, pathMetricAppr = 0; //0 --> eq. (8a) and (11b), 1 --> eq. (9) and (12)
Guy De Souza's avatar
Guy De Souza committed
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

	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
85
	if (polarMessageType == 0) { //PBCH
Guy De Souza's avatar
Guy De Souza committed
86 87
		testLength = NR_POLAR_PBCH_PAYLOAD_BITS;
		coderLength = NR_POLAR_PBCH_E;
88
		aggregation_level = NR_POLAR_PBCH_AGGREGATION_LEVEL;
yilmazt's avatar
yilmazt committed
89
	} else if (polarMessageType == 1) { //DCI
90 91 92
		//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
93
	} else if (polarMessageType == -1) { //UCI
Guy De Souza's avatar
Guy De Souza committed
94 95 96 97 98 99 100
		//testLength = ;
		//coderLength = ;
	}

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

yilmazt's avatar
yilmazt committed
103 104 105
	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
106 107 108
	strftime(currentTimeInfo, 25, "_%Y-%m-%d-%H-%M-%S.csv", localtime(&currentTime));
	strcat(fileName,currentTimeInfo);

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

yilmazt's avatar
yilmazt committed
113
	FILE* logFile;
Guy De Souza's avatar
Guy De Souza committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
    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");

	uint8_t *testInput = malloc(sizeof(uint8_t) * testLength); //generate randomly
	uint8_t *encoderOutput = malloc(sizeof(uint8_t) * coderLength);
	double *modulatedInput = malloc (sizeof(double) * coderLength); //channel input

	double *channelOutput  = malloc (sizeof(double) * coderLength); //add noise
	uint8_t *estimatedOutput = malloc(sizeof(uint8_t) * testLength); //decoder output

yilmazt's avatar
yilmazt committed
128
	t_nrPolar_paramsPtr nrPolar_params = NULL;
129
	nr_polar_init(&nrPolar_params, polarMessageType, testLength, aggregation_level);
yilmazt's avatar
yilmazt committed
130 131 132 133 134 135
#ifdef DEBUG_POLAR_PARAMS
	nr_polar_init(&nrPolar_params, polarMessageType, testLength, aggregation_level);
	nr_polar_init(&nrPolar_params, 1, 20, 1);
	nr_polar_init(&nrPolar_params, 1, 21, 1);
	nr_polar_init(&nrPolar_params, polarMessageType, testLength, aggregation_level);
	nr_polar_print_polarParams(nrPolar_params);
yilmazt's avatar
yilmazt committed
136 137 138 139 140

	uint32_t in[4];
	in[0]=0x01189400;
	in[1]=0xffffff0f;
	uint8_t *out = malloc(sizeof(uint8_t) * 41);
yilmazt's avatar
yilmazt committed
141
	nr_bit2byte_uint32_8_t(in, 41, out);
yilmazt's avatar
yilmazt committed
142 143
	for (int i=0;i<41;i++)
		printf("out[%d]=%d\n",i,out[i]);
yilmazt's avatar
yilmazt committed
144 145 146
	uint32_t inn[4];
	nr_byte2bit_uint8_32_t(out, 41, inn);
	printf("inn[0]=%#x, inn[1]=%#x\n",inn[0],inn[1]);
yilmazt's avatar
yilmazt committed
147 148 149
	return (0);
#endif

150
	t_nrPolar_paramsPtr currentPtr = nr_polar_params(nrPolar_params, polarMessageType, testLength);
Guy De Souza's avatar
Guy De Souza committed
151 152

	// We assume no a priori knowledge available about the payload.
yilmazt's avatar
yilmazt committed
153 154
	double aPrioriArray[currentPtr->payloadBits];
	for (int i=0; i<currentPtr->payloadBits; i++) aPrioriArray[i] = NAN;
Guy De Souza's avatar
Guy De Souza committed
155 156 157 158 159 160 161 162

	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
163
		polar_encoder(testInput, encoderOutput, currentPtr);
Guy De Souza's avatar
Guy De Souza committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177
		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)));
		}


		start_meas(&timeDecoder);
yilmazt's avatar
yilmazt committed
178 179 180 181 182 183
		decoderState = polar_decoder(channelOutput,
									 estimatedOutput,
									 currentPtr,
									 decoderListSize,
									 aPrioriArray,
									 pathMetricAppr);
Guy De Souza's avatar
Guy De Souza committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		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);
	free(testInput);
	free(encoderOutput);
	free(modulatedInput);
	free(channelOutput);
	free(estimatedOutput);

	return (0);
}