sim.h 18.1 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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
 */
ghaddab's avatar
ghaddab committed
21

22 23
#ifndef __SIMULATION_TOOLS_DEFS_H__
#define __SIMULATION_TOOLS_DEFS_H__
24
#include "PHY/defs_common.h"
25
#include <pthread.h>
26 27 28 29 30 31 32 33 34 35
/** @defgroup _numerical_ Useful Numerical Functions
 *@{
The present clause specifies several numerical functions for testing of digital communication systems.

-# Generation of Uniform Random Bits
-# Generation of Quantized Gaussian Random Variables
-# Generation of Floating-point Gaussian Random Variables
-# Generic Multipath Channel Generator

 * @defgroup _channel_ Multipath channel generator
36
 * @ingroup _numerical_
37 38 39 40 41 42
 * @{

*/

#define NB_SAMPLES_CHANNEL_OFFSET 4

43 44 45 46 47 48 49 50 51 52
typedef enum {
  UNSPECIFIED_MODID=0,
  RFSIMU_MODULEID=1
} channelmod_moduleid_t;
#define MODULEID_STR_INIT {"","rfsimulator"}

#define CHANMODEL_FREE_DELAY       1<<0
#define CHANMODEL_FREE_RSQRT_6     1<<1
#define CHANMODEL_FREE_RSQRT_NTAPS 1<<2
#define CHANMODEL_FREE_AMPS        1<<3
53 54
typedef struct {
  ///Number of tx antennas
55
  uint8_t nb_tx;
56
  ///Number of rx antennas
57
  uint8_t nb_rx;
58
  ///number of taps
59
  uint8_t nb_taps;
60
  ///linear amplitudes of taps
61 62 63 64 65
  double *amps;
  ///Delays of the taps in mus. length(delays)=nb_taps. Has to be between 0 and Td.
  double *delays;
  ///length of impulse response. should be set to 11+2*bw*t_max
  uint8_t channel_length;
66
  ///channel state vector. size(state) = nb_taps * (n_tx * n_rx);
67
  struct complex **a;
68
  ///interpolated (sample-spaced) channel impulse response. size(ch) = (n_tx * n_rx) * channel_length. ATTENTION: the dimensions of ch are the transposed ones of a. This is to allow the use of BLAS when applying the correlation matrices to the state.
69
  struct complex **ch;
70
  ///Sampled frequency response (90 kHz resolution)
71
  struct complex **chF;
72
  ///Maximum path delay in mus.
73
  double Td;
74
  ///Channel bandwidth in MHz.
Florian Kaltenberger's avatar
Florian Kaltenberger committed
75 76 77
  double channel_bandwidth;
  ///System sampling rate in Msps.
  double sampling_rate;
78
  ///Ricean factor of first tap wrt other taps (0..1, where 0 means AWGN and 1 means Rayleigh channel).
79 80 81 82
  double ricean_factor;
  ///Angle of arrival of wavefront (in radians). For Ricean channel only. This assumes that both RX and TX have linear antenna arrays with lambda/2 antenna spacing. Furhter it is assumed that the arrays are parallel to each other and that they are far enough apart so that we can safely assume plane wave propagation.
  double aoa;
  ///If set to 1, aoa is randomized according to a uniform random distribution
83
  int8_t random_aoa;
84
  ///in Hz. if >0 generate a channel with a Clarke's Doppler profile with a maximum Doppler bandwidth max_Doppler. CURRENTLY NOT IMPLEMENTED!
85
  double max_Doppler;
86
  ///Square root of the full correlation matrix size(R_tx) = nb_taps * (n_tx * n_rx) * (n_tx * n_rx).
87 88
  struct complex **R_sqrt;
  ///path loss including shadow fading in dB
89
  double path_loss_dB;
90 91
  ///additional delay of channel in samples.
  int32_t channel_offset;
92 93 94
  ///This parameter (0...1) allows for simple 1st order temporal variation. 0 means a new channel every call, 1 means keep channel constant all the time
  double forgetting_factor;
  ///needs to be set to 1 for the first call, 0 otherwise.
95
  uint8_t first_run;
96
  /// initial phase for frequency offset simulation
97 98
  double ip;
  /// number of paths taken by transmit signal
99
  uint16_t nb_paths;
100 101 102 103 104
  /// timing measurements
  time_stats_t random_channel;
  time_stats_t interp_time;
  time_stats_t interp_freq;
  time_stats_t convolution;
105 106 107 108
  /// index in the channel descriptors array
  unsigned int chan_idx;
  /// id of the channel modeling algorithm
  int modelid;
109 110 111 112
  /// identifies channel descriptor owner (the module which created this descriptor)
  channelmod_moduleid_t module_id;
  /// flags to properly trigger memory free
  unsigned int free_flags;
113 114 115 116
} channel_desc_t;

