nprach_NB_IoT.c 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
/*! \file PHY/LTE_TRANSPORT/nprach_eNb_NB_IoT.c
 * function for NPRACH signal detection and Timing Advance estimation
 * \author V. Savaux
24
 * \date 2018
25 26 27 28 29 30 31 32 33 34
 * \version 0.1
 * \company b<>com
 * \email: vincent.savaux@b-com.com
 * \note
 * \warning
 */

//#include "PHY/sse_intrin.h"
#include "PHY/defs_NB_IoT.h"
#include "PHY/TOOLS/defs.h" // to take into account the dft functions
35
#include "tables_nprach_NB_IoT.h"
Matthieu Kanj's avatar
Matthieu Kanj committed
36
#include "first_sc_NB_IoT.h"
37 38 39 40 41 42 43
//#include "PHY/extern.h"
//#include "prach.h"
//#include "PHY/LTE_TRANSPORT/if4_tools.h"
//#include "SCHED/defs.h"
//#include "SCHED/extern.h"
//#include "UTIL/LOG/vcd_signal_dumper.h"

44 45 46 47
int filter_xx[40] = {-2161, 453, 489, 570, 688, 838, 1014, 1209, 1420, 1639,
		  1862, 2082, 2295, 2495, 2677, 2837, 2969, 3072, 3142, 3178, 
		  3178, 3142, 3072, 2969, 2837, 2677, 2495, 2295, 2082, 1862, 
		  1639, 1420, 1209, 1014, 838, 688, 570, 489, 453, -2161}; // this is a low-pass filter
48

49 50 51 52 53
int16_t buffer_nprach[153600];
int16_t filtered_buffer[153600];
int16_t signal_compensed_re[153600];
int16_t signal_compensed_im[153600];
int16_t output_buffer[4800];
54

55
uint8_t NPRACH_detection_NB_IoT(int16_t *input_buffer,uint32_t input_length){
56

57 58 59 60
	uint8_t cp_type = 0; // 0: short ; 1: extended
	uint32_t nb_signal_samples,nb_noise_samples,n1,n2; 
	uint64_t energy_signal=0,energy_noise=0; 
	uint32_t n;
61

62
	if(cp_type){
63

64 65 66
	}else{
		nb_signal_samples = (uint32_t)(((uint64_t) 62670*input_length)/100000); 
		nb_noise_samples = input_length - nb_signal_samples;
67
	}
68 69 70 71
	n1 = nb_signal_samples; 
	n2 = nb_noise_samples;

	// printf("n samples = %i %i\n",FRAME_LENGTH_COMPLEX_SAMPLESx,nb_signal_samples); 
72

73 74 75 76 77 78 79 80
	for(n=0;n<nb_signal_samples;n++){
		energy_signal += (((uint64_t)input_buffer[2*n]*input_buffer[2*n] + (uint64_t)input_buffer[2*n+1]*input_buffer[2*n+1])/n1); 
		//energy_signal += (uint64_t)(((uint64_t)input_buffer[2*n]*input_buffer[2*n] + (uint64_t)input_buffer[2*n+1]*input_buffer[2*n+1])/10);
	}
	for(n=nb_signal_samples;n<input_length;n++){
		energy_noise += (((uint64_t)input_buffer[2*n]*input_buffer[2*n] + (uint64_t)input_buffer[2*n+1]*input_buffer[2*n+1])/n2); 
		//energy_noise += (uint64_t)(((uint64_t)input_buffer[2*n]*input_buffer[2*n] + (uint64_t)input_buffer[2*n+1]*input_buffer[2*n+1])/10); 
	}
81

82 83 84 85 86 87 88
	 //printf("energies = %ld %ld\n",energy_signal,energy_noise);
	if ((uint64_t)(((uint64_t) energy_signal))<(uint64_t)energy_noise>>2){
			
		return 1;
	}else{
		return 0;
	}
89 90
}

