int main(int argc, char **argv) { cf_t *input_buffer, *sf_symbols = NULL; int frame_cnt; ue_sync_t s; int pos; pss_synch_t pss; float peak; struct timeval t[3]; float mean_ce_time=0; bool signal_detected; lte_fft_t fft; lte_cell_t cell; bzero(&cell, sizeof(lte_cell_t)); parse_args(argc, argv); #ifndef DISABLE_GRAPHICS if (!disable_plots) { init_plots(); } #endif input_init(); if (ue_sync_init(&s, cuhd_set_rx_srate, cuhd_recv_wrapper, uhd)) { fprintf(stderr, "Error initiating UE sync module\n"); exit(-1); } ue_sync_pbch_enable(&s, true); signal_detected = true; frame_cnt = 0; mean_ce_time=0; uint32_t valid_frames=0; //uint32_t unaligned = 0; while (frame_cnt < nof_frames || nof_frames == -1) { int n = ue_sync_get_buffer(&s, &input_buffer); if (n < 0) { fprintf(stderr, "Error calling sync work()\n"); exit(-1); } if (n == 1 && ue_sync_get_sfidx(&s) == 0) { if (signal_detected) { cell = ue_sync_get_cell(&s); pss_synch_init_fft(&pss, SF_LEN(lte_symbol_sz(cell.nof_prb), cell.cp), lte_symbol_sz(cell.nof_prb)); pss_synch_set_N_id_2(&pss, cell.id%3); sf_symbols = vec_malloc(SLOT_LEN_RE(cell.nof_prb, cell.cp) * sizeof(cf_t)); if (!sf_symbols) { perror("malloc"); exit(-1); } lte_fft_init(&fft, cell.cp, cell.nof_prb); signal_detected = false; } mean_ce_time = (float) (mean_ce_time + (float) t[0].tv_usec * valid_frames) / (valid_frames+1); valid_frames++; #ifndef DISABLE_GRAPHICS if (!disable_plots && !(valid_frames % 5) && sf_symbols) { /* Run FFT for the second slot */ lte_fft_run_slot(&fft, input_buffer, sf_symbols); int i; int nof_re = SLOT_LEN_RE(cell.nof_prb, cell.cp); for (i = 0; i < nof_re; i++) { tmp_plot[i] = 10 * log10f(cabsf(sf_symbols[i])); if (isinf(tmp_plot[i])) { tmp_plot[i] = -80; } } plot_real_setNewData(&poutfft, tmp_plot, nof_re); } #endif pos = pss_synch_find_pss(&pss, input_buffer, &peak, NULL); /*if (pos > 962 || pos < 958) { unaligned++; } */ printf("CELL_ID: %3d CFO: %+.4f KHz, SFO: %+.4f Khz, TimeOffset: %4d, Exec: %3.2f\r", cell.id, ue_sync_get_cfo(&s)/1000, ue_sync_get_sfo(&s)/1000, pos, s.mean_exec_time); fflush(stdout); if (VERBOSE_ISINFO()) { printf("\n"); } } frame_cnt++; } printf("\nBye\n"); exit(0); }
/* Setup USRP or input file */ int iodev_init(iodev_t *q, iodev_cfg_t *config, lte_cell_t *cell, pbch_mib_t *mib) { if (config->input_file_name) { mib->phich_resources = R_1; mib->phich_length = PHICH_NORM; cell->id = config->cell_id_file; cell->cp = CPNORM; cell->nof_ports = config->nof_ports_file; cell->nof_prb = config->nof_prb_file; if (filesource_init(&q->fsrc, config->input_file_name, COMPLEX_FLOAT_BIN)) { return LIBLTE_ERROR; } q->mode = FILESOURCE; int symbol_sz = lte_symbol_sz(cell->nof_prb); if (symbol_sz > 0) { q->sf_len = SF_LEN(symbol_sz); } else { fprintf(stderr, "Invalid number of PRB %d\n", cell->nof_prb); return LIBLTE_ERROR; } q->input_buffer_file = vec_malloc(q->sf_len * sizeof(cf_t)); if (!q->input_buffer_file) { perror("malloc"); return LIBLTE_ERROR; } q->sf_idx = 9; } else { #ifndef DISABLE_UHD printf("Opening UHD device...\n"); if (cuhd_open(config->uhd_args, &q->uhd)) { fprintf(stderr, "Error opening uhd\n"); return LIBLTE_ERROR; } cuhd_set_rx_gain(q->uhd, config->uhd_gain); /* set receiver frequency */ cuhd_set_rx_freq(q->uhd, (double) config->uhd_freq); cuhd_rx_wait_lo_locked(q->uhd); DEBUG("Set uhd_freq to %.3f MHz\n", (double ) config->uhd_freq); int n; ue_celldetect_t cd; ue_celldetect_result_t found_cells[3]; cf_t *buffer = vec_malloc(sizeof(cf_t) * 96000); if (!buffer) { perror("malloc"); return LIBLTE_ERROR; } if (ue_celldetect_init(&cd)) { fprintf(stderr, "Error initiating UE cell detect\n"); exit(-1); } n = find_cell(q->uhd, &cd, buffer, found_cells); if (n < 0) { fprintf(stderr, "Error searching cell\n"); exit(-1); } int max_peak_cell = 0; float max_peak_value = -1.0; if (n > 0) { for (int i=0;i<3;i++) { if (found_cells[i].peak > max_peak_value) { max_peak_value = found_cells[i].peak; max_peak_cell = i; } } if (decode_pbch(q->uhd, buffer, &found_cells[max_peak_cell], 400, mib)) { fprintf(stderr, "Could not decode PBCH from CELL ID %d\n", found_cells[max_peak_cell].cell_id); return LIBLTE_ERROR; } } else { fprintf(stderr, "Could not find any cell in this frequency\n"); return LIBLTE_ERROR; } free(buffer); cell->cp = found_cells[max_peak_cell].cp; cell->id = found_cells[max_peak_cell].cell_id; cell->nof_prb = mib->nof_prb; cell->nof_ports = mib->nof_ports; /* set sampling frequency */ int srate = lte_sampling_freq_hz(cell->nof_prb); if (srate != -1) { cuhd_set_rx_srate(q->uhd, (double) srate); } else { fprintf(stderr, "Invalid number of PRB %d\n", cell->nof_prb); return LIBLTE_ERROR; } DEBUG("Starting receiver...\n", 0); cuhd_start_rx_stream(q->uhd); if (ue_sync_init(&q->sframe, *cell, cuhd_recv_wrapper, q->uhd)) { fprintf(stderr, "Error initiating ue_sync\n"); return LIBLTE_ERROR; } /* Decodes the SSS signal during the tracking phase. Extra overhead, but makes sure we are in the correct subframe */ ue_sync_decode_sss_on_track(&q->sframe, true); // Here, the subframe length and input buffer is managed by ue_sync q->mode = UHD; #else printf("Error UHD not available. Select an input file\n"); return LIBLTE_ERROR; #endif } memcpy(&q->config, config, sizeof(iodev_cfg_t)); return LIBLTE_SUCCESS; }