typedef struct {
  /// Number of sectors (set to 1 in case of an omnidirectional antenna)
117
  uint8_t n_sectors;
118 119
  /// Antenna orientation for each sector (for non-omnidirectional antennas) in radians wrt north
  double alpha_rad[3];
120
  /// Antenna 3dB beam width (in radians)
121 122 123 124 125 126 127 128
  double phi_rad;
  /// Antenna gain (dBi)
  double ant_gain_dBi;
  /// Tx power (dBm)
  double tx_power_dBm;
  /// Rx noise level (dB)
  double rx_noise_level;
  ///x coordinate (cartesian, in m)
129
  double x;
130
  ///y coordinate (cartesian, in m)
131
  double y;
132 133 134 135 136 137 138 139 140 141 142 143 144
  ///z coordinate (antenna height, in m)
  double z;
  /// direction of travel in radians wrt north
  double direction_rad;
  /// speed of node (m/s)
  double speed;
} node_desc_t;

typedef enum {
  rural=0,
  urban,
  indoor
} scenario_t;
145

146 147 148 149 150 151 152 153 154
typedef struct {
  /// Scenario classifcation
  scenario_t scenario;
  /// Carrier frequency in Hz
  double carrier_frequency;
  /// Bandwidth (in Hz)
  double bandwidth;
  /// path loss at 0m distance in dB
  double path_loss_0;
155
  /// path loss exponent
156 157 158
  double path_loss_exponent;
  /// shadow fading standard deviation [dB] (assuming log-normal shadow fading with 0 mean)
  double shadow_fading_std;
159
  /// correlation distance of shadow fading
160 161 162 163 164 165 166 167
  double shadow_fading_correlation_distance;
  /// Shadowing correlation between cells
  double shadow_fading_correlation_cells;
  /// Shadowing correlation between sectors
  double shadow_fading_correlation_sectors;
  /// Rice factor???
  /// Walls (penetration loss)
  /// Nodes in the scenario
168
  node_desc_t *nodes;
169 170 171
} scenario_desc_t;

typedef enum {
172
  custom=0,
173 174 175 176 177 178 179 180
  SCM_A,
  SCM_B,
  SCM_C,
  SCM_D,
  EPA,
  EVA,
  ETU,
  MBSFN,
181 182 183 184 185
  TDL_A,
  TDL_B,
  TDL_C,
  TDL_D,
  TDL_E,
186 187 188 189 190 191 192 193 194
  Rayleigh8,
  Rayleigh1,
  Rayleigh1_800,
  Rayleigh1_corr,
  Rayleigh1_anticorr,
  Rice8,
  Rice1,
  Rice1_corr,
  Rice1_anticorr,
195 196 197 198 199 200 201
  AWGN,
  Rayleigh1_orthogonal,
  Rayleigh1_orth_eff_ch_TM4_prec_real,
  Rayleigh1_orth_eff_ch_TM4_prec_imag,
  Rayleigh8_orth_eff_ch_TM4_prec_real,
  Rayleigh8_orth_eff_ch_TM4_prec_imag,
  TS_SHIFT,
202 203 204
  EPA_low,
  EPA_medium,
  EPA_high,
205
} SCM_t;
206 207 208 209 210 211 212 213 214 215
#define CHANNELMOD_MAP_INIT \
  {"custom",custom},\
  {"SCM_A",SCM_A},\
  {"SCM_B",SCM_B},\
  {"SCM_C",SCM_C},\
  {"SCM_D",SCM_D},\
  {"EPA",EPA},\
  {"EVA",EVA},\
  {"ETU",ETU},\
  {"MBSFN",MBSFN},\
