diff --git a/lib/examples/cell_search.c b/lib/examples/cell_search.c index 72b73af43..4156a076e 100644 --- a/lib/examples/cell_search.c +++ b/lib/examples/cell_search.c @@ -130,7 +130,8 @@ void sig_int_handler(int signo) } } -double srslte_rf_set_rx_gain_wrapper(void *h, double f) { +float srslte_rf_set_rx_gain_wrapper(void* h, float f) +{ return srslte_rf_set_rx_gain((srslte_rf_t*) h, f); } @@ -249,14 +250,13 @@ int main(int argc, char **argv) { printf("\n\nFound %d cells\n", n_found_cells); for (int i=0;i -#include #include "srslte/config.h" - +#include +#include +#include #define SRSLTE_MAX(a,b) ((a)>(b)?(a):(b)) #define SRSLTE_MIN(a,b) ((a)<(b)?(a):(b)) @@ -51,6 +51,26 @@ extern "C" { // Exponential moving average #define SRSLTE_VEC_EMA(data, average, alpha) ((alpha)*(data)+(1-alpha)*(average)) +static inline float srslte_convert_amplitude_to_dB(float v) +{ + return 20.0f * log10f(v); +} +static inline float srslte_convert_power_to_dB(float v) +{ + return 10.0f * log10f(v); +} +static inline float srslte_convert_power_to_dBm(float v) +{ + return srslte_convert_power_to_dB(v) + 30.0f; +} +static inline float srslte_convert_dB_to_amplitude(float v) +{ + return powf(10.0f, v / 20.0f); +} +static inline float srslte_convert_dB_to_power(float v) +{ + return powf(10.0f, v / 10.0f); +} /*logical operations */ SRSLTE_API void srslte_vec_xor_bbb(int8_t *x,int8_t *y,int8_t *z, const uint32_t len); diff --git a/lib/include/srslte/radio/radio.h b/lib/include/srslte/radio/radio.h index d154a34a7..2ec2be42f 100644 --- a/lib/include/srslte/radio/radio.h +++ b/lib/include/srslte/radio/radio.h @@ -98,7 +98,7 @@ class radio { void set_tx_gain(float gain); void set_rx_gain(float gain); void set_tx_rx_gain_offset(float offset); - double set_rx_gain_th(float gain); + float set_rx_gain_th(float gain); void set_freq_offset(double freq); void set_tx_freq(uint32_t chan, double freq); diff --git a/lib/include/srslte/radio/radio_multi.h b/lib/include/srslte/radio/radio_multi.h index 04453b2ae..228a8444a 100644 --- a/lib/include/srslte/radio/radio_multi.h +++ b/lib/include/srslte/radio/radio_multi.h @@ -84,7 +84,7 @@ public: return radios.at(radio_idx)->rx_now(buffer, nof_samples, rxd_time); } void set_rx_gain(const uint32_t& radio_idx, const float& gain) override { radios.at(radio_idx)->set_rx_gain(gain); } - double set_rx_gain_th(const float& gain) override { return radios.at(0)->set_rx_gain_th(gain); } + float set_rx_gain_th(const float& gain) override { return radios.at(0)->set_rx_gain_th(gain); } float get_rx_gain(const uint32_t& radio_idx) override { return radios.at(radio_idx)->get_rx_gain(); } void set_tx_freq(const uint32_t& radio_idx, const uint32_t& channel_idx, const double& freq) override { diff --git a/lib/src/phy/agc/agc.c b/lib/src/phy/agc/agc.c index e1d45340d..8b533e88f 100644 --- a/lib/src/phy/agc/agc.c +++ b/lib/src/phy/agc/agc.c @@ -54,7 +54,12 @@ int srslte_agc_init_acc(srslte_agc_t *q, srslte_agc_mode_t mode, uint32_t nof_fr return SRSLTE_SUCCESS; } -int srslte_agc_init_uhd(srslte_agc_t *q, srslte_agc_mode_t mode, uint32_t nof_frames, double (set_gain_callback)(void*, double), void *uhd_handler) { +int srslte_agc_init_uhd(srslte_agc_t* q, + srslte_agc_mode_t mode, + uint32_t nof_frames, + float(set_gain_callback)(void*, float), + void* uhd_handler) +{ if (!srslte_agc_init_acc(q, mode, nof_frames)) { q->set_gain_callback = set_gain_callback; q->uhd_handler = uhd_handler; @@ -73,16 +78,17 @@ void srslte_agc_free(srslte_agc_t *q) { void srslte_agc_reset(srslte_agc_t *q) { q->bandwidth = SRSLTE_AGC_DEFAULT_BW; - q->lock = false; - q->gain = pow(10,50/10); + q->lock = false; + q->gain = srslte_convert_dB_to_power(50.0f); q->y_out = 1.0; q->isfirst = true; if (q->set_gain_callback && q->uhd_handler) { - q->set_gain_callback(q->uhd_handler, 10*log10(q->gain)); + q->set_gain_callback(q->uhd_handler, srslte_convert_power_to_dB(q->gain)); } } -void srslte_agc_set_gain_range(srslte_agc_t *q, double min_gain, double max_gain) { +void srslte_agc_set_gain_range(srslte_agc_t* q, float min_gain, float max_gain) +{ if (q) { q->min_gain = min_gain; q->max_gain = max_gain; @@ -119,8 +125,8 @@ void srslte_agc_lock(srslte_agc_t *q, bool enable) { void srslte_agc_process(srslte_agc_t *q, cf_t *signal, uint32_t len) { if (!q->lock) { - double gain_db = 10.0 * log10(q->gain); - double gain_uhd_db = 50.0; + float gain_db = srslte_convert_power_to_dB(q->gain); + float gain_uhd_db = 50.0f; float y = 0; // Apply current gain to input signal @@ -140,7 +146,7 @@ void srslte_agc_process(srslte_agc_t *q, cf_t *signal, uint32_t len) { // Set gain gain_uhd_db = q->set_gain_callback(q->uhd_handler, gain_db); - q->gain = pow(10, gain_uhd_db / 10); + q->gain = srslte_convert_dB_to_power(gain_uhd_db); } float *t; switch(q->mode) { diff --git a/lib/src/phy/ch_estimation/chest_dl.c b/lib/src/phy/ch_estimation/chest_dl.c index 839e5197d..64edce975 100644 --- a/lib/src/phy/ch_estimation/chest_dl.c +++ b/lib/src/phy/ch_estimation/chest_dl.c @@ -839,33 +839,32 @@ static float get_rsrp_neighbour(srslte_chest_dl_t* q) return max; } -#define dbm(a) (10 * log10(a) + 30) -#define db(a) (10 * log10(a)) - static void fill_res(srslte_chest_dl_t* q, srslte_chest_dl_res_t* res) { res->noise_estimate = get_noise(q); - res->noise_estimate_dbm = dbm(res->noise_estimate); + res->noise_estimate_dbm = srslte_convert_power_to_dBm(res->noise_estimate); res->cfo = q->cfo; res->rsrp = get_rsrp(q); - res->rsrp_dbm = dbm(res->rsrp); + res->rsrp_dbm = srslte_convert_power_to_dBm(res->rsrp); res->rsrp_neigh = get_rsrp_neighbour(q); res->rsrq = get_rsrq(q); - res->rsrq_db = db(res->rsrq); - res->snr_db = db(get_snr(q)); - res->rssi_dbm = dbm(get_rssi(q)); + res->rsrq_db = srslte_convert_power_to_dB(res->rsrq); + res->snr_db = srslte_convert_power_to_dB(get_snr(q)); + res->rssi_dbm = srslte_convert_power_to_dBm(get_rssi(q)); res->sync_error = q->sync_err[0][0]; // Take only the channel used for synch for (uint32_t port_id = 0; port_id < q->cell.nof_ports; port_id++) { - res->rsrp_port_dbm[port_id] = dbm(get_rsrp_port(q, port_id)); + res->rsrp_port_dbm[port_id] = srslte_convert_power_to_dBm(get_rsrp_port(q, port_id)); for (uint32_t a = 0; a < q->nof_rx_antennas; a++) { if (q->noise_estimate[a]) { - res->snr_ant_port_db[a][port_id] = db(q->rsrp[a][port_id] / q->noise_estimate[a][port_id]); + res->snr_ant_port_db[a][port_id] = + srslte_convert_power_to_dB(q->rsrp[a][port_id] / q->noise_estimate[a][port_id]); } else { - res->snr_ant_port_db[a][port_id] = 0.0; + res->snr_ant_port_db[a][port_id] = 0.0f; } - res->rsrp_ant_port_dbm[a][port_id] = dbm(q->rsrp[a][port_id]); - res->rsrq_ant_port_db[a][port_id] = db(q->cell.nof_prb * q->rsrp[a][port_id] / q->rssi[a][port_id]); + res->rsrp_ant_port_dbm[a][port_id] = srslte_convert_power_to_dBm(q->rsrp[a][port_id]); + res->rsrq_ant_port_db[a][port_id] = + srslte_convert_power_to_dB(q->cell.nof_prb * q->rsrp[a][port_id] / q->rssi[a][port_id]); } } } diff --git a/lib/src/phy/ch_estimation/chest_ul.c b/lib/src/phy/ch_estimation/chest_ul.c index c62c5b291..4b1549ae1 100644 --- a/lib/src/phy/ch_estimation/chest_ul.c +++ b/lib/src/phy/ch_estimation/chest_ul.c @@ -320,8 +320,8 @@ int srslte_chest_ul_estimate_pusch( res->snr = NAN; } - res->snr_db = 10 * log10(res->snr); - res->noise_estimate_dbm = 10 * log10(res->noise_estimate) + 30; + res->snr_db = srslte_convert_power_to_dB(res->snr); + res->noise_estimate_dbm = srslte_convert_power_to_dBm(res->noise_estimate); return 0; } diff --git a/lib/src/phy/ch_estimation/test/chest_nbiot_test_dl.c b/lib/src/phy/ch_estimation/test/chest_nbiot_test_dl.c index 0230884f5..4154f3e74 100644 --- a/lib/src/phy/ch_estimation/test/chest_nbiot_test_dl.c +++ b/lib/src/phy/ch_estimation/test/chest_nbiot_test_dl.c @@ -47,7 +47,6 @@ srslte_nbiot_cell_t cell = {.base = {.nof_prb = 1, .cp = SRSLTE_CP_NORM, .id = 0}, .base.nof_ports = 1, - .base.nof_prb = 1, .nbiot_prb = 0, .n_id_ncell = 0}; @@ -184,7 +183,7 @@ int main(int argc, char** argv) if (have_channel) { // Add noise - float std_dev = powf(10, -(snr_db + 3.0f) / 20.0f) * 0.1f; + float std_dev = srslte_convert_dB_to_amplitude(-(snr_db + 3.0f)) * 0.1f; srslte_ch_awgn_c(est.pilot_recv_signal, est.pilot_recv_signal, std_dev, SRSLTE_REFSIGNAL_MAX_NUM_SF(1)); } diff --git a/lib/src/phy/channel/ch_awgn.c b/lib/src/phy/channel/ch_awgn.c index 224f4d38a..4e60929bb 100644 --- a/lib/src/phy/channel/ch_awgn.c +++ b/lib/src/phy/channel/ch_awgn.c @@ -22,14 +22,14 @@ #include #include #include -#include #include "gauss.h" -#include "srslte/phy/channel/ch_awgn.h" +#include +#include float srslte_ch_awgn_get_variance(float ebno_db, float rate) { - float esno_db = ebno_db + 10 * log10f(rate); - return sqrtf(1 / (powf(10, esno_db / 10))); + float esno_db = ebno_db + srslte_convert_power_to_dB(rate); + return srslte_convert_dB_to_amplitude(-esno_db); } void srslte_ch_awgn_c(const cf_t* x, cf_t* y, float variance, uint32_t len) { diff --git a/lib/src/phy/channel/fading.c b/lib/src/phy/channel/fading.c index 87f00c108..41c3a8b44 100644 --- a/lib/src/phy/channel/fading.c +++ b/lib/src/phy/channel/fading.c @@ -90,7 +90,7 @@ static inline float get_doppler_dispersion(double t, double a, double w, double static inline void generate_tap(float delay_ns, float power_db, float srate, float phase, cf_t* buf, uint32_t N, uint32_t path_delay) { - float amplitude = powf(10.0f, power_db / 20.0f); + float amplitude = srslte_convert_dB_to_power(power_db); float O = (delay_ns * 1e-9f * srate + path_delay) / (float)N; cf_t a0 = amplitude * cexpf(-_Complex_I * phase) / N; diff --git a/lib/src/phy/channel/gauss.c b/lib/src/phy/channel/gauss.c index 2b3d7b9fb..d8b30df56 100644 --- a/lib/src/phy/channel/gauss.c +++ b/lib/src/phy/channel/gauss.c @@ -33,5 +33,5 @@ float rand_gauss (void) { s = v1*v1 + v2*v2; } while ( s >= 1.0 || s == 0.0); - return (v1*sqrt(-2.0 * log(s) / s)); + return (v1 * sqrtf(-2.0 * log(s) / s)); } diff --git a/lib/src/phy/channel/test/fading_channel_test.c b/lib/src/phy/channel/test/fading_channel_test.c index 55a0c76d1..6ad744a32 100644 --- a/lib/src/phy/channel/test/fading_channel_test.c +++ b/lib/src/phy/channel/test/fading_channel_test.c @@ -196,12 +196,12 @@ int main(int argc, char** argv) srslte_dft_run_c_zerocopy(&fft, output_buffer, fft_buffer); srslte_vec_prod_conj_ccc(fft_buffer, fft_buffer, fft_buffer, srate / 1000); for (int i = 0; i < srate / 1000; i++) { - fft_mag[i] = 10.0f * log10f(__real__ fft_buffer[i]); + fft_mag[i] = srslte_convert_power_to_dB(__real__ fft_buffer[i]); } plot_real_setNewData(&plot_fft, fft_mag, srate / 1000); for (int i = 0; i < channel_fading.N; i++) { - fft_mag[i] = 20.0f * log10f(cabsf(channel_fading.h_freq[i])); + fft_mag[i] = srslte_convert_amplitude_to_dB(cabsf(channel_fading.h_freq[i])); } plot_real_setNewData(&plot_h, fft_mag, channel_fading.N); diff --git a/lib/src/phy/dft/dft_fftw.c b/lib/src/phy/dft/dft_fftw.c index c008ac8da..7228de3b7 100644 --- a/lib/src/phy/dft/dft_fftw.c +++ b/lib/src/phy/dft/dft_fftw.c @@ -313,7 +313,7 @@ void srslte_dft_run_c(srslte_dft_plan_t *plan, const cf_t *in, cf_t *out) { } if (plan->db) { for (i=0;isize;i++) { - f_out[i] = 10*log10(f_out[i]); + f_out[i] = srslte_convert_power_to_dB(f_out[i]); } } copy_post((uint8_t*)out, (uint8_t*)plan->out, sizeof(cf_t), plan->size, @@ -342,7 +342,7 @@ void srslte_dft_run_r(srslte_dft_plan_t *plan, const float *in, float *out) { } if (plan->db) { for (i=0;iout,sizeof(float)*plan->size); diff --git a/lib/src/phy/enb/enb_dl.c b/lib/src/phy/enb/enb_dl.c index c7c7c81bd..7acab8eb3 100644 --- a/lib/src/phy/enb/enb_dl.c +++ b/lib/src/phy/enb/enb_dl.c @@ -402,7 +402,7 @@ int srslte_enb_dl_put_pmch(srslte_enb_dl_t* q, srslte_pmch_cfg_t* pmch_cfg, uint void srslte_enb_dl_gen_signal(srslte_enb_dl_t* q) { // TODO: PAPR control - float norm_factor = 0.05f / sqrt(q->cell.nof_prb); + float norm_factor = 0.05f / sqrtf(q->cell.nof_prb); if (q->dl_sf.sf_type == SRSLTE_SF_MBSFN) { srslte_ofdm_tx_sf(&q->ifft_mbsfn); diff --git a/lib/src/phy/fec/test/turbodecoder_test.c b/lib/src/phy/fec/test/turbodecoder_test.c index 8fc573d53..adabf5766 100644 --- a/lib/src/phy/fec/test/turbodecoder_test.c +++ b/lib/src/phy/fec/test/turbodecoder_test.c @@ -27,9 +27,10 @@ #include #include +#include "srslte/srslte.h" +#include #include #include -#include "srslte/srslte.h" #include "turbodecoder_test.h" @@ -114,6 +115,7 @@ void parse_args(int argc, char **argv) { int main(int argc, char **argv) { + srslte_random_t random_gen = srslte_random_init(0); uint32_t frame_cnt; float *llr; short *llr_s; @@ -122,7 +124,7 @@ int main(int argc, char **argv) { uint32_t i, j; float var[SNR_POINTS]; uint32_t snr_points; - uint32_t errors; + uint32_t errors = 0; uint32_t coded_length; struct timeval tdata[3]; float mean_usec; @@ -210,12 +212,12 @@ int main(int argc, char **argv) { snr_points = SNR_POINTS; for (i = 0; i < snr_points; i++) { ebno_db = SNR_MIN + i * ebno_inc; - esno_db = ebno_db + 10 * log10((double) 1 / 3); - var[i] = sqrt(1 / (pow(10, esno_db / 10))); + esno_db = ebno_db + srslte_convert_power_to_dB(1.0f / 3.0f); + var[i] = srslte_convert_dB_to_amplitude(-esno_db); } } else { - esno_db = ebno_db + 10 * log10((double) 1 / 3); - var[0] = sqrt(1 / (pow(10, esno_db / 10))); + esno_db = ebno_db + srslte_convert_power_to_dB(1.0f / 3.0f); + var[0] = srslte_convert_dB_to_amplitude(-esno_db); snr_points = 1; } for (i = 0; i < snr_points; i++) { @@ -229,7 +231,7 @@ int main(int argc, char **argv) { if (test_known_data) { data_tx[j] = known_data[j]; } else { - data_tx[j] = rand() % 2; + data_tx[j] = srslte_random_uniform_int_dist(random_gen, 0, 1); } } @@ -292,9 +294,7 @@ int main(int argc, char **argv) { } } - if (data_rx_bytes) { - free(data_rx_bytes); - } + free(data_rx_bytes); free(data_tx); free(symbols); free(llr); @@ -304,6 +304,7 @@ int main(int argc, char **argv) { srslte_tdec_free(&tdec); srslte_tcod_free(&tcod); + srslte_random_free(random_gen); printf("\n"); printf("Done\n"); diff --git a/lib/src/phy/fec/test/viterbi_test.c b/lib/src/phy/fec/test/viterbi_test.c index 6776a3827..08beaee63 100644 --- a/lib/src/phy/fec/test/viterbi_test.c +++ b/lib/src/phy/fec/test/viterbi_test.c @@ -19,13 +19,14 @@ * */ +#include +#include #include #include #include #include -#include -#include #include +#include #include "srslte/srslte.h" @@ -87,7 +88,7 @@ int main(int argc, char **argv) { uint16_t *llr_s; uint8_t *llr_c; uint8_t *data_tx, *data_rx, *data_rx2, *symbols; - int i, j; + int j; float var[SNR_POINTS], varunc[SNR_POINTS]; int snr_points; uint32_t errors; @@ -170,20 +171,20 @@ int main(int argc, char **argv) { ebno_inc = (SNR_MAX - SNR_MIN) / SNR_POINTS; if (ebno_db == 100.0) { snr_points = SNR_POINTS; - for (i = 0; i < snr_points; i++) { + for (uint32_t i = 0; i < snr_points; i++) { ebno_db = SNR_MIN + i * ebno_inc; - esno_db = ebno_db + 10 * log10((double) 1 / 3); - var[i] = sqrt(1 / (pow(10, esno_db / 10))); - varunc[i] = sqrt(1 / (pow(10, ebno_db / 10))); + esno_db = ebno_db + srslte_convert_power_to_dB(1.0f / 3.0f); + var[i] = srslte_convert_dB_to_amplitude(esno_db); + varunc[i] = srslte_convert_dB_to_amplitude(ebno_db); } } else { - esno_db = ebno_db + 10 * log10((double) 1 / 3); - var[0] = sqrt(1 / (pow(10, esno_db / 10))); - varunc[0] = sqrt(1 / (pow(10, ebno_db / 10))); + esno_db = ebno_db + srslte_convert_power_to_dB(1.0f / 3.0f); + var[0] = srslte_convert_dB_to_amplitude(esno_db); + varunc[0] = srslte_convert_dB_to_amplitude(ebno_db); snr_points = 1; } - for (i = 0; i < snr_points; i++) { + for (uint32_t i = 0; i < snr_points; i++) { frame_cnt = 0; errors = 0; #ifdef TEST_SSE @@ -192,9 +193,11 @@ int main(int argc, char **argv) { while (frame_cnt < nof_frames) { /* generate data_tx */ + srslte_random_t random_gen = srslte_random_init(0); for (j = 0; j < frame_length; j++) { - data_tx[j] = rand() % 2; + data_tx[j] = srslte_random_uniform_int_dist(random_gen, 0, 1); } + srslte_random_free(random_gen); /* uncoded BER */ for (j = 0; j < frame_length; j++) { @@ -217,10 +220,9 @@ int main(int argc, char **argv) { struct timeval t[3]; gettimeofday(&t[1], NULL); - int M = 1; - - - for (int i=0;icell.nof_prb; /* Set power allocation according to 3GPP 36.213 clause 5.2 Downlink power allocation */ - float rho_a = powf(10.0f, cfg->p_a / 20.0f) * ((q->cell.nof_ports == 1) ? 1.0f : M_SQRT2); + float rho_a = srslte_convert_dB_to_amplitude(cfg->p_a) * ((q->cell.nof_ports == 1) ? 1.0f : M_SQRT2); uint32_t idx0 = (q->cell.nof_ports == 1) ? 0 : 1; float cell_specific_ratio = pdsch_cfg_cell_specific_ratio_table[idx0][cfg->p_b]; diff --git a/lib/src/phy/rf/rf_utils.c b/lib/src/phy/rf/rf_utils.c index 550e59697..619f90844 100644 --- a/lib/src/phy/rf/rf_utils.c +++ b/lib/src/phy/rf/rf_utils.c @@ -63,7 +63,8 @@ int rf_rssi_scan(srslte_rf_t *rf, float *freqs, float *rssi, int nof_bands, doub } } 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); + printf("[%3d]: Freq %4.1f Mhz - RSSI: %3.2f dBm\r", i, f / 1000000, srslte_convert_power_to_dBm(rssi[i])); + fflush(stdout); if (SRSLTE_VERBOSE_ISINFO()) { printf("\n"); } @@ -205,7 +206,7 @@ int rf_cell_search(srslte_rf_t *rf, uint32_t nof_rx_antennas, srslte_cp_string(found_cells[i].cp), found_cells[i].mode * 100, found_cells[i].psr, - 20 * log10(found_cells[i].peak * 1000)); + srslte_convert_amplitude_to_dB(found_cells[i].peak * 1000)); } // Save result diff --git a/lib/src/phy/rf/rf_zmq_imp.c b/lib/src/phy/rf/rf_zmq_imp.c index 2619e2861..b0ad437dd 100644 --- a/lib/src/phy/rf/rf_zmq_imp.c +++ b/lib/src/phy/rf/rf_zmq_imp.c @@ -627,7 +627,7 @@ int rf_zmq_recv_with_time_multi( } // Set gain - float scale = powf(10.0f, handler->rx_gain / 20); + float scale = srslte_convert_dB_to_amplitude(handler->rx_gain); srslte_vec_sc_prod_cfc(data[0], scale, data[0], nsamples); // update rx time diff --git a/lib/src/phy/sync/refsignal_dl_sync.c b/lib/src/phy/sync/refsignal_dl_sync.c index f88565a1d..81751eb64 100644 --- a/lib/src/phy/sync/refsignal_dl_sync.c +++ b/lib/src/phy/sync/refsignal_dl_sync.c @@ -278,13 +278,13 @@ void srslte_refsignal_dl_sync_run(srslte_refsignal_dl_sync_t* q, cf_t* buffer, u } // Calculate in dBm - q->rsrp_dBfs = 10.0f * log10f(rsrp_lin) + 30.0f; + q->rsrp_dBfs = srslte_convert_power_to_dBm(rsrp_lin); // Calculate RSSI in dBm - q->rssi_dBfs = 10.0f * log10f(rssi_lin) + 30.0f; + q->rssi_dBfs = srslte_convert_power_to_dBm(rssi_lin); // Calculate RSRQ - q->rsrq_dB = 10.0f * log10f(q->refsignal.cell.nof_prb) + q->rsrp_dBfs - q->rssi_dBfs; + q->rsrq_dB = srslte_convert_power_to_dB(q->refsignal.cell.nof_prb) + q->rsrp_dBfs - q->rssi_dBfs; q->found = true; q->cfo_Hz = cfo_acc; diff --git a/lib/src/phy/sync/test/sync_nbiot_test.c b/lib/src/phy/sync/test/sync_nbiot_test.c index 2f3e6a201..6abac8704 100644 --- a/lib/src/phy/sync/test/sync_nbiot_test.c +++ b/lib/src/phy/sync/test/sync_nbiot_test.c @@ -184,7 +184,7 @@ int main(int argc, char** argv) if (snr != -1.0) { snr -= 10.0; printf("Adding AWGN with target SNR: %.2fdB\n", snr); - float nstd = powf(10.0f, -snr / 20.0f); + float nstd = srslte_convert_dB_to_amplitude(-snr); srslte_ch_awgn_c(fft_buffer, fft_buffer, nstd, SFLEN); } diff --git a/lib/src/phy/ue/ue_dl.c b/lib/src/phy/ue/ue_dl.c index 93de005b2..452e3602c 100644 --- a/lib/src/phy/ue/ue_dl.c +++ b/lib/src/phy/ue/ue_dl.c @@ -725,7 +725,7 @@ static int select_pmi(srslte_ue_dl_t* q, uint32_t ri, uint32_t* pmi, float* sinr /* Set PMI */ if (sinr_db != NULL) { - *sinr_db = 10.0f * log10f(sinr_list[*pmi % SRSLTE_MAX_CODEBOOKS]); + *sinr_db = srslte_convert_power_to_dB(sinr_list[*pmi % SRSLTE_MAX_CODEBOOKS]); } } diff --git a/lib/src/phy/ue/ue_sync.c b/lib/src/phy/ue/ue_sync.c index 5fa17f6e6..9f49d3478 100644 --- a/lib/src/phy/ue/ue_sync.c +++ b/lib/src/phy/ue/ue_sync.c @@ -136,11 +136,12 @@ void srslte_ue_sync_reset(srslte_ue_sync_t *q) { q->frame_find_cnt = 0; } -int srslte_ue_sync_start_agc(srslte_ue_sync_t *q, - double (set_gain_callback)(void *, double), - double min_gain, - double max_gain, - double init_gain_value) { +int srslte_ue_sync_start_agc(srslte_ue_sync_t* q, + float(set_gain_callback)(void*, float), + float min_gain, + float max_gain, + float init_gain_value) +{ int n = srslte_agc_init_uhd(&q->agc, SRSLTE_AGC_MODE_PEAK_AMPLITUDE, 0, set_gain_callback, q->stream); q->do_agc = n==0?true:false; if (q->do_agc) { diff --git a/lib/src/phy/ue/ue_ul.c b/lib/src/phy/ue/ue_ul.c index b1ee777dc..b1539155d 100644 --- a/lib/src/phy/ue/ue_ul.c +++ b/lib/src/phy/ue/ue_ul.c @@ -354,7 +354,7 @@ float srslte_ue_ul_pusch_power(srslte_ue_ul_t* q, srslte_ue_ul_cfg_t* cfg, float MPR = q->pusch_cfg.last_O_cqi; } MPR /= q->pusch.dci.nof_re; - delta = 10 * log10((pow(2, MPR * 1.25) - 1) * beta_offset_pusch); + delta = 10 * log10f((powf(2, MPR * 1.25) - 1) * beta_offset_pusch); } #else printf("Do this in pusch??"); @@ -362,10 +362,10 @@ float srslte_ue_ul_pusch_power(srslte_ue_ul_t* q, srslte_ue_ul_cfg_t* cfg, float // TODO: This implements closed-loop power control float f = 0; - float pusch_power = 10 * log10(cfg->ul_cfg.pusch.grant.L_prb) + p0_pusch + alpha * PL + delta + f; + float pusch_power = 10 * log10f(cfg->ul_cfg.pusch.grant.L_prb) + p0_pusch + alpha * PL + delta + f; DEBUG("PUSCH: P=%f -- 10M=%f, p0=%f,alpha=%f,PL=%f,\n", pusch_power, - 10 * log10(cfg->ul_cfg.pusch.grant.L_prb), + 10 * log10f(cfg->ul_cfg.pusch.grant.L_prb), p0_pusch, alpha, PL); @@ -464,12 +464,12 @@ float srs_power(srslte_ue_ul_t* q, srslte_ue_ul_cfg_t* cfg, float PL) p_srs_offset = -10.5 + 1.5 * cfg->ul_cfg.power_ctrl.p_srs_offset; } - float p_srs = p_srs_offset + 10 * log10(M_sc) + p0_pusch + alpha * PL + f; + float p_srs = p_srs_offset + 10 * log10f(M_sc) + p0_pusch + alpha * PL + f; DEBUG("SRS: P=%f -- p_offset=%f, 10M=%f, p0_pusch=%f, alpha=%f, PL=%f, f=%f\n", p_srs, p_srs_offset, - 10 * log10(M_sc), + 10 * log10f(M_sc), p0_pusch, alpha, PL, diff --git a/lib/src/phy/utils/vector.c b/lib/src/phy/utils/vector.c index 473f63396..8e82c7abf 100644 --- a/lib/src/phy/utils/vector.c +++ b/lib/src/phy/utils/vector.c @@ -390,7 +390,7 @@ void srslte_vec_abs_dB_cf(const cf_t* x, float default_value, float* abs, const // Check boundaries if (isnormal(abs[i])) { // Avoid infinites and zeros - abs[i] = 20.0f * log10f(abs[i]); + abs[i] = srslte_convert_amplitude_to_dB(abs[i]); } else { // Set to default value instead abs[i] = default_value; diff --git a/lib/src/radio/radio.cc b/lib/src/radio/radio.cc index af932cda0..d26b79a78 100644 --- a/lib/src/radio/radio.cc +++ b/lib/src/radio/radio.cc @@ -274,7 +274,7 @@ void radio::set_rx_gain(float gain) srslte_rf_set_rx_gain(&rf_device, gain); } -double radio::set_rx_gain_th(float gain) +float radio::set_rx_gain_th(float gain) { return srslte_rf_set_rx_gain_th(&rf_device, gain); } diff --git a/lib/src/radio/test/benchmark_radio.cc b/lib/src/radio/test/benchmark_radio.cc index a9f9c75e7..6924c55a3 100644 --- a/lib/src/radio/test/benchmark_radio.cc +++ b/lib/src/radio/test/benchmark_radio.cc @@ -150,9 +150,9 @@ void parse_args(int argc, char **argv) { } } -static double set_gain_callback(void* h, double gain) +static float set_gain_callback(void* h, float gain) { - radio* r = (radio*)h; + auto r = (radio*)h; return r->set_rx_gain_th(gain); } @@ -181,7 +181,7 @@ static void* plot_thread_run(void* arg) srslte_vec_abs_square_cf(fft_plot_buffer[r], fft_plot_temp, fft_plot_buffer_size); for (uint32_t j = 0; j < fft_plot_buffer_size; j++) { - fft_plot_temp[j] = 10.0f * log10f(fft_plot_temp[j]); + fft_plot_temp[j] = srslte_convert_power_to_dB(fft_plot_temp[j]); } plot_real_setNewData(&fft_plot[r], fft_plot_temp, fft_plot_buffer_size); @@ -189,7 +189,7 @@ static void* plot_thread_run(void* arg) } } - return NULL; + return nullptr; } static int init_plots(uint32_t frame_size) diff --git a/srsenb/src/metrics_csv.cc b/srsenb/src/metrics_csv.cc index 279291a0a..e234a53ee 100644 --- a/srsenb/src/metrics_csv.cc +++ b/srsenb/src/metrics_csv.cc @@ -107,7 +107,7 @@ void metrics_csv::set_metrics(enb_metrics_t &metrics, const uint32_t period_usec std::string metrics_csv::float_to_string(float f, int digits, bool add_semicolon) { std::ostringstream os; - const int precision = (f == 0.0) ? digits-1 : digits - log10(fabs(f))-2*DBL_EPSILON; + const int precision = (f == 0.0) ? digits - 1 : digits - log10f(fabs(f)) - 2 * DBL_EPSILON; os << std::fixed << std::setprecision(precision) << f; if (add_semicolon) os << ';'; diff --git a/srsenb/src/metrics_stdout.cc b/srsenb/src/metrics_stdout.cc index a93ec0d31..c4c64a881 100644 --- a/srsenb/src/metrics_stdout.cc +++ b/srsenb/src/metrics_stdout.cc @@ -150,7 +150,7 @@ std::string metrics_stdout::float_to_string(float f, int digits) precision = digits-1; } else { - precision = digits - (int)(log10(fabs(f))-2*DBL_EPSILON); + precision = digits - (int)(log10f(fabs(f)) - 2 * DBL_EPSILON); } os << std::setw(6) << std::fixed << std::setprecision(precision) << f; return os.str(); @@ -158,7 +158,7 @@ std::string metrics_stdout::float_to_string(float f, int digits) std::string metrics_stdout::float_to_eng_string(float f, int digits) { - const int degree = (f == 0.0) ? 0 : lrint( floor( log10( fabs( f ) ) / 3) ); + const int degree = (f == 0.0) ? 0 : lrint(floor(log10f(fabs(f)) / 3)); std::string factor; diff --git a/srsue/src/metrics_csv.cc b/srsue/src/metrics_csv.cc index b6a1a8113..8c9401cbd 100644 --- a/srsue/src/metrics_csv.cc +++ b/srsue/src/metrics_csv.cc @@ -144,7 +144,7 @@ void metrics_csv::set_metrics(ue_metrics_t &metrics, const uint32_t period_usec) std::string metrics_csv::float_to_string(float f, int digits, bool add_semicolon) { std::ostringstream os; - const int precision = (f == 0.0) ? digits-1 : digits - log10(fabs(f))-2*DBL_EPSILON; + const int precision = (f == 0.0) ? digits - 1 : digits - log10f(fabs(f)) - 2 * DBL_EPSILON; os << std::fixed << std::setprecision(precision) << f; if (add_semicolon) os << ';'; diff --git a/srsue/src/metrics_stdout.cc b/srsue/src/metrics_stdout.cc index d36021963..83affebb8 100644 --- a/srsue/src/metrics_stdout.cc +++ b/srsue/src/metrics_stdout.cc @@ -129,7 +129,7 @@ std::string metrics_stdout::float_to_string(float f, int digits) std::string metrics_stdout::float_to_eng_string(float f, int digits) { - const int degree = (f == 0.0) ? 0 : lrint( floor( log10( fabs( f ) ) / 3) ); + const int degree = (f == 0.0) ? 0 : lrint(floor(log10f(fabs(f)) / 3)); std::string factor; diff --git a/srsue/src/phy/scell/async_scell_recv.cc b/srsue/src/phy/scell/async_scell_recv.cc index e183ddaf7..797e94d04 100644 --- a/srsue/src/phy/scell/async_scell_recv.cc +++ b/srsue/src/phy/scell/async_scell_recv.cc @@ -79,9 +79,9 @@ static int radio_recv_callback(void* obj, cf_t* data[SRSLTE_MAX_PORTS], uint32_t return ((async_scell_recv*)obj)->radio_recv_fnc(data, nsamples, rx_time); } -static double callback_set_rx_gain(void* h, double gain) +static float callback_set_rx_gain(void* h, float gain) { - return ((async_scell_recv*)h)->set_rx_gain(gain); + return (float)((async_scell_recv*)h)->set_rx_gain(gain); } void async_scell_recv::init(srslte::radio_interface_phy* _radio_handler, phy_common* _worker_com, srslte::log* _log_h) diff --git a/srsue/src/phy/scell/measure.cc b/srsue/src/phy/scell/measure.cc index f1dd0a149..7360f3453 100644 --- a/srsue/src/phy/scell/measure.cc +++ b/srsue/src/phy/scell/measure.cc @@ -84,17 +84,17 @@ void measure::set_cell(srslte_cell_t cell) float measure::rssi() { - return 10 * log10(mean_rssi); + return srslte_convert_power_to_dB(mean_rssi); } float measure::rsrp() { - return 10 * log10(mean_rsrp) + 30 - rx_gain_offset; + return srslte_convert_power_to_dBm(mean_rsrp) - rx_gain_offset; } float measure::rsrq() { - return 10 * log10(mean_rsrq); + return srslte_convert_power_to_dB(mean_rsrq); } float measure::snr() @@ -112,9 +112,9 @@ uint32_t measure::frame_st_idx() return final_offset; } -void measure::set_rx_gain_offset(float rx_gain_offset) +void measure::set_rx_gain_offset(float rx_gain_offset_) { - this->rx_gain_offset = rx_gain_offset; + rx_gain_offset = rx_gain_offset_; } measure::ret_code measure::run_multiple_subframes(cf_t* input_buffer, uint32_t offset, uint32_t sf_idx, uint32_t max_sf) diff --git a/srsue/src/phy/sf_worker.cc b/srsue/src/phy/sf_worker.cc index 5ad7efda5..87cd3d8a2 100644 --- a/srsue/src/phy/sf_worker.cc +++ b/srsue/src/phy/sf_worker.cc @@ -294,11 +294,11 @@ void sf_worker::update_measurements() if (get_id() == 0) { // Average RSSI over all symbols in antenna port 0 (make sure SF length is non-zero) - float rssi_dbm = - SRSLTE_SF_LEN_PRB(cell.nof_prb) > 0 - ? (10 * log10(srslte_vec_avg_power_cf(cc_workers[0]->get_rx_buffer(0), SRSLTE_SF_LEN_PRB(cell.nof_prb))) + - 30) - : 0; + float rssi_dbm = SRSLTE_SF_LEN_PRB(cell.nof_prb) > 0 + ? (srslte_convert_power_to_dB(srslte_vec_avg_power_cf(cc_workers[0]->get_rx_buffer(0), + SRSLTE_SF_LEN_PRB(cell.nof_prb))) + + 30) + : 0; if (std::isnormal(rssi_dbm)) { phy->avg_rssi_dbm = SRSLTE_VEC_EMA(rssi_dbm, phy->avg_rssi_dbm, phy->args->snr_ema_coeff); } diff --git a/srsue/src/phy/sync.cc b/srsue/src/phy/sync.cc index 35dae9655..c09413893 100644 --- a/srsue/src/phy/sync.cc +++ b/srsue/src/phy/sync.cc @@ -38,7 +38,8 @@ int radio_recv_callback(void *obj, cf_t *data[SRSLTE_MAX_PORTS], uint32_t nsampl return ((sync*)obj)->radio_recv_fnc(data, nsamples, rx_time); } -double callback_set_rx_gain(void *h, double gain) { +float callback_set_rx_gain(void* h, float gain) +{ return ((sync*)h)->set_rx_gain(gain); } diff --git a/srsue/test/phy/scell_search_test.cc b/srsue/test/phy/scell_search_test.cc index aa422243c..2db51a417 100644 --- a/srsue/test/phy/scell_search_test.cc +++ b/srsue/test/phy/scell_search_test.cc @@ -194,12 +194,11 @@ public: } // Undo srslte_enb_dl_gen_signal scaling - float scale = sqrt(cell_base.nof_prb) / 0.05f / enb_dl.ifft->symbol_sz; + float scale = sqrtf(cell_base.nof_prb) / 0.05f / enb_dl.ifft->symbol_sz; // Apply Neighbour cell attenuation if (enb_dl.cell.id != cell_id_start) { - float scale_dB = -ncell_attenuation_dB; - scale *= powf(10.0f, scale_dB / 20.0f); + scale *= srslte_convert_dB_to_amplitude(-ncell_attenuation_dB); } // Scale signal