test.c 9.95 KB
Newer Older
ghaddab's avatar
ghaddab committed
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    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
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19 20 21 22 23
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

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
#include <string.h>
#include <math.h>
#include "SIMULATION/TOOLS/defs.h"
#include "SIMULATION/RF/defs.h"

//#define DEBUG_PHY
#define RF
#define IFFT_FPGA

#define BW 7.68

#define N_TRIALS 1

#define FRAME_LENGTH_COMPLEX_SAMPLES (lte_frame_parms->samples_per_tti>>1)
#define FRAME_LENGTH_BYTES (FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(int))

45 46
int main(int argc, char **argv)
{
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

  int i,l,aa,sector;
  double sigma2, sigma2_dB=0;
  mod_sym_t **txdataF;
#ifdef IFFT_FPGA
  int **txdataF2;
#endif
  int **txdata,**rxdata;
  double **s_re,**s_im,**r_re,**r_im;
  double amps[8] = {0.3868472 , 0.3094778 , 0.1547389 , 0.0773694 , 0.0386847 , 0.0193424 , 0.0096712 , 0.0038685};
  double aoa=.03,ricean_factor=1,Td=1.0;
  int channel_length;
  int amp;

  unsigned char pbch_pdu[6];
  int sync_pos, sync_pos_slot;
  FILE *rx_frame_file;
  int result;
  int freq_offset;
  int subframe_offset;
  char fname[40], vname[40];
  int trial, n_errors=0;
  unsigned int nb_rb = 25;
  unsigned int first_rb = 0;
  unsigned int eNb_id = 0;
  unsigned int slot_offset = 0;
  unsigned int sample_offset = 0;
  unsigned int channel_offset = 0;
  int n_frames;

  int slot=0,last_slot=0,next_slot=0;

  double nf[2] = {3.0,3.0}; //currently unused
  double ip =0.0;
  double N0W, path_loss, path_loss_dB, tx_pwr, rx_pwr;
  double rx_gain;
  int rx_pwr2, target_rx_pwr_dB;

  struct complex **ch;
  unsigned char first_call = 1;

  LTE_DL_FRAME_PARMS frame_parms;
  LTE_DL_FRAME_PARMS *lte_frame_parms = &frame_parms;

  if (argc==2)
    amp = atoi(argv[1]);
93
  else
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
    amp = 1024;

  // we normalize the tx power to 0dBm, assuming the amplitude of the signal is 1024
  // the SNR is this given by the difference of the path loss and the thermal noise (~-105dBm)
  // the rx_gain is adjusted automatically to achieve the target_rx_pwr_dB

  path_loss_dB = -90;
  path_loss    = pow(10,path_loss_dB/10);
  target_rx_pwr_dB = 60;

  lte_frame_parms->N_RB_DL            = 25;
  lte_frame_parms->N_RB_UL            = 25;
  lte_frame_parms->Ng_times6          = 1;
  lte_frame_parms->Ncp                = 1;
  lte_frame_parms->Nid_cell           = 0;
  lte_frame_parms->nushift            = 0;
  lte_frame_parms->nb_antennas_tx     = 2;
  lte_frame_parms->nb_antennas_rx     = 2;
  lte_frame_parms->first_dlsch_symbol = 4;
  lte_frame_parms->num_dlsch_symbols  = 6;
  lte_frame_parms->Csrs = 2;
  lte_frame_parms->Bsrs = 0;
  lte_frame_parms->kTC = 0;
  lte_frame_parms->n_RRC = 0;
  lte_frame_parms->mode1_flag = 1;
  lte_frame_parms->ofdm_symbol_size = 512;
  lte_frame_parms->log2_symbol_size = 9;
  lte_frame_parms->samples_per_tti = 7680;
  lte_frame_parms->first_carrier_offset = 362;
  lte_frame_parms->nb_prefix_samples>>=2;

#ifdef IFFT_FPGA
  txdata    = (int **)malloc16(2*sizeof(int*));
  txdata[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
  txdata[1] = (int *)malloc16(FRAME_LENGTH_BYTES);

  bzero(txdata[0],FRAME_LENGTH_BYTES);
  bzero(txdata[1],FRAME_LENGTH_BYTES);

  rxdata    = (int **)malloc16(2*sizeof(int*));
  rxdata[0] = (int *)malloc16(2*FRAME_LENGTH_BYTES);
  rxdata[1] = (int *)malloc16(2*FRAME_LENGTH_BYTES);

  bzero(rxdata[0],2*FRAME_LENGTH_BYTES);
  bzero(rxdata[1],2*FRAME_LENGTH_BYTES);

  txdataF2    = (int **)malloc16(2*sizeof(int*));
  txdataF2[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
  txdataF2[1] = (int *)malloc16(FRAME_LENGTH_BYTES);

  bzero(txdataF2[0],FRAME_LENGTH_BYTES);
  bzero(txdataF2[1],FRAME_LENGTH_BYTES);
#endif
147

148 149 150 151
  s_re = malloc(2*sizeof(double*));
  s_im = malloc(2*sizeof(double*));
  r_re = malloc(2*sizeof(double*));
  r_im = malloc(2*sizeof(double*));
152 153

  for (i=0; i<2; i++) {
154 155 156 157 158 159 160 161 162 163 164

    s_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
  }

165 166
  for (i=0; i<2; i++) {
    for (l=0; l<FRAME_LENGTH_COMPLEX_SAMPLES; l++) {
167 168 169 170
      ((short*) txdata[i])[2*l]   = amp * cos(M_PI/2*l);
      ((short*) txdata[i])[2*l+1] = amp * sin(M_PI/2*l);
    }
  }
171

172
  tx_pwr = signal_energy(txdata[0],lte_frame_parms->samples_per_tti>>1);
173 174
  printf("tx_pwr (DAC in) %d dB for slot %d (subframe %d)\n",dB_fixed(tx_pwr),next_slot,next_slot>>1);

175 176 177 178 179


  channel_length = (int) 11+2*BW*Td;

  ch = (struct complex**) malloc(4 * sizeof(struct complex*));
180

181 182 183 184 185 186 187
  for (i = 0; i<4; i++)
    ch[i] = (struct complex*) malloc(channel_length * sizeof(struct complex));

  randominit(0);
  set_taus_seed(0);

#ifdef RF
188 189 190 191 192 193 194 195 196
  tx_pwr = dac_fixed_gain(s_re,
                          s_im,
                          txdata,
                          lte_frame_parms->nb_antennas_tx,
                          lte_frame_parms->samples_per_tti>>1,
                          14,
                          18); //this should give 0dBm output level for input with amplitude 1024

  printf("tx_pwr (DAC out) %f dB for slot %d (subframe %d)\n",10*log10(tx_pwr),next_slot,next_slot>>1);
197 198
#else

199 200 201 202 203 204 205
  for (i=0; i<(lte_frame_parms->samples_per_tti>>1); i++) {
    for (aa=0; aa<lte_frame_parms->nb_antennas_tx; aa++) {
      s_re[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)]);
      s_im[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)+1]);
    }
  }