216 217 218 219 220
  {"TDL_A",TDL_A},\
  {"TDL_B",TDL_B},\
  {"TDL_C",TDL_C},\
  {"TDL_D",TDL_D},\
  {"TDL_E",TDL_E},\
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  {"Rayleigh8",Rayleigh8},\
  {"Rayleigh1",Rayleigh1},\
  {"Rayleigh1_800",Rayleigh1_800},\
  {"Rayleigh1_corr",Rayleigh1_corr},\
  {"Rayleigh1_anticorr",Rayleigh1_anticorr},\
  {"Rice8",Rice8},\
  {"Rice1",Rice1},\
  {"Rice1_corr",Rice1_corr},\
  {"Rice1_anticorr",Rice1_anticorr},\
  {"AWGN",AWGN},\
  {"Rayleigh1_orthogonal",Rayleigh1_orthogonal},\
  {"Rayleigh1_orth_eff_ch_TM4_prec_real",Rayleigh1_orth_eff_ch_TM4_prec_real},\
  {"Rayleigh1_orth_eff_ch_TM4_prec_imag",Rayleigh1_orth_eff_ch_TM4_prec_imag},\
  {"Rayleigh8_orth_eff_ch_TM4_prec_real",Rayleigh8_orth_eff_ch_TM4_prec_real},\
  {"Rayleigh8_orth_eff_ch_TM4_prec_imag",Rayleigh8_orth_eff_ch_TM4_prec_imag},\
  {"TS_SHIFT",TS_SHIFT},\
  {"EPA_low",EPA_low},\
  {"EPA_medium",EPA_medium},\
  {"EPA_high",EPA_high},\
  {NULL, -1}
241

yilmazt's avatar
yilmazt committed
242
#define CONFIG_HLP_SNR     "Set average SNR in dB (for --siml1 option)\n"
243 244
#define CHANNELMOD_SECTION "channelmod"
#define CHANNELMOD_PARAMS_DESC {  \
245 246 247
    {"s"      , CONFIG_HLP_SNR,         PARAMFLAG_CMDLINE_NOPREFIXENABLED, dblptr:&snr_dB,    defdblval:25, TYPE_DOUBLE, 0},\
    {"sinr_dB", NULL,                   0                                , dblptr:&sinr_dB,   defdblval:0 , TYPE_DOUBLE, 0},\
    {"max_chan, CONFIG_HLP_MAX_CHAN",   0,                                 uptr:&max_chan,    defintval:10,  TYPE_UINT,   0},\
248
  }
249

250 251 252
#include "platform_constants.h"

typedef struct {
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  channel_desc_t *RU2UE[NUMBER_OF_RU_MAX][NUMBER_OF_UE_MAX][MAX_NUM_CCs];
  channel_desc_t *UE2RU[NUMBER_OF_UE_MAX][NUMBER_OF_RU_MAX][MAX_NUM_CCs];
  double r_re_DL[NUMBER_OF_UE_MAX][2][30720];
  double r_im_DL[NUMBER_OF_UE_MAX][2][30720];
  double r_re_UL[NUMBER_OF_eNB_MAX][2][30720];
  double r_im_UL[NUMBER_OF_eNB_MAX][2][30720];
  int RU_output_mask[NUMBER_OF_UE_MAX];
  int UE_output_mask[NUMBER_OF_RU_MAX];
  pthread_mutex_t RU_output_mutex[NUMBER_OF_UE_MAX];
  pthread_mutex_t UE_output_mutex[NUMBER_OF_RU_MAX];
  pthread_mutex_t subframe_mutex;
  int subframe_ru_mask;
  int subframe_UE_mask;
  openair0_timestamp current_ru_rx_timestamp[NUMBER_OF_RU_MAX][MAX_NUM_CCs];
  openair0_timestamp current_UE_rx_timestamp[MAX_MOBILES_PER_ENB][MAX_NUM_CCs];
  openair0_timestamp last_ru_rx_timestamp[NUMBER_OF_RU_MAX][MAX_NUM_CCs];
  openair0_timestamp last_UE_rx_timestamp[MAX_MOBILES_PER_ENB][MAX_NUM_CCs];
  double ru_amp[NUMBER_OF_RU_MAX];
  pthread_t rfsim_thread;
272 273 274
} sim_t;


