srsLTE/lib/examples/synch_file.c

270 lines
7.5 KiB
C
Raw Normal View History

2019-04-26 12:27:38 -07:00
/*
* Copyright 2013-2019 Software Radio Systems Limited
*
2019-04-26 12:27:38 -07:00
* This file is part of srsLTE.
*
* 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,
* 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.
*
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 <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/time.h>
#include "srslte/srslte.h"
2014-01-28 03:41:17 -08:00
char *input_file_name;
char *output_file_name="abs_corr.txt";
2014-05-13 08:00:49 -07:00
int nof_frames=100, frame_length=9600, symbol_sz=128;
2014-01-28 03:41:17 -08:00
float corr_peak_threshold=25.0;
int out_N_id_2 = 0, force_N_id_2=-1;
2014-06-17 02:11:41 -07:00
#define CFO_AUTO -9999.0
2014-01-28 03:41:17 -08:00
float force_cfo = CFO_AUTO;
void usage(char *prog) {
2014-06-17 02:11:41 -07:00
printf("Usage: %s [olntsNfcv] -i input_file\n", prog);
printf("\t-o output_file [Default %s]\n", output_file_name);
printf("\t-l frame_length [Default %d]\n", frame_length);
printf("\t-n number of frames [Default %d]\n", nof_frames);
printf("\t-t correlation threshold [Default %g]\n", corr_peak_threshold);
printf("\t-s symbol_sz [Default %d]\n", symbol_sz);
printf("\t-N out_N_id_2 [Default %d]\n", out_N_id_2);
printf("\t-f force_N_id_2 [Default %d]\n", force_N_id_2);
printf("\t-c force_cfo [Default disabled]\n");
2015-03-18 11:14:24 -07:00
printf("\t-v srslte_verbose\n");
2014-01-28 03:41:17 -08:00
}
void parse_args(int argc, char **argv) {
2014-06-17 02:11:41 -07:00
int opt;
while ((opt = getopt(argc, argv, "ionltsNfcv")) != -1) {
switch(opt) {
case 'i':
input_file_name = argv[optind];
break;
case 'o':
output_file_name = argv[optind];
break;
case 'n':
2019-11-28 07:09:15 -08:00
nof_frames = (int)strtol(argv[optind], NULL, 10);
2014-06-17 02:11:41 -07:00
break;
case 'l':
2019-11-28 07:09:15 -08:00
frame_length = (int)strtol(argv[optind], NULL, 10);
2014-06-17 02:11:41 -07:00
break;
case 't':
2019-11-28 07:09:15 -08:00
corr_peak_threshold = strtof(argv[optind], NULL);
2014-06-17 02:11:41 -07:00
break;
case 's':
2019-11-28 07:09:15 -08:00
symbol_sz = (int)strtol(argv[optind], NULL, 10);
2014-06-17 02:11:41 -07:00
break;
case 'N':
2019-11-28 07:09:15 -08:00
out_N_id_2 = (int)strtol(argv[optind], NULL, 10);
2014-06-17 02:11:41 -07:00
break;
case 'f':
2019-11-28 07:09:15 -08:00
force_N_id_2 = (int)strtol(argv[optind], NULL, 10);
2014-06-17 02:11:41 -07:00
break;
case 'c':
2019-11-28 07:09:15 -08:00
force_cfo = strtof(argv[optind], NULL);
2014-06-17 02:11:41 -07:00
break;
case 'v':
2015-03-18 11:14:24 -07:00
srslte_verbose++;
2014-06-17 02:11:41 -07:00
break;
default:
usage(argv[0]);
exit(-1);
}
}
if (!input_file_name) {
usage(argv[0]);
exit(-1);
}
2014-01-28 03:41:17 -08:00
}
int main(int argc, char **argv) {
2015-03-18 08:05:38 -07:00
srslte_filesource_t fsrc;
srslte_filesink_t fsink;
srslte_pss_t pss[3]; // One for each N_id_2
srslte_sss_t sss[3]; // One for each N_id_2
2015-03-18 11:14:24 -07:00
srslte_cfo_t cfocorr;
2014-06-17 02:11:41 -07:00
int peak_pos[3];
float *cfo;
float peak_value[3];
int frame_cnt;
cf_t *input;
uint32_t m0, m1;
2014-06-17 02:11:41 -07:00
float m0_value, m1_value;
uint32_t N_id_2;
int sss_idx;
2014-06-17 02:11:41 -07:00
struct timeval tdata[3];
int *exec_time;
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
if (argc < 3) {
usage(argv[0]);
exit(-1);
}
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
parse_args(argc,argv);
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
gettimeofday(&tdata[1], NULL);
printf("Initializing...");fflush(stdout);
2014-01-28 03:41:17 -08:00
2015-03-18 08:05:38 -07:00
if (srslte_filesource_init(&fsrc, input_file_name, SRSLTE_COMPLEX_FLOAT_BIN)) {
2019-04-23 01:53:11 -07:00
ERROR("Error opening file %s\n", input_file_name);
2014-06-17 02:11:41 -07:00
exit(-1);
}
2015-03-18 08:05:38 -07:00
if (srslte_filesink_init(&fsink, output_file_name, SRSLTE_COMPLEX_FLOAT_BIN)) {
2019-04-23 01:53:11 -07:00
ERROR("Error opening file %s\n", output_file_name);
2014-06-17 02:11:41 -07:00
exit(-1);
}
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
input = malloc(frame_length*sizeof(cf_t));
if (!input) {
perror("malloc");
exit(-1);
}
cfo = malloc(nof_frames*sizeof(float));
if (!cfo) {
perror("malloc");
exit(-1);
}
exec_time = malloc(nof_frames*sizeof(int));
if (!exec_time) {
perror("malloc");
exit(-1);
}
2014-01-28 03:41:17 -08:00
2015-03-18 11:14:24 -07:00
if (srslte_cfo_init(&cfocorr, frame_length)) {
2019-04-23 01:53:11 -07:00
ERROR("Error initiating CFO\n");
2014-06-17 02:11:41 -07:00
return -1;
}
2014-03-03 15:54:02 -08:00
2014-06-17 02:11:41 -07:00
/* We have 2 options here:
* a) We create 3 pss objects, each initialized with a different N_id_2
* b) We create 1 pss object which scans for each N_id_2 one after another.
* a) requries more memory but has less latency and is paralellizable.
*/
for (N_id_2=0;N_id_2<3;N_id_2++) {
if (srslte_pss_init_fft(&pss[N_id_2], frame_length, symbol_sz)) {
2019-04-23 01:53:11 -07:00
ERROR("Error initializing PSS object\n");
2014-06-17 02:11:41 -07:00
exit(-1);
}
if (srslte_pss_set_N_id_2(&pss[N_id_2], N_id_2)) {
2019-04-23 01:53:11 -07:00
ERROR("Error initializing N_id_2\n");
2014-06-17 02:11:41 -07:00
exit(-1);
}
if (srslte_sss_init(&sss[N_id_2], symbol_sz)) {
2019-04-23 01:53:11 -07:00
ERROR("Error initializing SSS object\n");
2014-06-17 02:11:41 -07:00
exit(-1);
}
if (srslte_sss_set_N_id_2(&sss[N_id_2], N_id_2)) {
2019-04-23 01:53:11 -07:00
ERROR("Error initializing N_id_2\n");
2014-06-17 02:11:41 -07:00
exit(-1);
}
}
gettimeofday(&tdata[2], NULL);
get_time_interval(tdata);
printf("done in %ld s %ld ms\n", tdata[0].tv_sec, tdata[0].tv_usec/1000);
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
printf("\n\tFr.Cnt\tN_id_2\tN_id_1\tSubf\tPSS Peak/Avg\tIdx\tm0\tm1\tCFO\n");
printf("\t===============================================================================\n");
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
/* read all file or nof_frames */
frame_cnt = 0;
2015-03-18 08:05:38 -07:00
while (frame_length == srslte_filesource_read(&fsrc, input, frame_length)
2014-06-17 02:11:41 -07:00
&& frame_cnt < nof_frames) {
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
gettimeofday(&tdata[1], NULL);
if (force_cfo != CFO_AUTO) {
2015-03-18 11:14:24 -07:00
srslte_cfo_correct(&cfocorr, input, input, force_cfo/128);
2014-06-17 02:11:41 -07:00
}
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
if (force_N_id_2 != -1) {
N_id_2 = force_N_id_2;
peak_pos[N_id_2] = srslte_pss_find_pss(&pss[N_id_2], input, &peak_value[N_id_2]);
2014-06-17 02:11:41 -07:00
} else {
for (N_id_2=0;N_id_2<3;N_id_2++) {
peak_pos[N_id_2] = srslte_pss_find_pss(&pss[N_id_2], input, &peak_value[N_id_2]);
2014-06-17 02:11:41 -07:00
}
float max_value=-99999;
N_id_2=-1;
int i;
for (i=0;i<3;i++) {
if (peak_value[i] > max_value) {
max_value = peak_value[i];
N_id_2 = i;
}
}
}
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
/* If peak detected */
if (peak_value[N_id_2] > corr_peak_threshold) {
2014-01-28 03:41:17 -08:00
2015-04-15 01:06:40 -07:00
sss_idx = peak_pos[N_id_2]-2*(symbol_sz+SRSLTE_CP_LEN(symbol_sz,SRSLTE_CP_NORM_LEN));
2014-06-17 02:11:41 -07:00
if (sss_idx >= 0) {
srslte_sss_m0m1_diff(&sss[N_id_2], &input[sss_idx],
2014-06-17 02:11:41 -07:00
&m0, &m0_value, &m1, &m1_value);
2014-01-28 03:41:17 -08:00
cfo[frame_cnt] = srslte_pss_cfo_compute(&pss[N_id_2], &input[peak_pos[N_id_2]-128]);
2014-06-17 02:11:41 -07:00
printf("\t%d\t%d\t%d\t%d\t%.3f\t\t%3d\t%d\t%d\t%.3f\n",
frame_cnt,
N_id_2,
srslte_sss_N_id_1(&sss[N_id_2], m0, m1, m1_value + m0_value),
srslte_sss_subframe(m0, m1),
peak_value[N_id_2],
peak_pos[N_id_2],
m0,
m1,
cfo[frame_cnt]);
2014-06-17 02:11:41 -07:00
}
}
gettimeofday(&tdata[2], NULL);
get_time_interval(tdata);
exec_time[frame_cnt] = tdata[0].tv_usec;
frame_cnt++;
}
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
int i;
float avg_time=0;
for (i=0;i<frame_cnt;i++) {
avg_time += (float) exec_time[i];
}
avg_time /= frame_cnt;
printf("\n");
printf("Average exec time: %.3f ms / frame. %.3f Msamp/s (%.3f\%% CPU)\n",
avg_time / 1000, frame_length / avg_time, 100 * avg_time / 5000 * (9600 / (float) frame_length ));
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
float cfo_mean=0;
for (i=0;i<frame_cnt;i++) {
cfo_mean += cfo[i] / frame_cnt * (9600 / frame_length);
}
printf("Average CFO: %.3f\n", cfo_mean);
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
for (N_id_2=0;N_id_2<3;N_id_2++) {
srslte_pss_free(&pss[N_id_2]);
srslte_sss_free(&sss[N_id_2]);
2014-06-17 02:11:41 -07:00
}
2014-01-28 03:41:17 -08:00
2015-03-18 08:05:38 -07:00
srslte_filesource_free(&fsrc);
srslte_filesink_free(&fsink);
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
free(input);
free(cfo);
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
printf("Done\n");
exit(0);
2014-01-28 03:41:17 -08:00
}