From 41877812683cdcda57e78626cede6ef2e3abde19 Mon Sep 17 00:00:00 2001 From: Xavier Arteaga Date: Tue, 30 Apr 2019 14:48:13 +0200 Subject: [PATCH] Random module return pointer object and solved NAN issue --- lib/include/srslte/phy/utils/random.h | 10 ++++----- lib/src/phy/utils/random.cpp | 31 +++++++++++++++++++++------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/include/srslte/phy/utils/random.h b/lib/include/srslte/phy/utils/random.h index 11650446d..baf3cd76b 100644 --- a/lib/include/srslte/phy/utils/random.h +++ b/lib/include/srslte/phy/utils/random.h @@ -32,15 +32,15 @@ extern "C" { typedef void* srslte_random_t; -SRSLTE_API srslte_random_t* srslte_random_init(uint32_t seed); +SRSLTE_API srslte_random_t srslte_random_init(uint32_t seed); -SRSLTE_API int srslte_random_uniform_int_dist(srslte_random_t* q, int min, int max); +SRSLTE_API int srslte_random_uniform_int_dist(srslte_random_t q, int min, int max); -SRSLTE_API float srslte_random_uniform_real_dist(srslte_random_t* q, float min, float max); +SRSLTE_API float srslte_random_uniform_real_dist(srslte_random_t q, float min, float max); -SRSLTE_API float srslte_random_gauss_dist(srslte_random_t* q, float std_dev); +SRSLTE_API float srslte_random_gauss_dist(srslte_random_t q, float std_dev); -SRSLTE_API void srslte_random_free(srslte_random_t* q); +SRSLTE_API void srslte_random_free(srslte_random_t q); #ifdef __cplusplus } diff --git a/lib/src/phy/utils/random.cpp b/lib/src/phy/utils/random.cpp index c44cc6e22..bccf08a44 100644 --- a/lib/src/phy/utils/random.cpp +++ b/lib/src/phy/utils/random.cpp @@ -20,6 +20,8 @@ */ #include "srslte/phy/utils/random.h" +#include +#include #include class random_wrap @@ -53,12 +55,12 @@ public: extern "C" { -srslte_random_t* srslte_random_init(uint32_t seed) +srslte_random_t srslte_random_init(uint32_t seed) { - return (srslte_random_t*)(new random_wrap(seed)); + return (srslte_random_t)(new random_wrap(seed)); } -int srslte_random_uniform_int_dist(srslte_random_t* q, int min, int max) +int srslte_random_uniform_int_dist(srslte_random_t q, int min, int max) { int ret = 0; @@ -70,19 +72,34 @@ int srslte_random_uniform_int_dist(srslte_random_t* q, int min, int max) return ret; } -float srslte_random_uniform_real_dist(srslte_random_t* q, float min, float max) +float srslte_random_uniform_real_dist(srslte_random_t q, float min, float max) { float ret = NAN; if (q) { auto* h = (random_wrap*)q; - ret = h->uniform_real_dist(min, max); + while (isnan(ret)) { + ret = h->uniform_real_dist(min, max); + } } return ret; } -float srslte_random_gauss_dist(srslte_random_t* q, float std_dev) +cf_t srslte_random_uniform_complex_dist(srslte_random_t q, float min, float max) +{ + cf_t ret = NAN; + + if (q) { + auto* h = (random_wrap*)q; + std::complex x = std::complex(h->uniform_real_dist(min, max), h->uniform_real_dist(min, max)); + ret = *((cf_t*)&x); + } + + return ret; +} + +float srslte_random_gauss_dist(srslte_random_t q, float std_dev) { float ret = NAN; @@ -94,7 +111,7 @@ float srslte_random_gauss_dist(srslte_random_t* q, float std_dev) return ret; } -void srslte_random_free(srslte_random_t* q) +void srslte_random_free(srslte_random_t q) { if (q) { delete (random_wrap*)q;