srsLTE/srslte/lib/fec/rm_conv.c

155 lines
3.8 KiB
C
Raw Normal View History

/**
2014-01-28 03:41:17 -08:00
*
* \section COPYRIGHT
2014-01-28 03:41:17 -08:00
*
2015-11-13 04:22:33 -08:00
* Copyright 2013-2015 Software Radio Systems Limited
*
* \section LICENSE
*
* This file is part of the srsLTE library.
*
* 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
* 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-01-28 03:41:17 -08: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-01-28 03:41:17 -08:00
*
2015-05-08 08:05:40 -07:00
* A copy of the GNU Affero 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/.
*
2014-01-28 03:41:17 -08:00
*/
#include <string.h>
2014-01-28 03:41:17 -08:00
#include <stdio.h>
#include <stdint.h>
#include "srslte/fec/rm_conv.h"
2014-01-28 03:41:17 -08:00
#define NCOLS 32
#define NROWS_MAX NCOLS
uint8_t RM_PERM_CC[NCOLS] = { 1, 17, 9, 25, 5, 21, 13, 29, 3, 19, 11, 27,
2014-06-17 07:32:19 -07:00
7, 23, 15, 31, 0, 16, 8, 24, 4, 20, 12, 28, 2, 18, 10, 26, 6, 22, 14, 30 };
uint8_t RM_PERM_CC_INV[NCOLS] =
2014-06-17 07:32:19 -07:00
{ 16, 0, 24, 8, 20, 4, 28, 12, 18, 2, 26, 10, 22, 6, 30, 14, 17, 1, 25, 9,
21, 5, 29, 13, 19, 3, 27, 11, 23, 7, 31, 15 };
2014-01-28 03:41:17 -08:00
2015-03-18 08:05:38 -07:00
int srslte_rm_conv_tx(uint8_t *input, uint32_t in_len, uint8_t *output, uint32_t out_len) {
2014-10-17 11:44:01 -07:00
uint8_t tmp[3 * NCOLS * NROWS_MAX];
2014-06-17 02:11:41 -07:00
int nrows, ndummy, K_p;
int i, j, k, s;
nrows = (uint32_t) (in_len / 3 - 1) / NCOLS + 1;
2014-06-17 02:11:41 -07:00
if (nrows > NROWS_MAX) {
fprintf(stderr, "Input too large. Max input length is %d\n",
2014-06-17 07:32:19 -07:00
3 * NCOLS * NROWS_MAX);
2014-06-17 02:11:41 -07:00
return -1;
}
K_p = nrows * NCOLS;
2014-06-17 07:32:19 -07:00
ndummy = K_p - in_len / 3;
2014-06-17 02:11:41 -07:00
if (ndummy < 0) {
ndummy = 0;
}
/* Sub-block interleaver 5.1.4.2.1 */
2014-06-17 07:32:19 -07:00
k = 0;
2014-06-17 02:11:41 -07:00
for (s = 0; s < 3; s++) {
for (j = 0; j < NCOLS; j++) {
for (i = 0; i < nrows; i++) {
2014-06-17 07:32:19 -07:00
if (i * NCOLS + RM_PERM_CC[j] < ndummy) {
2015-03-18 08:05:38 -07:00
tmp[k] = SRSLTE_TX_NULL;
2014-06-17 02:11:41 -07:00
} else {
2014-06-17 07:32:19 -07:00
tmp[k] = input[(i * NCOLS + RM_PERM_CC[j] - ndummy) * 3 + s];
2014-06-17 02:11:41 -07:00
}
k++;
}
}
}
/* Bit collection, selection and transmission 5.1.4.2.2 */
k = 0;
j = 0;
while (k < out_len) {
2015-03-18 08:05:38 -07:00
if (tmp[j] != SRSLTE_TX_NULL) {
2014-06-17 02:11:41 -07:00
output[k] = tmp[j];
k++;
}
j++;
2014-06-17 07:32:19 -07:00
if (j == 3 * K_p) {
2014-06-17 02:11:41 -07:00
j = 0;
}
}
return 0;
}
2014-01-28 03:41:17 -08:00
/* Undoes Convolutional Code Rate Matching.
* 3GPP TS 36.212 v10.1.0 section 5.1.4.2
*/
2015-03-18 08:05:38 -07:00
int srslte_rm_conv_rx(float *input, uint32_t in_len, float *output, uint32_t out_len) {
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
int nrows, ndummy, K_p;
int i, j, k;
int d_i, d_j;
2014-01-28 03:41:17 -08:00
2014-06-17 07:32:19 -07:00
float tmp[3 * NCOLS * NROWS_MAX];
nrows = (uint32_t) (out_len / 3 - 1) / NCOLS + 1;
2014-06-17 02:11:41 -07:00
if (nrows > NROWS_MAX) {
fprintf(stderr, "Output too large. Max output length is %d\n",
2014-06-17 07:32:19 -07:00
3 * NCOLS * NROWS_MAX);
2014-06-17 02:11:41 -07:00
return -1;
}
K_p = nrows * NCOLS;
2014-01-28 03:41:17 -08:00
2014-06-17 07:32:19 -07:00
ndummy = K_p - out_len / 3;
2014-06-17 02:11:41 -07:00
if (ndummy < 0) {
ndummy = 0;
}
2014-01-28 03:41:17 -08:00
2014-06-17 07:32:19 -07:00
for (i = 0; i < 3 * K_p; i++) {
2015-03-18 08:05:38 -07:00
tmp[i] = SRSLTE_RX_NULL;
2014-06-17 02:11:41 -07:00
}
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
/* Undo bit collection. Account for dummy bits */
k = 0;
j = 0;
while (k < in_len) {
d_i = (j % K_p) / nrows;
d_j = (j % K_p) % nrows;
2014-01-28 03:41:17 -08:00
2014-06-17 07:32:19 -07:00
if (d_j * NCOLS + RM_PERM_CC[d_i] >= ndummy) {
2015-03-18 08:05:38 -07:00
if (tmp[j] == SRSLTE_RX_NULL) {
2014-06-17 02:11:41 -07:00
tmp[j] = input[k];
2015-03-18 08:05:38 -07:00
} else if (input[k] != SRSLTE_RX_NULL) {
2014-06-17 02:11:41 -07:00
tmp[j] += input[k]; /* soft combine LLRs */
}
k++;
}
j++;
2014-06-17 07:32:19 -07:00
if (j == 3 * K_p) {
2014-06-17 02:11:41 -07:00
j = 0;
}
}
/* interleaving and bit selection */
2014-06-17 07:32:19 -07:00
for (i = 0; i < out_len / 3; i++) {
2014-06-17 02:11:41 -07:00
d_i = (i + ndummy) / NCOLS;
d_j = (i + ndummy) % NCOLS;
2014-06-17 07:32:19 -07:00
for (j = 0; j < 3; j++) {
float o = tmp[K_p * j + RM_PERM_CC_INV[d_j] * nrows + d_i];
2015-03-18 08:05:38 -07:00
if (o != SRSLTE_RX_NULL) {
2014-06-17 07:32:19 -07:00
output[i * 3 + j] = o;
2014-06-17 02:11:41 -07:00
} else {
2014-06-17 07:32:19 -07:00
output[i * 3 + j] = 0;
2014-06-17 02:11:41 -07:00
}
}
}
return 0;
2014-01-28 03:41:17 -08:00
}