srsLTE/srslte/lib/phch/src/pusch.c

445 lines
14 KiB
C
Raw Normal View History

/**
*
* \section COPYRIGHT
*
* Copyright 2013-2014 The libLTE Developers. See the
* COPYRIGHT file at the top-level directory of this distribution.
*
* \section LICENSE
*
* This file is part of the libLTE library.
*
* libLTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libLTE 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 Lesser General Public License for more details.
*
* A copy of the GNU Lesser General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <math.h>
#include "srslte/phch/pusch.h"
#include "srslte/phch/uci.h"
#include "srslte/common/phy_common.h"
#include "srslte/utils/bit.h"
#include "srslte/utils/debug.h"
#include "srslte/utils/vector.h"
2015-03-18 11:14:24 -07:00
#include "srslte/dft/dft_precoding.h"
2015-03-18 05:59:29 -07:00
#define MAX_PUSCH_RE(cp) (2 * SRSLTE_CP_NSYMB(cp) * 12)
2015-03-18 05:59:29 -07:00
const static srslte_mod_t modulations[4] =
2015-03-18 11:14:24 -07:00
{ SRSLTE_MOD_BPSK, SRSLTE_MOD_QPSK, SRSLTE_MOD_16QAM, SRSLTE_MOD_64QAM };
2015-03-18 11:14:24 -07:00
static int f_hop_sum(srslte_pusch_t *q, uint32_t i) {
2015-02-15 23:55:17 -08:00
uint32_t sum = 0;
for (uint32_t k=i*10+1;k<i*10+9;i++) {
sum += (q->seq_type2_fo.c[k]<<(k-(i*10+1)));
}
return sum;
}
2015-03-18 11:14:24 -07:00
static int f_hop(srslte_pusch_t *q, srslte_pusch_hopping_cfg_t *hopping, int i) {
2015-02-15 23:55:17 -08:00
if (i == -1) {
return 0;
} else {
if (hopping->n_sb == 1) {
return 0;
} else if (hopping->n_sb == 2) {
return (f_hop(q, hopping, i-1) + f_hop_sum(q, i))%2;
} else {
return (f_hop(q, hopping, i-1) + f_hop_sum(q, i)%(hopping->n_sb-1)+1)%hopping->n_sb;
}
}
}
2015-03-18 11:14:24 -07:00
static int f_m(srslte_pusch_t *q, srslte_pusch_hopping_cfg_t *hopping, uint32_t i) {
2015-02-15 23:55:17 -08:00
if (hopping->n_sb == 1) {
2015-03-18 11:14:24 -07:00
if (hopping->hop_mode == SRSLTE_PUSCH_HOP_MODE_INTER_SF) {
2015-02-15 23:55:17 -08:00
return hopping->current_tx_nb%2;
} else {
return i%2;
}
} else {
return q->seq_type2_fo.c[i*10];
}
}
2015-03-18 11:14:24 -07:00
int pusch_cp(srslte_pusch_t *q, srslte_harq_t *harq, cf_t *input, cf_t *output, bool advance_input)
{
cf_t *in_ptr = input;
cf_t *out_ptr = output;
2015-03-18 11:14:24 -07:00
srslte_pusch_hopping_cfg_t *hopping = &q->hopping_cfg;
uint32_t L_ref = 3;
2015-03-18 05:59:29 -07:00
if (SRSLTE_CP_ISEXT(q->cell.cp)) {
L_ref = 2;
}
2015-02-15 23:55:17 -08:00
INFO("PUSCH Freq hopping: %d\n", harq->ul_alloc.freq_hopping);
for (uint32_t slot=0;slot<2;slot++) {
2015-02-15 23:55:17 -08:00
uint32_t n_prb_tilde = harq->ul_alloc.n_prb[slot];
if (harq->ul_alloc.freq_hopping == 1) {
2015-03-18 11:14:24 -07:00
if (hopping->hop_mode == SRSLTE_PUSCH_HOP_MODE_INTER_SF) {
2015-02-15 23:55:17 -08:00
n_prb_tilde = harq->ul_alloc.n_prb[hopping->current_tx_nb%2];
} else {
n_prb_tilde = harq->ul_alloc.n_prb[slot];
}
}
if (harq->ul_alloc.freq_hopping == 2) {
/* Freq hopping type 2 as defined in 5.3.4 of 36.211 */
uint32_t n_vrb_tilde = harq->ul_alloc.n_prb[0];
if (hopping->n_sb > 1) {
n_vrb_tilde -= (hopping->hopping_offset-1)/2+1;
}
int i=0;
2015-03-18 11:14:24 -07:00
if (hopping->hop_mode == SRSLTE_PUSCH_HOP_MODE_INTER_SF) {
2015-02-15 23:55:17 -08:00
i = harq->sf_idx;
} else {
i = 2*harq->sf_idx+slot;
}
uint32_t n_rb_sb = q->cell.nof_prb;
if (hopping->n_sb > 1) {
n_rb_sb = (n_rb_sb-hopping->hopping_offset-hopping->hopping_offset%2)/hopping->n_sb;
}
n_prb_tilde = (n_vrb_tilde+f_hop(q, hopping, i)*n_rb_sb+
(n_rb_sb-1)-2*(n_vrb_tilde%n_rb_sb)*f_m(q, hopping, i))%(n_rb_sb*hopping->n_sb);
2015-03-11 09:42:36 -07:00
INFO("n_prb_tilde: %d, n_vrb_tilde: %d, n_rb_sb: %d, n_sb: %d\n",
n_prb_tilde, n_vrb_tilde, n_rb_sb, hopping->n_sb);
2015-02-15 23:55:17 -08:00
if (hopping->n_sb > 1) {
n_prb_tilde += (hopping->hopping_offset-1)/2+1;
}
}
harq->ul_alloc.n_prb_tilde[slot] = n_prb_tilde;
2015-02-15 23:55:17 -08:00
INFO("Allocating PUSCH %d PRB to index %d at slot %d\n",harq->ul_alloc.L_prb, n_prb_tilde,slot);
2015-03-18 05:59:29 -07:00
for (uint32_t l=0;l<SRSLTE_CP_NSYMB(q->cell.cp);l++) {
if (l != L_ref) {
2015-03-18 05:59:29 -07:00
uint32_t idx = SRSLTE_RE_IDX(q->cell.nof_prb, l+slot*SRSLTE_CP_NSYMB(q->cell.cp),
n_prb_tilde*SRSLTE_NRE);
2015-02-15 23:55:17 -08:00
if (advance_input) {
out_ptr = &output[idx];
} else {
in_ptr = &input[idx];
}
2015-03-18 05:59:29 -07:00
memcpy(out_ptr, in_ptr, harq->ul_alloc.L_prb * SRSLTE_NRE * sizeof(cf_t));
2015-02-15 23:55:17 -08:00
if (advance_input) {
2015-03-18 05:59:29 -07:00
in_ptr += harq->ul_alloc.L_prb*SRSLTE_NRE;
2015-02-15 23:55:17 -08:00
} else {
2015-03-18 05:59:29 -07:00
out_ptr += harq->ul_alloc.L_prb*SRSLTE_NRE;
}
}
}
}
2015-03-18 05:59:29 -07:00
return SRSLTE_NRE*harq->ul_alloc.L_prb;
}
2015-03-18 11:14:24 -07:00
int pusch_put(srslte_pusch_t *q, srslte_harq_t *harq, cf_t *input, cf_t *output) {
2015-02-15 23:55:17 -08:00
return pusch_cp(q, harq, input, output, true);
}
2015-03-18 11:14:24 -07:00
int get(srslte_pusch_t *q, srslte_harq_t *harq, cf_t *input, cf_t *output) {
2015-02-15 23:55:17 -08:00
return pusch_cp(q, harq, input, output, false);
}
/** Initializes the PDCCH transmitter and receiver */
2015-03-18 11:14:24 -07:00
int srslte_pusch_init(srslte_pusch_t *q, srslte_cell_t cell) {
int ret = SRSLTE_ERROR_INVALID_INPUTS;
int i;
if (q != NULL &&
2015-03-18 05:59:29 -07:00
srslte_cell_isvalid(&cell))
{
2015-03-18 11:14:24 -07:00
bzero(q, sizeof(srslte_pusch_t));
ret = SRSLTE_ERROR;
q->cell = cell;
q->max_re = q->cell.nof_prb * MAX_PUSCH_RE(q->cell.cp);
INFO("Init PUSCH: %d ports %d PRBs, max_symbols: %d\n", q->cell.nof_ports,
q->cell.nof_prb, q->max_re);
for (i = 0; i < 4; i++) {
2015-03-18 11:14:24 -07:00
if (srslte_modem_table_lte(&q->mod[i], modulations[i], true)) {
goto clean;
}
}
2015-02-15 23:55:17 -08:00
/* Precompute sequence for type2 frequency hopping */
2015-03-18 08:05:38 -07:00
if (srslte_sequence_LTE_pr(&q->seq_type2_fo, 210, q->cell.id)) {
2015-02-15 23:55:17 -08:00
fprintf(stderr, "Error initiating type2 frequency hopping sequence\n");
goto clean;
}
2015-03-18 11:14:24 -07:00
srslte_demod_soft_init(&q->demod, q->max_re);
srslte_demod_soft_alg_set(&q->demod, SRSLTE_DEMOD_SOFT_ALG_APPROX);
2015-03-18 11:14:24 -07:00
srslte_sch_init(&q->dl_sch);
2015-03-18 11:14:24 -07:00
if (srslte_dft_precoding_init(&q->dft_precoding, cell.nof_prb)) {
fprintf(stderr, "Error initiating DFT transform precoding\n");
goto clean;
}
/* This is for equalization at receiver */
2015-03-18 11:14:24 -07:00
if (srslte_precoding_init(&q->equalizer, SRSLTE_SF_LEN_RE(cell.nof_prb, cell.cp))) {
fprintf(stderr, "Error initializing precoding\n");
goto clean;
}
q->rnti_is_set = false;
2015-02-11 23:57:37 -08:00
// Allocate floats for reception (LLRs). Buffer casted to uint8_t for transmission
2015-03-18 11:14:24 -07:00
q->q = srslte_vec_malloc(sizeof(float) * q->max_re * srslte_mod_bits_x_symbol(SRSLTE_MOD_64QAM));
if (!q->q) {
2015-02-11 23:57:37 -08:00
goto clean;
}
// Allocate floats for reception (LLRs). Buffer casted to uint8_t for transmission
2015-03-18 11:14:24 -07:00
q->g = srslte_vec_malloc(sizeof(float) * q->max_re * srslte_mod_bits_x_symbol(SRSLTE_MOD_64QAM));
if (!q->g) {
goto clean;
}
2015-03-18 11:14:24 -07:00
q->d = srslte_vec_malloc(sizeof(cf_t) * q->max_re);
if (!q->d) {
goto clean;
}
2015-03-18 11:14:24 -07:00
q->ce = srslte_vec_malloc(sizeof(cf_t) * q->max_re);
if (!q->ce) {
goto clean;
}
2015-03-18 11:14:24 -07:00
q->z = srslte_vec_malloc(sizeof(cf_t) * q->max_re);
if (!q->z) {
goto clean;
}
ret = SRSLTE_SUCCESS;
}
clean:
if (ret == SRSLTE_ERROR) {
2015-03-18 11:14:24 -07:00
srslte_pusch_free(q);
}
return ret;
}
2015-03-18 11:14:24 -07:00
void srslte_pusch_free(srslte_pusch_t *q) {
int i;
2015-03-18 11:14:24 -07:00
if (q->q) {
free(q->q);
}
2015-03-18 11:14:24 -07:00
if (q->d) {
free(q->d);
}
2015-03-18 11:14:24 -07:00
if (q->g) {
free(q->g);
2015-02-12 01:38:53 -08:00
}
if (q->ce) {
free(q->ce);
}
2015-03-18 11:14:24 -07:00
if (q->z) {
free(q->z);
}
2015-03-18 11:14:24 -07:00
srslte_dft_precoding_free(&q->dft_precoding);
2015-03-18 11:14:24 -07:00
srslte_precoding_free(&q->equalizer);
2015-03-18 05:41:50 -07:00
for (i = 0; i < SRSLTE_NSUBFRAMES_X_FRAME; i++) {
2015-03-18 11:14:24 -07:00
srslte_sequence_free(&q->seq[i]);
}
for (i = 0; i < 4; i++) {
2015-03-18 11:14:24 -07:00
srslte_modem_table_free(&q->mod[i]);
}
2015-03-18 11:14:24 -07:00
srslte_demod_soft_free(&q->demod);
srslte_sch_free(&q->dl_sch);
2015-03-18 11:14:24 -07:00
bzero(q, sizeof(srslte_pusch_t));
}
2015-03-18 11:14:24 -07:00
void srslte_pusch_set_hopping_cfg(srslte_pusch_t *q, srslte_pusch_hopping_cfg_t *cfg)
{
2015-03-18 11:14:24 -07:00
memcpy(&q->hopping_cfg, cfg, sizeof(srslte_pusch_hopping_cfg_t));
}
2015-03-02 02:11:44 -08:00
/* Precalculate the PUSCH scramble sequences for a given RNTI. This function takes a while
* to execute, so shall be called once the final C-RNTI has been allocated for the session.
2015-03-18 11:14:24 -07:00
* For the connection procedure, use srslte_pusch_encode_rnti() or srslte_pusch_decode_rnti() functions */
int srslte_pusch_set_rnti(srslte_pusch_t *q, uint16_t rnti) {
uint32_t i;
2015-03-18 05:41:50 -07:00
for (i = 0; i < SRSLTE_NSUBFRAMES_X_FRAME; i++) {
2015-03-18 11:14:24 -07:00
if (srslte_sequence_pusch(&q->seq[i], rnti, 2 * i, q->cell.id,
q->max_re * srslte_mod_bits_x_symbol(SRSLTE_MOD_64QAM))) {
return SRSLTE_ERROR;
}
}
q->rnti_is_set = true;
q->rnti = rnti;
return SRSLTE_SUCCESS;
}
/** Decodes the PUSCH from the received symbols
*/
2015-03-18 11:14:24 -07:00
int srslte_pusch_decode(srslte_pusch_t *q, srslte_harq_t *harq, cf_t *sf_symbols, cf_t *ce, float noise_estimate, uint8_t *data)
{
uint32_t n;
if (q != NULL &&
sf_symbols != NULL &&
data != NULL &&
harq != NULL)
{
if (q->rnti_is_set) {
INFO("Decoding PUSCH SF: %d, Mod %s, NofBits: %d, NofSymbols: %d, NofBitsE: %d, rv_idx: %d\n",
2015-03-18 05:59:29 -07:00
harq->sf_idx, srslte_mod_string(harq->mcs.mod), harq->mcs.tbs, harq->nof_re, harq->nof_bits, harq->rv);
/* extract symbols */
2015-03-18 11:14:24 -07:00
n = get(q, harq, sf_symbols, q->d);
if (n != harq->nof_re) {
fprintf(stderr, "Error expecting %d symbols but got %d\n", harq->nof_re, n);
return SRSLTE_ERROR;
}
/* extract channel estimates */
2015-03-18 11:14:24 -07:00
n = get(q, harq, ce, q->ce);
if (n != harq->nof_re) {
fprintf(stderr, "Error expecting %d symbols but got %d\n", harq->nof_re, n);
return SRSLTE_ERROR;
}
2015-03-18 11:14:24 -07:00
srslte_predecoding_single(&q->equalizer, q->d, q->ce, q->z,
harq->nof_re, noise_estimate);
2015-03-18 11:14:24 -07:00
srslte_dft_predecoding(&q->dft_precoding, q->z, q->d,
2015-02-15 23:55:17 -08:00
harq->ul_alloc.L_prb, harq->nof_symb);
/* demodulate symbols
* The MAX-log-MAP algorithm used in turbo decoding is unsensitive to SNR estimation,
* thus we don't need tot set it in the LLRs normalization
*/
2015-03-18 11:14:24 -07:00
srslte_demod_soft_sigma_set(&q->demod, sqrt(0.5));
srslte_demod_soft_table_set(&q->demod, &q->mod[harq->mcs.mod]);
srslte_demod_soft_demodulate(&q->demod, q->d, q->q, harq->nof_re);
/* descramble */
2015-03-18 11:14:24 -07:00
srslte_scrambling_f_offset(&q->seq[harq->sf_idx], q->q, 0, harq->nof_bits);
2015-03-18 11:14:24 -07:00
return srslte_ulsch_decode(&q->dl_sch, harq, q->q, data);
} else {
2015-03-18 11:14:24 -07:00
fprintf(stderr, "Must call srslte_pusch_set_rnti() before calling srslte_pusch_decode()\n");
return SRSLTE_ERROR;
}
} else {
return SRSLTE_ERROR_INVALID_INPUTS;
}
}
2015-03-18 11:14:24 -07:00
int srslte_pusch_encode_rnti(srslte_pusch_t *q, srslte_harq_t *harq_process, uint8_t *data, uint16_t rnti, cf_t *sf_symbols)
{
2015-03-18 11:14:24 -07:00
srslte_uci_data_t uci_data;
bzero(&uci_data, sizeof(srslte_uci_data_t));
return srslte_pusch_uci_encode_rnti(q, harq_process, data, uci_data, rnti, sf_symbols);
2015-03-02 02:11:44 -08:00
}
2015-03-18 11:14:24 -07:00
int srslte_pusch_encode(srslte_pusch_t *q, srslte_harq_t *harq_process, uint8_t *data, cf_t *sf_symbols)
2015-03-02 02:11:44 -08:00
{
if (q->rnti_is_set) {
2015-03-18 11:14:24 -07:00
srslte_uci_data_t uci_data;
bzero(&uci_data, sizeof(srslte_uci_data_t));
return srslte_pusch_uci_encode_rnti(q, harq_process, data, uci_data, q->rnti, sf_symbols);
2015-03-02 02:11:44 -08:00
} else {
2015-03-18 11:14:24 -07:00
fprintf(stderr, "Must call srslte_pusch_set_rnti() to set the encoder/decoder RNTI\n");
return SRSLTE_ERROR;
2015-03-02 02:11:44 -08:00
}
}
2015-03-18 11:14:24 -07:00
int srslte_pusch_uci_encode(srslte_pusch_t *q, srslte_harq_t *harq, uint8_t *data, srslte_uci_data_t uci_data, cf_t *sf_symbols)
2015-03-02 02:11:44 -08:00
{
if (q->rnti_is_set) {
2015-03-18 11:14:24 -07:00
return srslte_pusch_uci_encode_rnti(q, harq, data, uci_data, q->rnti, sf_symbols);
2015-03-02 02:11:44 -08:00
} else {
2015-03-18 11:14:24 -07:00
fprintf(stderr, "Must call srslte_pusch_set_rnti() to set the encoder/decoder RNTI\n");
return SRSLTE_ERROR;
2015-03-02 02:11:44 -08:00
}
}
/** Converts the PUSCH data bits to symbols mapped to the slot ready for transmission
*/
2015-03-18 11:14:24 -07:00
int srslte_pusch_uci_encode_rnti(srslte_pusch_t *q, srslte_harq_t *harq, uint8_t *data, srslte_uci_data_t uci_data, uint16_t rnti, cf_t *sf_symbols)
{
int ret = SRSLTE_ERROR_INVALID_INPUTS;
if (q != NULL &&
data != NULL &&
harq != NULL)
{
2015-03-02 02:11:44 -08:00
if (harq->mcs.tbs > harq->nof_bits) {
fprintf(stderr, "Invalid code rate %.2f\n", (float) harq->mcs.tbs / harq->nof_bits);
return SRSLTE_ERROR_INVALID_INPUTS;
2015-03-02 02:11:44 -08:00
}
2015-03-02 02:11:44 -08:00
if (harq->nof_re > q->max_re) {
fprintf(stderr, "Error too many RE per subframe (%d). PUSCH configured for %d RE (%d PRB)\n",
harq->nof_re, q->max_re, q->cell.nof_prb);
return SRSLTE_ERROR_INVALID_INPUTS;
2015-03-02 02:11:44 -08:00
}
INFO("Encoding PUSCH SF: %d, Mod %s, RNTI: %d, TBS: %d, NofSymbols: %d, NofBitsE: %d, rv_idx: %d\n",
2015-03-18 05:59:29 -07:00
harq->sf_idx, srslte_mod_string(harq->mcs.mod), rnti, harq->mcs.tbs, harq->nof_re, harq->nof_bits, harq->rv);
2015-03-02 02:11:44 -08:00
2015-03-18 11:14:24 -07:00
bzero(q->q, harq->nof_bits);
if (srslte_ulsch_uci_encode(&q->dl_sch, harq, data, uci_data, q->g, q->q)) {
2015-03-02 02:11:44 -08:00
fprintf(stderr, "Error encoding TB\n");
return SRSLTE_ERROR;
2015-03-02 02:11:44 -08:00
}
if (rnti != q->rnti) {
2015-03-18 08:05:38 -07:00
srslte_sequence_t seq;
if (srslte_sequence_pusch(&seq, rnti, 2 * harq->sf_idx, q->cell.id, harq->nof_bits)) {
return SRSLTE_ERROR;
}
2015-03-18 11:14:24 -07:00
srslte_scrambling_b_offset_pusch(&seq, (uint8_t*) q->q, 0, harq->nof_bits);
2015-03-18 08:05:38 -07:00
srslte_sequence_free(&seq);
} else {
2015-03-18 11:14:24 -07:00
srslte_scrambling_b_offset_pusch(&q->seq[harq->sf_idx], (uint8_t*) q->q, 0, harq->nof_bits);
}
2015-03-02 02:11:44 -08:00
2015-03-18 11:14:24 -07:00
srslte_mod_modulate(&q->mod[harq->mcs.mod], (uint8_t*) q->q, q->d, harq->nof_bits);
2015-03-02 02:11:44 -08:00
2015-03-18 11:14:24 -07:00
srslte_dft_precoding(&q->dft_precoding, q->d, q->z,
2015-03-02 02:11:44 -08:00
harq->ul_alloc.L_prb, harq->nof_symb);
/* mapping to resource elements */
2015-03-18 11:14:24 -07:00
pusch_put(q, harq, q->z, sf_symbols);
2015-03-02 02:11:44 -08:00
ret = SRSLTE_SUCCESS;
}
return ret;
}