srsLTE/srslte/lib/phch/pcfich.c

273 lines
7.8 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/pcfich.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 5.3.4-1
static uint8_t cfi_table[4][PCFICH_CFI_LEN] = {
{ 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1,
1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 1,
2014-06-17 07:32:19 -07:00
1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,
0, 1, 1, 0 },
{ 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,
0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2014-06-17 07:32:19 -07:00
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } // reserved
2014-03-27 09:31:25 -07:00
};
2015-03-18 11:14:24 -07:00
bool srslte_pcfich_exists(int nframe, int nslot) {
2014-06-17 02:11:41 -07:00
return true;
2014-03-27 09:31:25 -07:00
}
/** Initializes the pcfich channel receiver.
* On error, returns -1 and frees the structrure
*/
2015-03-18 11:14:24 -07:00
int srslte_pcfich_init(srslte_pcfich_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))
{
ret = SRSLTE_ERROR;
2015-03-18 11:14:24 -07:00
bzero(q, sizeof(srslte_pcfich_t));
q->cell = cell;
q->regs = regs;
q->nof_symbols = PCFICH_RE;
if (srslte_modem_table_lte(&q->mod, SRSLTE_MOD_QPSK)) {
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_pcfich(&q->seq[nsf], 2 * nsf, q->cell.id)) {
goto clean;
}
2014-06-17 02:11:41 -07:00
}
/* convert cfi bit tables to floats for demodulation */
for (int i=0;i<3;i++) {
for (int j=0;j<PCFICH_CFI_LEN;j++) {
q->cfi_table_float[i][j] = (float) 2.0*cfi_table[i][j]-1.0;
}
}
2014-06-17 02:11:41 -07:00
ret = SRSLTE_SUCCESS;
}
clean:
if (ret == SRSLTE_ERROR) {
2015-03-18 11:14:24 -07:00
srslte_pcfich_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_pcfich_free(srslte_pcfich_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_pcfich_t));
2014-03-27 09:31:25 -07:00
}
/** Finds the CFI with minimum distance with the vector of received 32 bits.
* Saves the CFI value in the cfi pointer and returns the distance.
*/
2015-03-18 11:14:24 -07:00
float srslte_pcfich_cfi_decode(srslte_pcfich_t *q, uint32_t *cfi) {
int i;
int index = 0;
float max_corr = 0;
2016-05-18 02:51:10 -07:00
float corr[3];
2014-06-17 07:32:19 -07:00
for (i = 0; i < 3; i++) {
2016-05-18 02:51:10 -07:00
corr[i] = srslte_vec_dot_prod_fff(q->cfi_table_float[i], q->data_f, PCFICH_CFI_LEN);
if (corr[i] > max_corr) {
max_corr = corr[i];
index = i;
2014-06-17 02:11:41 -07:00
}
}
2016-05-18 02:51:10 -07:00
2014-06-17 02:11:41 -07:00
if (cfi) {
2014-06-17 07:32:19 -07:00
*cfi = index + 1;
2014-06-17 02:11:41 -07:00
}
return max_corr;
2014-03-27 09:31:25 -07:00
}
/** Encodes the CFI producing a vector of 32 bits.
* 36.211 10.3 section 5.3.4
*/
int srslte_pcfich_cfi_encode(uint32_t cfi, uint8_t bits[PCFICH_CFI_LEN]) {
2014-06-17 02:11:41 -07:00
if (cfi < 1 || cfi > 3) {
return SRSLTE_ERROR_INVALID_INPUTS;
} else{
2014-10-17 11:44:01 -07:00
memcpy(bits, cfi_table[cfi - 1], PCFICH_CFI_LEN * sizeof(uint8_t));
return SRSLTE_SUCCESS;
2014-06-17 02:11:41 -07:00
}
2014-03-27 09:31:25 -07:00
}
/* Decodes the PCFICH 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_pcfich_decode(srslte_pcfich_t *q, cf_t *slot_symbols, cf_t *ce[SRSLTE_MAX_PORTS], float noise_estimate,
uint32_t nsubframe, uint32_t *cfi, float *corr_result)
{
2014-06-17 02:11:41 -07:00
/* Set pointers for layermapping & precoding */
int i;
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];
2014-06-17 02:11:41 -07:00
if (q != NULL &&
slot_symbols != NULL &&
2015-03-18 05:41:50 -07:00
nsubframe < SRSLTE_NSUBFRAMES_X_FRAME)
{
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];
}
2015-03-18 05:41:50 -07:00
for (i = 0; i < SRSLTE_MAX_PORTS; i++) {
ce_precoding[i] = q->ce[i];
}
2014-06-17 02:11:41 -07:00
/* extract symbols */
if (q->nof_symbols
2015-03-18 11:14:24 -07:00
!= srslte_regs_pcfich_get(q->regs, slot_symbols, q->symbols[0])) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "There was an error getting the PCFICH 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 (q->nof_symbols != srslte_regs_pcfich_get(q->regs, ce[i], q->ce[i])) {
fprintf(stderr, "There was an error getting the PCFICH 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) {
/* no need for layer demapping */
2015-10-16 02:05:13 -07:00
srslte_predecoding_single(q->symbols[0], q->ce[0], q->d, q->nof_symbols, noise_estimate);
} else {
srslte_predecoding_diversity(q->symbols[0], ce_precoding, x,
q->cell.nof_ports, q->nof_symbols);
2015-03-18 11:14:24 -07:00
srslte_layerdemap_diversity(x, q->d, q->cell.nof_ports,
q->nof_symbols / q->cell.nof_ports);
}
2014-06-17 02:11:41 -07:00
/* demodulate symbols */
srslte_demod_soft_demodulate(SRSLTE_MOD_QPSK, q->d, q->data_f, q->nof_symbols);
2014-06-17 02:11:41 -07:00
/* Scramble with the sequence for slot nslot */
2015-03-18 11:14:24 -07:00
srslte_scrambling_f(&q->seq[nsubframe], q->data_f);
/* decode CFI */
2015-03-18 11:14:24 -07:00
float corr = srslte_pcfich_cfi_decode(q, cfi);
if (corr_result) {
*corr_result = corr;
}
return 1;
2014-06-17 02:11:41 -07:00
} else {
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
2014-03-27 09:31:25 -07:00
}
/** Encodes CFI and maps symbols to the slot
*/
2015-03-18 11:14:24 -07:00
int srslte_pcfich_encode(srslte_pcfich_t *q, uint32_t cfi, cf_t *slot_symbols[SRSLTE_MAX_PORTS],
uint32_t subframe) {
2014-06-17 02:11:41 -07:00
int i;
if (q != NULL &&
cfi <= 3 &&
slot_symbols != NULL &&
2015-03-18 05:41:50 -07:00
subframe < SRSLTE_NSUBFRAMES_X_FRAME)
{
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];
}
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
/* pack CFI */
2015-03-18 11:14:24 -07:00
srslte_pcfich_cfi_encode(cfi, q->data);
2014-06-17 02:11:41 -07:00
/* scramble for slot sequence nslot */
2015-03-18 11:14:24 -07:00
srslte_scrambling_b(&q->seq[subframe], 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->d, PCFICH_CFI_LEN);
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->d, x, q->cell.nof_ports, q->nof_symbols);
srslte_precoding_diversity(x, symbols_precoding, q->cell.nof_ports,
q->nof_symbols / q->cell.nof_ports);
} else {
2015-03-18 11:14:24 -07:00
memcpy(q->symbols[0], q->d, q->nof_symbols * 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_pcfich_put(q->regs, q->symbols[i], slot_symbols[i]) < 0) {
fprintf(stderr, "Error putting PCHICH resource elements\n");
return SRSLTE_ERROR;
}
2014-06-17 02:11:41 -07:00
}
return SRSLTE_SUCCESS;
} else {
return SRSLTE_ERROR_INVALID_INPUTS;
2014-06-17 02:11:41 -07:00
}
2014-03-27 09:31:25 -07:00
}