srsLTE/lte/phy/lib/phch/src/pcfich.c

260 lines
6.9 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 "liblte/phy/phch/regs.h"
#include "liblte/phy/phch/pcfich.h"
#include "liblte/phy/common/phy_common.h"
#include "liblte/phy/utils/bit.h"
#include "liblte/phy/utils/vector.h"
#include "liblte/phy/utils/debug.h"
2014-03-27 09:31:25 -07:00
// Table 5.3.4-1
2014-06-17 07:32:19 -07:00
static char 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,
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,
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
};
bool 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 */
2014-06-17 07:32:19 -07:00
int pcfich_init(pcfich_t *q, regs_t *regs, int cell_id, int nof_prb,
int nof_ports, lte_cp_t cp) {
2014-06-17 02:11:41 -07:00
int ret = -1;
if (cell_id < 0) {
return -1;
}
bzero(q, sizeof(pcfich_t));
q->cell_id = cell_id;
q->cp = cp;
q->regs = regs;
q->nof_prb = nof_prb;
2014-06-17 07:32:19 -07:00
q->nof_ports = nof_ports;
2014-06-17 02:11:41 -07:00
if (modem_table_std(&q->mod, LTE_QPSK, false)) {
goto clean;
}
demod_hard_init(&q->demod);
demod_hard_table_set(&q->demod, LTE_QPSK);
2014-06-17 07:32:19 -07:00
for (int nsf = 0; nsf < NSUBFRAMES_X_FRAME; nsf++) {
if (sequence_pcfich(&q->seq_pcfich[nsf], 2 * nsf, q->cell_id)) {
2014-06-17 02:11:41 -07:00
goto clean;
}
}
q->nof_symbols = PCFICH_RE;
ret = 0;
2014-06-17 07:32:19 -07:00
clean: if (ret == -1) {
2014-06-17 02:11:41 -07:00
pcfich_free(q);
}
return ret;
2014-03-27 09:31:25 -07:00
}
void pcfich_free(pcfich_t *q) {
2014-06-17 07:32:19 -07:00
for (int ns = 0; ns < NSUBFRAMES_X_FRAME; ns++) {
2014-06-17 02:11:41 -07:00
sequence_free(&q->seq_pcfich[ns]);
}
modem_table_free(&q->mod);
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.
*/
int pcfich_cfi_decode(char bits[PCFICH_CFI_LEN], int *cfi) {
2014-06-17 02:11:41 -07:00
int i, j;
2014-06-17 07:32:19 -07:00
int distance, index = -1;
2014-06-17 02:11:41 -07:00
int min = 32;
2014-06-17 07:32:19 -07:00
for (i = 0; i < 3; i++) {
2014-06-17 02:11:41 -07:00
distance = 0;
2014-06-17 07:32:19 -07:00
for (j = 0; j < PCFICH_CFI_LEN; j++) {
2014-06-17 02:11:41 -07:00
distance += (bits[j] ^ cfi_table[i][j]);
}
DEBUG("CFI=%d, distance:%d\n", i, distance);
if (distance < min) {
min = distance;
index = i;
}
}
if (cfi) {
2014-06-17 07:32:19 -07:00
*cfi = index + 1;
2014-06-17 02:11:41 -07:00
}
return min;
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 pcfich_cfi_encode(int cfi, char bits[PCFICH_CFI_LEN]) {
2014-06-17 02:11:41 -07:00
if (cfi < 1 || cfi > 3) {
fprintf(stderr, "Invalid CFI %d\n", cfi);
return -1;
}
2014-06-17 07:32:19 -07:00
memcpy(bits, cfi_table[cfi - 1], PCFICH_CFI_LEN * sizeof(char));
2014-06-17 02:11:41 -07:00
return 0;
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
*/
int pcfich_decode(pcfich_t *q, cf_t *slot_symbols, cf_t *ce[MAX_PORTS],
2014-06-17 07:32:19 -07:00
int nsubframe, int *cfi, int *distance) {
2014-06-17 02:11:41 -07:00
int dist;
/* Set pointers for layermapping & precoding */
int i;
cf_t *x[MAX_LAYERS];
cf_t *ce_precoding[MAX_PORTS];
if (nsubframe < 0 || nsubframe > NSUBFRAMES_X_FRAME) {
fprintf(stderr, "Invalid nslot %d\n", nsubframe);
return -1;
}
/* 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->pcfich_x[i];
}
2014-06-17 07:32:19 -07:00
for (i = 0; i < MAX_PORTS; i++) {
2014-06-17 02:11:41 -07:00
ce_precoding[i] = q->ce[i];
}
/* extract symbols */
2014-06-17 07:32:19 -07:00
if (q->nof_symbols
!= regs_pcfich_get(q->regs, slot_symbols, q->pcfich_symbols[0])) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "There was an error getting the PCFICH symbols\n");
return -1;
}
/* extract channel estimates */
2014-06-17 07:32:19 -07:00
for (i = 0; i < q->nof_ports; i++) {
2014-06-17 02:11:41 -07:00
if (q->nof_symbols != regs_pcfich_get(q->regs, ce[i], q->ce[i])) {
fprintf(stderr, "There was an error getting the PCFICH symbols\n");
return -1;
}
}
/* in control channels, only diversity is supported */
2014-06-17 07:32:19 -07:00
if (q->nof_ports == 1) {
2014-06-17 02:11:41 -07:00
/* no need for layer demapping */
2014-06-17 07:32:19 -07:00
predecoding_single_zf(q->pcfich_symbols[0], q->ce[0], q->pcfich_d,
q->nof_symbols);
2014-06-17 02:11:41 -07:00
} else {
2014-06-17 07:32:19 -07:00
predecoding_diversity_zf(q->pcfich_symbols[0], ce_precoding, x,
q->nof_ports, q->nof_symbols);
layerdemap_diversity(x, q->pcfich_d, q->nof_ports,
q->nof_symbols / q->nof_ports);
2014-06-17 02:11:41 -07:00
}
/* demodulate symbols */
demod_hard_demodulate(&q->demod, q->pcfich_d, q->data, q->nof_symbols);
/* Scramble with the sequence for slot nslot */
scrambling_b(&q->seq_pcfich[nsubframe], q->data);
/* decode CFI */
dist = pcfich_cfi_decode(q->data, cfi);
if (distance) {
*distance = dist;
}
if (dist < PCFICH_MAX_DISTANCE) {
return 1;
} else {
return 0;
}
2014-03-27 09:31:25 -07:00
}
/** Encodes CFI and maps symbols to the slot
*/
int pcfich_encode(pcfich_t *q, int cfi, cf_t *slot_symbols[MAX_PORTS],
2014-06-17 07:32:19 -07:00
int nsubframe) {
2014-06-17 02:11:41 -07:00
int i;
if (nsubframe < 0 || nsubframe > NSUBFRAMES_X_FRAME) {
fprintf(stderr, "Invalid nslot %d\n", nsubframe);
return -1;
}
/* Set pointers for layermapping & precoding */
cf_t *x[MAX_LAYERS];
cf_t *symbols_precoding[MAX_PORTS];
/* number of layers equals number of ports */
2014-06-17 07:32:19 -07:00
for (i = 0; i < q->nof_ports; i++) {
2014-06-17 02:11:41 -07:00
x[i] = q->pcfich_x[i];
}
2014-06-17 07:32:19 -07:00
for (i = 0; i < MAX_PORTS; i++) {
2014-06-17 02:11:41 -07:00
symbols_precoding[i] = q->pcfich_symbols[i];
}
/* pack CFI */
2014-06-17 02:11:41 -07:00
pcfich_cfi_encode(cfi, q->data);
/* scramble for slot sequence nslot */
scrambling_b(&q->seq_pcfich[nsubframe], q->data);
mod_modulate(&q->mod, q->data, q->pcfich_d, PCFICH_CFI_LEN);
/* layer mapping & precoding */
2014-06-17 07:32:19 -07:00
if (q->nof_ports > 1) {
layermap_diversity(q->pcfich_d, x, q->nof_ports, q->nof_symbols);
precoding_diversity(x, symbols_precoding, q->nof_ports,
q->nof_symbols / q->nof_ports);
2014-06-17 02:11:41 -07:00
} else {
memcpy(q->pcfich_symbols[0], q->pcfich_d, q->nof_symbols * sizeof(cf_t));
}
/* mapping to resource elements */
2014-06-17 07:32:19 -07:00
for (i = 0; i < q->nof_ports; i++) {
2014-06-17 02:11:41 -07:00
if (regs_pcfich_put(q->regs, q->pcfich_symbols[i], slot_symbols[i]) < 0) {
fprintf(stderr, "Error putting PCHICH resource elements\n");
return -1;
}
}
return 0;
2014-03-27 09:31:25 -07:00
}