Пример #1
0
void srslte_scrambling_bytes(srslte_sequence_t *s, uint8_t *data, int len) {
  scrambling_b_word(s->c_bytes, data, len/8);  
  // Scramble last bits
  if (len%8) {
    uint8_t tmp_bits[8];
    srslte_bit_unpack_vector(&data[len/8], tmp_bits, len%8);
    scrambling_b(&s->c[8*(len/8)], tmp_bits, len%8);
    srslte_bit_pack_vector(tmp_bits, &data[len/8], len%8);
  }    
}
Пример #2
0
int main(int argc, char **argv) {
  int i;
  srslte_modem_table_t mod;
  uint8_t *input, *input_bytes, *output;
  cf_t *symbols, *symbols_bytes;
  float *llr, *llr2;

  parse_args(argc, argv);

  /* initialize objects */
  if (srslte_modem_table_lte(&mod, modulation)) {
    fprintf(stderr, "Error initializing modem table\n");
    exit(-1);
  }
  
  srslte_modem_table_bytes(&mod);

  /* check that num_bits is multiple of num_bits x symbol */
  if (num_bits % mod.nbits_x_symbol) {
    fprintf(stderr, "Error num_bits must be multiple of %d\n", mod.nbits_x_symbol);
    exit(-1);
  }

  /* allocate buffers */
  input = srslte_vec_malloc(sizeof(uint8_t) * num_bits);
  if (!input) {
    perror("malloc");
    exit(-1);
  }
  input_bytes = srslte_vec_malloc(sizeof(uint8_t) * num_bits/8);
  if (!input_bytes) {
    perror("malloc");
    exit(-1);
  }
  output = srslte_vec_malloc(sizeof(uint8_t) * num_bits);
  if (!output) {
    perror("malloc");
    exit(-1);
  }
  symbols = srslte_vec_malloc(sizeof(cf_t) * num_bits / mod.nbits_x_symbol);
  if (!symbols) {
    perror("malloc");
    exit(-1);
  }
  symbols_bytes = srslte_vec_malloc(sizeof(cf_t) * num_bits / mod.nbits_x_symbol);
  if (!symbols_bytes) {
    perror("malloc");
    exit(-1);
  }

  llr = srslte_vec_malloc(sizeof(float) * num_bits);
  if (!llr) {
    perror("malloc");
    exit(-1);
  }

  llr2 = srslte_vec_malloc(sizeof(float) * num_bits);
  if (!llr2) {
    perror("malloc");
    exit(-1);
  }

  /* generate random data */
  for (i=0;i<num_bits;i++) {
    input[i] = rand()%2;
  }

  /* modulate */
  struct timeval t[3];
  gettimeofday(&t[1], NULL);
  int ntrials = 100; 
  for (int i=0;i<ntrials;i++) {
    srslte_mod_modulate(&mod, input, symbols, num_bits);
  }
  gettimeofday(&t[2], NULL);
  get_time_interval(t);
  
  printf("Bit: %d us\n", t[0].tv_usec);
  
  /* Test packed implementation */
  srslte_bit_pack_vector(input, input_bytes, num_bits);
  gettimeofday(&t[1], NULL);
  for (int i=0;i<ntrials;i++) {
    srslte_mod_modulate_bytes(&mod, input_bytes, symbols_bytes, num_bits);
  }
  gettimeofday(&t[2], NULL);
  get_time_interval(t);
  
  printf("Byte: %d us\n", t[0].tv_usec);
  
  for (int i=0;i<num_bits/mod.nbits_x_symbol;i++) {
    if (symbols[i] != symbols_bytes[i]) {
      printf("error in symbol %d\n", i);
      exit(-1);
    }
  }
  printf("Symbols OK\n");  
  /* demodulate */
  gettimeofday(&x, NULL);
  srslte_demod_soft_demodulate(modulation, symbols, llr, num_bits / mod.nbits_x_symbol);
  gettimeofday(&y, NULL);
  printf("\nElapsed time [ns]: %d\n", (int) y.tv_usec - (int) x.tv_usec);
  
  for (i=0;i<num_bits;i++) {
    output[i] = llr[i]>=0 ? 1 : 0;
  }

  /* check errors */
  for (i=0;i<num_bits;i++) {
    if (input[i] != output[i]) {
      fprintf(stderr, "Error in bit %d\n", i);
      exit(-1);
    }
  }

  free(llr);
  free(symbols);
  free(symbols_bytes);
  free(output);
  free(input);
  free(input_bytes);

  srslte_modem_table_free(&mod);

  printf("Ok\n");
  exit(0);
}
Пример #3
0
/* Encode a transport block according to 36.212 5.3.2
 *
 */