206 207
#endif

208 209 210 211 212 213 214 215 216 217 218 219 220
  //      printf("channel for slot %d (subframe %d)\n",next_slot,next_slot>>1);
  multipath_channel(ch,s_re,s_im,r_re,r_im,
                    amps,Td,BW,ricean_factor,aoa,
                    lte_frame_parms->nb_antennas_tx,
                    lte_frame_parms->nb_antennas_rx,
                    lte_frame_parms->samples_per_tti>>1,
                    channel_length,
                    0,
                    .9,
                    (first_call == 1) ? 1 : 0);

  if (first_call == 1)
    first_call = 0;
221 222

#ifdef RF
223 224 225 226 227 228 229 230 231 232 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

  //path_loss_dB = 0;
  //path_loss = 1;

  for (i=0; i<(lte_frame_parms->samples_per_tti>>1); i++) {
    for (aa=0; aa<lte_frame_parms->nb_antennas_rx; aa++) {
      r_re[aa][i]=r_re[aa][i]*sqrt(path_loss);
      r_im[aa][i]=r_im[aa][i]*sqrt(path_loss);

    }
  }

  rx_pwr = signal_energy_fp(r_re,r_im,lte_frame_parms->nb_antennas_rx,lte_frame_parms->samples_per_tti>>1,0);
  printf("rx_pwr (RF in) %f dB for slot %d (subframe %d)\n",10*log10(rx_pwr),next_slot,next_slot>>1);

  rx_gain = target_rx_pwr_dB - 10*log10(rx_pwr);

  // RF model
  rf_rx(r_re,
        r_im,
        NULL,
        NULL,
        0,
        lte_frame_parms->nb_antennas_rx,
        lte_frame_parms->samples_per_tti>>1,
        1.0/7.68e6 * 1e9,  // sampling time (ns)
        0.0,               // freq offset (Hz) (-20kHz..20kHz)
        0.0,               // drift (Hz) NOT YET IMPLEMENTED
        nf,                // noise_figure NOT YET IMPLEMENTED
        rx_gain-66.227,    // rx gain (66.227 = 20*log10(pow2(11)) = gain from the adc that will be applied later)
        200,               // IP3_dBm (dBm)
        &ip,               // initial phase
        30.0e3,            // pn_cutoff (kHz)
        -500.0,            // pn_amp (dBc) default: 50
        0.0,               // IQ imbalance (dB),
        0.0);              // IQ phase imbalance (rad)

  rx_pwr = signal_energy_fp(r_re,r_im,lte_frame_parms->nb_antennas_rx,lte_frame_parms->samples_per_tti>>1,0);

  printf("rx_pwr (ADC in) %f dB for slot %d (subframe %d)\n",10*log10(rx_pwr),next_slot,next_slot>>1);
