Commit 43ff9ccf authored by Florian Kaltenberger's avatar Florian Kaltenberger

Merge branch '403-memory-leak-in-polar_init' into 'develop-nr'

Resolve "memory leak in polar_init"

See merge request !518
parents d383cd52 9a46d487
Pipeline #11706 failed with stage
in 0 seconds
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
//#define DEBUG_CRC //#define DEBUG_CRC
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
//Initiate timing. (Results depend on CPU Frequency. Therefore, might change due to performance variances during simulation.) //Initiate timing. (Results depend on CPU Frequency. Therefore, might change due to performance variances during simulation.)
time_stats_t timeEncoder,timeDecoder; time_stats_t timeEncoder,timeDecoder;
opp_enabled=1; opp_enabled=1;
...@@ -27,11 +26,9 @@ int main(int argc, char *argv[]) { ...@@ -27,11 +26,9 @@ int main(int argc, char *argv[]) {
reset_meas(&timeDecoder); reset_meas(&timeDecoder);
randominit(0); randominit(0);
crcTableInit(); crcTableInit();
//Default simulation values (Aim for iterations = 1000000.) //Default simulation values (Aim for iterations = 1000000.)
int itr, iterations = 1000, arguments, polarMessageType = 0; //0=PBCH, 1=DCI, -1=UCI int itr, iterations = 1000, arguments, polarMessageType = 0; //0=PBCH, 1=DCI, -1=UCI
double SNRstart = -20.0, SNRstop = 0.0, SNRinc= 0.5; //dB double SNRstart = -20.0, SNRstop = 0.0, SNRinc= 0.5; //dB
double SNR, SNR_lin; double SNR, SNR_lin;
int16_t nBitError = 0; // -1 = Decoding failed (All list entries have failed the CRC checks). int16_t nBitError = 0; // -1 = Decoding failed (All list entries have failed the CRC checks).
uint32_t decoderState=0, blockErrorState=0; //0 = Success, -1 = Decoding failed, 1 = Block Error. uint32_t decoderState=0, blockErrorState=0; //0 = Success, -1 = Decoding failed, 1 = Block Error.
...@@ -40,8 +37,7 @@ int main(int argc, char *argv[]) { ...@@ -40,8 +37,7 @@ int main(int argc, char *argv[]) {
uint8_t aggregation_level = 8, decoderListSize = 8, pathMetricAppr = 0; uint8_t aggregation_level = 8, decoderListSize = 8, pathMetricAppr = 0;
while ((arguments = getopt (argc, argv, "s:d:f:m:i:l:a:hqg")) != -1) while ((arguments = getopt (argc, argv, "s:d:f:m:i:l:a:hqg")) != -1)
switch (arguments) switch (arguments) {
{
case 's': case 's':
SNRstart = atof(optarg); SNRstart = atof(optarg);
break; break;
...@@ -103,65 +99,54 @@ int main(int argc, char *argv[]) { ...@@ -103,65 +99,54 @@ int main(int argc, char *argv[]) {
//coderLength = ; //coderLength = ;
} }
//Logging //Logging
time_t currentTime; time_t currentTime;
time (&currentTime); time (&currentTime);
char fileName[512], currentTimeInfo[25]; char fileName[512], currentTimeInfo[25];
char folderName[] = "."; char folderName[] = ".";
/* /*
folderName=getenv("HOME"); folderName=getenv("HOME");
strcat(folderName,"/Desktop/polartestResults"); strcat(folderName,"/Desktop/polartestResults");
*/ */
#ifdef DEBUG_POLAR_TIMING
#ifdef DEBUG_POLAR_TIMING
sprintf(fileName,"%s/TIMING_ListSize_%d_pmAppr_%d_Payload_%d_Itr_%d",folderName,decoderListSize,pathMetricAppr,testLength,iterations); sprintf(fileName,"%s/TIMING_ListSize_%d_pmAppr_%d_Payload_%d_Itr_%d",folderName,decoderListSize,pathMetricAppr,testLength,iterations);
#else #else
sprintf(fileName,"%s/_ListSize_%d_pmAppr_%d_Payload_%d_Itr_%d",folderName,decoderListSize,pathMetricAppr,testLength,iterations); sprintf(fileName,"%s/_ListSize_%d_pmAppr_%d_Payload_%d_Itr_%d",folderName,decoderListSize,pathMetricAppr,testLength,iterations);
#endif #endif
strftime(currentTimeInfo, 25, "_%Y-%m-%d-%H-%M-%S.csv", localtime(&currentTime)); strftime(currentTimeInfo, 25, "_%Y-%m-%d-%H-%M-%S.csv", localtime(&currentTime));
strcat(fileName,currentTimeInfo); strcat(fileName,currentTimeInfo);
//Create "~/Desktop/polartestResults" folder if it doesn't already exist. //Create "~/Desktop/polartestResults" folder if it doesn't already exist.
/* /*
struct stat folder = {0}; struct stat folder = {0};
if (stat(folderName, &folder) == -1) mkdir(folderName, S_IRWXU | S_IRWXG | S_IRWXO); if (stat(folderName, &folder) == -1) mkdir(folderName, S_IRWXU | S_IRWXG | S_IRWXO);
*/ */
FILE *logFile;
FILE* logFile;
logFile = fopen(fileName, "w"); logFile = fopen(fileName, "w");
if (logFile==NULL) { if (logFile==NULL) {
fprintf(stderr,"[polartest.c] Problem creating file %s with fopen\n",fileName); fprintf(stderr,"[polartest.c] Problem creating file %s with fopen\n",fileName);
exit(-1); exit(-1);
} }
#ifdef DEBUG_POLAR_TIMING #ifdef DEBUG_POLAR_TIMING
fprintf(logFile,",timeEncoderCRCByte[us],timeEncoderCRCBit[us],timeEncoderInterleaver[us],timeEncoderBitInsertion[us],timeEncoder1[us],timeEncoder2[us],timeEncoderRateMatching[us],timeEncoderByte2Bit[us]\n"); fprintf(logFile,
",timeEncoderCRCByte[us],timeEncoderCRCBit[us],timeEncoderInterleaver[us],timeEncoderBitInsertion[us],timeEncoder1[us],timeEncoder2[us],timeEncoderRateMatching[us],timeEncoderByte2Bit[us]\n");
#else #else
fprintf(logFile,",SNR,nBitError,blockErrorState,t_encoder[us],t_decoder[us]\n"); fprintf(logFile,",SNR,nBitError,blockErrorState,t_encoder[us],t_decoder[us]\n");
#endif #endif
uint8_t testArrayLength = ceil(testLength / 32.0); uint8_t testArrayLength = ceil(testLength / 32.0);
uint8_t coderArrayLength = ceil(coderLength / 32.0); uint8_t coderArrayLength = ceil(coderLength / 32.0);
uint32_t testInput[testArrayLength]; //generate randomly uint32_t testInput[testArrayLength]; //generate randomly
uint32_t encoderOutput[coderArrayLength]; uint32_t encoderOutput[coderArrayLength];
uint32_t estimatedOutput[testArrayLength]; //decoder output uint32_t estimatedOutput[testArrayLength]; //decoder output
memset(testInput,0,sizeof(uint32_t) * testArrayLength); memset(testInput,0,sizeof(uint32_t) * testArrayLength);
memset(encoderOutput,0,sizeof(uint32_t) * coderArrayLength); memset(encoderOutput,0,sizeof(uint32_t) * coderArrayLength);
memset(estimatedOutput,0,sizeof(uint32_t) * testArrayLength); memset(estimatedOutput,0,sizeof(uint32_t) * testArrayLength);
uint8_t encoderOutputByte[coderLength]; uint8_t encoderOutputByte[coderLength];
double modulatedInput[coderLength]; //channel input double modulatedInput[coderLength]; //channel input
double channelOutput[coderLength]; //add noise double channelOutput[coderLength]; //add noise
int16_t channelOutput_int16[coderLength]; int16_t channelOutput_int16[coderLength];
t_nrPolar_params *currentPtr = nr_polar_params(polarMessageType, testLength, aggregation_level);
t_nrPolar_paramsPtr nrPolar_params = NULL, currentPtr = NULL;
nr_polar_init(&nrPolar_params, polarMessageType, testLength, aggregation_level);
currentPtr = nr_polar_params(nrPolar_params, polarMessageType, testLength, aggregation_level);
#ifdef DEBUG_DCI_POLAR_PARAMS #ifdef DEBUG_DCI_POLAR_PARAMS
uint32_t dci_pdu[4]; uint32_t dci_pdu[4];
memset(dci_pdu,0,sizeof(uint32_t)*4); memset(dci_pdu,0,sizeof(uint32_t)*4);
...@@ -173,11 +158,10 @@ int main(int argc, char *argv[]) { ...@@ -173,11 +158,10 @@ int main(int argc, char *argv[]) {
uint16_t size=41; uint16_t size=41;
uint16_t rnti=3; uint16_t rnti=3;
aggregation_level=8; aggregation_level=8;
nr_polar_init(&nrPolar_params, 1, size, aggregation_level); t_nrPolar_params *currentPtrDCI=nr_polar_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); polar_encoder_dci(dci_pdu, encoder_output, currentPtrDCI, rnti);
for (int i=0;i<54;i++)
for (int i=0; i<54; i++)
printf("encoder_output: [%2d]->0x%08x \n",i, encoder_output[i]); printf("encoder_output: [%2d]->0x%08x \n",i, encoder_output[i]);
uint8_t *encoder_outputByte = malloc(sizeof(uint8_t) * currentPtrDCI->encoderLength); uint8_t *encoder_outputByte = malloc(sizeof(uint8_t) * currentPtrDCI->encoderLength);
...@@ -188,13 +172,15 @@ int main(int argc, char *argv[]) { ...@@ -188,13 +172,15 @@ int main(int argc, char *argv[]) {
dci_estimation[0], dci_estimation[1], dci_estimation[2], dci_estimation[3]); dci_estimation[0], dci_estimation[1], dci_estimation[2], dci_estimation[3]);
nr_bit2byte_uint32_8_t(encoder_output, currentPtrDCI->encoderLength, encoder_outputByte); nr_bit2byte_uint32_8_t(encoder_output, currentPtrDCI->encoderLength, encoder_outputByte);
printf("[polartest] encoder_outputByte: "); printf("[polartest] encoder_outputByte: ");
for (int i = 0; i < currentPtrDCI->encoderLength; i++) printf("%d-", encoder_outputByte[i]); for (int i = 0; i < currentPtrDCI->encoderLength; i++) printf("%d-", encoder_outputByte[i]);
printf("\n"); printf("\n");
for(int i=0; i<currentPtrDCI->encoderLength; i++) { for(int i=0; i<currentPtrDCI->encoderLength; i++) {
if (encoder_outputByte[i] == 0) { if (encoder_outputByte[i] == 0) {
channel_output[i]=1/sqrt(2); channel_output[i]=1/sqrt(2);
} } else {
else {
channel_output[i]=(-1)/sqrt(2); channel_output[i]=(-1)/sqrt(2);
} }
} }
...@@ -211,7 +197,6 @@ int main(int argc, char *argv[]) { ...@@ -211,7 +197,6 @@ int main(int argc, char *argv[]) {
free(channel_output); free(channel_output);
return 0; return 0;
#endif #endif
#ifdef DEBUG_CRC #ifdef DEBUG_CRC
uint32_t crc; uint32_t crc;
unsigned int poly24c = 0xb2b11700; unsigned int poly24c = 0xb2b11700;
...@@ -221,10 +206,8 @@ int main(int argc, char *argv[]) { ...@@ -221,10 +206,8 @@ int main(int argc, char *argv[]) {
testInputCRC[1]=0x00000000; testInputCRC[1]=0x00000000;
testInputCRC[2]=0x00000000; testInputCRC[2]=0x00000000;
testInputCRC[3]=0x00000000; testInputCRC[3]=0x00000000;
uint32_t testInputcrc=0x01189400; uint32_t testInputcrc=0x01189400;
uint32_t testInputcrc2=0x00291880; uint32_t testInputcrc2=0x00291880;
uint8_t testInputCRC2[8]; uint8_t testInputCRC2[8];
nr_crc_bit2bit_uint32_8_t(testInputCRC, 32, testInputCRC2); nr_crc_bit2bit_uint32_8_t(testInputCRC, 32, testInputCRC2);
printf("testInputCRC2: [0]->%x \t [1]->%x \t [2]->%x \t [3]->%x\n" printf("testInputCRC2: [0]->%x \t [1]->%x \t [2]->%x \t [3]->%x\n"
...@@ -234,35 +217,38 @@ int main(int argc, char *argv[]) { ...@@ -234,35 +217,38 @@ int main(int argc, char *argv[]) {
unsigned int crc41 = crc24c(testInputCRC, 32); unsigned int crc41 = crc24c(testInputCRC, 32);
unsigned int crc65 = crc24c(testInputCRC, 56); unsigned int crc65 = crc24c(testInputCRC, 56);
printf("crc41: [0]->0x%08x\tcrc65: [0]->0x%08x\n",crc41, crc65); 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);
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; crc = crc24c(testInputCRC, testLength)>>8;
for (int i=0;i<24;i++) printf("[i]=%d\n",(crc>>i)&1);
for (int i=0; i<24; i++) printf("[i]=%d\n",(crc>>i)&1);
printf("crc: [0]->0x%08x\n",crc); printf("crc: [0]->0x%08x\n",crc);
//crcbit(testInputCRC, sizeof(test) - 1, poly24c)); //crcbit(testInputCRC, sizeof(test) - 1, poly24c));
testInputCRC[testLength>>3] = ((uint8_t *)&crc)[2];
testInputCRC[testLength>>3] = ((uint8_t*)&crc)[2]; testInputCRC[1+(testLength>>3)] = ((uint8_t *)&crc)[1];
testInputCRC[1+(testLength>>3)] = ((uint8_t*)&crc)[1]; testInputCRC[2+(testLength>>3)] = ((uint8_t *)&crc)[0];
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", 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]); testInputCRC[0], testInputCRC[1], testInputCRC[2], testInputCRC[3]);
//uint32_t trial32 = 0xffffffff; //uint32_t trial32 = 0xffffffff;
uint32_t trial32 = 0xf10fffff; uint32_t trial32 = 0xf10fffff;
uint8_t a[4]; uint8_t a[4];
//memcpy(a, &trial32, sizeof(trial32)); //memcpy(a, &trial32, sizeof(trial32));
*(uint32_t *)a = trial32; *(uint32_t *)a = trial32;
unsigned char trial[4]; unsigned char trial[4];
trial[0]=0xff; trial[1]=0xff; trial[2]=0x0f; trial[3]=0xf1; trial[0]=0xff;
trial[1]=0xff;
trial[2]=0x0f;
trial[3]=0xf1;
uint32_t trialcrc = crc24c(trial, 32); uint32_t trialcrc = crc24c(trial, 32);
uint32_t trialcrc32 = crc24c((uint8_t *)&trial32, 32); uint32_t trialcrc32 = crc24c((uint8_t *)&trial32, 32);
//uint32_t trialcrc32 = crc24c(a, 32); //uint32_t trialcrc32 = crc24c(a, 32);
printf("crcbit(trial = %x\n", crcbit(trial, 4, poly24c)); printf("crcbit(trial = %x\n", crcbit(trial, 4, poly24c));
printf("trialcrc = %x\n", trialcrc); printf("trialcrc = %x\n", trialcrc);
printf("trialcrc32 = %x\n", trialcrc32); 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);
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,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_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};
...@@ -273,16 +259,19 @@ int main(int argc, char *argv[]) { ...@@ -273,16 +259,19 @@ int main(int argc, char *argv[]) {
nr_polar_crc, nr_polar_crc,
32, 32,
24); 24);
for (uint8_t i = 0; i < 24; i++){
for (uint8_t i = 0; i < 24; i++) {
nr_polar_crc[i] = (nr_polar_crc[i] % 2); nr_polar_crc[i] = (nr_polar_crc[i] % 2);
printf("nr_polar_crc[%d]=%d\n",i,nr_polar_crc[i]); printf("nr_polar_crc[%d]=%d\n",i,nr_polar_crc[i]);
} }
return 0; return 0;
#endif #endif
#ifdef DEBUG_POLAR_TIMING #ifdef DEBUG_POLAR_TIMING
for (SNR = SNRstart; SNR <= SNRstop; SNR += SNRinc) { for (SNR = SNRstart; SNR <= SNRstop; SNR += SNRinc) {
SNR_lin = pow(10, SNR / 10); SNR_lin = pow(10, SNR / 10);
for (itr = 1; itr <= iterations; itr++) { for (itr = 1; itr <= iterations; itr++) {
for (int j=0; j<ceil(testLength / 32.0); j++) { for (int j=0; j<ceil(testLength / 32.0); j++) {
for(int i=0; i<32; i++) { for(int i=0; i<32; i++) {
...@@ -290,10 +279,12 @@ int main(int argc, char *argv[]) { ...@@ -290,10 +279,12 @@ int main(int argc, char *argv[]) {
testInput[j]<<=1; testInput[j]<<=1;
} }
} }
printf("testInput: [0]->0x%08x \n", testInput[0]); printf("testInput: [0]->0x%08x \n", testInput[0]);
polar_encoder_timing(testInput, encoderOutput, currentPtr, cpu_freq_GHz, logFile); polar_encoder_timing(testInput, encoderOutput, currentPtr, cpu_freq_GHz, logFile);
} }
} }
fclose(logFile); fclose(logFile);
free(testInput); free(testInput);
free(encoderOutput); free(encoderOutput);
...@@ -302,43 +293,41 @@ int main(int argc, char *argv[]) { ...@@ -302,43 +293,41 @@ int main(int argc, char *argv[]) {
free(estimatedOutput); free(estimatedOutput);
return (0); return (0);
#endif #endif
// We assume no a priori knowledge available about the payload. // We assume no a priori knowledge available about the payload.
double aPrioriArray[currentPtr->payloadBits]; double aPrioriArray[currentPtr->payloadBits];
for (int i=0; i<currentPtr->payloadBits; i++) aPrioriArray[i] = NAN; for (int i=0; i<currentPtr->payloadBits; i++) aPrioriArray[i] = NAN;
for (SNR = SNRstart; SNR <= SNRstop; SNR += SNRinc) { for (SNR = SNRstart; SNR <= SNRstop; SNR += SNRinc) {
printf("SNR %f\n",SNR); printf("SNR %f\n",SNR);
SNR_lin = pow(10, SNR/10); SNR_lin = pow(10, SNR/10);
for (itr = 1; itr <= iterations; itr++) {
for (itr = 1; itr <= iterations; itr++) {
for (int i = 0; i < testArrayLength; i++) { for (int i = 0; i < testArrayLength; i++) {
for (int j = 0; j < (sizeof(testInput[0])*8)-1; j++) { for (int j = 0; j < (sizeof(testInput[0])*8)-1; j++) {
testInput[i] |= ( ((uint32_t) (rand()%2)) &1); testInput[i] |= ( ((uint32_t) (rand()%2)) &1);
testInput[i]<<=1; testInput[i]<<=1;
} }
testInput[i] |= ( ((uint32_t) (rand()%2)) &1); testInput[i] |= ( ((uint32_t) (rand()%2)) &1);
} }
/*printf("testInput: [0]->0x%08x\n", testInput[0]); /*printf("testInput: [0]->0x%08x\n", testInput[0]);
for (int i=0; i<32; i++) for (int i=0; i<32; i++)
printf("%d\n",(testInput[0]>>i)&1);*/ printf("%d\n",(testInput[0]>>i)&1);*/
int len_mod64=currentPtr->payloadBits&63; int len_mod64=currentPtr->payloadBits&63;
((uint64_t*)testInput)[currentPtr->payloadBits/64]&=((((uint64_t)1)<<len_mod64)-1); ((uint64_t *)testInput)[currentPtr->payloadBits/64]&=((((uint64_t)1)<<len_mod64)-1);
start_meas(&timeEncoder); start_meas(&timeEncoder);
if (decoder_int16==0) if (decoder_int16==0)
polar_encoder(testInput, encoderOutput, currentPtr); polar_encoder(testInput, encoderOutput, currentPtr);
else else
polar_encoder_fast((uint64_t*)testInput, encoderOutput,0, currentPtr); polar_encoder_fast((uint64_t *)testInput, encoderOutput,0, currentPtr);
//polar_encoder_fast((uint64_t*)testInput, (uint64_t*)encoderOutput,0, currentPtr); //polar_encoder_fast((uint64_t*)testInput, (uint64_t*)encoderOutput,0, currentPtr);
stop_meas(&timeEncoder); stop_meas(&timeEncoder);
/*printf("encoderOutput: [0]->0x%08x\n", encoderOutput[0]); /*printf("encoderOutput: [0]->0x%08x\n", encoderOutput[0]);
printf("encoderOutput: [1]->0x%08x\n", encoderOutput[1]);*/ printf("encoderOutput: [1]->0x%08x\n", encoderOutput[1]);*/
//Bit-to-byte: //Bit-to-byte:
nr_bit2byte_uint32_8_t(encoderOutput, coderLength, encoderOutputByte); nr_bit2byte_uint32_8_t(encoderOutput, coderLength, encoderOutputByte);
...@@ -351,17 +340,15 @@ int main(int argc, char *argv[]) { ...@@ -351,17 +340,15 @@ int main(int argc, char *argv[]) {
channelOutput[i] = modulatedInput[i] + (gaussdouble(0.0,1.0) * (1/sqrt(2*SNR_lin))); channelOutput[i] = modulatedInput[i] + (gaussdouble(0.0,1.0) * (1/sqrt(2*SNR_lin)));
if (decoder_int16==1) { if (decoder_int16==1) {
if (channelOutput[i] > 15) channelOutput_int16[i] = 127; if (channelOutput[i] > 15) channelOutput_int16[i] = 127;
else if (channelOutput[i] < -16) channelOutput_int16[i] = -128; else if (channelOutput[i] < -16) channelOutput_int16[i] = -128;
else channelOutput_int16[i] = (int16_t) (8*channelOutput[i]); else channelOutput_int16[i] = (int16_t) (8*channelOutput[i]);
} }
} }
start_meas(&timeDecoder); start_meas(&timeDecoder);
/*decoderState = polar_decoder(channelOutput, /*decoderState = polar_decoder(channelOutput,
estimatedOutput, estimatedOutput,
currentPtr, currentPtr,
...@@ -377,15 +364,13 @@ int main(int argc, char *argv[]) { ...@@ -377,15 +364,13 @@ int main(int argc, char *argv[]) {
aPrioriArray); aPrioriArray);
else else
decoderState = polar_decoder_int16(channelOutput_int16, decoderState = polar_decoder_int16(channelOutput_int16,
(uint64_t*)estimatedOutput, (uint64_t *)estimatedOutput,
currentPtr); currentPtr);
stop_meas(&timeDecoder); stop_meas(&timeDecoder);
/*printf("testInput: [0]->0x%08x\n", testInput[0]); /*printf("testInput: [0]->0x%08x\n", testInput[0]);
printf("estimatedOutput: [0]->0x%08x\n", estimatedOutput[0]);*/ printf("estimatedOutput: [0]->0x%08x\n", estimatedOutput[0]);*/
//calculate errors //calculate errors
if (decoderState!=0) { if (decoderState!=0) {
blockErrorState=-1; blockErrorState=-1;
...@@ -393,10 +378,10 @@ int main(int argc, char *argv[]) { ...@@ -393,10 +378,10 @@ int main(int argc, char *argv[]) {
} else { } else {
for (int j = 0; j < currentPtr->payloadBits; j++) { for (int j = 0; j < currentPtr->payloadBits; j++) {
if (((estimatedOutput[0]>>j) & 1) != ((testInput[0]>>j) & 1)) nBitError++; if (((estimatedOutput[0]>>j) & 1) != ((testInput[0]>>j) & 1)) nBitError++;
// printf("bit %d: %d => %d\n",j,(testInput[0]>>j)&1,(estimatedOutput[0]>>j)&1); // printf("bit %d: %d => %d\n",j,(testInput[0]>>j)&1,(estimatedOutput[0]>>j)&1);
} }
if (nBitError>0) { if (nBitError>0) {
blockErrorState=1; blockErrorState=1;
// printf("Error: Input %x, Output %x\n",testInput[0],estimatedOutput[0]); // printf("Error: Input %x, Output %x\n",testInput[0],estimatedOutput[0]);
...@@ -420,8 +405,8 @@ int main(int argc, char *argv[]) { ...@@ -420,8 +405,8 @@ int main(int argc, char *argv[]) {
decoderState=0; decoderState=0;
nBitError=0; nBitError=0;
blockErrorState=0; blockErrorState=0;
} }
//Calculate error statistics for the SNR. //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", 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), decoderListSize, pathMetricAppr, SNR, ((double)blockErrorCumulative/iterations),
...@@ -432,13 +417,14 @@ int main(int argc, char *argv[]) { ...@@ -432,13 +417,14 @@ int main(int argc, char *argv[]) {
if (blockErrorCumulative==0 && bitErrorCumulative==0) if (blockErrorCumulative==0 && bitErrorCumulative==0)
break; break;
blockErrorCumulative = 0; bitErrorCumulative = 0; blockErrorCumulative = 0;
timeEncoderCumulative = 0; timeDecoderCumulative = 0; bitErrorCumulative = 0;
timeEncoderCumulative = 0;
timeDecoderCumulative = 0;
} }
print_meas(&timeEncoder,"polar_encoder",NULL,NULL); print_meas(&timeEncoder,"polar_encoder",NULL,NULL);
print_meas(&timeDecoder,"polar_decoder",NULL,NULL); print_meas(&timeDecoder,"polar_decoder",NULL,NULL);
fclose(logFile); fclose(logFile);
return (0); return (0);
} }
...@@ -42,12 +42,10 @@ ...@@ -42,12 +42,10 @@
int8_t polar_decoder( int8_t polar_decoder(
double *input, double *input,
uint8_t *out, uint8_t *out,
t_nrPolar_paramsPtr polarParams, t_nrPolar_params *polarParams,
uint8_t listSize, uint8_t listSize,
uint8_t pathMetricAppr) uint8_t pathMetricAppr) {
{
//Assumes no a priori knowledge. //Assumes no a priori knowledge.
uint8_t ***bit = nr_alloc_uint8_3D_array(polarParams->N, (polarParams->n+1), 2*listSize); uint8_t ***bit = nr_alloc_uint8_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
uint8_t **bitUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True uint8_t **bitUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
uint8_t **llrUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True uint8_t **llrUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
...@@ -60,6 +58,7 @@ int8_t polar_decoder( ...@@ -60,6 +58,7 @@ int8_t polar_decoder(
pathMetric[i] = 0; pathMetric[i] = 0;
crcState[i]=1; crcState[i]=1;
} }
for (int i=0; i<polarParams->N; i++) { for (int i=0; i<polarParams->N; i++) {
llrUpdated[i][polarParams->n]=1; llrUpdated[i][polarParams->n]=1;
bitUpdated[i][0]=((polarParams->information_bit_pattern[i]+1) % 2); bitUpdated[i][0]=((polarParams->information_bit_pattern[i]+1) % 2);
...@@ -67,7 +66,8 @@ int8_t polar_decoder( ...@@ -67,7 +66,8 @@ int8_t polar_decoder(
uint8_t **extended_crc_generator_matrix = malloc(polarParams->K * sizeof(uint8_t *)); //G_P3 uint8_t **extended_crc_generator_matrix = malloc(polarParams->K * sizeof(uint8_t *)); //G_P3
uint8_t **tempECGM = malloc(polarParams->K * sizeof(uint8_t *)); //G_P2 uint8_t **tempECGM = malloc(polarParams->K * sizeof(uint8_t *)); //G_P2
for (int i = 0; i < polarParams->K; i++){
for (int i = 0; i < polarParams->K; i++) {
extended_crc_generator_matrix[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t)); extended_crc_generator_matrix[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
tempECGM[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t)); tempECGM[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
} }
...@@ -77,9 +77,10 @@ int8_t polar_decoder( ...@@ -77,9 +77,10 @@ int8_t polar_decoder(
tempECGM[i][j]=polarParams->crc_generator_matrix[i][j]; tempECGM[i][j]=polarParams->crc_generator_matrix[i][j];
} }
} }
for (int i=polarParams->payloadBits; i<polarParams->K; i++) { for (int i=polarParams->payloadBits; i<polarParams->K; i++) {
for (int j=0; j<polarParams->crcParityBits; j++) { for (int j=0; j<polarParams->crcParityBits; j++) {
if( (i-polarParams->payloadBits) == j ){ if( (i-polarParams->payloadBits) == j ) {
tempECGM[i][j]=1; tempECGM[i][j]=1;
} else { } else {
tempECGM[i][j]=0; tempECGM[i][j]=0;
...@@ -95,6 +96,7 @@ int8_t polar_decoder( ...@@ -95,6 +96,7 @@ int8_t polar_decoder(
//The index of the last 1-valued bit that appears in each column. //The index of the last 1-valued bit that appears in each column.
uint16_t last1ind[polarParams->crcParityBits]; uint16_t last1ind[polarParams->crcParityBits];
for (int j=0; j<polarParams->crcParityBits; j++) { for (int j=0; j<polarParams->crcParityBits; j++) {
for (int i=0; i<polarParams->K; i++) { for (int i=0; i<polarParams->K; i++) {
if (extended_crc_generator_matrix[i][j]==1) last1ind[j]=i; if (extended_crc_generator_matrix[i][j]==1) last1ind[j]=i;
...@@ -103,21 +105,21 @@ int8_t polar_decoder( ...@@ -103,21 +105,21 @@ int8_t polar_decoder(
double *d_tilde = malloc(sizeof(double) * polarParams->N); double *d_tilde = malloc(sizeof(double) * polarParams->N);
nr_polar_rate_matching(input, d_tilde, polarParams->rate_matching_pattern, polarParams->K, polarParams->N, polarParams->encoderLength); nr_polar_rate_matching(input, d_tilde, polarParams->rate_matching_pattern, polarParams->K, polarParams->N, polarParams->encoderLength);
for (int j = 0; j < polarParams->N; j++) llr[j][polarParams->n][0]=d_tilde[j];
for (int j = 0; j < polarParams->N; j++) llr[j][polarParams->n][0]=d_tilde[j];
/* /*
* SCL polar decoder. * SCL polar decoder.
*/ */
uint32_t nonFrozenBit=0; uint32_t nonFrozenBit=0;
uint8_t currentListSize=1; uint8_t currentListSize=1;
uint8_t decoderIterationCheck=0; uint8_t decoderIterationCheck=0;
int16_t checkCrcBits=-1; int16_t checkCrcBits=-1;
uint8_t listIndex[2*listSize], copyIndex; uint8_t listIndex[2*listSize], copyIndex;
for (uint16_t currentBit=0; currentBit<polarParams->N; currentBit++){ for (uint16_t currentBit=0; currentBit<polarParams->N; currentBit++) {
updateLLR(llr, llrUpdated, bit, bitUpdated, currentListSize, currentBit, 0, polarParams->N, (polarParams->n+1), pathMetricAppr); updateLLR(llr, llrUpdated, bit, bitUpdated, currentListSize, currentBit, 0, polarParams->N, (polarParams->n+1), pathMetricAppr);
if (polarParams->information_bit_pattern[currentBit]==0) { //Frozen bit. if (polarParams->information_bit_pattern[currentBit]==0) { //Frozen bit.
updatePathMetric(pathMetric, llr, currentListSize, 0, currentBit, pathMetricAppr); //approximation=0 --> 11b, approximation=1 --> 12 updatePathMetric(pathMetric, llr, currentListSize, 0, currentBit, pathMetricAppr); //approximation=0 --> 11b, approximation=1 --> 12
} else { //Information or CRC bit. } else { //Information or CRC bit.
...@@ -127,12 +129,18 @@ int8_t polar_decoder( ...@@ -127,12 +129,18 @@ int8_t polar_decoder(
for (int j = 0; j < polarParams->N; j++) { for (int j = 0; j < polarParams->N; j++) {
for (int k = 0; k < (polarParams->n+1); k++) { for (int k = 0; k < (polarParams->n+1); k++) {
bit[j][k][i+currentListSize]=bit[j][k][i]; bit[j][k][i+currentListSize]=bit[j][k][i];
llr[j][k][i+currentListSize]=llr[j][k][i];}}} llr[j][k][i+currentListSize]=llr[j][k][i];
}
}
}
for (int i = 0; i < currentListSize; i++) { for (int i = 0; i < currentListSize; i++) {
bit[currentBit][0][i]=0; bit[currentBit][0][i]=0;
crcState[i+currentListSize]=crcState[i]; crcState[i+currentListSize]=crcState[i];
} }
for (int i = currentListSize; i < 2*currentListSize; i++) bit[currentBit][0][i]=1; for (int i = currentListSize; i < 2*currentListSize; i++) bit[currentBit][0][i]=1;