llr_quantization.c 5.56 KB
Newer Older
ghaddab's avatar
ghaddab committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*******************************************************************************
    OpenAirInterface 
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is 
   included in this distribution in the file called "COPYING". If not, 
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
  
ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
ghaddab's avatar
ghaddab committed
27 28

 *******************************************************************************/
29 30 31 32 33 34 35 36 37 38 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 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
//#include <string.h>
#include <math.h>
//#include <unistd.h>
//#include <stdlib.h>
#include "PHY/TOOLS/defs.h"

//#define TEST_DEBUG

short max_array(const short *p, unsigned int size)
{
	short max = *p;
	unsigned int i; 

	for (i = 1; i < size; ++i)
		if (max < p[i])
			max = p[i];
	
	return max;
}


short min_array(const short *p, unsigned int size)
{
	short min = *p;
	unsigned int i; 

	for (i = 1; i < size; ++i)
		if (min > p[i])
			min = p[i];
	
	return min;
}


// Note that 'arraySize =  8*((3*8*6144)+12)';  
void dlsch_LLR_quant(const short *llr, const unsigned int arraySize, const short Mlevel, short *llr_quant)
{

	unsigned int i, j;
	short max_llr, min_llr;
	short llr_interval;
	float quant_step;
	float *transLevelArray;
	
	min_llr = min_array(llr, arraySize); 	
	max_llr = max_array(llr, arraySize); 	
	llr_interval = (max_llr - min_llr);
	quant_step = (float)llr_interval / Mlevel;	


	if ((Mlevel%2) != 0){
		printf("Mlevel should be mutiple of 2...\n");
		exit(-1);
	}
	
	transLevelArray = (float *)malloc((Mlevel+1)*sizeof(float));	
	if (!transLevelArray){
		printf("Cannot allocate memory for transLevelArray...!\n");
		exit(-1);
	} 
	
	for(j=0; j < Mlevel+1; j++){
		transLevelArray[j] = min_llr + j*quant_step; 
	} 

	for (i=0; i < arraySize; i++){		
		for(j=0; j < Mlevel; j++){
			if ((transLevelArray[j] <= llr[i]) && (llr[i] <= transLevelArray[j+1])){
				llr_quant[i] = (short)(transLevelArray[j] +  quant_step/2);  // mid-points are selected;
				break;
			}	
			if (transLevelArray[j+1] <= llr[i]){ // the last term;
				llr_quant[i] = (short)(transLevelArray[j] +  quant_step/2);  // mid-points are selected;
			}				
		} 	
	}
	#ifdef TEST_DEBUG
		printf("min_llr: %d  : max_llr: %d \n", min_llr, max_llr);
		printf("llr_interval: %d  : quant_step: %f \n", llr_interval, quant_step);
		printf("transLevelArray = [");
		for(j=0; j < Mlevel+1; j++)
			printf("%f ", transLevelArray[j]);		
		printf("] \n\n");	
	#endif // TEST_DEBUG	
	
}

/*

void dlsch_LLR_quant2(const short *llr, const unsigned int arraySize, const short bitsPerLLR, short *llr_quant)
{

	unsigned int i;
	short max_llr, min_llr;
	short peak_llr;
	unsigned char shift;
		
	min_llr = min_array(llr, arraySize); 	
	max_llr = max_array(llr, arraySize); 	
	
	peak_llr = max(abs(max_llr), abs(min_llr));
	shift = log2_approx(peak_llr);

	if (bitsPerLLR < 1){
		printf("Bits per LLR should be greater than 0\n");
		exit(-1);
	}
	
	for (i=0; i < 6072; i++){		
		llr_quant[i] = llr[i] >> max(0, shift-bitsPerLLR);  // quantize the LLRs; 
		llr_quant[i] <<= max(0, shift-bitsPerLLR);          // reconstruct the LLRs;
		printf("[%d] llr: %d : llr_quant %d \n",i, llr[i], llr_quant[i]);	
	}
	exit(-1);
}


void clearbits(short *llr, const unsigned short startbit, const unsigned short nbits)
{

	unsigned short i;
		
	for (i=0; i < nbits; i++){			
		*llr &= ~(1 << (startbit + i));
	}
}

void dlsch_LLR_quant3(const short *llr, const unsigned int arraySize, const short bitsPerLLR, short *llr_quant)
{

	unsigned int i;
	unsigned char shift;
	unsigned int abs_mean_llr = 0;
				
	//mean of the LLRs; 	
	for(i=0; i < 6072; i++){			
		abs_mean_llr += abs(llr[i]);  
	}
	abs_mean_llr /= 6072;
		
	shift = log2_approx(abs_mean_llr);
	
	printf("abs_mean_llr=%d, shift = %d\n", abs_mean_llr, shift);
	exit(-1);
	
	for (i=0; i < 6072; i++){		
		llr_quant[i] = llr[i] >> max(0, shift-bitsPerLLR);  // quantize the LLRs; 
		printf("[%d] llr: %d : ", i, llr[i]);		
		//mask the llr_quant values such that their values do not exceed 2^(bitsPerLLR);  
		clearbits(&llr_quant[i], bitsPerLLR, sizeof(short)-bitsPerLLR-1);		
		printf("llr_quant_mask: %d : ", llr_quant[i]);
		llr_quant[i] <<= max(0, shift-bitsPerLLR);          // reconstruct the LLRs;
		printf("llr_quant: %d \n", llr_quant[i]);
		
	}
} 

*/


void dlsch_MRC_relay_LLR(const short *llr_quant, const unsigned int arraySize,  short *llr_quant_sum){
	
	unsigned int i; 
	
	for (i=0; i < arraySize; i++){		
		llr_quant_sum[i] += llr_quant[i];
	}
}



#ifdef TEST_DEBUG

void test_llr_quant()
{
  	short test[10] = {-1800, -5446, 345, 243, 130, -2111, 433, 4210, -10, -134};
  	short channel_output[10];
  	
  	unsigned int i;

  
	dlsch_LLR_quant(test, 10, 2, channel_output);
  	
  	for (i = 0; i < 10; i++){
    	printf("llr: %d : llr_quant %d \n",test[i], channel_output[i]);
  	}
}


void main() {

  	test_llr_quant();
}

#endif // TEST_DEBUG