int find_cell(void *uhd, ue_celldetect_t *s, cf_t *buffer, ue_celldetect_result_t found_cell[3]) { int n; INFO("Setting sampling frequency 960 KHz for PSS search\n", 0); cuhd_set_rx_srate(uhd, 960000.0); INFO("Starting receiver...\n", 0); cuhd_start_rx_stream(uhd); uint32_t nof_scanned_cells = 0; uint32_t flen = 4800; int nof_detected_cells = 0; do { if (cuhd_recv(uhd, buffer, flen, 1)<0) { fprintf(stderr, "Error receiving from USRP\n"); return LIBLTE_ERROR; } n = ue_celldetect_scan(s, buffer, flen, &found_cell[nof_scanned_cells]); switch(n) { case CS_FRAME_UNALIGNED: printf("Realigning frame\n"); if (cuhd_recv(uhd, buffer, flen/2, 1)<0) { fprintf(stderr, "Error receiving from USRP\n"); return LIBLTE_ERROR; } return LIBLTE_ERROR; case CS_CELL_DETECTED: nof_detected_cells++; if (found_cell[nof_scanned_cells].peak > 0) { printf("\n\tCELL ID: %d, CP: %s, Peak: %.2f, Mode: %d/%d\n", found_cell[nof_scanned_cells].cell_id, lte_cp_string(found_cell[nof_scanned_cells].cp), found_cell[nof_scanned_cells].peak, found_cell[nof_scanned_cells].mode, s->nof_frames_detected); } nof_scanned_cells++; break; case CS_CELL_NOT_DETECTED: nof_scanned_cells++; break; case LIBLTE_ERROR: case LIBLTE_ERROR_INVALID_INPUTS: fprintf(stderr, "Error calling cellsearch_scan()\n"); return LIBLTE_ERROR; } } while(nof_scanned_cells < 3); INFO("Stopping receiver...\n", 0); cuhd_stop_rx_stream(uhd); cuhd_flush_buffer(uhd); return nof_detected_cells; }
int decode_pbch(void *uhd, cf_t *buffer, ue_celldetect_result_t *found_cell, uint32_t nof_frames_total, pbch_mib_t *mib) { ue_mib_t uemib; int n; bzero(mib, sizeof(pbch_mib_t)); uint32_t nof_frames = 0; uint32_t flen = MIB_FRAME_SIZE; if (ue_mib_init(&uemib, found_cell->cell_id, found_cell->cp)) { fprintf(stderr, "Error initiating PBCH decoder\n"); return LIBLTE_ERROR; } INFO("Setting sampling frequency 1.92 MHz for PBCH decoding\n", 0); cuhd_set_rx_srate(uhd, 1920000.0); INFO("Starting receiver...\n", 0); cuhd_start_rx_stream(uhd); do { if (cuhd_recv(uhd, buffer, flen, 1)<0) { fprintf(stderr, "Error receiving from USRP\n"); return LIBLTE_ERROR; } INFO("Calling ue_mib_decode() %d/%d\n", nof_frames, nof_frames_total); n = ue_mib_decode(&uemib, buffer, flen, mib); if (n == LIBLTE_ERROR || n == LIBLTE_ERROR_INVALID_INPUTS) { fprintf(stderr, "Error calling ue_mib_decode()\n"); return LIBLTE_ERROR; } if (n == MIB_FRAME_UNALIGNED) { printf("Realigning frame\n"); if (cuhd_recv(uhd, buffer, flen/2, 1)<0) { fprintf(stderr, "Error receiving from USRP\n"); return LIBLTE_ERROR; } } nof_frames++; } while (n != MIB_FOUND && nof_frames < 2*nof_frames_total); if (n == MIB_FOUND) { printf("\n\nMIB decoded in %d ms (%d half frames)\n", nof_frames*5, nof_frames); pbch_mib_fprint(stdout, mib, found_cell->cell_id); } else { printf("\nCould not decode MIB\n"); } cuhd_stop_rx_stream(uhd); cuhd_flush_buffer(uhd); ue_mib_free(&uemib); return LIBLTE_SUCCESS; }
void cuhd_flush_buffer(void *h) { int n; _Complex float tmp[1024]; do { n = cuhd_recv(h, tmp, 1024, 0); } while (n > 0); }
int cuhd_rssi_scan(void *uhd, float *freqs, float *rssi, int nof_bands, double fs, int nsamp) { int i, j; int ret = -1; _Complex float *buffer; double f; buffer = calloc(nsamp, sizeof(_Complex float)); if (!buffer) { goto free_and_exit; } cuhd_set_rx_gain(uhd, 20.0); cuhd_set_rx_srate(uhd, fs); for (i=0;i<nof_bands;i++) { cuhd_stop_rx_stream(uhd); f = (double) freqs[i]; cuhd_set_rx_freq(uhd, f); cuhd_rx_wait_lo_locked(uhd); usleep(10000); cuhd_start_rx_stream(uhd); /* discard first samples */ for (j=0;j<2;j++) { if (cuhd_recv(uhd, buffer, nsamp, 1) != nsamp) { goto free_and_exit; } } rssi[i] = srslte_vec_avg_power_cf(buffer, nsamp); printf("[%3d]: Freq %4.1f Mhz - RSSI: %3.2f dBm\r", i, f/1000000, 10*log10f(rssi[i]) + 30); fflush(stdout); if (SRSLTE_VERBOSE_ISINFO()) { printf("\n"); } } cuhd_stop_rx_stream(uhd); ret = 0; free_and_exit: free(buffer); return ret; }
int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples, srslte_timestamp_t *q) { DEBUG(" ---- Receive %d samples ---- \n", nsamples); return cuhd_recv(h, data, nsamples, 1); }
int main(int argc, char **argv) { cf_t *buffer; int frame_cnt, n; void *uhd; srslte_pss_synch_t pss; srslte_cfo_t cfocorr, cfocorr64; srslte_sss_synch_t sss; int32_t flen; int peak_idx, last_peak; float peak_value; float mean_peak; uint32_t nof_det, nof_nodet, nof_nopeak, nof_nopeakdet; cf_t ce[SRSLTE_PSS_LEN]; parse_args(argc, argv); if (N_id_2_sync == -1) { N_id_2_sync = cell_id%3; } uint32_t N_id_2 = cell_id%3; uint32_t N_id_1 = cell_id/3; #ifndef DISABLE_GRAPHICS if (!disable_plots) init_plots(); #endif float srate = 15000.0*fft_size; flen = srate*5/1000; printf("Opening UHD device...\n"); if (cuhd_open(uhd_args, &uhd)) { fprintf(stderr, "Error opening uhd\n"); exit(-1); } if (srate < 10e6) { cuhd_set_master_clock_rate(uhd, 4*srate); } else { cuhd_set_master_clock_rate(uhd, srate); } printf("Set RX rate: %.2f MHz\n", cuhd_set_rx_srate(uhd, srate) / 1000000); printf("Set RX gain: %.1f dB\n", cuhd_set_rx_gain(uhd, uhd_gain)); printf("Set RX freq: %.2f MHz\n", cuhd_set_rx_freq(uhd, uhd_freq) / 1000000); cuhd_rx_wait_lo_locked(uhd); buffer = malloc(sizeof(cf_t) * flen * 2); if (!buffer) { perror("malloc"); exit(-1); } if (srslte_pss_synch_init_fft(&pss, flen, fft_size)) { fprintf(stderr, "Error initiating PSS\n"); exit(-1); } if (srslte_pss_synch_set_N_id_2(&pss, N_id_2_sync)) { fprintf(stderr, "Error setting N_id_2=%d\n",N_id_2_sync); exit(-1); } srslte_cfo_init(&cfocorr, flen); srslte_cfo_init(&cfocorr64, flen); if (srslte_sss_synch_init(&sss, fft_size)) { fprintf(stderr, "Error initializing SSS object\n"); return SRSLTE_ERROR; } srslte_sss_synch_set_N_id_2(&sss, N_id_2); printf("N_id_2: %d\n", N_id_2); cuhd_start_rx_stream(uhd); printf("Frame length %d samples\n", flen); printf("PSS detection threshold: %.2f\n", threshold); nof_det = nof_nodet = nof_nopeak = nof_nopeakdet = 0; frame_cnt = 0; last_peak = 0; mean_peak = 0; int peak_offset = 0; float cfo; float mean_cfo = 0; uint32_t m0, m1; uint32_t sss_error1 = 0, sss_error2 = 0, sss_error3 = 0; uint32_t cp_is_norm = 0; srslte_sync_t ssync; bzero(&ssync, sizeof(srslte_sync_t)); ssync.fft_size = fft_size; while(frame_cnt < nof_frames || nof_frames == -1) { n = cuhd_recv(uhd, buffer, flen - peak_offset, 1); if (n < 0) { fprintf(stderr, "Error receiving samples\n"); exit(-1); } peak_idx = srslte_pss_synch_find_pss(&pss, buffer, &peak_value); if (peak_idx < 0) { fprintf(stderr, "Error finding PSS peak\n"); exit(-1); } mean_peak = SRSLTE_VEC_CMA(peak_value, mean_peak, frame_cnt); if (peak_value >= threshold) { nof_det++; if (peak_idx >= fft_size) { // Estimate CFO cfo = srslte_pss_synch_cfo_compute(&pss, &buffer[peak_idx-fft_size]); mean_cfo = SRSLTE_VEC_CMA(cfo, mean_cfo, frame_cnt); // Correct CFO srslte_cfo_correct(&cfocorr, buffer, buffer, -mean_cfo / fft_size); // Estimate channel if (srslte_pss_synch_chest(&pss, &buffer[peak_idx-fft_size], ce)) { fprintf(stderr, "Error computing channel estimation\n"); exit(-1); } // Find SSS int sss_idx = peak_idx-2*fft_size-(SRSLTE_CP_ISNORM(cp)?SRSLTE_CP_LEN(fft_size, SRSLTE_CP_NORM_LEN):SRSLTE_CP_LEN(fft_size, SRSLTE_CP_EXT_LEN)); if (sss_idx >= 0 && sss_idx < flen-fft_size) { srslte_sss_synch_m0m1_partial(&sss, &buffer[sss_idx], 3, NULL, &m0, &m0_value, &m1, &m1_value); if (srslte_sss_synch_N_id_1(&sss, m0, m1) != N_id_1) { sss_error2++; } INFO("Partial N_id_1: %d\n", srslte_sss_synch_N_id_1(&sss, m0, m1)); srslte_sss_synch_m0m1_diff(&sss, &buffer[sss_idx], &m0, &m0_value, &m1, &m1_value); if (srslte_sss_synch_N_id_1(&sss, m0, m1) != N_id_1) { sss_error3++; } INFO("Diff N_id_1: %d\n", srslte_sss_synch_N_id_1(&sss, m0, m1)); srslte_sss_synch_m0m1_partial(&sss, &buffer[sss_idx], 1, NULL, &m0, &m0_value, &m1, &m1_value); if (srslte_sss_synch_N_id_1(&sss, m0, m1) != N_id_1) { sss_error1++; } INFO("Full N_id_1: %d\n", srslte_sss_synch_N_id_1(&sss, m0, m1)); } // Estimate CP if (peak_idx > 2*(fft_size + SRSLTE_CP_LEN_EXT(fft_size))) { srslte_cp_t cp = srslte_sync_detect_cp(&ssync, buffer, peak_idx); if (SRSLTE_CP_ISNORM(cp)) { cp_is_norm++; } } } else { INFO("No space for CFO computation. Frame starts at \n",peak_idx); } if(srslte_sss_synch_subframe(m0,m1) == 0) { #ifndef DISABLE_GRAPHICS if (!disable_plots) do_plots_sss(sss.corr_output_m0, sss.corr_output_m1); #endif } } else { nof_nodet++; } if (frame_cnt > 100) { if (abs(last_peak-peak_idx) > 4) { if (peak_value >= threshold) { nof_nopeakdet++; } nof_nopeak++; } } frame_cnt++; printf("[%5d]: Pos: %5d, PSR: %4.1f (~%4.1f) Pdet: %4.2f, " "FA: %4.2f, CFO: %+4.1f KHz SSSmiss: %4.2f/%4.2f/%4.2f CPNorm: %.0f%%\r", frame_cnt, peak_idx, peak_value, mean_peak, (float) nof_det/frame_cnt, (float) nof_nopeakdet/frame_cnt, mean_cfo*15, (float) sss_error1/nof_det,(float) sss_error2/nof_det,(float) sss_error3/nof_det, (float) cp_is_norm/nof_det * 100); if (SRSLTE_VERBOSE_ISINFO()) { printf("\n"); } #ifndef DISABLE_GRAPHICS if (!disable_plots) do_plots(pss.conv_output_avg, pss.conv_output_avg[peak_idx], pss.fft_size+pss.frame_size-1, ce); #endif last_peak = peak_idx; } srslte_pss_synch_free(&pss); free(buffer); cuhd_close(uhd); printf("Ok\n"); exit(0); }
int main(int argc, char **argv) { int frame_cnt, valid_frames; int freq; int cell_id; sync_t sfind, strack; float max_peak_to_avg; float sfo; int find_idx, track_idx, last_found; enum sync_state state; int n; filesink_t fs; if (argc < 3) { usage(argv[0]); exit(-1); } parse_args(argc,argv); if (base_init(FLEN)) { fprintf(stderr, "Error initializing memory\n"); exit(-1); } if (sync_init(&sfind, FLEN)) { fprintf(stderr, "Error initiating PSS/SSS\n"); exit(-1); } sync_pss_det_peak_to_avg(&sfind); if (sync_init(&strack, track_len)) { fprintf(stderr, "Error initiating PSS/SSS\n"); exit(-1); } sync_pss_det_peak_to_avg(&strack); nof_bands = lte_band_get_fd_band(band, channels, earfcn_start, earfcn_end, MAX_EARFCN); printf("RSSI scan: %d freqs in band %d, RSSI threshold %.2f dBm\n", nof_bands, band, rssi_threshold); n = rssi_scan(); if (n == -1) { exit(-1); } printf("\nDone. Starting PSS search on %d channels\n", n); usleep(500000); INFO("Setting sampling frequency %.2f MHz\n", (float) SAMP_FREQ/MHZ); cuhd_set_rx_srate(uhd, SAMP_FREQ); cuhd_set_rx_gain(uhd, uhd_gain); print_to_matlab(); filesink_init(&fs, "test.dat", COMPLEX_FLOAT_BIN); freq=0; state = INIT; find_idx = 0; max_peak_to_avg = 0; last_found = 0; frame_cnt = 0; while(freq<nof_bands) { /* scan only bands above rssi_threshold */ if (!IS_SIGNAL(freq)) { INFO("[%3d/%d]: Skipping EARFCN %d %.2f MHz RSSI %.2f dB\n", freq, nof_bands, channels[freq].id, channels[freq].fd,10*log10f(rssi[freq]) + 30); freq++; } else { if (state == TRACK || state == FIND) { cuhd_recv(uhd, &input_buffer[FLEN], FLEN, 1); } switch(state) { case INIT: DEBUG("Stopping receiver...\n",0); cuhd_stop_rx_stream(uhd); /* set freq */ cuhd_set_rx_freq(uhd, (double) channels[freq].fd * MHZ); cuhd_rx_wait_lo_locked(uhd); DEBUG("Set freq to %.3f MHz\n", (double) channels[freq].fd); DEBUG("Starting receiver...\n",0); cuhd_start_rx_stream(uhd); /* init variables */ frame_cnt = 0; max_peak_to_avg = -99; cell_id = -1; /* receive first frame */ cuhd_recv(uhd, input_buffer, FLEN, 1); /* set find_threshold and go to FIND state */ sync_set_threshold(&sfind, find_threshold); sync_force_N_id_2(&sfind, -1); state = FIND; break; case FIND: /* find peak in all frame */ find_idx = sync_run(&sfind, &input_buffer[FLEN]); DEBUG("[%3d/%d]: PAR=%.2f\n", freq, nof_bands, sync_get_peak_to_avg(&sfind)); if (find_idx != -1) { /* if found peak, go to track and set lower threshold */ frame_cnt = -1; last_found = 0; sync_set_threshold(&strack, track_threshold); sync_force_N_id_2(&strack, sync_get_N_id_2(&sfind)); state = TRACK; INFO("[%3d/%d]: EARFCN %d Freq. %.2f MHz PSS found PAR %.2f dB\n", freq, nof_bands, channels[freq].id, channels[freq].fd, 10*log10f(sync_get_peak_to_avg(&sfind))); } else { if (frame_cnt >= nof_frames_find) { state = INIT; printf("[%3d/%d]: EARFCN %d Freq. %.2f MHz No PSS found\r", freq, nof_bands, channels[freq].id, channels[freq].fd, frame_cnt - last_found); if (VERBOSE_ISINFO()) { printf("\n"); } freq++; } } break; case TRACK: INFO("Tracking PSS find_idx %d offset %d\n", find_idx, find_idx + track_len); filesink_write(&fs, &input_buffer[FLEN+find_idx+track_len], track_len); track_idx = sync_run(&strack, &input_buffer[FLEN + find_idx - track_len]); p2a_v[frame_cnt] = sync_get_peak_to_avg(&strack); /* save cell id for the best peak-to-avg */ if (p2a_v[frame_cnt] > max_peak_to_avg) { max_peak_to_avg = p2a_v[frame_cnt]; cell_id = sync_get_cell_id(&strack); } if (track_idx != -1) { cfo_v[frame_cnt] = sync_get_cfo(&strack); last_found = frame_cnt; find_idx += track_idx - track_len; idx_v[frame_cnt] = find_idx; } else { idx_v[frame_cnt] = -1; cfo_v[frame_cnt] = 0.0; } /* if we missed to many PSS it is not a cell, next freq */ if (frame_cnt - last_found > max_track_lost) { INFO("\n[%3d/%d]: EARFCN %d Freq. %.2f MHz %d frames lost\n", freq, nof_bands, channels[freq].id, channels[freq].fd, frame_cnt - last_found); state = INIT; freq++; } else if (frame_cnt >= nof_frames_track) { state = DONE; } break; case DONE: cfo[freq] = mean_valid(idx_v, cfo_v, frame_cnt); p2a[freq] = mean_valid(idx_v, p2a_v, frame_cnt); valid_frames = preprocess_idx(idx_v, idx_valid, t, frame_cnt); sfo = sfo_estimate_period(idx_valid, t, valid_frames, FLEN_PERIOD); printf("\n[%3d/%d]: FOUND EARFCN %d Freq. %.2f MHz. " "PAR %2.2f dB, CFO=%+.2f KHz, SFO=%+2.3f KHz, CELL_ID=%3d\n", freq, nof_bands, channels[freq].id, channels[freq].fd, 10*log10f(p2a[freq]), cfo[freq] * 15, sfo / 1000, cell_id); state = INIT; freq++; break; } if (state == TRACK || (state == FIND && frame_cnt)) { memcpy(input_buffer, &input_buffer[FLEN], FLEN * sizeof(cf_t)); } frame_cnt++; } } print_to_matlab(); sync_free(&sfind); base_free(); printf("\n\nDone\n"); exit(0); }