Improved cell search program

master
ismagom 11 years ago
parent f4eeec3df7
commit dbc3195830

@ -46,32 +46,34 @@
#define FLEN 9600 #define FLEN 9600
#define FLEN_PERIOD 0.005 #define FLEN_PERIOD 0.005
#define MAX_EARFCN 1000
int band = -1; int band = -1;
int earfcn_start=-1, earfcn_end = -1; int earfcn_start=-1, earfcn_end = -1;
int nof_frames_find=200; int nof_frames_total = 50;
int nof_frames_detected = 10;
float threshold = -1;
float uhd_gain = 60.0; float uhd_gain = 60.0;
char *uhd_args=""; char *uhd_args="";
#define MAX_EARFCN 1000
lte_earfcn_t channels[MAX_EARFCN];
void usage(char *prog) { void usage(char *prog) {
printf("Usage: %s [asefgv] -b band\n", prog); printf("Usage: %s [agsendtvb] -b band\n", prog);
printf("\t-a UHD args [Default %s]\n", uhd_args); printf("\t-a UHD args [Default %s]\n", uhd_args);
printf("\t-g UHD gain [Default %.2f dB]\n", uhd_gain); printf("\t-g UHD gain [Default %.2f dB]\n", uhd_gain);
printf("\t-s earfcn_start [Default All]\n"); printf("\t-s earfcn_start [Default All]\n");
printf("\t-e earfcn_end [Default All]\n"); printf("\t-e earfcn_end [Default All]\n");
printf("\t-f nof_frames_find [Default %d]\n", nof_frames_find); printf("\t-n nof_frames_total [Default 100]\n");
printf("\t-d nof_frames_detected [Default 10]\n");
printf("\t-t threshold [Default %.2f]\n",threshold);
printf("\t-v [set verbose to debug, default none]\n"); printf("\t-v [set verbose to debug, default none]\n");
} }
void parse_args(int argc, char **argv) { void parse_args(int argc, char **argv) {
int opt; int opt;
while ((opt = getopt(argc, argv, "asefgvb")) != -1) { while ((opt = getopt(argc, argv, "agsendtvb")) != -1) {
switch(opt) { switch(opt) {
case 'a': case 'a':
uhd_args = argv[optind]; uhd_args = argv[optind];
@ -85,8 +87,14 @@ void parse_args(int argc, char **argv) {
case 'e': case 'e':
earfcn_end = atoi(argv[optind]); earfcn_end = atoi(argv[optind]);
break; break;
case 'f': case 'n':
nof_frames_find = atoi(argv[optind]); nof_frames_total = atoi(argv[optind]);
break;
case 'd':
nof_frames_detected = atoi(argv[optind]);
break;
case 't':
threshold = atof(argv[optind]);
break; break;
case 'g': case 'g':
uhd_gain = atof(argv[optind]); uhd_gain = atof(argv[optind]);
@ -105,26 +113,127 @@ void parse_args(int argc, char **argv) {
} }
} }
int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples) { int decode_pbch(void *uhd, cf_t *buffer, ue_celldetect_result_t *found_cell)
DEBUG(" ---- Receive %d samples ---- \n", nsamples); {
return cuhd_recv(h, data, nsamples, 1); ue_mib_t uemib;
pbch_mib_t mib;
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;
}
int find_cell(void *uhd, ue_celldetect_t *s, cf_t *buffer, ue_celldetect_result_t *found_cell)
{
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;
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);
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:
if (found_cell->peak > 0) {
printf("\n\tCELL ID: %d, CP: %s, Peak: %.2f, Mode: %d/%d\n",
found_cell->cell_id, lte_cp_string(found_cell->cp),
found_cell->peak, found_cell->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 && n != CS_CELL_DETECTED);
INFO("Stopping receiver...\n", 0);
cuhd_stop_rx_stream(uhd);
cuhd_flush_buffer(uhd);
return n;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
int ret; int n;
int frame_cnt;
int nof_freqs;
uint32_t freq;
ue_sync_t uesync;
void *uhd; void *uhd;
ue_celldetect_t s;
ue_celldetect_result_t found_cell;
cf_t *buffer; cf_t *buffer;
lte_cell_t cell; int nof_freqs;
lte_earfcn_t channels[MAX_EARFCN];
if (argc < 3) { uint32_t freq;
usage(argv[0]);
exit(-1);
}
parse_args(argc, argv); parse_args(argc, argv);
@ -133,8 +242,6 @@ int main(int argc, char **argv) {
fprintf(stderr, "Error opening uhd\n"); fprintf(stderr, "Error opening uhd\n");
exit(-1); exit(-1);
} }
/* set uhd_gain */
cuhd_set_rx_gain(uhd, uhd_gain); cuhd_set_rx_gain(uhd, uhd_gain);
nof_freqs = lte_band_get_fd_band(band, channels, earfcn_start, earfcn_end, MAX_EARFCN); nof_freqs = lte_band_get_fd_band(band, channels, earfcn_start, earfcn_end, MAX_EARFCN);
@ -143,59 +250,58 @@ int main(int argc, char **argv) {
exit(-1); exit(-1);
} }
buffer = vec_malloc(sizeof(cf_t) * 96000);
if (!buffer) {
perror("malloc");
return LIBLTE_ERROR;
}
for (freq=0;freq<nof_freqs;freq++) { if (ue_celldetect_init(&s)) {
/* set freq */ fprintf(stderr, "Error initiating UE sync module\n");
cuhd_stop_rx_stream(uhd);
cuhd_set_rx_freq(uhd, (double) channels[freq].fd * MHZ);
cuhd_rx_wait_lo_locked(uhd);
if (ue_sync_init(&uesync, cell, cuhd_recv_wrapper, uhd)) {
fprintf(stderr, "Error initiating UE sync\n");
exit(-1); exit(-1);
} }
if (threshold > 0) {
ue_celldetect_set_threshold(&s, threshold);
}
ue_sync_decode_sss_on_track(&uesync, true); if (nof_frames_total > 0) {
ue_celldetect_set_nof_frames_total(&s, nof_frames_total);
DEBUG("Starting receiver...\n",0); }
cuhd_start_rx_stream(uhd); if (nof_frames_detected > 0) {
usleep(10000); ue_celldetect_set_nof_frames_detected(&s, nof_frames_detected);
}
for (freq=0;freq<nof_freqs;freq+=10) {
/* Receive up to a maximum of nof_frames_find */ /* set uhd_freq */
frame_cnt = 0; cuhd_set_rx_freq(uhd, (double) channels[freq].fd * MHZ);
ret = 0; cuhd_rx_wait_lo_locked(uhd);
ue_sync_reset(&uesync); usleep(10000);
INFO("Set uhd_freq to %.3f MHz\n", (double) channels[freq].fd * MHZ/1000000);
while(frame_cnt < nof_frames_find && ret == 0) {
ret = ue_sync_get_buffer(&uesync, &buffer);
if (ret < 0) {
fprintf(stderr, "Error calling ue_sync_work()\n");
exit(-1);
}
frame_cnt++;
printf("[%3d/%d]: EARFCN %d Freq. %.2f MHz looking for PSS. \r", freq, nof_freqs, printf("[%3d/%d]: EARFCN %d Freq. %.2f MHz looking for PSS. \r", freq, nof_freqs,
channels[freq].id, channels[freq].fd);fflush(stdout); channels[freq].id, channels[freq].fd);fflush(stdout);
if (VERBOSE_ISINFO()) { if (VERBOSE_ISINFO()) {
printf("\n"); printf("\n");
} }
n = find_cell(uhd, &s, buffer, &found_cell);
if (n < 0) {
fprintf(stderr, "Error searching cell\n");
exit(-1);
} }
if (ret == 1) { if (n == CS_CELL_DETECTED) {
if (VERBOSE_ISINFO()) { if (decode_pbch(uhd, buffer, &found_cell)) {
printf("[%3d/%d]: EARFCN %d Freq. %.2f MHz FOUND MIB ", freq, nof_freqs, fprintf(stderr, "Error decoding PBCH\n");
channels[freq].id, channels[freq].fd); exit(-1);
} }
printf("CFO: %+.4f KHz\n", ue_sync_get_cfo(&uesync));
printf("\n");fflush(stdout);
} }
ue_sync_free(&uesync);
} }
ue_celldetect_free(&s);
cuhd_close(uhd); cuhd_close(uhd);
printf("\n\nDone\n");
exit(0); exit(0);
} }

@ -95,7 +95,7 @@
#include "liblte/phy/ue/ue_sync.h" #include "liblte/phy/ue/ue_sync.h"
#include "liblte/phy/ue/ue_mib.h" #include "liblte/phy/ue/ue_mib.h"
#include "liblte/phy/ue/ue_cellsearch.h" #include "liblte/phy/ue/ue_celldetect.h"
#include "liblte/phy/ue/ue_dl.h" #include "liblte/phy/ue/ue_dl.h"
#include "liblte/phy/scrambling/scrambling.h" #include "liblte/phy/scrambling/scrambling.h"

@ -42,7 +42,7 @@
* This object scans a signal for LTE cells using the known PSS * This object scans a signal for LTE cells using the known PSS
* and SSS sequences. * and SSS sequences.
* *
* The function ue_cellsearch_scan() shall be called multiple times, * The function ue_celldetect_scan() shall be called multiple times,
* each passing a number of samples multiple of 4800, sampled at 960 KHz * each passing a number of samples multiple of 4800, sampled at 960 KHz
* (that is, 5 ms of samples). * (that is, 5 ms of samples).
* *
@ -74,7 +74,7 @@ typedef struct LIBLTE_API {
lte_cp_t cp; lte_cp_t cp;
float peak; float peak;
uint32_t mode; uint32_t mode;
} ue_cellsearch_result_t; } ue_celldetect_result_t;
typedef struct LIBLTE_API { typedef struct LIBLTE_API {
@ -92,35 +92,35 @@ typedef struct LIBLTE_API {
uint32_t *mode_ntimes; uint32_t *mode_ntimes;
char *mode_counted; char *mode_counted;
ue_cellsearch_result_t *candidates; ue_celldetect_result_t *candidates;
} ue_cellsearch_t; } ue_celldetect_t;
LIBLTE_API int ue_cellsearch_init(ue_cellsearch_t *q); LIBLTE_API int ue_celldetect_init(ue_celldetect_t *q);
LIBLTE_API int ue_cellsearch_init_max(ue_cellsearch_t *q, LIBLTE_API int ue_celldetect_init_max(ue_celldetect_t *q,
uint32_t max_frames_total, uint32_t max_frames_total,
uint32_t max_frames_detected); uint32_t max_frames_detected);
LIBLTE_API void ue_cellsearch_free(ue_cellsearch_t *q); LIBLTE_API void ue_celldetect_free(ue_celldetect_t *q);
LIBLTE_API void ue_cellsearch_reset(ue_cellsearch_t *q); LIBLTE_API void ue_celldetect_reset(ue_celldetect_t *q);
LIBLTE_API int ue_cellsearch_scan(ue_cellsearch_t *q, LIBLTE_API int ue_celldetect_scan(ue_celldetect_t *q,
cf_t *signal, cf_t *signal,
uint32_t nsamples, uint32_t nsamples,
ue_cellsearch_result_t *found_cell); ue_celldetect_result_t *found_cell);
LIBLTE_API int ue_cellsearch_set_nof_frames_total(ue_cellsearch_t *q, LIBLTE_API int ue_celldetect_set_nof_frames_total(ue_celldetect_t *q,
uint32_t nof_frames); uint32_t nof_frames);
LIBLTE_API int ue_cellsearch_set_nof_frames_detected(ue_cellsearch_t *q, LIBLTE_API int ue_celldetect_set_nof_frames_detected(ue_celldetect_t *q,
uint32_t nof_frames); uint32_t nof_frames);
LIBLTE_API void ue_cellsearch_set_threshold(ue_cellsearch_t *q, LIBLTE_API void ue_celldetect_set_threshold(ue_celldetect_t *q,
float threshold); float threshold);
LIBLTE_API void ue_cellsearch_reset(ue_cellsearch_t *q); LIBLTE_API void ue_celldetect_reset(ue_celldetect_t *q);

@ -143,83 +143,43 @@ lte_cp_t sync_get_cp(sync_t *q) {
return q->cp; return q->cp;
} }
static lte_cp_t detect_cp(cf_t *input, uint32_t peak_pos)
{
return CPNORM;
}
int sync_sss(sync_t *q, cf_t *input, uint32_t peak_pos) { int sync_sss(sync_t *q, cf_t *input, uint32_t peak_pos) {
uint32_t m0, m1; uint32_t m0, m1;
int sss_idx_n, sss_idx_e, ret; int sss_idx, ret;
float m0_value_e, m1_value_e,m0_value_n, m1_value_n; float m0_value, m1_value;
uint32_t sf_idx_e, N_id_1_e, sf_idx_n, N_id_1_n;
sss_synch_set_N_id_2(&q->sss, q->N_id_2); sss_synch_set_N_id_2(&q->sss, q->N_id_2);
/* Make sure we have enough room to find SSS sequence */
sss_idx_n = (int) peak_pos - 2*(q->fft_size + CP(q->fft_size, CPNORM_LEN));
sss_idx_e = (int) peak_pos - 2*(q->fft_size + CP(q->fft_size, CPEXT_LEN));
if (q->detect_cp) { if (q->detect_cp) {
if (sss_idx_n < 0 || sss_idx_e < 0) { q->cp = detect_cp(input, peak_pos);
INFO("Not enough room to decode SSS (%d, %d)\n", sss_idx_n, sss_idx_e);
return LIBLTE_SUCCESS;
}
} else {
if (CP_ISNORM(q->cp)) {
if (sss_idx_n < 0) {
INFO("Not enough room to decode normal CP SSS (sss_idx=%d, peak_pos=%d)\n", sss_idx_n, peak_pos);
return LIBLTE_SUCCESS;
}
} else {
if (sss_idx_e < 0) {
INFO("Not enough room to decode extended CP SSS (sss_idx=%d, peak_pos=%d)\n", sss_idx_e, peak_pos);
return LIBLTE_SUCCESS;
}
}
} }
sf_idx_n = 0; /* Make sure we have enough room to find SSS sequence */
sf_idx_e = 0; sss_idx = (int) peak_pos - 2*(q->fft_size + CP(q->fft_size, q->cp));
N_id_1_n = 0;
N_id_1_e = 0;
/* try Normal CP length */
if (q->detect_cp || CP_ISNORM(q->cp)) {
sss_synch_m0m1(&q->sss, &input[sss_idx_n], &m0, &m0_value_n, &m1, &m1_value_n);
sf_idx_n = sss_synch_subframe(m0, m1); if (sss_idx < 0) {
ret = sss_synch_N_id_1(&q->sss, m0, m1); INFO("Not enough room to decode CP SSS (sss_idx=%d, peak_pos=%d)\n", sss_idx, peak_pos);
if (ret >= 0) { return LIBLTE_SUCCESS;
N_id_1_n = (uint32_t) ret;
} else {
N_id_1_n = 1000;
}
} }
if (q->detect_cp || CP_ISEXT(q->cp)) { /* try Normal CP length */
/* Now try Extended CP length */ sss_synch_m0m1(&q->sss, &input[sss_idx], &m0, &m0_value, &m1, &m1_value);
sss_synch_m0m1(&q->sss, &input[sss_idx_e], &m0, &m0_value_e, &m1, &m1_value_e);
sf_idx_e = sss_synch_subframe(m0, m1); q->sf_idx = sss_synch_subframe(m0, m1);
ret = sss_synch_N_id_1(&q->sss, m0, m1); ret = sss_synch_N_id_1(&q->sss, m0, m1);
if (ret >= 0) { if (ret >= 0) {
N_id_1_e = (uint32_t) ret; q->N_id_1 = (uint32_t) ret;
} else { } else {
N_id_1_e = 1000; q->N_id_1 = 1000;
}
}
/* Correlation with extended CP hypoteshis is greater than with normal? */
if ((q->detect_cp && m0_value_e * m1_value_e > m0_value_n * m1_value_n)
|| CP_ISEXT(q->cp)) {
q->cp = CPEXT;
q->sf_idx = sf_idx_e;
q->N_id_1 = N_id_1_e;
/* otherwise is normal CP */
} else {
q->cp = CPNORM;
q->sf_idx = sf_idx_n;
q->N_id_1 = N_id_1_n;
} }
DEBUG("SSS detected N_id_1=%d, sf_idx=%d, position=%d/%d %s CP\n", DEBUG("SSS detected N_id_1=%d, sf_idx=%d, %s CP\n",
q->N_id_1, q->sf_idx, sss_idx_n, sss_idx_e, CP_ISNORM(q->cp)?"Normal":"Extended"); q->N_id_1, q->sf_idx, CP_ISNORM(q->cp)?"Normal":"Extended");
return 1; return 1;
} }
@ -276,7 +236,7 @@ int sync_find(sync_t *q, cf_t *input, uint32_t find_offset, uint32_t *peak_posit
} }
} }
} else { } else {
printf("Warning: no space for CFO computation\n"); INFO("Warning: no space for CFO computation\n",0);
} }
if (peak_position) { if (peak_position) {

@ -31,7 +31,7 @@
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include "liblte/phy/ue/ue_cellsearch.h" #include "liblte/phy/ue/ue_celldetect.h"
#include "liblte/phy/utils/debug.h" #include "liblte/phy/utils/debug.h"
#include "liblte/phy/utils/vector.h" #include "liblte/phy/utils/vector.h"
@ -39,19 +39,19 @@
#define FIND_FFTSIZE 64 #define FIND_FFTSIZE 64
#define FIND_SFLEN 5*SF_LEN(FIND_FFTSIZE) #define FIND_SFLEN 5*SF_LEN(FIND_FFTSIZE)
int ue_cellsearch_init(ue_cellsearch_t * q) { int ue_celldetect_init(ue_celldetect_t * q) {
return ue_cellsearch_init_max(q, CS_DEFAULT_MAXFRAMES_TOTAL, CS_DEFAULT_MAXFRAMES_DETECTED); return ue_celldetect_init_max(q, CS_DEFAULT_MAXFRAMES_TOTAL, CS_DEFAULT_MAXFRAMES_DETECTED);
} }
int ue_cellsearch_init_max(ue_cellsearch_t * q, uint32_t max_frames_total, uint32_t max_frames_detected) { int ue_celldetect_init_max(ue_celldetect_t * q, uint32_t max_frames_total, uint32_t max_frames_detected) {
int ret = LIBLTE_ERROR_INVALID_INPUTS; int ret = LIBLTE_ERROR_INVALID_INPUTS;
if (q != NULL) { if (q != NULL) {
ret = LIBLTE_ERROR; ret = LIBLTE_ERROR;
bzero(q, sizeof(ue_cellsearch_t)); bzero(q, sizeof(ue_celldetect_t));
q->candidates = malloc(sizeof(ue_cellsearch_result_t) * max_frames_detected); q->candidates = malloc(sizeof(ue_celldetect_result_t) * max_frames_detected);
if (!q->candidates) { if (!q->candidates) {
perror("malloc"); perror("malloc");
goto clean_exit; goto clean_exit;
@ -78,19 +78,19 @@ int ue_cellsearch_init_max(ue_cellsearch_t * q, uint32_t max_frames_total, uint3
q->nof_frames_total = CS_DEFAULT_NOFFRAMES_TOTAL; q->nof_frames_total = CS_DEFAULT_NOFFRAMES_TOTAL;
q->nof_frames_detected = CS_DEFAULT_NOFFRAMES_DETECTED; q->nof_frames_detected = CS_DEFAULT_NOFFRAMES_DETECTED;
ue_cellsearch_reset(q); ue_celldetect_reset(q);
ret = LIBLTE_SUCCESS; ret = LIBLTE_SUCCESS;
} }
clean_exit: clean_exit:
if (ret == LIBLTE_ERROR) { if (ret == LIBLTE_ERROR) {
ue_cellsearch_free(q); ue_celldetect_free(q);
} }
return ret; return ret;
} }
void ue_cellsearch_free(ue_cellsearch_t * q) void ue_celldetect_free(ue_celldetect_t * q)
{ {
if (q->candidates) { if (q->candidates) {
free(q->candidates); free(q->candidates);
@ -105,21 +105,21 @@ void ue_cellsearch_free(ue_cellsearch_t * q)
} }
void ue_cellsearch_reset(ue_cellsearch_t * q) void ue_celldetect_reset(ue_celldetect_t * q)
{ {
q->current_nof_detected = 0; q->current_nof_detected = 0;
q->current_nof_total = 0; q->current_nof_total = 0;
q->current_N_id_2 = 0; q->current_N_id_2 = 0;
} }
void ue_cellsearch_set_threshold(ue_cellsearch_t * q, float threshold) void ue_celldetect_set_threshold(ue_celldetect_t * q, float threshold)
{ {
sync_set_threshold(&q->sfind, threshold); sync_set_threshold(&q->sfind, threshold);
} }
int ue_cellsearch_set_nof_frames_total(ue_cellsearch_t * q, uint32_t nof_frames) int ue_celldetect_set_nof_frames_total(ue_celldetect_t * q, uint32_t nof_frames)
{ {
if (nof_frames > q->max_frames_total) { if (nof_frames <= q->max_frames_total) {
q->nof_frames_total = nof_frames; q->nof_frames_total = nof_frames;
return LIBLTE_SUCCESS; return LIBLTE_SUCCESS;
} else { } else {
@ -127,9 +127,9 @@ int ue_cellsearch_set_nof_frames_total(ue_cellsearch_t * q, uint32_t nof_frames)
} }
} }
int ue_cellsearch_set_nof_frames_detected(ue_cellsearch_t * q, uint32_t nof_frames) int ue_celldetect_set_nof_frames_detected(ue_celldetect_t * q, uint32_t nof_frames)
{ {
if (nof_frames > q->max_frames_detected) { if (nof_frames <= q->max_frames_detected) {
q->nof_frames_detected = nof_frames; q->nof_frames_detected = nof_frames;
return LIBLTE_SUCCESS; return LIBLTE_SUCCESS;
} else { } else {
@ -138,7 +138,7 @@ int ue_cellsearch_set_nof_frames_detected(ue_cellsearch_t * q, uint32_t nof_fram
} }
/* Decide the most likely cell based on the mode */ /* Decide the most likely cell based on the mode */
void decide_cell(ue_cellsearch_t * q, ue_cellsearch_result_t *found_cell) void decide_cell(ue_celldetect_t * q, ue_celldetect_result_t *found_cell)
{ {
uint32_t i, j; uint32_t i, j;
@ -186,10 +186,10 @@ void decide_cell(ue_cellsearch_t * q, ue_cellsearch_result_t *found_cell)
found_cell->mode = q->mode_ntimes[mode_pos]; found_cell->mode = q->mode_ntimes[mode_pos];
} }
int ue_cellsearch_scan(ue_cellsearch_t * q, int ue_celldetect_scan(ue_celldetect_t * q,
cf_t *signal, cf_t *signal,
uint32_t nsamples, uint32_t nsamples,
ue_cellsearch_result_t *found_cell) ue_celldetect_result_t *found_cell)
{ {
int ret = LIBLTE_ERROR_INVALID_INPUTS; int ret = LIBLTE_ERROR_INVALID_INPUTS;
uint32_t peak_idx; uint32_t peak_idx;

@ -29,8 +29,8 @@ IF(${CUHD_FIND} GREATER -1)
ADD_EXECUTABLE(ue_sync_usrp ue_sync_usrp.c) ADD_EXECUTABLE(ue_sync_usrp ue_sync_usrp.c)
TARGET_LINK_LIBRARIES(ue_sync_usrp lte_phy cuhd) TARGET_LINK_LIBRARIES(ue_sync_usrp lte_phy cuhd)
ADD_EXECUTABLE(ue_cell_detect ue_cell_detect.c) ADD_EXECUTABLE(ue_celldetect_mib_test ue_celldetect_mib_test.c)
TARGET_LINK_LIBRARIES(ue_cell_detect lte_phy cuhd) TARGET_LINK_LIBRARIES(ue_celldetect_mib_test lte_phy cuhd)
ENDIF(${CUHD_FIND} GREATER -1) ENDIF(${CUHD_FIND} GREATER -1)
IF(${GRAPHICS_FIND} EQUAL -1) IF(${GRAPHICS_FIND} EQUAL -1)

@ -40,7 +40,6 @@
#include "liblte/phy/phy.h" #include "liblte/phy/phy.h"
#include "liblte/cuhd/cuhd.h" #include "liblte/cuhd/cuhd.h"
void *uhd;
int nof_frames_total = CS_DEFAULT_NOFFRAMES_TOTAL; int nof_frames_total = CS_DEFAULT_NOFFRAMES_TOTAL;
int nof_frames_detected = CS_DEFAULT_NOFFRAMES_DETECTED; int nof_frames_detected = CS_DEFAULT_NOFFRAMES_DETECTED;
@ -95,83 +94,96 @@ void parse_args(int argc, char **argv) {
} }
} }
void input_init() { int decode_pbch(void *uhd, cf_t *buffer, ue_celldetect_result_t *found_cell)
{
printf("Opening UHD device...\n"); ue_mib_t uemib;
if (cuhd_open(uhd_args, &uhd)) { pbch_mib_t mib;
fprintf(stderr, "Error opening uhd\n"); int n;
exit(-1);
}
cuhd_set_rx_gain(uhd, uhd_gain);
/* set uhd_freq */ uint32_t nof_frames = 0;
cuhd_set_rx_freq(uhd, (double) uhd_freq); uint32_t flen = MIB_FRAME_SIZE;
cuhd_rx_wait_lo_locked(uhd);
DEBUG("Set uhd_freq to %.3f MHz\n", (double ) uhd_freq/1000000);
INFO("Setting sampling frequency 960 KHz for PSS search\n", 0); if (ue_mib_init(&uemib, found_cell->cell_id, found_cell->cp)) {
cuhd_set_rx_srate(uhd, 960000.0); fprintf(stderr, "Error initiating PBCH decoder\n");
return LIBLTE_ERROR;
}
DEBUG("Starting receiver...\n", 0); 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); cuhd_start_rx_stream(uhd);
do {
if (cuhd_recv(uhd, buffer, flen, 1)<0) {
fprintf(stderr, "Error receiving from USRP\n");
return LIBLTE_ERROR;
} }
int main(int argc, char **argv) { INFO("Calling ue_mib_decode() %d/%d\n", nof_frames, nof_frames_total);
ue_cellsearch_t s;
ue_mib_t uemib;
ue_cellsearch_result_t found_cell;
pbch_mib_t mib;
uint32_t flen;
cf_t *buffer;
int n;
parse_args(argc, argv);
input_init();
// allocate for the maximum size (10 ms at 1.92 MHz for PBCH decoding) n = ue_mib_decode(&uemib, buffer, flen, &mib);
buffer = vec_malloc(sizeof(cf_t) * 19200*30); if (n == LIBLTE_ERROR || n == LIBLTE_ERROR_INVALID_INPUTS) {
if (!buffer) { fprintf(stderr, "Error calling ue_mib_decode()\n");
perror("malloc"); return LIBLTE_ERROR;
exit(-1);
} }
if (n == MIB_FRAME_UNALIGNED) {
if (ue_cellsearch_init(&s)) { printf("Realigning frame\n");
fprintf(stderr, "Error initiating UE sync module\n"); if (cuhd_recv(uhd, buffer, flen/2, 1)<0) {
exit(-1); fprintf(stderr, "Error receiving from USRP\n");
return LIBLTE_ERROR;
} }
if (threshold > 0) {
ue_cellsearch_set_threshold(&s, threshold);
} }
nof_frames++;
if (nof_frames_total > 0) { } while (n != MIB_FOUND && nof_frames < nof_frames_total);
ue_cellsearch_set_nof_frames_total(&s, 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");
} }
if (nof_frames_detected > 0) {
ue_cellsearch_set_nof_frames_detected(&s, nof_frames_detected); cuhd_stop_rx_stream(uhd);
cuhd_flush_buffer(uhd);
ue_mib_free(&uemib);
return LIBLTE_SUCCESS;
} }
int find_cell(void *uhd, ue_celldetect_t *s, cf_t *buffer, ue_celldetect_result_t *found_cell)
{
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 nof_scanned_cells = 0;
flen = 4800; uint32_t flen = 4800;
do { do {
if (cuhd_recv(uhd, buffer, flen, 1)<0) { if (cuhd_recv(uhd, buffer, flen, 1)<0) {
fprintf(stderr, "Error receiving from USRP\n"); fprintf(stderr, "Error receiving from USRP\n");
exit(-1); return LIBLTE_ERROR;
} }
n = ue_cellsearch_scan(&s, buffer, flen, &found_cell); n = ue_celldetect_scan(s, buffer, flen, found_cell);
switch(n) { switch(n) {
case CS_FRAME_UNALIGNED: case CS_FRAME_UNALIGNED:
fprintf(stderr, "Unaliged frame!! Exiting\n"); printf("Realigning frame\n");
exit(-1); 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: case CS_CELL_DETECTED:
if (found_cell->peak > 0) {
printf("\tCELL ID: %d, CP: %s, Peak: %.2f, Mode: %d/%d\n", printf("\tCELL ID: %d, CP: %s, Peak: %.2f, Mode: %d/%d\n",
found_cell.cell_id, lte_cp_string(found_cell.cp), found_cell->cell_id, lte_cp_string(found_cell->cp),
found_cell.peak, found_cell.mode, s.nof_frames_detected); found_cell->peak, found_cell->mode, s->nof_frames_detected);
}
nof_scanned_cells++; nof_scanned_cells++;
break; break;
case CS_CELL_NOT_DETECTED: case CS_CELL_NOT_DETECTED:
@ -180,65 +192,72 @@ int main(int argc, char **argv) {
case LIBLTE_ERROR: case LIBLTE_ERROR:
case LIBLTE_ERROR_INVALID_INPUTS: case LIBLTE_ERROR_INVALID_INPUTS:
fprintf(stderr, "Error calling cellsearch_scan()\n"); fprintf(stderr, "Error calling cellsearch_scan()\n");
exit(-1); return LIBLTE_ERROR;
} }
} while(nof_scanned_cells < 3 && n != CS_CELL_DETECTED); } while(nof_scanned_cells < 3 && n != CS_CELL_DETECTED);
if (n == CS_CELL_DETECTED) {
INFO("Stopping receiver...\n", 0); INFO("Stopping receiver...\n", 0);
cuhd_stop_rx_stream(uhd); cuhd_stop_rx_stream(uhd);
cuhd_flush_buffer(uhd); cuhd_flush_buffer(uhd);
if (ue_mib_init(&uemib, found_cell.cell_id, found_cell.cp)) { return n;
fprintf(stderr, "Error initiating PBCH decoder\n");
exit(-1);
} }
INFO("Setting sampling frequency 1.92 MHz for PBCH decoding\n", 0); int main(int argc, char **argv) {
cuhd_set_rx_srate(uhd, 1920000.0); int n;
INFO("Starting receiver...\n", 0); void *uhd;
cuhd_start_rx_stream(uhd); ue_celldetect_t s;
ue_celldetect_result_t found_cell;
usleep(50000); cf_t *buffer;
uint32_t nof_frames = 0; parse_args(argc, argv);
flen = MIB_FRAME_SIZE;
do { printf("Opening UHD device...\n");
if (cuhd_recv(uhd, buffer, flen, 1)<0) { if (cuhd_open(uhd_args, &uhd)) {
fprintf(stderr, "Error receiving from USRP\n"); fprintf(stderr, "Error opening uhd\n");
exit(-1); exit(-1);
} }
cuhd_set_rx_gain(uhd, uhd_gain);
INFO("Calling ue_mib_decode() %d/%d\n", nof_frames, nof_frames_total); /* set uhd_freq */
cuhd_set_rx_freq(uhd, (double) uhd_freq);
cuhd_rx_wait_lo_locked(uhd);
DEBUG("Set uhd_freq to %.3f MHz\n", (double ) uhd_freq/1000000);
n = ue_mib_decode(&uemib, buffer, flen, &mib); buffer = vec_malloc(sizeof(cf_t) * 96000);
if (n == LIBLTE_ERROR || n == LIBLTE_ERROR_INVALID_INPUTS) { if (!buffer) {
fprintf(stderr, "Error calling ue_mib_decode()\n"); perror("malloc");
exit(-1); return LIBLTE_ERROR;
} }
if (n == MIB_FRAME_UNALIGNED) {
printf("Realigning frame\n"); if (ue_celldetect_init(&s)) {
if (cuhd_recv(uhd, buffer, flen/2, 1)<0) { fprintf(stderr, "Error initiating UE sync module\n");
fprintf(stderr, "Error receiving from USRP\n");
exit(-1); exit(-1);
} }
if (threshold > 0) {
ue_celldetect_set_threshold(&s, threshold);
} }
nof_frames++;
} while (n != MIB_FOUND && nof_frames < nof_frames_total); if (nof_frames_total > 0) {
if (n == MIB_FOUND) { ue_celldetect_set_nof_frames_total(&s, nof_frames_total);
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");
} }
if (nof_frames_detected > 0) {
ue_celldetect_set_nof_frames_detected(&s, nof_frames_detected);
} }
n = find_cell(uhd, &s, buffer, &found_cell);
if (n < 0) {
fprintf(stderr, "Error searching cell\n");
exit(-1);
}
if (n == CS_CELL_DETECTED) {
if (decode_pbch(uhd, buffer, &found_cell)) {
fprintf(stderr, "Error decoding PBCH\n");
exit(-1);
}
}
ue_celldetect_free(&s);
ue_mib_free(&uemib);
ue_cellsearch_free(&s);
cuhd_close(uhd); cuhd_close(uhd);
exit(0); exit(0);
} }
Loading…
Cancel
Save