91
/*uint32_t TA_estimation_NB_IoT(PHY_VARS_eNB *eNB, 
92 93 94 95
							  int16_t *Rx_sub_sampled_buffer, 
							  uint16_t sub_sampling_rate, 
							  uint16_t FRAME_LENGTH_COMPLEX_SUB_SAMPLES, 
							  uint32_t estimated_TA_coarse, 
96
							  uint8_t coarse){
97

98
	uint16_t length_seq_NPRACH,length_CP,length_symbol; // in number of samples, per NPRACH preamble: 4 sequences ; length of CP in number of samples 
99 100
	uint16_t length_CP_0 = 2048;//eNB->frame_parms.prach_config_common.nprach_CP_Length; //NB-IoT: 0: short, 1: long 
	uint32_t fs=30720000; //NB-IoT: sampling frequency of Rx_buffer, must be defined somewhere
101 102 103
	uint32_t fs_sub_sampled; 
	uint16_t length_correl_window,base_length; 
	int64_t *vec_correlation; 
104 105
	int64_t max_correlation = 0; 
	//int16_t **matrix_received_signal_re, **matrix_received_signal_im; 
106 107
	uint16_t offset_estimation, offset_start; // offset due to first coarse estimation
	// double *** mat_to_phase_estimation_re, *** mat_to_phase_estimation_im; 
108 109
	int64_t average_mat_to_phase_re, average_mat_to_phase_im; 
	float estimated_phase, estimated_CFO, CFO_correction, CFO_correction_k; 
110 111
	// int16_t *vec_CFO_compensation_re, *vec_CFO_compensation_im; 
	// int16_t *vec_received_signal_re, *vec_received_signal_im; 
112
	int16_t *signal_CFO_compensed_re, *signal_CFO_compensed_im; 
113 114 115
	int32_t **sub_sequence_reference_re, **sub_sequence_reference_im; 
	int32_t *sequence_reference_re, *sequence_reference_im; 
	uint32_t TA_sample_estimated = 0; 
116
	int32_t A;//,B; 
117
	int n,k,m,o; 
118 119
	int32_t pow_n1 = 1; 
	uint32_t index_av_ph1, index_av_ph2; 
120

121
	if (coarse){ // coarse = 1: first estimation at 240 kHz
122

123 124 125
		length_seq_NPRACH = (length_CP_0+5*8192)/128; 
		length_CP = length_CP_0/128; 
		length_symbol = 64;
126
		offset_start = 0; 
127 128
		length_correl_window = 80; //20512/sub_sampling_rate; // corresponds to the max TA, i.e. 667.66 micro s //FRAME_LENGTH_COMPLEX_SUB_SAMPLES - 4*length_seq_NPRACH+1; 
		fs_sub_sampled = (uint32_t)fs/128; 
129 130 131

	}else{

132 133 134 135
		length_seq_NPRACH = (length_CP_0+5*8192)/16; 
		length_CP = length_CP_0/16; 
		length_symbol = 8192/16;  

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
		offset_estimation = 8 * estimated_TA_coarse; 
		base_length = 32; 
		// we arbitrarily define the length of correl window as base_length samples. 
		// Check if offset_estimation is close to zero or 1282 (max lentgh of delays) 

		if (offset_estimation-base_length/2 <0){
			offset_start = 0; 
			length_correl_window = offset_estimation + base_length/2; 
		}
		if (offset_estimation+base_length/2 >1281){
			offset_start = offset_estimation-base_length/2; 
			length_correl_window = base_length;// 512 - (1282-offset_estimation); 
		}
		if ((offset_estimation-base_length/2 >=0) && (offset_estimation+base_length/2 <=1281)){
			offset_start = offset_estimation-base_length/2; 
			length_correl_window = base_length; 
		}
153 154
err
		fs_sub_sampled = (uint32_t)fs/16;
155 156 157
		
	}

158
	//fs_sub_sampled = (uint32_t)fs/sub_sampling_rate; 
159 160 161

	// Method: MMSE (sub-optimal) CFO estimation -> CFO compensation -> ML (sub-optimal) TA estimation /============================================================/

162 163 164 165 166 167 168 169
		//matrix_received_signal_re = (int16_t **)malloc(4*sizeof(int16_t *)); 
		//matrix_received_signal_im = (int16_t **)malloc(4*sizeof(int16_t *)); 
		// for (k=0;k<4;k++){ // # sequence
		// 	matrix_received_signal_re[k] = (int16_t *)malloc((length_seq_NPRACH-length_CP)*sizeof(int16_t)); // avoid CP in this process
		// 	matrix_received_signal_im[k] = (int16_t *)malloc((length_seq_NPRACH-length_CP)*sizeof(int16_t)); // avoid CP in this process
		// }
		signal_CFO_compensed_re = (int16_t *)malloc(4*length_seq_NPRACH*sizeof(int16_t));   /////to do : exact size of tables 
		signal_CFO_compensed_im = (int16_t *)malloc(4*length_seq_NPRACH*sizeof(int16_t)); 
170 171 172 173 174 175 176 177
		sub_sequence_reference_re = (int32_t **)malloc(4*sizeof(int32_t *));  
		sub_sequence_reference_im = (int32_t **)malloc(4*sizeof(int32_t *)); 
		for (k=0;k<4;k++){
			sub_sequence_reference_re[k] = (int32_t *)calloc(length_symbol,sizeof(int32_t)); 
			sub_sequence_reference_im[k] = (int32_t *)calloc(length_symbol,sizeof(int32_t)); 
		} 
		sequence_reference_re = (int32_t *)malloc(4*length_seq_NPRACH*sizeof(int32_t)); 
		sequence_reference_im = (int32_t *)malloc(4*length_seq_NPRACH*sizeof(int32_t)); 		
178
		vec_correlation = (int64_t *)calloc(length_correl_window,sizeof(int64_t));  
179 180 181 182 183 184 185 186

	for (n=0;n<length_correl_window;n++){ // loops over samples %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

		// MMSE (sub-optimal) CFO estimation /============================================================/ 
		average_mat_to_phase_re = 0; 
		average_mat_to_phase_im = 0; 
		for (k=0;k<4;k++){ // # sequence
			for (o=0;o<4;o++){ // # symbol in sequence
187 188 189
				for (m=0;m<length_symbol;m++){        ///// creation of two variables for tab indexes "n+offset_start+k*length_seq_NPRACH+length_CP+o*length_symbol+m"
					index_av_ph1 = (n+offset_start+k*length_seq_NPRACH+length_CP+o*length_symbol+m)<<1; 
					index_av_ph2 = index_av_ph1 + (length_symbol<<1);
190
					average_mat_to_phase_re = average_mat_to_phase_re 
191 192 193 194 195
												- (int64_t)(Rx_sub_sampled_buffer[index_av_ph1]
												* Rx_sub_sampled_buffer[index_av_ph2])
												- (int64_t)(Rx_sub_sampled_buffer[index_av_ph1+1]
												* Rx_sub_sampled_buffer[index_av_ph2+1]);

196
					average_mat_to_phase_im = average_mat_to_phase_im
197 198 199 200
												- (int64_t)(Rx_sub_sampled_buffer[index_av_ph1+1]
												* Rx_sub_sampled_buffer[index_av_ph2])
												+ (int64_t)(Rx_sub_sampled_buffer[index_av_ph1]
												* Rx_sub_sampled_buffer[index_av_ph2+1]);
201 202 203 204 205 206
				}
			} 
		} 

		average_mat_to_phase_re = average_mat_to_phase_re/(16*length_symbol); 
		average_mat_to_phase_im = average_mat_to_phase_im/(16*length_symbol); 
207 208 209
		estimated_phase = atan2f(average_mat_to_phase_im,average_mat_to_phase_re); 
		estimated_CFO = ((float)fs*estimated_phase)/(8192*2*(float)M_PI); 
		CFO_correction = 2*(float)M_PI*estimated_CFO/fs_sub_sampled;
210 211
		// CFO compensation /============================================================/ 

212 213 214 215 216 217 218
		for (k=0;k<4*length_seq_NPRACH;k++){     ///// creation of two variables for tab indexes /// replace "2*(float)M_PI*estimated_CFO*k/fs_sub_sampled" and "2*(n+offset_start+k)"
			CFO_correction_k = (float)k*CFO_correction;
		    signal_CFO_compensed_re[k] = (int16_t)((Rx_sub_sampled_buffer[(n+offset_start+k)<<1] * (int32_t)(cosf(CFO_correction_k)*32767) 
							- Rx_sub_sampled_buffer[((n+offset_start+k)<<1)+1] * (int32_t)(sinf(CFO_correction_k)*32767))>>15);
		    signal_CFO_compensed_im[k] = (int16_t)((Rx_sub_sampled_buffer[(n+offset_start+k)<<1] * (int32_t)(sinf(CFO_correction_k)*32767) 
							+ Rx_sub_sampled_buffer[((n+offset_start+k)<<1)+1] * (int32_t)(cosf(CFO_correction_k)*32767))>>15);
	
219 220 221 222
		} 

		// sub-optimal ML TA estimation /============================================================/ 
 
223 224 225
		
		for (k=0;k<4;k++){ // loop over the 4 sequences of a preamble 
			pow_n1 = 1;
226 227
			for (o=0;o<5;o++){ // loop over the symbols of a sequence 
				for (m=0;m<length_symbol;m++){
228 229 230
                         //  mon_variable=k*length_seq_NPRACH + o*length_symbol + length_CP + m ///////////////////////////////////////////////////////////////////////////////////////////////
				    sub_sequence_reference_re[k][m] = sub_sequence_reference_re[k][m] +  pow_n1 * signal_CFO_compensed_re[k*length_seq_NPRACH + o*length_symbol + length_CP + m] / 5; // average over the 5 symbols of a group
				    sub_sequence_reference_im[k][m] = sub_sequence_reference_im[k][m] +  pow_n1 * signal_CFO_compensed_im [k*length_seq_NPRACH + o*length_symbol + length_CP + m]/ 5; // average over the 5 symbols of a group
231
				}
232
				pow_n1 = -pow_n1;
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

		pow_n1 = 1;
		for (k=0;k<4;k++){ // loop over the 4 sequences of a preamble 
			pow_n1 = 1;
			for (o=0;o<5;o++){ // loop over the symbols of a sequence   //  mon_variable=k*length_seq_NPRACH+o*length_symbol +length_CP +m///////////////////////////////////////////////
				for (m=0;m<length_symbol;m++){
				    sequence_reference_re[k*length_seq_NPRACH+o*length_symbol +length_CP +m] = pow_n1 * sub_sequence_reference_re[k][m]; 
				    sequence_reference_im[k*length_seq_NPRACH+o*length_symbol +length_CP +m] = pow_n1 * sub_sequence_reference_im[k][m];
				}
				pow_n1 = -pow_n1;
			}
		}
		for (k=0;k<4;k++){ // loop over the 4 sequences of a preamble 
			for (m=0;m<length_CP;m++){
				sequence_reference_re[k*length_seq_NPRACH+m] = -sub_sequence_reference_re[k][length_symbol-length_CP+m]; 
				sequence_reference_im[k*length_seq_NPRACH+m] = -sub_sequence_reference_im[k][length_symbol-length_CP+m]; 
			}
		} 

		// for (m=0;m<length_seq_NPRACH;m++){
		// 	vec_correlation[n] = vec_correlation[n] + (double)signal_CFO_compensed_re[m] * sequence_reference_re[m] + (double)signal_CFO_compensed_im[m] * sequence_reference_im[m];  
		// 	printf("seq=%i\n",sequence_reference_re[m]); 
		// }
		
		for (m=0;m<4*length_seq_NPRACH;m++){
			A = (int64_t)((signal_CFO_compensed_re[m] * sequence_reference_re[m] 
				+ signal_CFO_compensed_im[m] * sequence_reference_im[m])); 
			//B = -(int32_t)(((int64_t)signal_CFO_compensed_re[m] * (int64_t)sequence_reference_im[m] 
			//	- (int64_t)signal_CFO_compensed_im[m] * (int64_t)sequence_reference_re[m])>>32); 
			vec_correlation[n] = vec_correlation[n] + A;//(int32_t)(((int64_t)A*(int64_t)A + 2*(int64_t)B*(int64_t)B)>>32);
		}

		for (k=0;k<4;k++){ // re-initialize sub_sequence_reference matrices   ////////////////////////////////////////////
268 269 270 271 272 273 274 275
			for (m=0;m<length_symbol;m++){ 
				sub_sequence_reference_re[k][m] = 0; 
				sub_sequence_reference_im[k][m] = 0; 
			}
		} 

	} 
	for (n=0;n<length_correl_window;n++){ 
276
		//printf("\ncorr=%li \n",vec_correlation[n]);
277 278
		if(vec_correlation[n]>=max_correlation){ 
			max_correlation = vec_correlation[n]; 
279
			TA_sample_estimated = offset_start+ n; 
280 281 282
		}
	}

283
	free(vec_correlation);       
284
	for (k=0;k<4;k++){ // # sequence
285 286
		//free(matrix_received_signal_re[k]); 
		err//free(matrix_received_signal_im[k]); 
287 288 289
		free(sub_sequence_reference_re[k]); 
		free(sub_sequence_reference_im[k]); 
	} 
290 291
	//free(matrix_received_signal_re); 
	//free(matrix_received_signal_im); 
292 293 294 295 296
	free(signal_CFO_compensed_re); 
	free(signal_CFO_compensed_im); 
	free(sub_sequence_reference_re); 
	free(sub_sequence_reference_im); 

297 298
	return TA_sample_estimated; 

Matthieu Kanj's avatar
Matthieu Kanj committed
299 300 301 302 303
} */ 


