summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan <stefan.wunsch@student.kit.edu>2015-09-01 13:21:51 +0200
committerStefan <stefan.wunsch@student.kit.edu>2015-09-01 13:21:51 +0200
commitebad2162b40b2b144449ff2927dbe89c683c4972 (patch)
tree5a8219cd5001f0402a21ac9eb28670c22aefeefb
parent1206251231696359270a260508551e044f3af33a (diff)
fix wrong laplacian random numbers and add testcase
-rw-r--r--gnuradio-runtime/include/gnuradio/random.h21
-rw-r--r--gnuradio-runtime/lib/math/random.cc23
-rw-r--r--gnuradio-runtime/python/gnuradio/gr/qa_random.py51
3 files changed, 54 insertions, 41 deletions
diff --git a/gnuradio-runtime/include/gnuradio/random.h b/gnuradio-runtime/include/gnuradio/random.h
index e01fcb7be7..454c88ee6c 100644
--- a/gnuradio-runtime/include/gnuradio/random.h
+++ b/gnuradio-runtime/include/gnuradio/random.h
@@ -60,18 +60,33 @@ namespace gr {
void reseed(long seed);
/*!
- * \brief uniform random deviate in the range [0.0, 1.0)
+ * \brief Uniform random numbers in the range [0.0, 1.0)
*/
float ran1();
/*!
- * \brief normally distributed deviate with zero mean and variance 1
+ * \brief Normally distributed random numbers (Gaussian distribution with zero mean and variance 1)
*/
float gasdev();
+ /*!
+ * \brief Laplacian distributed random numbers with zero mean and variance 1
+ */
float laplacian();
- float impulse(float factor);
+
+ /*!
+ * \brief Rayleigh distributed random numbers (zero mean and variance 1 for the underlying Gaussian distributions)
+ */
float rayleigh();
+
+ /*!
+ * \brief FIXME: add description
+ */
+ float impulse(float factor);
+
+ /*!
+ * \brief Normally distributed random numbers with zero mean and variance 1 on real and imaginary part. This results in a Rayleigh distribution for the amplitude and an uniform distribution for the phase.
+ */
gr_complex rayleigh_complex();
};
diff --git a/gnuradio-runtime/lib/math/random.cc b/gnuradio-runtime/lib/math/random.cc
index 7170f27edf..ecf70e0f9a 100644
--- a/gnuradio-runtime/lib/math/random.cc
+++ b/gnuradio-runtime/lib/math/random.cc
@@ -130,18 +130,12 @@ namespace gr {
return d_gset;
}
- /*
- * Copied from The KC7WW / OH2BNS Channel Simulator
- * FIXME Need to check how good this is at some point
- */
float
random::laplacian()
{
- float z = ran1();
- if(z < 0.5)
- return log(2.0 * z) / M_SQRT2;
- else
- return -log(2.0 * (1.0 - z)) / M_SQRT2;
+ float z = ran1()-0.5;
+ if(z>0) return -log(1-2*z);
+ else return log(1+2*z);
}
/*
@@ -161,10 +155,6 @@ namespace gr {
/*
* Complex rayleigh is really gaussian I and gaussian Q
- * It can also be generated by real rayleigh magnitude and
- * uniform random angle
- * Adapted from The KC7WW / OH2BNS Channel Simulator
- * FIXME Need to check how good this is at some point
*/
gr_complex
random::rayleigh_complex()
@@ -172,13 +162,6 @@ namespace gr {
return gr_complex(gasdev(),gasdev());
}
- /* Other option
- mag = rayleigh();
- ang = 2.0 * M_PI * RNG();
- *Rx = rxx * cos(z);
- *Iy = rxx * sin(z);
- */
-
float
random::rayleigh()
{
diff --git a/gnuradio-runtime/python/gnuradio/gr/qa_random.py b/gnuradio-runtime/python/gnuradio/gr/qa_random.py
index 39d75f3afa..ee4018327b 100644
--- a/gnuradio-runtime/python/gnuradio/gr/qa_random.py
+++ b/gnuradio-runtime/python/gnuradio/gr/qa_random.py
@@ -22,78 +22,93 @@
from gnuradio import gr, gr_unittest
import numpy as np
-from scipy.stats import norm, laplace
+from scipy.stats import norm, laplace, rayleigh
class test_random(gr_unittest.TestCase):
+ num_tests = 10000
+
# Disclaimer
def test_0(self):
- print 'NOTE: Following tests are not statistically significant! Check out fulltest_random.py for full testing.'
+ print 'NOTE: Following tests are not statistically significant!'
+ print 'Realisations per test:',self.num_tests
self.assertEqual(1,1)
# Check for range [0,1) of uniform distributed random numbers and print minimal and maximal value
def test_1(self):
print '# TEST 1'
print 'Uniform distributed numbers: Range'
- num_tests = 10000
- values = np.zeros(num_tests)
+ values = np.zeros(self.num_tests)
rndm = gr.random()
- for k in range(num_tests):
+ for k in range(self.num_tests):
values[k] = rndm.ran1()
for value in values:
self.assertLess(value, 1)
self.assertGreaterEqual(value, 0)
- print 'Uniform random numbers (num/min/max):', num_tests, min(values), max(values)
+ print 'Uniform random numbers (num/min/max):', self.num_tests, min(values), max(values)
# Check uniformly distributed random numbers on uniformity (without assert, only printing)
def test_2(self):
print '# TEST 2'
print 'Uniform random numbers: Distribution'
- num_tests = 10000
num_bins = 11
- values = np.zeros(num_tests)
+ values = np.zeros(self.num_tests)
rndm = gr.random()
- for k in range(num_tests):
+ for k in range(self.num_tests):
values[k] = rndm.ran1()
bins = np.linspace(0,1,num_bins) # These are the bin edges!
hist = np.histogram(values,bins)
print 'Lower edge bin / upper edge bin / count / expected'
for k in range(len(hist[0])):
- print hist[1][k], hist[1][k+1], hist[0][k], float(num_tests)/(num_bins-1)
+ print hist[1][k], hist[1][k+1], hist[0][k], float(self.num_tests)/(num_bins-1)
# Check distribution of normally (gaussian, mean=0, variance=1) distributed random numbers (no assert)
def test_3(self):
print '# TEST 3'
print 'Normal random numbers: Distribution'
- num_tests = 10000
num_bins = 11
hist_range = [-5,5]
- values = np.zeros(num_tests)
+ values = np.zeros(self.num_tests)
rndm = gr.random()
- for k in range(num_tests):
+ for k in range(self.num_tests):
values[k] = rndm.gasdev()
bins = np.linspace(hist_range[0],hist_range[1],num_bins)
hist = np.histogram(values,bins)
print 'Lower edge bin / upper edge bin / count / expected'
for k in range(len(hist[0])):
- print hist[1][k], hist[1][k+1], hist[0][k], float(norm.cdf(hist[1][k+1])-norm.cdf(hist[1][k]))*num_tests
+ print hist[1][k], hist[1][k+1], hist[0][k], float(norm.cdf(hist[1][k+1])-norm.cdf(hist[1][k]))*self.num_tests
# Check distribution of laplacian (mean=0, variance=1) distributed random numbers (no assert)
def test_4(self):
print '# TEST 4'
print 'Laplacian random numbers: Distribution'
- num_tests = 100000
num_bins = 11
hist_range = [-5,5]
- values = np.zeros(num_tests)
+ values = np.zeros(self.num_tests)
rndm = gr.random()
- for k in range(num_tests):
+ for k in range(self.num_tests):
values[k] = rndm.laplacian()
bins = np.linspace(hist_range[0],hist_range[1],num_bins)
hist = np.histogram(values,bins)
print 'Lower edge bin / upper edge bin / count / expected'
for k in range(len(hist[0])):
- print hist[1][k], hist[1][k+1], hist[0][k], float(laplace.cdf(hist[1][k+1])-laplace.cdf(hist[1][k]))*num_tests
+ print hist[1][k], hist[1][k+1], hist[0][k], float(laplace.cdf(hist[1][k+1])-laplace.cdf(hist[1][k]))*self.num_tests
+
+ # Check distribution of laplacian (mean=0, variance=1) distributed random numbers (no assert)
+ def test_5(self):
+ print '# TEST 5'
+ print 'Rayleigh random numbers: Distribution'
+ num_bins = 11
+ hist_range = [0,10]
+ values = np.zeros(self.num_tests)
+ rndm = gr.random()
+ for k in range(self.num_tests):
+ values[k] = rndm.rayleigh()
+ bins = np.linspace(hist_range[0],hist_range[1],num_bins)
+ hist = np.histogram(values,bins)
+ print 'Lower edge bin / upper edge bin / count / expected'
+ for k in range(len(hist[0])):
+ print hist[1][k], hist[1][k+1], hist[0][k], float(rayleigh.cdf(hist[1][k+1])-rayleigh.cdf(hist[1][k]))*self.num_tests
if __name__ == '__main__':
gr_unittest.run(test_random, "test_random.xml")