Improved PRACH test USRP

master
Xavier Arteaga 4 years ago committed by Xavier Arteaga
parent cd463d1b87
commit 258f7a180c

@ -32,45 +32,72 @@
#define MAX_LEN 70176 #define MAX_LEN 70176
uint32_t nof_prb = 25; // PRACH Parameters
uint32_t preamble_format = 0; static uint32_t nof_prb = 25;
uint32_t root_seq_idx = 0; static uint32_t preamble_format = 0;
uint32_t seq_idx = 0; static uint32_t root_seq_idx = 0;
uint32_t frequency_offset = 0; static uint32_t seq_idx = 0;
uint32_t zero_corr_zone = 11; static uint32_t frequency_offset = 0;
float timeadv = 0; static uint32_t zero_corr_zone = 11;
uint32_t nof_frames = 20; static bool high_speed_flag = false;
uint32_t num_ra_preambles = 0; // use default
// Simulation parameters
float uhd_rx_gain = 40, uhd_tx_gain = 60, uhd_freq = 2.4e9; static float timeadv = 0;
char* uhd_args = ""; static uint32_t num_ra_preambles = 0; // use default
char* output_filename = "prach_rx"; static uint32_t nof_repetitions = 1;
static bool continous_tx = true;
static char* output_filename = NULL;
static const uint32_t nof_frames = 20;
static const uint32_t tx_delay_ms = 4;
// RF parameters
static float uhd_rx_gain = 40, uhd_tx_gain = 60, uhd_freq = 2.4e9;
static char* uhd_args = "";
// SRSLTE Verbose
SRSLTE_API extern int srslte_verbose;
void usage(char* prog) void usage(char* prog)
{ {
printf("Usage: %s \n", prog); printf("Usage: %s \n", prog);
printf("\t-a UHD args [Default %s]\n", uhd_args); printf(" -a UHD args [Default %s]\n", uhd_args);
printf("\t-f UHD TX/RX frequency [Default %.2f MHz]\n", uhd_freq / 1e6); printf(" -c Continous Tx? [Default %s]\n", continous_tx ? "true" : "false");
printf("\t-g UHD RX gain [Default %.1f dB]\n", uhd_rx_gain); printf(" -f UHD TX/RX frequency [Default %.2f MHz]\n", uhd_freq / 1e6);
printf("\t-G UHD TX gain [Default %.1f dB]\n", uhd_tx_gain); printf(" -g UHD RX gain [Default %.1f dB]\n", uhd_rx_gain);
printf("\t-p Number of UL RB [Default %d]\n", nof_prb); printf(" -G UHD TX gain [Default %.1f dB]\n", uhd_tx_gain);
printf("\t-F Preamble format [Default %d]\n", preamble_format); printf(" -p Number of UL RB [Default %d]\n", nof_prb);
printf("\t-O Frequency offset [Default %d]\n", frequency_offset); printf(" -F Preamble format [Default %d]\n", preamble_format);
printf("\t-s sequence index [Default %d]\n", seq_idx); printf(" -O Frequency offset [Default %d]\n", frequency_offset);
printf("\t-r Root sequence index [Default %d]\n", root_seq_idx); printf(" -s sequence index [Default %d]\n", seq_idx);
printf("\t-t Time advance (us) [Default %.1f us]\n", timeadv); printf(" -r Root sequence index [Default %d]\n", root_seq_idx);
printf("\t-z Zero correlation zone config [Default %d]\n", zero_corr_zone); printf(" -R Number of repetitions of %d ms [Default %d]\n", nof_frames, nof_repetitions);
printf("\t-o Save transmitted PRACH in file [Default no]\n"); printf(" -t Time advance (us) [Default %.1f us]\n", timeadv);
printf(" -z Zero correlation zone config [Default %d]\n", zero_corr_zone);
printf(" -o Save transmitted PRACH in file [Default no]\n");
printf(" -v [set srslte_verbose to info, debug, default none]\n");
printf("\n");
printf("Device arguments for:\n");
printf(" X300: type=x300,addr=192.168.40.2,send_frame_size=2000,recv_frame_size=2000\n");
printf(" B200: type=b200\n");
printf("\n");
printf("Scripted example:\n");
printf(" Iterate over all bandwidth for 5 minutes and saving all stdout and displays delay stats at the end:\n");
printf(" for i in 6 15 25 50 75 100; do sudo ./lib/src/phy/phch/test/prach_test_usrp -a "
"type=x300,addr=192.168.40.2,send_frame_size=2000,recv_frame_size=2000 -g 20 -G 20 -p $i -f 875e6 -R 15000 | "
"tee prach_test_usrp_$i.txt; done; grep \"delay:\" prach_*\n");
} }
void parse_args(int argc, char** argv) void parse_args(int argc, char** argv)
{ {
int opt; int opt;
while ((opt = getopt(argc, argv, "apfFgGrstoPOz")) != -1) { while ((opt = getopt(argc, argv, "acpfFgGrRstoPOvz")) != -1) {
switch (opt) { switch (opt) {
case 'a': case 'a':
uhd_args = argv[optind]; uhd_args = argv[optind];
break; break;
case 'c':
continous_tx = !continous_tx;
break;
case 'o': case 'o':
output_filename = argv[optind]; output_filename = argv[optind];
break; break;
@ -105,9 +132,15 @@ void parse_args(int argc, char** argv)
case 'r': case 'r':
root_seq_idx = (uint32_t)strtol(argv[optind], NULL, 10); root_seq_idx = (uint32_t)strtol(argv[optind], NULL, 10);
break; break;
case 'R':
nof_repetitions = (uint32_t)strtol(argv[optind], NULL, 10);
break;
case 's': case 's':
seq_idx = (uint32_t)strtol(argv[optind], NULL, 10); seq_idx = (uint32_t)strtol(argv[optind], NULL, 10);
break; break;
case 'v':
srslte_verbose++;
break;
case 'z': case 'z':
zero_corr_zone = (uint32_t)strtol(argv[optind], NULL, 10); zero_corr_zone = (uint32_t)strtol(argv[optind], NULL, 10);
break; break;
@ -118,48 +151,69 @@ void parse_args(int argc, char** argv)
} }
} }
void rf_msg_callback(void* arg, srslte_rf_error_t error)
{
switch (error.type) {
case SRSLTE_RF_ERROR_LATE:
printf("L");
break;
case SRSLTE_RF_ERROR_UNDERFLOW:
printf("U");
break;
case SRSLTE_RF_ERROR_OVERFLOW:
printf("O");
break;
case SRSLTE_RF_ERROR_RX:
printf("R");
break;
case SRSLTE_RF_ERROR_OTHER:
printf("X");
break;
}
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
parse_args(argc, argv); parse_args(argc, argv);
srslte_prach_t prach; srslte_prach_t prach = {};
int srate = srslte_sampling_freq_hz(nof_prb);
uint32_t flen = srate / 1000;
// Allocate buffers
cf_t* preamble = srslte_vec_cf_malloc(MAX_LEN);
cf_t* zeros = srslte_vec_cf_malloc(flen);
cf_t* buffer = srslte_vec_cf_malloc(flen * nof_frames);
bool high_speed_flag = false; if (!preamble || !zeros || !buffer) {
return SRSLTE_ERROR;
}
cf_t preamble[MAX_LEN]; srslte_vec_cf_zero(preamble, MAX_LEN);
memset(preamble, 0, sizeof(cf_t) * MAX_LEN); srslte_vec_cf_zero(zeros, flen);
srslte_vec_cf_zero(buffer, flen * nof_frames);
srslte_prach_cfg_t prach_cfg; srslte_prach_cfg_t prach_cfg = {};
ZERO_OBJECT(prach_cfg); prach_cfg.config_idx = preamble_format;
prach_cfg.config_idx = preamble_format; prach_cfg.hs_flag = high_speed_flag;
prach_cfg.hs_flag = high_speed_flag; prach_cfg.freq_offset = 0;
prach_cfg.freq_offset = 0; prach_cfg.root_seq_idx = root_seq_idx;
prach_cfg.root_seq_idx = root_seq_idx; prach_cfg.zero_corr_zone = zero_corr_zone;
prach_cfg.zero_corr_zone = zero_corr_zone; prach_cfg.num_ra_preambles = num_ra_preambles;
prach_cfg.num_ra_preambles = num_ra_preambles;
if (srslte_prach_init(&prach, srslte_symbol_sz(nof_prb))) { if (srslte_prach_init(&prach, srslte_symbol_sz(nof_prb))) {
return -1; return SRSLTE_ERROR;
} }
if (srslte_prach_set_cfg(&prach, &prach_cfg, nof_prb)) { if (srslte_prach_set_cfg(&prach, &prach_cfg, nof_prb)) {
ERROR("Error initiating PRACH object\n"); ERROR("Error initiating PRACH object\n");
return -1; return SRSLTE_ERROR;
} }
int srate = srslte_sampling_freq_hz(nof_prb);
uint32_t flen = srate / 1000;
printf("Generating PRACH\n"); printf("Generating PRACH\n");
srslte_vec_cf_zero(preamble, flen);
srslte_prach_gen(&prach, seq_idx, frequency_offset, preamble); srslte_prach_gen(&prach, seq_idx, frequency_offset, preamble);
uint32_t prach_len = prach.N_seq + prach.N_cp;
srslte_vec_save_file("generated", preamble, prach_len * sizeof(cf_t));
cf_t* buffer = srslte_vec_cf_malloc(flen * nof_frames);
// Send through UHD // Send through UHD
srslte_rf_t rf; srslte_rf_t rf;
printf("Opening RF device...\n"); printf("Opening RF device...\n");
@ -167,10 +221,15 @@ int main(int argc, char** argv)
ERROR("Error opening &uhd\n"); ERROR("Error opening &uhd\n");
exit(-1); exit(-1);
} }
printf("Subframe len: %d samples\n", flen); printf("Test summary:\n");
printf("Set RX gain: %.1f dB\n", uhd_rx_gain); printf(" Sub-frame len: %d samples\n", flen);
printf("Set TX gain: %.1f dB\n", uhd_tx_gain); printf(" Set RX gain: %.1f dB\n", uhd_rx_gain);
printf("Set TX/RX freq: %.2f MHz\n", uhd_freq / 1000000); printf(" Set TX gain: %.1f dB\n", uhd_tx_gain);
printf(" Sampling rate: %.3f MHz\n", srate / 1e6);
printf(" Set TX/RX freq: %.2f MHz\n", uhd_freq / 1000000);
printf(" Total duration: %d milli-seconds\n", nof_repetitions * nof_frames);
printf(" Continuous Tx: %s\n", continous_tx ? "true" : "false");
printf("\n");
srslte_rf_set_rx_gain(&rf, uhd_rx_gain); srslte_rf_set_rx_gain(&rf, uhd_rx_gain);
srslte_rf_set_tx_gain(&rf, uhd_tx_gain); srslte_rf_set_tx_gain(&rf, uhd_tx_gain);
@ -178,7 +237,7 @@ int main(int argc, char** argv)
srslte_rf_set_tx_freq(&rf, 0, uhd_freq); srslte_rf_set_tx_freq(&rf, 0, uhd_freq);
printf("Setting sampling rate %.2f MHz\n", (float)srate / 1000000); printf("Setting sampling rate %.2f MHz\n", (float)srate / 1000000);
float srate_rf = srslte_rf_set_rx_srate(&rf, (double)srate); int srate_rf = (int)srslte_rf_set_rx_srate(&rf, (double)srate);
if (srate_rf != srate) { if (srate_rf != srate) {
ERROR("Could not set sampling rate\n"); ERROR("Could not set sampling rate\n");
exit(-1); exit(-1);
@ -186,47 +245,118 @@ int main(int argc, char** argv)
srslte_rf_set_tx_srate(&rf, (double)srate); srslte_rf_set_tx_srate(&rf, (double)srate);
sleep(1); sleep(1);
cf_t* zeros = calloc(sizeof(cf_t), flen);
srslte_timestamp_t tstamp; srslte_timestamp_t tstamp;
// Register error handler
srslte_rf_register_error_handler(&rf, rf_msg_callback, NULL);
// Start streaming
srslte_rf_start_rx_stream(&rf, false); srslte_rf_start_rx_stream(&rf, false);
uint32_t nframe = 0;
// Print Table legend
while (nframe < nof_frames) { printf("%8s; %5s; %5s; %5s; %6s; %5s; %9s;\n", "Time", "i", "count", "index", "usec", "samp", "Norm Peak");
printf("Rx subframe %d\n", nframe);
srslte_rf_recv_with_time(&rf, &buffer[flen * nframe], flen, true, &tstamp.full_secs, &tstamp.frac_secs); // Statistics variables
nframe++; double delay_us_min = +INFINITY;
if (nframe == 9 || nframe == 8) { double delay_us_max = -INFINITY;
srslte_timestamp_add(&tstamp, 0, 2e-3 - timeadv * 1e-6); double delay_us_avg = 0.0;
if (nframe == 8) { double peak_min = +INFINITY;
srslte_rf_send_timed2(&rf, zeros, flen, tstamp.full_secs, tstamp.frac_secs, true, false); double peak_max = -INFINITY;
printf("Transmitting zeros\n"); double peak_avg = 0.0;
} else { uint32_t count = 0;
srslte_rf_send_timed2(&rf, preamble, flen, tstamp.full_secs, tstamp.frac_secs, false, true);
printf("Transmitting PRACH\n"); // Perform experiment for given a number of repetitions
for (uint32_t rep = 0; rep < nof_repetitions; rep++) {
// First transmission shall be flagged as start of burst
bool is_start_of_burst = true;
// For a the number of frames
for (uint32_t nframe = 0; nframe < nof_frames; nframe++) {
INFO("Rep %d. Receiving frame %d\n", rep, nframe);
srslte_rf_recv_with_time(&rf, &buffer[flen * nframe], flen, true, &tstamp.full_secs, &tstamp.frac_secs);
srslte_timestamp_add(&tstamp, 0, tx_delay_ms * 1e-3 - timeadv * 1e-6);
if (nframe == 10 - tx_delay_ms) {
srslte_rf_send_timed2(&rf, preamble, flen, tstamp.full_secs, tstamp.frac_secs, false, !continous_tx);
INFO("Rep %d. Transmitting PRACH\n", rep);
} else if (nframe == 10 - tx_delay_ms - 1 || continous_tx) {
srslte_rf_send_timed2(&rf, zeros, flen, tstamp.full_secs, tstamp.frac_secs, is_start_of_burst, false);
INFO("Rep %d. Transmitting Zeros\n", rep);
is_start_of_burst = false;
} }
} }
}
uint32_t indices[1024]; // PRACH detection
float offsets[1024]; uint32_t indices[1024] = {};
uint32_t nof_detected; float offsets[1024] = {};
if (srslte_prach_detect_offset( float peak_to_avg[1024] = {};
&prach, frequency_offset, &buffer[flen * 10 + prach.N_cp], flen, indices, offsets, NULL, &nof_detected)) { uint32_t nof_detected = 0;
printf("Error detecting prach\n"); if (srslte_prach_detect_offset(&prach,
} frequency_offset,
printf("Nof detected PRACHs: %d\n", nof_detected); &buffer[flen * 10 + prach.N_cp],
for (int i = 0; i < nof_detected; i++) { flen,
printf("%d/%d index=%d, offset=%.2f us (%d samples)\n", indices,
offsets,
peak_to_avg,
&nof_detected)) {
printf("Rep %d. Error detecting prach\n", rep);
}
// Prompt detected PRACH
INFO("Rep %d. Nof detected PRACHs: %d\n", rep, nof_detected);
for (int i = 0; i < nof_detected; i++) {
INFO("%d/%d index=%d, offset=%.2f us (%d samples)\n",
i, i,
nof_detected, nof_detected,
indices[i], indices[i],
offsets[i] * 1e6, offsets[i] * 1e6,
(int)(offsets[i] * srate)); (int)(offsets[i] * srate));
printf("%8.3f; %5d; %5d; %5d; %6.2f; %5d; %9.3f;\n",
(double)(rep * nof_frames) * 1e-3,
i + 1,
nof_detected,
indices[i],
offsets[i] * 1e6,
(int)(offsets[i] * srate),
peak_to_avg[i]);
// Update stats
delay_us_min = SRSLTE_MIN(delay_us_min, offsets[i] * 1e6);
delay_us_max = SRSLTE_MAX(delay_us_max, offsets[i] * 1e6);
delay_us_avg = SRSLTE_VEC_CMA(offsets[i] * 1e6, delay_us_avg, count);
peak_min = SRSLTE_MIN(peak_min, peak_to_avg[i]);
peak_max = SRSLTE_MAX(peak_max, peak_to_avg[i]);
peak_avg = SRSLTE_VEC_CMA(peak_to_avg[i], peak_avg, count);
count++;
}
} }
srslte_vec_save_file(output_filename, buffer, 11 * flen * sizeof(cf_t)); // End burst
if (continous_tx) {
srslte_rf_send_timed2(&rf, zeros, 0, tstamp.full_secs, tstamp.frac_secs, false, true);
}
// Print statistics
printf("\n");
printf("Statistics:\n");
printf(" PRACH count: %d\n", count);
printf(" Minimum delay: %.2f us\n", delay_us_min);
printf(" Maximum delay: %.2f us\n", delay_us_max);
printf(" Average delay: %.2f us\n", delay_us_avg);
printf(" Minimum peak: %.2f\n", peak_min);
printf(" Maximum peak: %.2f\n", peak_max);
printf(" Average peak: %.2f\n", peak_avg);
// Save in file if filename is not empty
if (output_filename) {
// Save generated PRACH signal
srslte_vec_save_file("generated", preamble, (prach.N_seq + prach.N_cp) * sizeof(cf_t));
// Save last received buffer
srslte_vec_save_file(output_filename, buffer, 11 * flen * (uint32_t)sizeof(cf_t));
}
srslte_rf_close(&rf); srslte_rf_close(&rf);
srslte_prach_free(&prach); srslte_prach_free(&prach);

Loading…
Cancel
Save