srsLTE/srslte/lib/phch/test/pdsch_test_mex.c

248 lines
6.2 KiB
C
Raw Normal View History

2014-12-27 14:09:41 -08:00
/**
*
* \section COPYRIGHT
*
2015-05-08 08:05:40 -07:00
* Copyright 2013-2015 The srsLTE Developers. See the
2014-12-27 14:09:41 -08:00
* COPYRIGHT file at the top-level directory of this distribution.
*
* \section LICENSE
*
2015-05-08 08:05:40 -07:00
* This file is part of the srsLTE library.
2014-12-27 14:09:41 -08:00
*
2015-05-08 08:05:40 -07:00
* srsLTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
2014-12-27 14:09:41 -08:00
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
2015-05-08 08:05:40 -07:00
* srsLTE is distributed in the hope that it will be useful,
2014-12-27 14:09:41 -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-12-27 14:09:41 -08:00
*
2015-05-08 08:05:40 -07:00
* A copy of the GNU Affero General Public License can be found in
2014-12-27 14:09:41 -08:00
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#include <string.h>
#include "srslte/srslte.h"
#include "srslte/mex/mexutils.h"
2014-12-27 14:09:41 -08:00
/** MEX function to be called from MATLAB to test the channel estimator
*/
#define ENBCFG prhs[0]
#define PDSCHCFG prhs[1]
#define TBS prhs[2]
#define INPUT prhs[3]
2014-12-27 14:09:41 -08:00
#define NOF_INPUTS 4
void help()
{
mexErrMsgTxt
("[decoded_ok, llr, rm, bits, symbols] = srslte_pdsch(enbConfig, pdschConfig, trblklen, rxWaveform)\n\n");
2014-12-27 14:09:41 -08:00
}
extern int indices[2048];
2015-01-17 21:33:28 -08:00
2014-12-27 14:09:41 -08:00
/* the gateway function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int i;
2015-03-18 05:41:50 -07:00
srslte_cell_t cell;
2015-03-18 11:14:24 -07:00
srslte_pdsch_t pdsch;
2015-03-18 05:41:50 -07:00
srslte_chest_dl_t chest;
2015-03-18 11:14:24 -07:00
srslte_ofdm_t fft;
2014-12-27 14:09:41 -08:00
cf_t *input_fft, *input_signal;
int nof_re;
srslte_pdsch_cfg_t cfg;
srslte_softbuffer_rx_t softbuffer;
uint32_t rnti32;
uint32_t cfi;
2014-12-27 14:09:41 -08:00
2015-01-17 21:33:28 -08:00
if (nrhs < NOF_INPUTS) {
2014-12-27 14:09:41 -08:00
help();
return;
}
bzero(&cfg, sizeof(srslte_pdsch_cfg_t));
2014-12-27 14:09:41 -08:00
if (mexutils_read_cell(ENBCFG, &cell)) {
help();
return;
}
if (mexutils_read_uint32_struct(PDSCHCFG, "RNTI", &rnti32)) {
mexErrMsgTxt("Field RNTI not found in pdsch config\n");
return;
}
2014-12-27 14:09:41 -08:00
if (mexutils_read_uint32_struct(ENBCFG, "CFI", &cfi)) {
help();
return;
}
if (mexutils_read_uint32_struct(ENBCFG, "NSubframe", &cfg.sf_idx)) {
2014-12-27 14:09:41 -08:00
help();
return;
}
2015-03-18 11:14:24 -07:00
if (srslte_pdsch_init(&pdsch, cell)) {
mexErrMsgTxt("Error initiating PDSCH\n");
return;
}
2015-03-18 11:14:24 -07:00
srslte_pdsch_set_rnti(&pdsch, (uint16_t) (rnti32 & 0xffff));
if (srslte_softbuffer_rx_init(&softbuffer, cell)) {
mexErrMsgTxt("Error initiating soft buffer\n");
return;
}
2015-03-18 05:41:50 -07:00
if (srslte_chest_dl_init(&chest, cell)) {
mexErrMsgTxt("Error initializing equalizer\n");
2014-12-27 14:09:41 -08:00
return;
}
2015-04-08 01:50:01 -07:00
if (srslte_ofdm_rx_init(&fft, cell.cp, cell.nof_prb)) {
mexErrMsgTxt("Error initializing FFT\n");
2014-12-27 14:09:41 -08:00
return;
}
2015-04-05 07:32:35 -07:00
nof_re = 2 * SRSLTE_CP_NORM_NSYMB * cell.nof_prb * SRSLTE_NRE;
2014-12-27 14:09:41 -08:00
2015-07-19 13:52:56 -07:00
srslte_ra_dl_grant_t grant;
grant.mcs.tbs = mxGetScalar(TBS);
if (grant.mcs.tbs == 0) {
mexErrMsgTxt("Error trblklen is zero\n");
return;
}
2015-07-19 13:52:56 -07:00
if (srslte_cbsegm(&cfg.cb_segm, grant.mcs.tbs)) {
mexErrMsgTxt("Error computing CB segmentation\n");
return;
}
if (mexutils_read_uint32_struct(PDSCHCFG, "RV", &cfg.rv)) {
mexErrMsgTxt("Field RV not found in pdsch config\n");
2014-12-27 14:09:41 -08:00
return;
}
char *mod_str = mexutils_get_char_struct(PDSCHCFG, "Modulation");
if (!strcmp(mod_str, "QPSK")) {
2015-07-19 13:52:56 -07:00
grant.mcs.mod = SRSLTE_MOD_QPSK;
} else if (!strcmp(mod_str, "16QAM")) {
2015-07-19 13:52:56 -07:00
grant.mcs.mod = SRSLTE_MOD_16QAM;
} else if (!strcmp(mod_str, "64QAM")) {
2015-07-19 13:52:56 -07:00
grant.mcs.mod = SRSLTE_MOD_64QAM;
} else {
mexErrMsgTxt("Unknown modulation\n");
return;
}
mxFree(mod_str);
float *prbset;
mxArray *p;
p = mxGetField(PDSCHCFG, 0, "PRBSet");
if (!p) {
mexErrMsgTxt("Error field PRBSet not found\n");
return;
}
// Only localized PRB supported
2015-07-19 13:52:56 -07:00
grant.nof_prb = mexutils_read_f(p, &prbset);
for (i=0;i<cell.nof_prb;i++) {
2015-07-19 13:52:56 -07:00
grant.prb_idx[0][i] = false;
for (int j=0;j<grant.nof_prb && !grant.prb_idx[0][i];j++) {
if ((int) prbset[j] == i) {
2015-07-19 13:52:56 -07:00
grant.prb_idx[0][i] = true;
}
}
2015-07-19 13:52:56 -07:00
grant.prb_idx[1][i] = grant.prb_idx[0][i];
2014-12-27 14:09:41 -08:00
}
free(prbset);
/* Configure rest of pdsch_cfg parameters */
2015-07-19 13:52:56 -07:00
grant.Qm = srslte_mod_bits_x_symbol(grant.mcs.mod);
if (srslte_pdsch_cfg(&cfg, cell, &grant, cfi, cfg.sf_idx, (uint16_t) (rnti32 & 0xffff), cfg.rv)) {
fprintf(stderr, "Error configuring PDSCH\n");
exit(-1);
}
2014-12-27 14:09:41 -08:00
/** Allocate input buffers */
if (mexutils_read_cf(INPUT, &input_signal) < 0) {
mexErrMsgTxt("Error reading input signal\n");
return;
}
2015-03-18 11:14:24 -07:00
input_fft = srslte_vec_malloc(SRSLTE_SF_LEN_RE(cell.nof_prb, cell.cp) * sizeof(cf_t));
2014-12-27 14:09:41 -08:00
2015-03-18 05:41:50 -07:00
cf_t *ce[SRSLTE_MAX_PORTS];
2014-12-27 14:09:41 -08:00
for (i=0;i<cell.nof_ports;i++) {
2015-03-18 11:14:24 -07:00
ce[i] = srslte_vec_malloc(SRSLTE_SF_LEN_RE(cell.nof_prb, cell.cp) * sizeof(cf_t));
2014-12-27 14:09:41 -08:00
}
2015-04-08 01:50:01 -07:00
srslte_ofdm_rx_sf(&fft, input_signal, input_fft);
2014-12-27 14:09:41 -08:00
if (nrhs > NOF_INPUTS) {
cf_t *cearray = NULL;
2014-12-27 14:09:41 -08:00
nof_re = mexutils_read_cf(prhs[NOF_INPUTS], &cearray);
cf_t *cearray_ptr = cearray;
2014-12-27 14:09:41 -08:00
for (i=0;i<cell.nof_ports;i++) {
for (int j=0;j<nof_re/cell.nof_ports;j++) {
2014-12-27 14:09:41 -08:00
ce[i][j] = *cearray;
cearray++;
}
}
if (cearray_ptr)
free(cearray_ptr);
2014-12-27 14:09:41 -08:00
} else {
srslte_chest_dl_estimate(&chest, input_fft, ce, cfg.sf_idx);
2014-12-27 14:09:41 -08:00
}
float noise_power;
if (nrhs > NOF_INPUTS + 1) {
noise_power = mxGetScalar(prhs[NOF_INPUTS+1]);
} else {
2015-03-18 05:41:50 -07:00
noise_power = srslte_chest_dl_get_noise_estimate(&chest);
2014-12-27 14:09:41 -08:00
}
2015-07-19 13:52:56 -07:00
uint8_t *data = malloc(sizeof(uint8_t) * grant.mcs.tbs);
if (!data) {
return;
}
int r = srslte_pdsch_decode(&pdsch, &cfg, &softbuffer, input_fft, ce, noise_power, data);
2014-12-27 14:09:41 -08:00
if (nlhs >= 1) {
plhs[0] = mxCreateLogicalScalar(r == 0);
2014-12-27 14:09:41 -08:00
}
if (nlhs >= 2) {
2015-07-19 13:52:56 -07:00
mexutils_write_uint8(data, &plhs[1], grant.mcs.tbs, 1);
2014-12-27 14:09:41 -08:00
}
if (nlhs >= 3) {
mexutils_write_cf(pdsch.symbols[0], &plhs[2], cfg.nbits.nof_re, 1);
}
if (nlhs >= 4) {
mexutils_write_cf(pdsch.d, &plhs[3], cfg.nbits.nof_re, 1);
2014-12-27 14:09:41 -08:00
}
2015-01-17 21:33:28 -08:00
if (nlhs >= 5) {
mexutils_write_f(pdsch.e, &plhs[4], cfg.nbits.nof_bits, 1);
2015-01-17 21:33:28 -08:00
}
2014-12-27 14:09:41 -08:00
2015-03-18 05:41:50 -07:00
srslte_chest_dl_free(&chest);
2015-04-08 01:50:01 -07:00
srslte_ofdm_rx_free(&fft);
2015-03-18 11:14:24 -07:00
srslte_pdsch_free(&pdsch);
2014-12-27 14:09:41 -08:00
for (i=0;i<cell.nof_ports;i++) {
free(ce[i]);
}
free(data);
2014-12-27 14:09:41 -08:00
free(input_signal);
free(input_fft);
return;
}