static int encode_tb(srslte_sch_t *q, 
                     srslte_softbuffer_tx_t *soft_buffer, srslte_cbsegm_t *cb_segm, 
                     uint32_t Qm, uint32_t rv, uint32_t nof_e_bits,  
                     uint8_t *data, uint8_t *e_bits) 
{
  uint8_t parity[3] = {0, 0, 0};
  uint32_t par;
  uint32_t i;
  uint32_t cb_len, rp, wp, rlen, F, n_e;
  int ret = SRSLTE_ERROR_INVALID_INPUTS; 
  
  if (q            != NULL &&
      e_bits       != NULL &&
      cb_segm      != NULL &&
      soft_buffer  != NULL)
  {
  
    uint32_t Gp = nof_e_bits / Qm;
    
    uint32_t gamma = Gp;
    if (cb_segm->C > 0) {
      gamma = Gp%cb_segm->C;
    }

    if (data) {

      /* Compute transport block CRC */
      par = srslte_crc_checksum_byte(&q->crc_tb, data, cb_segm->tbs);

      /* parity bits will be appended later */
      parity[0] = (par&(0xff<<16))>>16;
      parity[1] = (par&(0xff<<8))>>8;
      parity[2] = par&0xff;

      if (SRSLTE_VERBOSE_ISDEBUG()) {
        DEBUG("DATA: ", 0);
        srslte_vec_fprint_byte(stdout, data, cb_segm->tbs/8);
        DEBUG("PARITY: ", 0);
        srslte_vec_fprint_byte(stdout, parity, 3);
      }      
    }
    
    wp = 0;
    rp = 0;
    for (i = 0; i < cb_segm->C; i++) {

      /* Get read lengths */
      if (i < cb_segm->C2) {
        cb_len = cb_segm->K2;
      } else {
        cb_len = cb_segm->K1;
      }
      if (cb_segm->C > 1) {
        rlen = cb_len - 24;
      } else {
        rlen = cb_len;
      }
      if (i == 0) {
        F = cb_segm->F;
      } else {
        F = 0;
      }
      if (i <= cb_segm->C - gamma - 1) {
        n_e = Qm * (Gp/cb_segm->C);
      } else {
        n_e = Qm * ((uint32_t) ceilf((float) Gp/cb_segm->C));
      }

      INFO("CB#%d: cb_len: %d, rlen: %d, wp: %d, rp: %d, F: %d, E: %d\n", i,
          cb_len, rlen - F, wp, rp, F, n_e);

      if (data) {

        /* Copy data to another buffer, making space for the Codeblock CRC */
        if (i < cb_segm->C - 1) {
          // Copy data 
          memcpy(&q->cb_in[F/8], &data[rp/8], (rlen - F) * sizeof(uint8_t)/8);
        } else {
          INFO("Last CB, appending parity: %d from %d and 24 to %d\n",
              rlen - F - 24, rp, rlen - 24);
          
          /* Append Transport Block parity bits to the last CB */
          memcpy(&q->cb_in[F/8], &data[rp/8], (rlen - 24 - F) * sizeof(uint8_t)/8);
          memcpy(&q->cb_in[(rlen - 24)/8], parity, 3 * sizeof(uint8_t));
        }        
        
        /* Filler bits are treated like zeros for the CB CRC calculation */
        for (int j = 0; j < F/8; j++) {
          q->cb_in[j] = 0;
        }
        
        /* Attach Codeblock CRC */
        if (cb_segm->C > 1) {
          srslte_crc_attach_byte(&q->crc_cb, q->cb_in, rlen);
        }
        
        /* pack bits to temporal buffer for encoding */
        srslte_bit_pack_vector(q->cb_in, q->cb_temp, cb_len);

        /* Set the filler bits to <NULL> */
        for (int j = 0; j < F; j++) {
          q->cb_temp[j] = SRSLTE_TX_NULL;
        }
        
        if (SRSLTE_VERBOSE_ISDEBUG()) {
          DEBUG("CB#%d: ", i);
          srslte_vec_fprint_hex(stdout, q->cb_temp, cb_len);
        }
        
        /* Turbo Encoding */
        srslte_tcod_encode(&q->encoder, q->cb_temp, (uint8_t*) q->cb_out, cb_len);

        if (SRSLTE_VERBOSE_ISDEBUG()) {
          DEBUG("CB#%d encoded: ", i);
          srslte_vec_fprint_b(stdout, q->cb_out, 3*cb_len+12);
        }
      }
      
      /* Rate matching */
      if (srslte_rm_turbo_tx(soft_buffer->buffer_b[i], soft_buffer->buff_size, 
                  (uint8_t*) q->cb_out, 3 * cb_len + 12,
                  &e_bits[wp], n_e, rv))
      {
        fprintf(stderr, "Error in rate matching\n");
        return SRSLTE_ERROR;
      }

      /* Set read/write pointers */
      rp += (rlen - F);
      wp += n_e;
    }
    INFO("END CB#%d: wp: %d, rp: %d\n", i, wp, rp);
   
    ret = SRSLTE_SUCCESS;      
  } 
Пример #4
0
/* the gateway function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    srslte_sch_t ulsch;

    srslte_pusch_cfg_t cfg;
    srslte_softbuffer_tx_t softbuffer;
    srslte_uci_data_t uci_data;
    bzero(&uci_data, sizeof(srslte_uci_data_t));
    uint32_t rv;

    if (nrhs < NOF_INPUTS) {
        help();
        return;
    }
    bzero(&cfg, sizeof(srslte_pusch_cfg_t));

    if (srslte_sch_init(&ulsch)) {
        mexErrMsgTxt("Error initiating ULSCH\n");
        return;
    }
    srslte_cell_t cell;
    cell.nof_prb = 100;
    cell.id=1;
    cell.cp=SRSLTE_CP_NORM;

    srslte_verbose = SRSLTE_VERBOSE_NONE;

    if (srslte_softbuffer_tx_init(&softbuffer, cell.nof_prb)) {
        mexErrMsgTxt("Error initiating HARQ\n");
        return;
    }

    uint8_t *trblkin_bits = NULL;
    cfg.grant.mcs.tbs = mexutils_read_uint8(TRBLKIN, &trblkin_bits);

    uint8_t *trblkin = srslte_vec_malloc(cfg.grant.mcs.tbs/8);
    srslte_bit_pack_vector(trblkin_bits, trblkin, cfg.grant.mcs.tbs);
    free(trblkin_bits);


    uint8_t *tmp;
    uci_data.uci_cqi_len = mexutils_read_uint8(CQI, &tmp);
    memcpy(uci_data.uci_cqi, tmp, uci_data.uci_cqi_len);
    free(tmp);
    uci_data.uci_ri_len = mexutils_read_uint8(RI, &tmp);
    if (uci_data.uci_ri_len > 0) {
        uci_data.uci_ri = *tmp;
    }
    free(tmp);
    uci_data.uci_ack_len = mexutils_read_uint8(ACK, &tmp);
    if (uci_data.uci_ack_len > 0) {
        uci_data.uci_ack = *tmp;
    }
    free(tmp);

    mexPrintf("TRBL_len: %d, CQI_len: %d, ACK_len: %d, RI_len: %d\n", cfg.grant.mcs.tbs,
              uci_data.uci_cqi_len, uci_data.uci_ack_len, uci_data.uci_ri_len);

    if (mexutils_read_uint32_struct(PUSCHCFG, "RV", &rv)) {
        mexErrMsgTxt("Field RV not found in pdsch config\n");
        return;
    }

    float beta;
    if (mexutils_read_float_struct(PUSCHCFG, "BetaCQI", &beta)) {
        cfg.uci_cfg.I_offset_cqi = 7;
    } else {
        cfg.uci_cfg.I_offset_cqi = srslte_sch_find_Ioffset_cqi(beta);
    }
    if (mexutils_read_float_struct(PUSCHCFG, "BetaRI", &beta)) {
        cfg.uci_cfg.I_offset_ri = 2;
    } else {
        cfg.uci_cfg.I_offset_ri = srslte_sch_find_Ioffset_ri(beta);
    }
    if (mexutils_read_float_struct(PUSCHCFG, "BetaACK", &beta)) {
        cfg.uci_cfg.I_offset_ack = 0;
    } else {
        cfg.uci_cfg.I_offset_ack = srslte_sch_find_Ioffset_ack(beta);
    }

    char *mod_str = mexutils_get_char_struct(PUSCHCFG, "Modulation");

    if (!strcmp(mod_str, "QPSK")) {
        cfg.grant.mcs.mod = SRSLTE_MOD_QPSK;
    } else if (!strcmp(mod_str, "16QAM")) {
        cfg.grant.mcs.mod = SRSLTE_MOD_16QAM;
    } else if (!strcmp(mod_str, "64QAM")) {
        cfg.grant.mcs.mod = SRSLTE_MOD_64QAM;
    } else {
        mexErrMsgTxt("Unknown modulation\n");
        return;
    }

    mxFree(mod_str);

    float *prbset;
    mxArray *p;
    p = mxGetField(PUSCHCFG, 0, "PRBSet");
    if (!p) {
        mexErrMsgTxt("Error field PRBSet not found\n");
        return;
    }

    uint32_t N_srs = 0;
    mexutils_read_uint32_struct(PUSCHCFG, "Shortened", &N_srs);


    cfg.grant.L_prb = mexutils_read_f(p, &prbset);
    cfg.grant.n_prb[0] = prbset[0];
    cfg.grant.n_prb[1] = prbset[0];
    free(prbset);
    cfg.grant.L_prb = mexutils_read_f(p, &prbset);
    cfg.grant.n_prb[0] = prbset[0];
    cfg.grant.n_prb[1] = prbset[0];
    cfg.nbits.lstart = 0;
    cfg.nbits.nof_symb = 2*(SRSLTE_CP_NSYMB(cell.cp)-1) - N_srs;
    cfg.grant.M_sc = cfg.grant.L_prb*SRSLTE_NRE;
    cfg.grant.M_sc_init = cfg.grant.M_sc; // FIXME: What should M_sc_init be?
    cfg.nbits.nof_re = cfg.nbits.nof_symb*cfg.grant.M_sc;
    cfg.grant.Qm = srslte_mod_bits_x_symbol(cfg.grant.mcs.mod);
    cfg.nbits.nof_bits = cfg.nbits.nof_re * cfg.grant.Qm;

    mexPrintf("Q_m: %d, NPRB: %d, RV: %d, Nsrs=%d\n", srslte_mod_bits_x_symbol(cfg.grant.mcs.mod), cfg.grant.L_prb, cfg.rv, N_srs);

    mexPrintf("I_cqi: %d, I_ri: %d, I_ack=%d\n", cfg.uci_cfg.I_offset_cqi, cfg.uci_cfg.I_offset_ri, cfg.uci_cfg.I_offset_ack);

    if (srslte_cbsegm(&cfg.cb_segm, cfg.grant.mcs.tbs)) {
        mexErrMsgTxt("Error configuring HARQ process\n");
        return;
    }

    uint8_t *q_bits = srslte_vec_malloc(cfg.nbits.nof_bits * sizeof(uint8_t)/8);
    if (!q_bits) {
        return;
    }
    uint8_t *q_bits_unpacked = srslte_vec_malloc(cfg.nbits.nof_bits * sizeof(uint8_t));
    if (!q_bits_unpacked) {
        return;
    }
    uint8_t *g_bits = srslte_vec_malloc(cfg.nbits.nof_bits * sizeof(uint8_t)/8);
    if (!g_bits) {
        return;
    }

    if (srslte_ulsch_uci_encode(&ulsch, &cfg, &softbuffer, trblkin, uci_data, g_bits, q_bits))
    {
        mexErrMsgTxt("Error encoding TB\n");
        return;
    }
    if (rv > 0) {
        cfg.rv = rv;
        if (srslte_ulsch_uci_encode(&ulsch, &cfg, &softbuffer, trblkin, uci_data, g_bits, q_bits)) {
            mexErrMsgTxt("Error encoding TB\n");
            return;
        }
    }

    srslte_bit_unpack_vector(q_bits, q_bits_unpacked, cfg.nbits.nof_bits);

    if (nlhs >= 1) {
        mexutils_write_uint8(q_bits_unpacked, &plhs[0], cfg.nbits.nof_bits, 1);
    }

    srslte_sch_free(&ulsch);
    srslte_softbuffer_tx_free(&softbuffer);

    free(trblkin);
    free(g_bits);
    free(q_bits_unpacked);
    free(q_bits);


    return;
}
Пример #5
0
int main(int argc, char **argv) {
  int i;
  uint8_t *rm_bits, *rm_bits2, *rm_bits2_bytes;
  short *rm_bits_s; 
  float *rm_bits_f; 
  
  parse_args(argc, argv);
  
  srslte_rm_turbo_gentables();

  rm_bits_s = srslte_vec_malloc(sizeof(short) * nof_e_bits);
  if (!rm_bits_s) {
    perror("malloc");
    exit(-1);
  }
  rm_bits_f = srslte_vec_malloc(sizeof(float) * nof_e_bits);
  if (!rm_bits_f) {
    perror("malloc");
    exit(-1);
  }
  rm_bits = srslte_vec_malloc(sizeof(uint8_t) * nof_e_bits);
  if (!rm_bits) {
    perror("malloc");
    exit(-1);
  }
  rm_bits2 = malloc(sizeof(uint8_t) * nof_e_bits);
  if (!rm_bits2) {
    perror("malloc");
    exit(-1);
  }
  rm_bits2_bytes = malloc(sizeof(uint8_t) * nof_e_bits/8 + 1);
  if (!rm_bits2_bytes) {
    perror("malloc");
    exit(-1);
  }

  uint32_t st=0, end=188;
  if (cb_idx != -1) {
    st=cb_idx;
    end=cb_idx+1;
  }
  uint32_t rv_st=0, rv_end=4;
  if (rv_idx != -1) {
    rv_st=rv_idx;
    rv_end=rv_idx+1;
  }
  
  for (cb_idx=st;cb_idx<end;cb_idx++) {
    for (rv_idx=rv_st;rv_idx<rv_end;rv_idx++) {
      uint32_t long_cb_enc = 3*srslte_cbsegm_cbsize(cb_idx)+12;
      
      printf("checking cb_idx=%3d rv_idx=%d...", cb_idx, rv_idx);
      
      for (i = 0; i < long_cb_enc; i++) {
        bits[i] = rand() % 2;
      }
      
      bzero(buff_b, BUFFSZ * sizeof(uint8_t));
      
      srslte_rm_turbo_tx(buff_b, BUFFSZ, bits, long_cb_enc, rm_bits, nof_e_bits, 0);

      if (rv_idx > 0) {
        srslte_rm_turbo_tx(buff_b, BUFFSZ, bits, long_cb_enc, rm_bits, nof_e_bits, rv_idx);
      }

      for (int i=0;i<long_cb_enc/3;i++) {
        systematic[i] = bits[3*i];
        parity[i] = bits[3*i+1];
        parity[i+long_cb_enc/3] = bits[3*i+2];
      }
      
      srslte_bit_pack_vector(systematic, systematic_bytes, long_cb_enc/3);
      srslte_bit_pack_vector(parity, parity_bytes, 2*long_cb_enc/3);
      
      bzero(buff_b, BUFFSZ * sizeof(uint8_t));

      bzero(rm_bits2_bytes, nof_e_bits/8);
      srslte_rm_turbo_tx_lut(buff_b, systematic_bytes, parity_bytes, rm_bits2_bytes, cb_idx, nof_e_bits, 0, 0);
      if (rv_idx > 0) {
        bzero(rm_bits2_bytes, nof_e_bits/8);
        srslte_rm_turbo_tx_lut(buff_b, systematic_bytes, parity_bytes, rm_bits2_bytes, cb_idx, nof_e_bits, 0, rv_idx);
      }

      srslte_bit_unpack_vector(rm_bits2_bytes, rm_bits2, nof_e_bits);

      for (int i=0;i<nof_e_bits;i++) {
        if (rm_bits2[i] != rm_bits[i]) {
          printf("Error in TX bit %d\n", i);
          exit(-1);
        }
      }
      
      printf("OK TX...");
      
      for (int i=0;i<nof_e_bits;i++) {
        rm_bits_f[i] = rand()%10-5;
        rm_bits_s[i] = (short) rm_bits_f[i];
      }

      bzero(buff_f, BUFFSZ*sizeof(float));
      srslte_rm_turbo_rx(buff_f, BUFFSZ, rm_bits_f, nof_e_bits, bits_f, long_cb_enc, rv_idx, 0);

      bzero(bits2_s, long_cb_enc*sizeof(short));
      srslte_rm_turbo_rx_lut(rm_bits_s, bits2_s, nof_e_bits, cb_idx, rv_idx);

      for (int i=0;i<long_cb_enc;i++) {
        if (bits_f[i] != bits2_s[i]) {
          printf("error RX in bit %d %f!=%d\n", i, bits_f[i], bits2_s[i]);
          exit(-1);
        }
      }
    
      printf("OK RX\n");

    }
  }

  free(rm_bits);
  free(rm_bits2);
  free(rm_bits2_bytes);
  
  exit(0);
}