uint16_t subcarrier_estimation(int16_t *input_buffer){
	
Matthieu Kanj's avatar
Matthieu Kanj committed
304
	uint16_t estimated_sc=0; 
Matthieu Kanj's avatar
Matthieu Kanj committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	int16_t *s_n_re, *s_n_im; 
	uint16_t k,m,n; 
	int64_t max_correl_sc_m = 0; 
	int64_t max_correl_sc_k = 0; 
	int64_t max_correl_sc_glob = 0; 
	int n_start_offset = 1920; // start at t=8 ms

	for (k=0;k<12;k++){
		s_n_re = &s_n_12_re[k*336]; 
		s_n_im = &s_n_12_im[k*336]; 

		for (m=0;m<20;m++){
			for (n=0;n<336;n++){
				max_correl_sc_m = max_correl_sc_m + 
							(int16_t)(((int32_t)input_buffer[(m<<1)+((n+n_start_offset)<<1)]*(int32_t)s_n_re[n] )>>15) 
							+ (int16_t)(((int32_t)input_buffer[(m<<1)+((n+n_start_offset)<<1)+1]*(int32_t)s_n_im[n])>>15);
			}

			if (max_correl_sc_m>max_correl_sc_k){
				max_correl_sc_k = max_correl_sc_m;
			}
			max_correl_sc_m = 0;
		}

		//printf("correl = %li\n",max_correl_sc_k);

		if (max_correl_sc_k>max_correl_sc_glob){
			max_correl_sc_glob = max_correl_sc_k; 
			estimated_sc = k; 
		}
		max_correl_sc_k = 0; 
	} 

	return estimated_sc;

}
341