263 264 265
#endif

#ifdef RF
266 267 268 269 270 271 272 273 274 275 276 277
  adc(r_re,
      r_im,
      0,
      slot_offset,
      rxdata,
      lte_frame_parms->nb_antennas_rx,
      lte_frame_parms->samples_per_tti>>1,
      12);

  rx_pwr2 = signal_energy(rxdata[0]+slot_offset,lte_frame_parms->samples_per_tti>>1);

  printf("rx_pwr (ADC out) %f dB (%d) for slot %d (subframe %d)\n",10*log10((double)rx_pwr2),rx_pwr2,next_slot,next_slot>>1);
278 279

#else
280 281 282 283 284 285 286 287 288

  for (i=0; i<(lte_frame_parms->samples_per_tti>>1); i++) {
    for (aa=0; aa<lte_frame_parms->nb_antennas_rx; aa++) {
      ((short*) rxdata[aa])[2*slot_offset + (2*i)]   = (short) ((r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0));
      ((short*) rxdata[aa])[2*slot_offset + (2*i)+1] = (short) ((r_im[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0));

    }
  }

289 290
#endif

291 292 293
  write_output("rxsig0.m","rxs0",rxdata[0],lte_frame_parms->samples_per_tti>>1,1,1);
  write_output("rxsig1.m","rxs1",rxdata[1],lte_frame_parms->samples_per_tti>>1,1,1);

294 295 296 297 298 299 300 301 302 303 304

#ifdef IFFT_FPGA
  free(txdataF2[0]);
  free(txdataF2[1]);
  free(txdataF2);
  free(txdata[0]);
  free(txdata[1]);
  free(txdata);
  free(rxdata[0]);
  free(rxdata[1]);
  free(rxdata);
305
#endif
306

307
  for (i=0; i<2; i++) {
308 309 310 311 312
    free(s_re[i]);
    free(s_im[i]);
    free(r_re[i]);
    free(r_im[i]);
  }
313

314 315 316 317
  free(s_re);
  free(s_im);
  free(r_re);
  free(r_im);
318

319 320
  return(0);
}