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

397 lines
11 KiB
C
Raw Normal View History

2014-03-27 09:31:25 -07:00
/**
*
* \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/regs.h"
#include "srslte/phch/phich.h"
#include "srslte/common/phy_common.h"
#include "srslte/utils/bit.h"
#include "srslte/utils/vector.h"
#include "srslte/utils/debug.h"
2014-03-27 09:31:25 -07:00
/** Table 6.9.1-2 */
const cf_t w_normal[PHICH_NORM_NSEQUENCES][4] = { { 1, 1, 1, 1 },
2014-06-17 02:11:41 -07:00
{ 1, -1, 1, -1 }, { 1, 1, -1, -1 }, { 1, -1, -1, 1 }, { I, I, I, I }, {
I, -I, I, -I }, { I, I, -I, -I }, { I, -I, -I, I } };
2014-03-27 09:31:25 -07:00
const cf_t w_ext[PHICH_EXT_NSEQUENCES][2] = { { 1, 1 }, { 1, -1 }, { I, I }, {
I, -I } };
uint32_t phich_ngroups(phich_t *q) {
2014-06-17 02:11:41 -07:00
return regs_phich_ngroups(q->regs);
2014-03-27 09:31:25 -07:00
}
void phich_reset(phich_t *q, cf_t *slot_symbols[MAX_PORTS]) {
2014-06-17 02:11:41 -07:00
int i;
for (i = 0; i < MAX_PORTS; i++) {
2014-06-17 02:11:41 -07:00
regs_phich_reset(q->regs, slot_symbols[i]);
}
2014-03-27 09:31:25 -07:00
}
/** Initializes the phich channel receiver */
int phich_init(phich_t *q, regs_t *regs, lte_cell_t cell) {
int ret = SRSLTE_ERROR_INVALID_INPUTS;
if (q != NULL &&
regs != NULL &&
lte_cell_isvalid(&cell))
{
bzero(q, sizeof(phich_t));
ret = SRSLTE_ERROR;
q->cell = cell;
q->regs = regs;
if (precoding_init(&q->precoding, SF_LEN_RE(cell.nof_prb, cell.cp))) {
fprintf(stderr, "Error initializing precoding\n");
}
if (modem_table_lte(&q->mod, LTE_BPSK, false)) {
goto clean;
}
2014-06-17 02:11:41 -07:00
demod_hard_init(&q->demod);
demod_hard_table_set(&q->demod, LTE_BPSK);
2014-06-17 02:11:41 -07:00
for (int nsf = 0; nsf < NSUBFRAMES_X_FRAME; nsf++) {
if (sequence_phich(&q->seq_phich[nsf], 2 * nsf, q->cell.id)) {
goto clean;
}
2014-06-17 02:11:41 -07:00
}
ret = SRSLTE_SUCCESS;
2014-06-17 02:11:41 -07:00
}
clean:
if (ret == SRSLTE_ERROR) {
2014-06-17 02:11:41 -07:00
phich_free(q);
}
return ret;
2014-03-27 09:31:25 -07:00
}
void phich_free(phich_t *q) {
2014-06-17 02:11:41 -07:00
for (int ns = 0; ns < NSUBFRAMES_X_FRAME; ns++) {
sequence_free(&q->seq_phich[ns]);
}
modem_table_free(&q->mod);
precoding_free(&q->precoding);
bzero(q, sizeof(phich_t));
2014-03-27 09:31:25 -07:00
}
/* Decodes ACK
*
*/
2014-10-17 11:44:01 -07:00
uint8_t phich_ack_decode(uint8_t bits[PHICH_NBITS], uint32_t *distance) {
2014-06-17 02:11:41 -07:00
int i, n;
n = 0;
for (i = 0; i < PHICH_NBITS; i++) {
n += bits[i];
}
INFO("PHICH decoder: %d, %d, %d\n", bits[0], bits[1], bits[2]);
if (n >= 2) {
if (distance) {
2014-06-17 07:32:19 -07:00
*distance = 3 - n;
2014-06-17 02:11:41 -07:00
}
return 1;
} else {
if (distance) {
*distance = n;
}
return 0;
}
2014-03-27 09:31:25 -07:00
}
/** Encodes the ACK
* 36.212
*/
2014-10-17 11:44:01 -07:00
void phich_ack_encode(uint8_t ack, uint8_t bits[PHICH_NBITS]) {
memset(bits, ack, 3 * sizeof(uint8_t));
2014-03-27 09:31:25 -07:00
}
/* Decodes the phich channel and saves the CFI in the cfi pointer.
*
* Returns 1 if successfully decoded the CFI, 0 if not and -1 on error
*/
int phich_decode(phich_t *q, cf_t *slot_symbols, cf_t *ce[MAX_PORTS], float noise_estimate,
2014-10-17 11:44:01 -07:00
uint32_t ngroup, uint32_t nseq, uint32_t subframe, uint8_t *ack, uint32_t *distance) {
2014-06-17 02:11:41 -07:00
/* Set pointers for layermapping & precoding */
int i, j;
cf_t *x[MAX_LAYERS];
cf_t *ce_precoding[MAX_PORTS];
if (q == NULL || slot_symbols == NULL) {
return SRSLTE_ERROR_INVALID_INPUTS;
}
2014-06-17 02:11:41 -07:00
if (subframe >= NSUBFRAMES_X_FRAME) {
fprintf(stderr, "Invalid nslot %d\n", subframe);
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
if (CP_ISEXT(q->cell.cp)) {
if (nseq >= PHICH_EXT_NSEQUENCES) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "Invalid nseq %d\n", nseq);
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
} else {
if (nseq >= PHICH_NORM_NSEQUENCES) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "Invalid nseq %d\n", nseq);
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
}
if (ngroup >= regs_phich_ngroups(q->regs)) {
fprintf(stderr, "Invalid ngroup %d\n", ngroup);
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
DEBUG("Decoding PHICH Ngroup: %d, Nseq: %d\n", ngroup, nseq);
2014-06-17 02:11:41 -07:00
/* number of layers equals number of ports */
for (i = 0; i < MAX_PORTS; i++) {
2014-06-17 02:11:41 -07:00
x[i] = q->phich_x[i];
}
for (i = 0; i < MAX_PORTS; i++) {
ce_precoding[i] = q->ce[i];
}
/* extract symbols */
if (PHICH_MAX_NSYMB
2014-06-17 07:32:19 -07:00
!= regs_phich_get(q->regs, slot_symbols, q->phich_symbols[0], ngroup)) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "There was an error getting the phich symbols\n");
return SRSLTE_ERROR;
2014-06-17 02:11:41 -07:00
}
/* extract channel estimates */
for (i = 0; i < q->cell.nof_ports; i++) {
2014-06-17 07:32:19 -07:00
if (PHICH_MAX_NSYMB != regs_phich_get(q->regs, ce[i], q->ce[i], ngroup)) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "There was an error getting the phich symbols\n");
return SRSLTE_ERROR;
2014-06-17 02:11:41 -07:00
}
}
/* in control channels, only diversity is supported */
if (q->cell.nof_ports == 1) {
2014-06-17 02:11:41 -07:00
/* no need for layer demapping */
predecoding_single(&q->precoding, q->phich_symbols[0], q->ce[0], q->phich_d0,
PHICH_MAX_NSYMB, noise_estimate);
2014-06-17 02:11:41 -07:00
} else {
predecoding_diversity(&q->precoding, q->phich_symbols[0], ce_precoding, x,
q->cell.nof_ports, PHICH_MAX_NSYMB, noise_estimate);
layerdemap_diversity(x, q->phich_d0, q->cell.nof_ports,
PHICH_MAX_NSYMB / q->cell.nof_ports);
2014-06-17 02:11:41 -07:00
}
2014-06-17 07:32:19 -07:00
DEBUG("Recv!!: \n", 0);
DEBUG("d0: ", 0);
if (VERBOSE_ISDEBUG())
vec_fprint_c(stdout, q->phich_d0, PHICH_MAX_NSYMB);
2014-06-17 02:11:41 -07:00
if (CP_ISEXT(q->cell.cp)) {
2014-06-17 02:11:41 -07:00
if (ngroup % 2) {
for (i = 0; i < PHICH_EXT_MSYMB / 2; i++) {
q->phich_d[2 * i + 0] = q->phich_d0[4 * i + 2];
q->phich_d[2 * i + 1] = q->phich_d0[4 * i + 3];
}
} else {
for (i = 0; i < PHICH_EXT_MSYMB / 2; i++) {
q->phich_d[2 * i + 0] = q->phich_d0[4 * i];
q->phich_d[2 * i + 1] = q->phich_d0[4 * i + 1];
}
}
} else {
memcpy(q->phich_d, q->phich_d0, PHICH_MAX_NSYMB * sizeof(cf_t));
}
2014-06-17 07:32:19 -07:00
DEBUG("d: ", 0);
if (VERBOSE_ISDEBUG())
vec_fprint_c(stdout, q->phich_d, PHICH_EXT_MSYMB);
2014-06-17 02:11:41 -07:00
scrambling_c(&q->seq_phich[subframe], q->phich_d);
2014-06-17 02:11:41 -07:00
/* De-spreading */
if (CP_ISEXT(q->cell.cp)) {
2014-06-17 07:32:19 -07:00
for (i = 0; i < PHICH_NBITS; i++) {
2014-06-17 02:11:41 -07:00
q->phich_z[i] = 0;
2014-06-17 07:32:19 -07:00
for (j = 0; j < PHICH_EXT_NSF; j++) {
q->phich_z[i] += conjf(w_ext[nseq][j])
* q->phich_d[i * PHICH_EXT_NSF + j] / PHICH_EXT_NSF;
2014-06-17 02:11:41 -07:00
}
}
} else {
2014-06-17 07:32:19 -07:00
for (i = 0; i < PHICH_NBITS; i++) {
2014-06-17 02:11:41 -07:00
q->phich_z[i] = 0;
2014-06-17 07:32:19 -07:00
for (j = 0; j < PHICH_NORM_NSF; j++) {
q->phich_z[i] += conjf(w_normal[nseq][j])
* q->phich_d[i * PHICH_NORM_NSF + j] / PHICH_NORM_NSF;
2014-06-17 02:11:41 -07:00
}
}
}
2014-06-17 07:32:19 -07:00
DEBUG("z: ", 0);
if (VERBOSE_ISDEBUG())
vec_fprint_c(stdout, q->phich_z, PHICH_NBITS);
2014-06-17 02:11:41 -07:00
demod_hard_demodulate(&q->demod, q->phich_z, q->data, PHICH_NBITS);
if (ack) {
*ack = phich_ack_decode(q->data, distance);
}
return SRSLTE_SUCCESS;
2014-03-27 09:31:25 -07:00
}
/** Encodes ACK/NACK bits, modulates and inserts into resource.
2014-10-17 11:44:01 -07:00
* The parameter ack is an array of phich_ngroups() pointers to buffers of nof_sequences uint8_ts
2014-03-27 09:31:25 -07:00
*/
2014-10-17 11:44:01 -07:00
int phich_encode(phich_t *q, uint8_t ack, uint32_t ngroup, uint32_t nseq, uint32_t subframe,
cf_t *slot_symbols[MAX_PORTS]) {
2014-06-17 02:11:41 -07:00
int i;
if (q == NULL || slot_symbols == NULL) {
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
if (subframe >= NSUBFRAMES_X_FRAME) {
fprintf(stderr, "Invalid nslot %d\n", subframe);
return SRSLTE_ERROR_INVALID_INPUTS;
}
if (CP_ISEXT(q->cell.cp)) {
if (nseq >= PHICH_EXT_NSEQUENCES) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "Invalid nseq %d\n", nseq);
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
} else {
if (nseq >= PHICH_NORM_NSEQUENCES) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "Invalid nseq %d\n", nseq);
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
}
if (ngroup >= regs_phich_ngroups(q->regs)) {
fprintf(stderr, "Invalid ngroup %d\n", ngroup);
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
2014-06-17 02:11:41 -07:00
/* Set pointers for layermapping & precoding */
cf_t *x[MAX_LAYERS];
cf_t *symbols_precoding[MAX_PORTS];
/* number of layers equals number of ports */
for (i = 0; i < q->cell.nof_ports; i++) {
2014-06-17 02:11:41 -07:00
x[i] = q->phich_x[i];
}
for (i = 0; i < MAX_PORTS; i++) {
symbols_precoding[i] = q->phich_symbols[i];
}
/* encode ACK/NACK bit */
phich_ack_encode(ack, q->data);
mod_modulate(&q->mod, q->data, q->phich_z, PHICH_NBITS);
2014-06-17 07:32:19 -07:00
DEBUG("data: ", 0);
if (VERBOSE_ISDEBUG())
vec_fprint_c(stdout, q->phich_z, PHICH_NBITS);
2014-06-17 02:11:41 -07:00
/* Spread with w */
if (CP_ISEXT(q->cell.cp)) {
2014-06-17 02:11:41 -07:00
for (i = 0; i < PHICH_EXT_MSYMB; i++) {
q->phich_d[i] = w_ext[nseq][i % PHICH_EXT_NSF]
* q->phich_z[i / PHICH_EXT_NSF];
}
} else {
for (i = 0; i < PHICH_NORM_MSYMB; i++) {
q->phich_d[i] = w_normal[nseq][i % PHICH_NORM_NSF]
* q->phich_z[i / PHICH_NORM_NSF];
}
}
2014-06-17 07:32:19 -07:00
DEBUG("d: ", 0);
if (VERBOSE_ISDEBUG())
vec_fprint_c(stdout, q->phich_d, PHICH_EXT_MSYMB);
2014-06-17 02:11:41 -07:00
scrambling_c(&q->seq_phich[subframe], q->phich_d);
2014-06-17 02:11:41 -07:00
/* align to REG */
if (CP_ISEXT(q->cell.cp)) {
2014-06-17 02:11:41 -07:00
if (ngroup % 2) {
for (i = 0; i < PHICH_EXT_MSYMB / 2; i++) {
q->phich_d0[4 * i + 0] = 0;
q->phich_d0[4 * i + 1] = 0;
q->phich_d0[4 * i + 2] = q->phich_d[2 * i];
q->phich_d0[4 * i + 3] = q->phich_d[2 * i + 1];
}
} else {
for (i = 0; i < PHICH_EXT_MSYMB / 2; i++) {
q->phich_d0[4 * i + 0] = q->phich_d[2 * i];
q->phich_d0[4 * i + 1] = q->phich_d[2 * i + 1];
q->phich_d0[4 * i + 2] = 0;
q->phich_d0[4 * i + 3] = 0;
}
}
} else {
memcpy(q->phich_d0, q->phich_d, PHICH_MAX_NSYMB * sizeof(cf_t));
}
2014-06-17 07:32:19 -07:00
DEBUG("d0: ", 0);
if (VERBOSE_ISDEBUG())
vec_fprint_c(stdout, q->phich_d0, PHICH_MAX_NSYMB);
2014-06-17 02:11:41 -07:00
/* layer mapping & precoding */
if (q->cell.nof_ports > 1) {
layermap_diversity(q->phich_d0, x, q->cell.nof_ports, PHICH_MAX_NSYMB);
precoding_diversity(&q->precoding, x, symbols_precoding, q->cell.nof_ports,
PHICH_MAX_NSYMB / q->cell.nof_ports);
2014-06-17 02:11:41 -07:00
/**FIXME: According to 6.9.2, Precoding for 4 tx ports is different! */
} else {
memcpy(q->phich_symbols[0], q->phich_d0, PHICH_MAX_NSYMB * sizeof(cf_t));
}
/* mapping to resource elements */
for (i = 0; i < q->cell.nof_ports; i++) {
2014-06-17 07:32:19 -07:00
if (regs_phich_add(q->regs, q->phich_symbols[i], ngroup, slot_symbols[i])
< 0) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "Error putting PCHICH resource elements\n");
return SRSLTE_ERROR;
2014-06-17 02:11:41 -07:00
}
}
return SRSLTE_SUCCESS;
2014-03-27 09:31:25 -07:00
}