342
int16_t* sub_sampling_NB_IoT(int16_t *input_buffer, uint32_t length_input, uint32_t *length_ouput, uint16_t sub_sampling_rate){  // void function ////// adding flag for switching between output_buffers 
343 344 345

	int k; 
	uint32_t L; 
346 347 348 349
	//int16_t *output_buffer;    
	int16_t *p_output_buffer;
	L = (uint32_t)(length_input / sub_sampling_rate);  
	*length_ouput = L;    ///// to remove
350

351
	
352 353

	for (k=0;k<L;k++){
354 355
		output_buffer[k<<1] = input_buffer[sub_sampling_rate*(k<<1)]; 
		output_buffer[(k<<1)+1] = input_buffer[sub_sampling_rate*(k<<1)+1]; 
356 357 358 359
	} 
	// for (k=0;k<2*L;k++){
	// 	 printf("%i\n",output_buffer[k]); 
	// }
360 361 362 363 364 365 366
p_output_buffer=output_buffer;
	return p_output_buffer;

} 

void filtering_signal(int16_t *input_buffer, int16_t *filtered_buffer, uint32_t FRAME_LENGTH_COMPLEX_SAMPLESx){

Matthieu Kanj's avatar
Matthieu Kanj committed
367 368
	int n;
	//int k; 
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	//float f_s_RB22 = 1807.5; 
	//float f_s = 7680; 
	//int16_t *signal_compensed_re, *signal_compensed_im; 
	int16_t *cos_x, *sin_x; 

	cos_x = cos_x_rb22; 
	sin_x = sin_x_rb22; 

	
	for (n=0;n<FRAME_LENGTH_COMPLEX_SAMPLESx;n++){

		signal_compensed_re[n] = (int16_t)((input_buffer[n<<1] * (int32_t)(cos_x[n])      
								+ input_buffer[(n<<1)+1] * (int32_t)(sin_x[n]))>>15); 
		signal_compensed_im[n] = (int16_t)((- input_buffer[n<<1] * (int32_t)(sin_x[n]) 
								+ input_buffer[(n<<1)+1] * (int32_t)(cos_x[n]))>>15); 
Matthieu Kanj's avatar
Matthieu Kanj committed
384 385 386 387 388
		
		filtered_buffer[n<<1] = signal_compensed_re[n]; 
		filtered_buffer[(n<<1)+1] = signal_compensed_im[n]; 


389
	}
390

Matthieu Kanj's avatar
Matthieu Kanj committed
391
	/*for (n=0;n<FRAME_LENGTH_COMPLEX_SAMPLESx-10;n++){
392 393 394 395 396 397 398 399 400 401 402
		if (n<20){
			for (k=-n;k<20;k++){
				filtered_buffer[n<<1] = filtered_buffer[n<<1] + (int16_t)(((int32_t)filter_xx[20+k]*(int32_t)signal_compensed_re[n+k])>>15); 
				filtered_buffer[(n<<1)+1] = filtered_buffer[(n<<1)+1] + (int16_t)(((int32_t)filter_xx[20+k]*(int32_t)signal_compensed_im[n+k])>>15); 
			}
		}else{
			for (k=-20;k<20;k++){
				filtered_buffer[n<<1] = filtered_buffer[n<<1] + (int16_t)(((int32_t)filter_xx[20+k]*(int32_t)signal_compensed_re[n+k])>>15); 
				filtered_buffer[(n<<1)+1] = filtered_buffer[(n<<1)+1] + (int16_t)(((int32_t)filter_xx[20+k]*(int32_t)signal_compensed_im[n+k])>>15); 
			}
		}
Matthieu Kanj's avatar
Matthieu Kanj committed
403
	}*/
404
	
405 406 407

}