275
channel_desc_t *new_channel_desc_scm(uint8_t nb_tx,
276 277
                                     uint8_t nb_rx,
                                     SCM_t channel_model,
278
				     double sampling_rate,
Florian Kaltenberger's avatar
Florian Kaltenberger committed
279
                                     double channel_bandwidth,
280
				     double TDL_DS,
281 282 283
                                     double forgetting_factor,
                                     int32_t channel_offset,
                                     double path_loss_dB);
284 285 286 287 288
/**
\brief free memory allocated for a model descriptor
\param ch points to the model, which cannot be used after calling this fuction
*/
void free_channel_desc_scm(channel_desc_t *ch);
289

290 291 292 293 294 295
/**
\brief This set the ownerid of a model descriptor, can be later used to check what module created a channel model
\param cdesc points to the model descriptor
\param module_id identifies the channel model. should be define as a macro in simu.h
*/
void set_channeldesc_owner(channel_desc_t *cdesc, channelmod_moduleid_t module_id);
296
/** \fn void random_channel(channel_desc_t *desc)
297
\brief This routine generates a random channel response (time domain) according to a tapped delay line model.
298 299
\param desc Pointer to the channel descriptor
*/
300
int random_channel(channel_desc_t *desc, uint8_t abstraction_flag);
301 302

/**\fn void multipath_channel(channel_desc_t *desc,
303 304 305 306
           double tx_sig_re[2],
           double tx_sig_im[2],
           double rx_sig_re[2],
           double rx_sig_im[2],
307
           uint32_t length,
308 309
           uint8_t keep_channel,
	   int log_channel)
310 311 312

\brief This function generates and applys a random frequency selective random channel model.
@param desc Pointer to channel descriptor
313 314
@param tx_sig_re input signal (real component)
@param tx_sig_im input signal (imaginary component)
315 316 317 318
@param rx_sig_re output signal (real component)
@param rx_sig_im output signal (imaginary component)
@param length Length of input signal
@param keep_channel Set to 1 to keep channel constant for null-B/F
319
@param log_channel=1 make channel coefficients come out for first sample of input
320 321 322
*/

void multipath_channel(channel_desc_t *desc,
323 324 325 326
                       double *tx_sig_re[2],
                       double *tx_sig_im[2],
                       double *rx_sig_re[2],
                       double *rx_sig_im[2],
327
                       uint32_t length,
328 329
                       uint8_t keep_channel,
		       int log_channel);
330 331
/*
\fn double compute_pbch_sinr(channel_desc_t *desc,
332 333 334 335 336
                             channel_desc_t *desc_i1,
           channel_desc_t *desc_i2,
           double snr_dB,double snr_i1_dB,
           double snr_i2_dB,
           uint16_t nb_rb)
337 338 339 340 341 342 343 344 345 346 347

\brief This function computes the average SINR over all frequency resources of the PBCH.  It is used for PHY abstraction of the PBCH BLER
@param desc Pointer to channel descriptor of eNB
@param desc Pointer to channel descriptor of interfering eNB 1
@param desc Pointer to channel descriptor of interfering eNB 2
@param snr_dB SNR of eNB
@param snr_i1_dB SNR of interfering eNB 1
@param snr_i2_dB SNR of interfering eNB 2
@param nb_rb Number of RBs in system
*/
double compute_pbch_sinr(channel_desc_t *desc,
348 349 350 351 352
                         channel_desc_t *desc_i1,
                         channel_desc_t *desc_i2,
                         double snr_dB,double snr_i1_dB,
                         double snr_i2_dB,
                         uint16_t nb_rb);
