srsLTE/examples/mib_test.c

328 lines
7.0 KiB
C
Raw Normal View History

/**
*
* \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/.
*
*/
2014-01-28 03:41:17 -08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "lte.h"
char *input_file_name = NULL;
2014-03-08 03:46:19 -08:00
int nof_slots=100;
float corr_peak_threshold=30;
2014-01-28 03:41:17 -08:00
int force_N_id_2=-1;
2014-01-31 06:35:43 -08:00
FILE *fmatlab;
2014-01-31 06:35:43 -08:00
#define NOF_PORTS 2
2014-01-31 06:35:43 -08:00
#define FLEN 9600
2014-01-28 03:41:17 -08:00
filesource_t fsrc;
cf_t *input_buffer, *fft_buffer, *ce[MAX_PORTS_CTRL];
2014-01-28 03:41:17 -08:00
pbch_t pbch;
2014-01-31 06:35:43 -08:00
lte_fft_t fft;
2014-01-28 03:41:17 -08:00
chest_t chest;
sync_t synch;
2014-03-03 15:54:02 -08:00
cfo_t cfocorr;
2014-01-28 03:41:17 -08:00
void usage(char *prog) {
printf("Usage: %s [onlt] -i input_file\n", prog);
2014-03-08 03:46:19 -08:00
printf("\t-n number of frames [Default %d]\n", nof_slots);
2014-01-28 03:41:17 -08:00
printf("\t-t correlation threshold [Default %g]\n", corr_peak_threshold);
printf("\t-v [set verbose to debug, default none]\n");
printf("\t-f force_N_id_2 [Default %d]\n", force_N_id_2);
}
void parse_args(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "intvf")) != -1) {
2014-01-28 03:41:17 -08:00
switch(opt) {
case 'i':
input_file_name = argv[optind];
break;
case 'n':
2014-03-08 03:46:19 -08:00
nof_slots = atoi(argv[optind]);
2014-01-28 03:41:17 -08:00
break;
case 't':
corr_peak_threshold = atof(argv[optind]);
break;
case 'v':
verbose++;
break;
case 'f':
force_N_id_2 = atoi(argv[optind]);
break;
default:
usage(argv[0]);
exit(-1);
}
}
if (!input_file_name) {
usage(argv[0]);
exit(-1);
}
}
int base_init() {
2014-01-31 06:35:43 -08:00
int i;
if (filesource_init(&fsrc, input_file_name, COMPLEX_FLOAT_BIN)) {
2014-01-28 03:41:17 -08:00
fprintf(stderr, "Error opening file %s\n", input_file_name);
exit(-1);
}
fmatlab = fopen("output.m", "w");
if (!fmatlab) {
perror("fopen");
return -1;
}
input_buffer = malloc(FLEN * sizeof(cf_t));
2014-01-28 03:41:17 -08:00
if (!input_buffer) {
perror("malloc");
exit(-1);
}
2014-01-31 06:35:43 -08:00
fft_buffer = malloc(CPNORM_NSYMB * 72 * sizeof(cf_t));
2014-01-28 03:41:17 -08:00
if (!fft_buffer) {
perror("malloc");
return -1;
}
for (i=0;i<MAX_PORTS_CTRL;i++) {
2014-01-31 06:35:43 -08:00
ce[i] = malloc(CPNORM_NSYMB * 72 * sizeof(cf_t));
if (!ce[i]) {
perror("malloc");
return -1;
}
}
2014-03-03 15:54:02 -08:00
if (cfo_init(&cfocorr, FLEN)) {
fprintf(stderr, "Error initiating CFO\n");
return -1;
}
if (chest_init(&chest, LINEAR, CPNORM, 6, NOF_PORTS)) {
2014-01-31 06:35:43 -08:00
fprintf(stderr, "Error initializing equalizer\n");
return -1;
}
if (lte_fft_init(&fft, CPNORM, 6)) {
fprintf(stderr, "Error initializing FFT\n");
2014-01-28 03:41:17 -08:00
return -1;
}
DEBUG("Memory init OK\n",0);
return 0;
}
void base_close() {
int i;
filesource_free(&fsrc);
fclose(fmatlab);
free(input_buffer);
free(fft_buffer);
sync_free(&synch);
filesource_free(&fsrc);
for (i=0;i<MAX_PORTS_CTRL;i++) {
free(ce[i]);
}
chest_free(&chest);
lte_fft_free(&fft);
}
2014-01-28 03:41:17 -08:00
int mib_decoder_init(int cell_id) {
2014-01-31 06:35:43 -08:00
if (chest_ref_LTEDL(&chest, cell_id)) {
fprintf(stderr, "Error initializing reference signal\n");
2014-01-28 03:41:17 -08:00
return -1;
}
if (pbch_init(&pbch, cell_id, CPNORM)) {
fprintf(stderr, "Error initiating PBCH\n");
return -1;
}
DEBUG("PBCH initiated cell_id=%d\n", cell_id);
return 0;
}
2014-01-31 06:35:43 -08:00
int mib_decoder_run(cf_t *input, pbch_mib_t *mib) {
int i, j;
2014-01-31 06:35:43 -08:00
lte_fft_run(&fft, input, fft_buffer);
fprintf(fmatlab, "outfft=");
vec_sc_prod_cfc(fft_buffer, 1000.0, fft_buffer, 72 * 7);
vec_fprint_c(fmatlab, fft_buffer, CP_NSYMB(CPNORM) * 6 * RE_X_RB);
fprintf(fmatlab, ";\n");
vec_sc_prod_cfc(fft_buffer, 0.001, fft_buffer, 72 * 7);
2014-01-31 06:35:43 -08:00
/* Get channel estimates for each port */
for (i=0;i<NOF_PORTS;i++) {
chest_ce_slot_port(&chest, fft_buffer, ce[i], 1, i);
chest_fprint(&chest, fmatlab, 1, i);
for (j=0;j<CP_NSYMB(CPNORM);j++) {
fprintf(fmatlab, "ce%d(%d,:)=", i, j + 1);
vec_fprint_c(fmatlab, &ce[i][j * 6 * RE_X_RB], 6 * RE_X_RB);
}
2014-01-28 03:41:17 -08:00
}
INFO("Decoding PBCH\n", 0);
return pbch_decode(&pbch, fft_buffer, ce, 6, 1, mib);
2014-01-28 03:41:17 -08:00
}
int get_samples(int length, int offset) {
int n = 0;
if (length != -1 && offset != -1) {
while(n < length) {
INFO("Reading %d samples offset=%d\n", length - n, offset + n);
2014-01-28 03:41:17 -08:00
n = filesource_read(&fsrc, &input_buffer[offset + n], length - n);
if (n == -1) {
fprintf(stderr, "Error reading %d samples from file\n", length - n);
break;
} else if (n == 0) {
printf("End of file\n");
return -1;
}
}
return n;
} else {
return -1;
}
}
enum radio_state { DONE, SYNC, MIB};
int main(int argc, char **argv) {
enum radio_state state;
int mib_idx;
2014-01-28 03:41:17 -08:00
int mib_attempts;
pbch_mib_t mib;
int cell_id, slot_id;
2014-01-28 03:41:17 -08:00
int frame_cnt;
float cfo;
int read_samples;
2014-01-28 03:41:17 -08:00
if (argc < 3) {
usage(argv[0]);
exit(-1);
}
parse_args(argc,argv);
if (base_init()) {
fprintf(stderr, "Error initializing memory\n");
exit(-1);
}
2014-01-31 06:35:43 -08:00
if (sync_init(&synch, FLEN)) {
2014-01-28 03:41:17 -08:00
fprintf(stderr, "Error initiating PSS/SSS\n");
exit(-1);
}
sync_force_N_id_2(&synch, force_N_id_2);
sync_set_threshold(&synch, corr_peak_threshold);
2014-01-31 06:35:43 -08:00
sync_pss_det_peakmean(&synch);
2014-01-28 03:41:17 -08:00
state = SYNC;
mib_idx = 0;
2014-01-28 03:41:17 -08:00
mib_attempts = 0;
frame_cnt = -1;
cfo = 0.0;
read_samples = FLEN;
slot_id = 0;
2014-01-28 03:41:17 -08:00
printf("\n\n-- Initiating MIB search --\n\n");
2014-03-08 03:46:19 -08:00
while(frame_cnt < nof_slots) {
if (get_samples(read_samples, 0) == -1) {
fprintf(stderr, "Error reading %d samples\n", FLEN);
2014-01-28 03:41:17 -08:00
break;
}
2014-03-03 15:54:02 -08:00
INFO("Correcting CFO=%.4f\n", cfo);
2014-03-03 15:54:02 -08:00
cfo_correct(&cfocorr, input_buffer, -cfo/128);
2014-01-28 03:41:17 -08:00
switch(state) {
case SYNC:
INFO("State Sync, Slot idx=%d\n", frame_cnt);
mib_idx = sync_run(&synch, input_buffer);
if (mib_idx != -1) {
2014-01-28 03:41:17 -08:00
cell_id = sync_get_cell_id(&synch);
cfo = sync_get_cfo(&synch);
slot_id = sync_get_slot_id(&synch);
read_samples = 0;
2014-01-28 03:41:17 -08:00
state = MIB;
if (mib_decoder_init(cell_id)) {
fprintf(stderr, "Error initiating MIB decoder\n");
exit(-1);
}
INFO("SYNC done, cell_id=%d mib_idx=%d frame_idx=%d\n", cell_id, mib_idx, frame_cnt);
2014-01-28 03:41:17 -08:00
}
break;
case MIB:
if (!read_samples) {
read_samples = FLEN;
}
INFO("State MIB, frame idx=%d MIB offset=%d slot_id=%d\n", frame_cnt, mib_idx, slot_id);
if (slot_id == 0) {
if (mib_decoder_run(&input_buffer[mib_idx], &mib)) {
INFO("MIB detected attempt=%d\n", mib_attempts+1);
2014-01-28 03:41:17 -08:00
state = DONE;
} else {
INFO("MIB not detected attempt=%d\n", mib_attempts+1);
2014-01-28 03:41:17 -08:00
}
mib_attempts++;
slot_id = 10;
} else {
slot_id = 0;
2014-01-28 03:41:17 -08:00
}
break;
case DONE:
INFO("State Done after %d frames\n", frame_cnt);
2014-01-28 03:41:17 -08:00
pbch_mib_fprint(stdout, &mib);
2014-03-08 03:46:19 -08:00
frame_cnt = nof_slots;
2014-01-28 03:41:17 -08:00
printf("Done\n");
break;
}
frame_cnt++;
2014-01-28 03:41:17 -08:00
}
base_close();
2014-01-28 03:41:17 -08:00
printf("Exit\n");
2014-01-28 03:41:17 -08:00
exit(0);
}