408 409
uint32_t RX_NPRACH_NB_IoT(PHY_VARS_eNB *eNB, int frame){ 

410

Matthieu Kanj's avatar
Matthieu Kanj committed
411 412 413
	//uint32_t estimated_TA_coarse=0;  
	//uint32_t estimated_TA;
	int16_t *Rx_sub_sampled_buffer_128; //       *Rx_sub_sampled_buffer_16; 
414
	uint16_t sub_sampling_rate; //NB-IoT: to be defined somewhere
415
	uint32_t FRAME_LENGTH_COMPLEX_SAMPLESx; // NB-IoT: length of input buffer, to be defined somewhere 
416 417
	uint32_t FRAME_LENGTH_COMPLEX_SUB_SAMPLES; // Length of buffer after sub-sampling
	uint32_t *length_ouput; // Length of buffer after sub-sampling 
Matthieu Kanj's avatar
Matthieu Kanj committed
418
	// uint8_t coarse=1; // flag that indicate the level of TA estimation
419 420
	int16_t *Rx_buffer;
	//int16_t *filtered_buffer;
Matthieu Kanj's avatar
Matthieu Kanj committed
421
	//int n;
422 423 424 425 426
 	
	//// 1. Coarse TA estimation using sub sampling rate = 128, i.e. fs = 240 kHz  

	FRAME_LENGTH_COMPLEX_SAMPLESx = 10*eNB->frame_parms.samples_per_tti; 
	Rx_buffer = (int16_t*)&eNB->common_vars.rxdata[0][0][0]; // get the whole frame
427

Matthieu Kanj's avatar
Matthieu Kanj committed
428
        memcpy(&buffer_nprach[0],&Rx_buffer[0],307200);
429 430 431 432 433
	
	
	//filtered_buffer = (int16_t *)calloc(2*FRAME_LENGTH_COMPLEX_SAMPLESx,sizeof(int16_t));  // calcule du taille exacte du tableau 76800
        memset(filtered_buffer,0,307200);
	filtering_signal(buffer_nprach,filtered_buffer,FRAME_LENGTH_COMPLEX_SAMPLESx); 
434 435 436

	// Sub-sampling stage /============================================================/ 

437
	sub_sampling_rate = FRAME_LENGTH_COMPLEX_SAMPLESx/2400; // gives the sub-sampling rate leading to f=240 kHz
438
	length_ouput = &FRAME_LENGTH_COMPLEX_SUB_SAMPLES; 
439
	Rx_sub_sampled_buffer_128 = sub_sampling_NB_IoT(filtered_buffer,FRAME_LENGTH_COMPLEX_SAMPLESx,length_ouput, sub_sampling_rate);  
440 441 442

	// Detection and TA estimation stage  /============================================================/ 

443 444
	if (NPRACH_detection_NB_IoT(Rx_sub_sampled_buffer_128,*length_ouput)){
		
Matthieu Kanj's avatar
Matthieu Kanj committed
445
		
446
		/*estimated_TA_coarse = TA_estimation_NB_IoT(eNB, 
447 448 449 450 451 452
												   Rx_sub_sampled_buffer_128, 
												   sub_sampling_rate, 
												   FRAME_LENGTH_COMPLEX_SUB_SAMPLES, 
												   estimated_TA_coarse, 
												   coarse); 

453 454

		// 2. Fine TA estimation using sub sampling rate = 16, i.e. fs = 1.92 MHz  
455 456
	
		// Sub-sampling stage /============================================================/
Matthieu Kanj's avatar
Matthieu Kanj committed
457
		//// sub_sampling_rate = FRAME_LENGTH_COMPLEX_SAMPLESx/(2400*8); 
458
		Rx_sub_sampled_buffer_16 = sub_sampling_NB_IoT(filtered_buffer,FRAME_LENGTH_COMPLEX_SAMPLESx,length_ouput, sub_sampling_rate); 
459 460 461 462 463 464 465 466 467 468


		// Fine TA estimation stage  /============================================================/ 
		// start1 = clock();
		coarse = 0;
		estimated_TA = TA_estimation_NB_IoT(eNB, 
											Rx_sub_sampled_buffer_16, 
											sub_sampling_rate, 
											FRAME_LENGTH_COMPLEX_SUB_SAMPLES, 
											estimated_TA_coarse, 
Matthieu Kanj's avatar
Matthieu Kanj committed
469
											coarse); //
470 471
		// Needs to be stored in a variable in PHY_VARS_eNB_NB_IoT structure

472 473 474 475 476 477
		//for (n=0;n<FRAME_LENGTH_COMPLEX_SAMPLESx;n++){
			//printf("   buf%i= %i",n,Rx_sub_sampled_buffer_128[2*n]);
		//	fprintf(f," %i %i ",Rx_buffer[2*n],Rx_buffer[2*n+1]);
			//fprintf(f,"%i \n",Rx_buffer[2*n+1]);
		//}*/

Matthieu Kanj's avatar
Matthieu Kanj committed
478 479 480
		printf("\ndetection !!!   at frame %i \n",frame);
		eNB->preamble_index_NB_IoT = subcarrier_estimation(Rx_sub_sampled_buffer_128);    // c'est un uint16_t 
		printf("estimated subaccier = %i\n",eNB->preamble_index_NB_IoT);
481 482 483 484 485
		return 1;//estimated_TA;
	}else{

		return 0;
	}
486

487 488
// }
 return 0;
489 490
}