353 354

double compute_sinr(channel_desc_t *desc,
355 356 357 358 359
                    channel_desc_t *desc_i1,
                    channel_desc_t *desc_i2,
                    double snr_dB,double snr_i1_dB,
                    double snr_i2_dB,
                    uint16_t nb_rb);
360 361

double pbch_bler(double sinr);
362 363 364

void load_pbch_desc(FILE *pbch_file_fd);

365 366 367 368
/**@}*/

/**
 * @defgroup _taus_ Tausworthe Uniform Random Variable Generator
369
 * @ingroup _numerical_
370
 * @{
371
\fn unsigned int taus()
372
\brief Tausworthe Uniform Random Generator.  This is based on the hardware implementation described in
373 374
  Lee et al, "A Hardware Gaussian Noise Generator Usign the Box-Muller Method and its Error Analysis," IEEE Trans. on Computers, 2006.
*/
375
unsigned int taus(void);
376 377


378
/**
379 380 381 382 383 384 385
\fn void set_taus_seed(unsigned int seed_init)
\brief Sets the seed for the Tausworthe generator.
@param seed_init 0 means generate based on CPU time, otherwise provide the seed
*/
void set_taus_seed(unsigned int seed_init);
/**@} */

386
/** @defgroup _gauss_ Generation of Quantized Gaussian Random Variables
387 388
 * @ingroup _numerical_
 * @{
389 390 391
This set of routines are used to generate quantized (i.e. fixed-point) Gaussian random noise efficiently.
The use of these routines allows for rapid computer simulation of digital communication systems. The method
is based on a lookup-table of the quantized normal probability distribution.  The routines assume that the
392
continuous-valued Gaussian random-variable,\f$x\f$ is quantized
393 394 395 396 397 398 399
to \f$N\f$ bits over the interval \f$[-L\sigma,L\sigma)\f$ where \f$N\f$ and \f$L\f$ control the precision
and range of the quantization.  The
random variable, \f$l\in\{-2^{N-1},-2^{N-1}+1,\cdots,0,1,\cdots,2^{N-1}-1\}\f$ corresponds to the event,
\f$E_l =
\begin{cases}
x\in\left[-\infty,-L\sigma\right) & l=-2^{N-1}, \\
x\in\left[\frac{lL\sigma}{2^{N-1}},\frac{(l+1)L\sigma}{2^{N-1}}\right) & <l>-2^{N-1}, \\
400 401 402
x\in\left[L\sigma,\infty\right) & l>-2^{N-1},
\end{cases}\f$
which occurs with probability
403 404 405 406
\f$\Pr(E_l) =
\begin{cases}
\mathrm{erfc}(L) & l=-2^{N-1}, \\
\mathrm{erfc}(L) & l>-2^{N-1}, \\
407 408 409 410 411 412 413
\mathrm{erf}\left(\frac{lL}{2^{N-1}}\right) \mathrm{erfc}\left(\frac{(l-1)L}{2^{N-1}}\right)& l>-2^{N-1}.
\end{cases}\f$
*/


/** \fn unsigned int *generate_gauss_LUT(unsigned char Nbits,unsigned char L)
\brief This routine generates a Gaussian pdf lookup table (LUT).  The table has \f$2^{\mathrm{Nbits}-1}\f$ entries which represent
414
the right half of the pdf.  The data stored in position \f$i\f$ is actually the scaled cumulative probability distribution,
415 416 417 418 419 420 421 422
\f$2^{31}\mathrm{erf}\left(\frac{iL}{2^{N-1}}\right)\f$.  This represents the average number of times that the random variable
falls in the interval \f$\left[0,\frac{i}{2^{N-1}}\right)\f$.  This format allows for rapid conversion of uniform 32-bit
random variables to \f$N\f$-bit Gaussian random variables using binary search.
@see gauss
@param Nbits Number of bits for the output variable
@param L Number of standard deviations in range
*/
unsigned int *generate_gauss_LUT(unsigned char Nbits,unsigned char L);
423

