srsLTE/srslte/lib/phch/phich.c

409 lines
12 KiB
C
Raw Normal View History

2014-03-27 09:31:25 -07:00
/**
*
* \section COPYRIGHT
*
2015-11-13 04:22:33 -08:00
* Copyright 2013-2015 Software Radio Systems Limited
2014-03-27 09:31:25 -07:00
*
* \section LICENSE
*
* This file is part of the srsLTE library.
2014-03-27 09:31:25 -07:00
*
* srsLTE is free software: you can redistribute it and/or modify
2015-05-08 08:05:40 -07:00
* it under the terms of the GNU Affero General Public License as
2014-03-27 09:31:25 -07:00
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE is distributed in the hope that it will be useful,
2014-03-27 09:31:25 -07:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-05-08 08:05:40 -07:00
* GNU Affero General Public License for more details.
2014-03-27 09:31:25 -07:00
*
2015-05-08 08:05:40 -07:00
* A copy of the GNU Affero General Public License can be found in
2014-03-27 09:31:25 -07:00
* 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 */
2015-03-18 05:59:29 -07:00
const cf_t w_normal[SRSLTE_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 } };
2015-03-18 05:59:29 -07:00
const cf_t w_ext[SRSLTE_PHICH_EXT_NSEQUENCES][2] = { { 1, 1 }, { 1, -1 }, { I, I }, {
2014-03-27 09:31:25 -07:00
I, -I } };
2015-03-18 11:14:24 -07:00
uint32_t srslte_phich_ngroups(srslte_phich_t *q) {
return srslte_regs_phich_ngroups(q->regs);
2014-03-27 09:31:25 -07:00
}
uint32_t srslte_phich_nsf(srslte_phich_t *q) {
if (SRSLTE_CP_ISNORM(q->cell.cp)) {
return SRSLTE_PHICH_NORM_NSF;
} else {
return SRSLTE_PHICH_EXT_NSF;
}
}
2015-03-18 11:14:24 -07:00
void srslte_phich_reset(srslte_phich_t *q, cf_t *slot_symbols[SRSLTE_MAX_PORTS]) {
2014-06-17 02:11:41 -07:00
int i;
2015-03-18 05:41:50 -07:00
for (i = 0; i < SRSLTE_MAX_PORTS; i++) {
2015-03-18 11:14:24 -07:00
srslte_regs_phich_reset(q->regs, slot_symbols[i]);
2014-06-17 02:11:41 -07:00
}
2014-03-27 09:31:25 -07:00
}
/** Initializes the phich channel receiver */
2015-03-18 11:14:24 -07:00
int srslte_phich_init(srslte_phich_t *q, srslte_regs_t *regs, srslte_cell_t cell) {
int ret = SRSLTE_ERROR_INVALID_INPUTS;
if (q != NULL &&
regs != 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_phich_t));
ret = SRSLTE_ERROR;
q->cell = cell;
q->regs = regs;
if (srslte_modem_table_lte(&q->mod, SRSLTE_MOD_BPSK)) {
goto clean;
}
2014-06-17 02:11:41 -07:00
2015-03-18 05:41:50 -07:00
for (int nsf = 0; nsf < SRSLTE_NSUBFRAMES_X_FRAME; nsf++) {
2015-03-18 11:14:24 -07:00
if (srslte_sequence_phich(&q->seq[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) {
2015-03-18 11:14:24 -07:00
srslte_phich_free(q);
2014-06-17 02:11:41 -07:00
}
return ret;
2014-03-27 09:31:25 -07:00
}
2015-03-18 11:14:24 -07:00
void srslte_phich_free(srslte_phich_t *q) {
2015-03-18 05:41:50 -07:00
for (int ns = 0; ns < SRSLTE_NSUBFRAMES_X_FRAME; ns++) {
2015-03-18 11:14:24 -07:00
srslte_sequence_free(&q->seq[ns]);
2014-06-17 02:11:41 -07:00
}
2015-03-18 11:14:24 -07:00
srslte_modem_table_free(&q->mod);
2015-03-18 11:14:24 -07:00
bzero(q, sizeof(srslte_phich_t));
2014-03-27 09:31:25 -07:00
}
2016-06-28 09:28:57 -07:00
/* Computes n_group and n_seq according to Section 9.1.2 in 36.213 */
void srslte_phich_calc(srslte_phich_t *q, uint32_t n_prb_lowest, uint32_t n_dmrs,
uint32_t *ngroup, uint32_t *nseq)
{
uint32_t Ngroups = srslte_phich_ngroups(q);
*ngroup = (n_prb_lowest+n_dmrs)%Ngroups;
*nseq = ((n_prb_lowest/Ngroups)+n_dmrs)%(2*srslte_phich_nsf(q));
}
2014-03-27 09:31:25 -07:00
/* Decodes ACK
*
*/
uint8_t srslte_phich_ack_decode(float bits[SRSLTE_PHICH_NBITS], float *distance) {
int i;
float ack_table[2][3] = {{-1.0, -1.0, -1.0}, {1.0, 1.0, 1.0}};
float max_corr = -9999;
uint8_t index=0;
if (SRSLTE_VERBOSE_ISINFO()) {
INFO("Received bits: ", 0);
srslte_vec_fprint_f(stdout, bits, SRSLTE_PHICH_NBITS);
2014-06-17 02:11:41 -07:00
}
for (i = 0; i < 2; i++) {
float corr = srslte_vec_dot_prod_fff(ack_table[i], bits, SRSLTE_PHICH_NBITS);
INFO("Corr%d=%f\n", i, corr);
if (corr > max_corr) {
max_corr = corr;
if (distance) {
*distance = max_corr;
}
index = i;
2014-06-17 02:11:41 -07:00
}
}
return index;
2014-03-27 09:31:25 -07:00
}
/** Encodes the ACK
* 36.212
*/
2015-03-18 11:14:24 -07:00
void srslte_phich_ack_encode(uint8_t ack, uint8_t bits[SRSLTE_PHICH_NBITS]) {
2014-10-17 11:44:01 -07:00
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
*/
2015-03-18 11:14:24 -07:00
int srslte_phich_decode(srslte_phich_t *q, cf_t *slot_symbols, cf_t *ce[SRSLTE_MAX_PORTS], float noise_estimate,
uint32_t ngroup, uint32_t nseq, uint32_t subframe, uint8_t *ack, float *distance) {
2014-06-17 02:11:41 -07:00
/* Set pointers for layermapping & precoding */
int i, j;
2015-03-18 05:59:29 -07:00
cf_t *x[SRSLTE_MAX_LAYERS];
2015-03-18 05:41:50 -07:00
cf_t *ce_precoding[SRSLTE_MAX_PORTS];
if (q == NULL || slot_symbols == NULL) {
return SRSLTE_ERROR_INVALID_INPUTS;
}
2014-06-17 02:11:41 -07:00
2015-03-18 05:41:50 -07:00
if (subframe >= SRSLTE_NSUBFRAMES_X_FRAME) {
fprintf(stderr, "Invalid nslot %d\n", subframe);
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
2015-03-18 05:59:29 -07:00
if (SRSLTE_CP_ISEXT(q->cell.cp)) {
if (nseq >= SRSLTE_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 {
2015-03-18 05:59:29 -07:00
if (nseq >= SRSLTE_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
}
}
2015-03-18 11:14:24 -07:00
if (ngroup >= srslte_regs_phich_ngroups(q->regs)) {
2014-06-17 02:11:41 -07:00
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 */
2015-03-18 05:41:50 -07:00
for (i = 0; i < SRSLTE_MAX_PORTS; i++) {
2015-03-18 11:14:24 -07:00
x[i] = q->x[i];
2014-06-17 02:11:41 -07:00
}
2015-03-18 05:41:50 -07:00
for (i = 0; i < SRSLTE_MAX_PORTS; i++) {
2014-06-17 02:11:41 -07:00
ce_precoding[i] = q->ce[i];
}
/* extract symbols */
2015-03-18 11:14:24 -07:00
if (SRSLTE_PHICH_MAX_NSYMB
!= srslte_regs_phich_get(q->regs, slot_symbols, q->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++) {
2015-03-18 11:14:24 -07:00
if (SRSLTE_PHICH_MAX_NSYMB != srslte_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 */
2015-10-16 02:05:13 -07:00
srslte_predecoding_single(q->symbols[0], q->ce[0], q->d0, SRSLTE_PHICH_MAX_NSYMB, noise_estimate);
2014-06-17 02:11:41 -07:00
} else {
srslte_predecoding_diversity(q->symbols[0], ce_precoding, x,
q->cell.nof_ports, SRSLTE_PHICH_MAX_NSYMB);
2015-03-18 11:14:24 -07:00
srslte_layerdemap_diversity(x, q->d0, q->cell.nof_ports,
SRSLTE_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);
2015-03-18 11:14:24 -07:00
if (SRSLTE_VERBOSE_ISDEBUG())
srslte_vec_fprint_c(stdout, q->d0, SRSLTE_PHICH_MAX_NSYMB);
2014-06-17 02:11:41 -07:00
2015-03-18 05:59:29 -07:00
if (SRSLTE_CP_ISEXT(q->cell.cp)) {
2014-06-17 02:11:41 -07:00
if (ngroup % 2) {
2015-03-18 05:59:29 -07:00
for (i = 0; i < SRSLTE_PHICH_EXT_MSYMB / 2; i++) {
2015-03-18 11:14:24 -07:00
q->d[2 * i + 0] = q->d0[4 * i + 2];
q->d[2 * i + 1] = q->d0[4 * i + 3];
2014-06-17 02:11:41 -07:00
}
} else {
2015-03-18 05:59:29 -07:00
for (i = 0; i < SRSLTE_PHICH_EXT_MSYMB / 2; i++) {
2015-03-18 11:14:24 -07:00
q->d[2 * i + 0] = q->d0[4 * i];
q->d[2 * i + 1] = q->d0[4 * i + 1];
2014-06-17 02:11:41 -07:00
}
}
} else {
2015-03-18 11:14:24 -07:00
memcpy(q->d, q->d0, SRSLTE_PHICH_MAX_NSYMB * sizeof(cf_t));
2014-06-17 02:11:41 -07:00
}
2014-06-17 07:32:19 -07:00
DEBUG("d: ", 0);
2015-03-18 11:14:24 -07:00
if (SRSLTE_VERBOSE_ISDEBUG())
srslte_vec_fprint_c(stdout, q->d, SRSLTE_PHICH_EXT_MSYMB);
2014-06-17 02:11:41 -07:00
2015-03-18 11:14:24 -07:00
srslte_scrambling_c(&q->seq[subframe], q->d);
2014-06-17 02:11:41 -07:00
/* De-spreading */
2015-03-18 05:59:29 -07:00
if (SRSLTE_CP_ISEXT(q->cell.cp)) {
2015-03-18 11:14:24 -07:00
for (i = 0; i < SRSLTE_PHICH_NBITS; i++) {
q->z[i] = 0;
2015-03-18 05:59:29 -07:00
for (j = 0; j < SRSLTE_PHICH_EXT_NSF; j++) {
2015-03-18 11:14:24 -07:00
q->z[i] += conjf(w_ext[nseq][j])
* q->d[i * SRSLTE_PHICH_EXT_NSF + j] / SRSLTE_PHICH_EXT_NSF;
2014-06-17 02:11:41 -07:00
}
}
} else {
2015-03-18 11:14:24 -07:00
for (i = 0; i < SRSLTE_PHICH_NBITS; i++) {
q->z[i] = 0;
2015-03-18 05:59:29 -07:00
for (j = 0; j < SRSLTE_PHICH_NORM_NSF; j++) {
2015-03-18 11:14:24 -07:00
q->z[i] += conjf(w_normal[nseq][j])
* q->d[i * SRSLTE_PHICH_NORM_NSF + j] / SRSLTE_PHICH_NORM_NSF;
2014-06-17 02:11:41 -07:00
}
}
}
2014-06-17 07:32:19 -07:00
DEBUG("z: ", 0);
2015-03-18 11:14:24 -07:00
if (SRSLTE_VERBOSE_ISDEBUG())
srslte_vec_fprint_c(stdout, q->z, SRSLTE_PHICH_NBITS);
2014-06-17 02:11:41 -07:00
srslte_demod_soft_demodulate(SRSLTE_MOD_BPSK, q->z, q->data_rx, SRSLTE_PHICH_NBITS);
2014-06-17 02:11:41 -07:00
if (ack) {
*ack = srslte_phich_ack_decode(q->data_rx, distance);
2014-06-17 02:11:41 -07:00
}
return SRSLTE_SUCCESS;
2014-03-27 09:31:25 -07:00
}
/** Encodes ACK/NACK bits, modulates and inserts into resource.
2015-03-18 11:14:24 -07:00
* The parameter ack is an array of srslte_phich_ngroups() pointers to buffers of nof_sequences uint8_ts
2014-03-27 09:31:25 -07:00
*/
2015-03-18 11:14:24 -07:00
int srslte_phich_encode(srslte_phich_t *q, uint8_t ack, uint32_t ngroup, uint32_t nseq, uint32_t subframe,
2015-03-18 05:41:50 -07:00
cf_t *slot_symbols[SRSLTE_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
}
2015-03-18 05:41:50 -07:00
if (subframe >= SRSLTE_NSUBFRAMES_X_FRAME) {
fprintf(stderr, "Invalid nslot %d\n", subframe);
return SRSLTE_ERROR_INVALID_INPUTS;
}
2015-03-18 05:59:29 -07:00
if (SRSLTE_CP_ISEXT(q->cell.cp)) {
if (nseq >= SRSLTE_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 {
2015-03-18 05:59:29 -07:00
if (nseq >= SRSLTE_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
}
}
2015-03-18 11:14:24 -07:00
if (ngroup >= srslte_regs_phich_ngroups(q->regs)) {
2014-06-17 02:11:41 -07:00
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 */
2015-03-18 05:59:29 -07:00
cf_t *x[SRSLTE_MAX_LAYERS];
2015-03-18 05:41:50 -07:00
cf_t *symbols_precoding[SRSLTE_MAX_PORTS];
2014-06-17 02:11:41 -07:00
/* number of layers equals number of ports */
for (i = 0; i < q->cell.nof_ports; i++) {
2015-03-18 11:14:24 -07:00
x[i] = q->x[i];
2014-06-17 02:11:41 -07:00
}
2015-03-18 05:41:50 -07:00
for (i = 0; i < SRSLTE_MAX_PORTS; i++) {
2015-03-18 11:14:24 -07:00
symbols_precoding[i] = q->symbols[i];
2014-06-17 02:11:41 -07:00
}
/* encode ACK/NACK bit */
2015-03-18 11:14:24 -07:00
srslte_phich_ack_encode(ack, q->data);
2014-06-17 02:11:41 -07:00
2015-03-18 11:14:24 -07:00
srslte_mod_modulate(&q->mod, q->data, q->z, SRSLTE_PHICH_NBITS);
2014-06-17 02:11:41 -07:00
2014-06-17 07:32:19 -07:00
DEBUG("data: ", 0);
2015-03-18 11:14:24 -07:00
if (SRSLTE_VERBOSE_ISDEBUG())
srslte_vec_fprint_c(stdout, q->z, SRSLTE_PHICH_NBITS);
2014-06-17 02:11:41 -07:00
/* Spread with w */
2015-03-18 05:59:29 -07:00
if (SRSLTE_CP_ISEXT(q->cell.cp)) {
for (i = 0; i < SRSLTE_PHICH_EXT_MSYMB; i++) {
2015-03-18 11:14:24 -07:00
q->d[i] = w_ext[nseq][i % SRSLTE_PHICH_EXT_NSF]
* q->z[i / SRSLTE_PHICH_EXT_NSF];
2014-06-17 02:11:41 -07:00
}
} else {
2015-03-18 05:59:29 -07:00
for (i = 0; i < SRSLTE_PHICH_NORM_MSYMB; i++) {
2015-03-18 11:14:24 -07:00
q->d[i] = w_normal[nseq][i % SRSLTE_PHICH_NORM_NSF]
* q->z[i / SRSLTE_PHICH_NORM_NSF];
2014-06-17 02:11:41 -07:00
}
}
2014-06-17 07:32:19 -07:00
DEBUG("d: ", 0);
2015-03-18 11:14:24 -07:00
if (SRSLTE_VERBOSE_ISDEBUG())
srslte_vec_fprint_c(stdout, q->d, SRSLTE_PHICH_EXT_MSYMB);
2014-06-17 02:11:41 -07:00
2015-03-18 11:14:24 -07:00
srslte_scrambling_c(&q->seq[subframe], q->d);
2014-06-17 02:11:41 -07:00
/* align to REG */
2015-03-18 05:59:29 -07:00
if (SRSLTE_CP_ISEXT(q->cell.cp)) {
2014-06-17 02:11:41 -07:00
if (ngroup % 2) {
2015-03-18 05:59:29 -07:00
for (i = 0; i < SRSLTE_PHICH_EXT_MSYMB / 2; i++) {
2015-03-18 11:14:24 -07:00
q->d0[4 * i + 0] = 0;
q->d0[4 * i + 1] = 0;
q->d0[4 * i + 2] = q->d[2 * i];
q->d0[4 * i + 3] = q->d[2 * i + 1];
2014-06-17 02:11:41 -07:00
}
} else {
2015-03-18 05:59:29 -07:00
for (i = 0; i < SRSLTE_PHICH_EXT_MSYMB / 2; i++) {
2015-03-18 11:14:24 -07:00
q->d0[4 * i + 0] = q->d[2 * i];
q->d0[4 * i + 1] = q->d[2 * i + 1];
q->d0[4 * i + 2] = 0;
q->d0[4 * i + 3] = 0;
2014-06-17 02:11:41 -07:00
}
}
} else {
2015-03-18 11:14:24 -07:00
memcpy(q->d0, q->d, SRSLTE_PHICH_MAX_NSYMB * sizeof(cf_t));
2014-06-17 02:11:41 -07:00
}
2014-06-17 07:32:19 -07:00
DEBUG("d0: ", 0);
2015-03-18 11:14:24 -07:00
if (SRSLTE_VERBOSE_ISDEBUG())
srslte_vec_fprint_c(stdout, q->d0, SRSLTE_PHICH_MAX_NSYMB);
2014-06-17 02:11:41 -07:00
/* layer mapping & precoding */
if (q->cell.nof_ports > 1) {
2015-03-18 11:14:24 -07:00
srslte_layermap_diversity(q->d0, x, q->cell.nof_ports, SRSLTE_PHICH_MAX_NSYMB);
srslte_precoding_diversity(x, symbols_precoding, q->cell.nof_ports,
2015-03-18 11:14:24 -07:00
SRSLTE_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 {
2015-03-18 11:14:24 -07:00
memcpy(q->symbols[0], q->d0, SRSLTE_PHICH_MAX_NSYMB * sizeof(cf_t));
2014-06-17 02:11:41 -07:00
}
/* mapping to resource elements */
for (i = 0; i < q->cell.nof_ports; i++) {
2015-03-18 11:14:24 -07:00
if (srslte_regs_phich_add(q->regs, q->symbols[i], ngroup, slot_symbols[i])
2014-06-17 07:32:19 -07:00
< 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
}