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

163 lines
4.1 KiB
C
Raw Normal View History

2014-12-17 12:52:58 -08:00
/**
*
* \section COPYRIGHT
*
2015-05-08 08:05:40 -07:00
* Copyright 2013-2015 The srsLTE Developers. See the
2014-12-17 12:52:58 -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-17 12:52:58 -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-17 12:52:58 -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-16 09:30:37 -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-17 12:52:58 -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-17 12:52:58 -08:00
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
2014-12-16 09:30:37 -08:00
*/
#include <string.h>
#include "srslte/srslte.h"
#include "srslte/mex/mexutils.h"
2014-12-16 09:30:37 -08:00
/** MEX function to be called from MATLAB to test the channel estimator
*/
#define ENBCFG prhs[0]
#define INPUT prhs[1]
#define NOF_INPUTS 2
void help()
{
mexErrMsgTxt
("[decoded_ok, symbols, bits] = srslte_pbch(enbConfig, rxWaveform)\n\n");
2014-12-16 09:30:37 -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_pbch_t pbch;
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-16 09:30:37 -08:00
cf_t *input_symbols, *input_fft;
int nof_re;
2015-03-18 05:41:50 -07:00
cf_t *ce[SRSLTE_MAX_PORTS], *ce_slot[SRSLTE_MAX_PORTS];
2014-12-16 09:30:37 -08:00
if (nrhs < NOF_INPUTS) {
2014-12-16 09:30:37 -08:00
help();
return;
}
if (mexutils_read_cell(ENBCFG, &cell)) {
help();
return;
}
// Read input symbols
mexutils_read_cf(INPUT, &input_symbols);
2015-03-18 05:59:29 -07:00
nof_re = SRSLTE_SF_LEN_RE(cell.nof_prb, cell.cp);
2014-12-16 09:30:37 -08:00
// Allocate memory
2015-03-18 11:14:24 -07:00
input_fft = srslte_vec_malloc(nof_re * sizeof(cf_t));
2015-03-18 05:41:50 -07:00
for (i=0;i<SRSLTE_MAX_PORTS;i++) {
2015-03-18 11:14:24 -07:00
ce[i] = srslte_vec_malloc(nof_re * sizeof(cf_t));
2014-12-16 09:30:37 -08:00
}
2015-03-18 05:41:50 -07:00
if (srslte_chest_dl_init(&chest, cell)) {
2014-12-16 09:30:37 -08:00
fprintf(stderr, "Error initializing equalizer\n");
return;
}
2015-04-08 01:50:01 -07:00
if (srslte_ofdm_rx_init(&fft, cell.cp, cell.nof_prb)) {
2014-12-16 09:30:37 -08:00
fprintf(stderr, "Error initializing FFT\n");
return;
}
2015-03-18 11:14:24 -07:00
if (srslte_pbch_init(&pbch, cell)) {
2014-12-16 09:30:37 -08:00
fprintf(stderr, "Error initiating PBCH\n");
return;
}
2015-04-08 01:50:01 -07:00
srslte_ofdm_rx_sf(&fft, input_symbols, input_fft);
if (nrhs > NOF_INPUTS) {
cf_t *cearray;
mexutils_read_cf(prhs[NOF_INPUTS], &cearray);
for (i=0;i<cell.nof_ports;i++) {
for (int j=0;j<nof_re;j++) {
ce[i][j] = *cearray;
cearray++;
}
}
} else {
2015-03-18 05:41:50 -07:00
srslte_chest_dl_estimate(&chest, input_fft, ce, 0);
}
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);
}
2015-03-18 05:41:50 -07:00
for (int i=0;i<SRSLTE_MAX_PORTS;i++) {
2015-03-18 05:59:29 -07:00
ce_slot[i] = &ce[i][SRSLTE_SLOT_LEN_RE(cell.nof_prb, cell.cp)];
2014-12-16 09:30:37 -08:00
}
uint32_t nof_ports;
2015-03-18 11:14:24 -07:00
int n = srslte_pbch_decode(&pbch, &input_fft[SRSLTE_SLOT_LEN_RE(cell.nof_prb, cell.cp)],
ce_slot, noise_power,
2014-12-16 09:30:37 -08:00
NULL, &nof_ports, NULL);
if (nlhs >= 1) {
if (n == 1) {
plhs[0] = mxCreateDoubleScalar(nof_ports);
} else {
plhs[0] = mxCreateDoubleScalar(0);
}
}
if (nlhs >= 2) {
2015-03-18 11:14:24 -07:00
mexutils_write_cf(pbch.d, &plhs[1], pbch.nof_symbols, 1);
2014-12-16 09:30:37 -08:00
}
if (nlhs >= 3) {
2015-03-18 11:14:24 -07:00
mexutils_write_f(pbch.llr, &plhs[2], 2*pbch.nof_symbols, 1);
2014-12-16 09:30:37 -08:00
}
if (nlhs >= 4) {
2015-03-18 05:59:29 -07:00
mexutils_write_cf(ce[0], &plhs[3], SRSLTE_SF_LEN_RE(cell.nof_prb,cell.cp)/14, 14);
2014-12-16 09:30:37 -08:00
}
if (nlhs >= 5) {
2015-03-18 05:59:29 -07:00
mexutils_write_cf(ce[1], &plhs[4], SRSLTE_SF_LEN_RE(cell.nof_prb,cell.cp)/14, 14);
2014-12-16 09:30:37 -08:00
}
if (nlhs >= 6) {
2015-03-18 11:14:24 -07:00
mexutils_write_cf(pbch.symbols[0], &plhs[5], pbch.nof_symbols, 1);
}
if (nlhs >= 7) {
mexutils_write_cf(pbch.ce[0], &plhs[6], pbch.nof_symbols, 1);
}
2014-12-16 09:30:37 -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_pbch_free(&pbch);
2014-12-16 09:30:37 -08:00
for (i=0;i<cell.nof_ports;i++) {
free(ce[i]);
}
free(input_symbols);
free(input_fft);
return;
}