424
/** \fn int gauss(unsigned int *gauss_LUT,unsigned char Nbits);
425 426 427 428 429
\brief This routine returns a zero-mean unit-variance Gaussian random variable.
 Given a 32-bit uniform random variable,
\f$\mathrm{u}\f$ (from \ref _taus_, we first extract the sign and then search in the monotonically increasing Gaussian LUT for
the two entries \f$(i,i+1)\f$ for which
\f$ 2^{31}\mathrm{erf}\left(\frac{i}{2^{Nbits-1}}\right) < |u| \leq 2^{31}\mathrm{erf}\left(\frac{i+1}{2^{Nbits-1}}\right) \f$ and assign
430 431 432 433
the value \f$\mathrm{sgn}(u)i\f$.  The search requires at most \f$Nbits-1\f$ comparisons.
@see generate_gauss_LUT
@see taus
@param gauss_LUT pointer to lookup-table
434
@param Nbits number of bits for output variable ( between 1 and 16)
435 436 437 438 439 440
*/
int gauss(unsigned int *gauss_LUT,unsigned char Nbits);

double gaussdouble(double,double);
void randominit(unsigned int seed_init);
double uniformrandom(void);
441 442
int freq_channel(channel_desc_t *desc,uint16_t nb_rb, int16_t n_samples);
int init_freq_channel(channel_desc_t *desc,uint16_t nb_rb,int16_t n_samples);
443
uint8_t multipath_channel_nosigconv(channel_desc_t *desc);
444
void multipath_tv_channel(channel_desc_t *desc,
445 446 447 448
                          double **tx_sig_re,
                          double **tx_sig_im,
                          double **rx_sig_re,
                          double **rx_sig_im,
449
                          uint32_t length,
450
                          uint8_t keep_channel);
451 452 453

/**@} */
/**@} */
Florian Kaltenberger's avatar
Florian Kaltenberger committed
454

455 456 457 458
int modelid_fromname(char *modelname);
double channelmod_get_snr_dB(void);
double channelmod_get_sinr_dB(void);
void init_channelmod(void) ;
459

Florian Kaltenberger's avatar
Florian Kaltenberger committed
460 461 462
double N_RB2sampling_rate(uint16_t N_RB);
double N_RB2channel_bandwidth(uint16_t N_RB);

463 464 465 466 467 468
/* Linear phase noise model */
/*!
  \brief This function produce phase noise and add to input signal
  \param ts Sampling time 
  \param *Re *Im Real and Imag part of the signal
*/
469 470 471 472
//look-up table for the sine (cosine) function
#define ResolSinCos 100
uint16_t LUTSin[ResolSinCos+1];
void InitSinLUT( void );
473 474
void phase_noise(double ts, int16_t * InRe, int16_t * InIm);

475 476 477
#include "targets/RT/USER/rfsim.h"

void do_DL_sig(sim_t *sim,
478 479 480
               uint16_t subframe,
               uint32_t offset,
               uint32_t length,
yilmazt's avatar
yilmazt committed
481 482
               uint8_t abstraction_flag,
               LTE_DL_FRAME_PARMS *ue_frame_parms,
483 484 485
               uint8_t UE_id,
               int CC_id);

486
void do_UL_sig(sim_t *sim,
yilmazt's avatar
yilmazt committed
487 488 489 490 491 492
               uint16_t subframe,
               uint8_t abstraction_flag,
               LTE_DL_FRAME_PARMS *frame_parms,
               uint32_t frame,
               int ru_id,
               uint8_t CC_id);
493

494
#endif