commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 01/07: random-analog: added new uniform int


From: git
Subject: [Commit-gnuradio] [gnuradio] 01/07: random-analog: added new uniform integer distribution source
Date: Wed, 7 Oct 2015 20:51:22 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch master
in repository gnuradio.

commit 44ef5e1c240b7d5aeac7ca6537c4b07f3cf606c5
Author: Johannes Demel <address@hidden>
Date:   Fri Sep 25 20:24:18 2015 +0200

    random-analog: added new uniform integer distribution source
---
 gnuradio-runtime/include/gnuradio/random.h         | 16 +++-
 gnuradio-runtime/lib/math/random.cc                | 21 ++++-
 gnuradio-runtime/python/gnuradio/gr/qa_random.py   | 37 ++++++---
 gr-analog/grc/CMakeLists.txt                       |  1 +
 gr-analog/grc/analog_random_uniform_source_x.xml   | 56 ++++++++++++++
 gr-analog/include/gnuradio/analog/CMakeLists.txt   |  1 +
 .../gnuradio/analog/random_uniform_source_X.h.t    | 61 +++++++++++++++
 gr-analog/lib/CMakeLists.txt                       |  1 +
 gr-analog/lib/random_uniform_source_X_impl.cc.t    | 83 ++++++++++++++++++++
 gr-analog/lib/random_uniform_source_X_impl.h.t     | 54 +++++++++++++
 .../python/analog/qa_random_uniform_source.py      | 90 ++++++++++++++++++++++
 gr-analog/swig/analog_swig.i                       |  9 +++
 12 files changed, 417 insertions(+), 13 deletions(-)

diff --git a/gnuradio-runtime/include/gnuradio/random.h 
b/gnuradio-runtime/include/gnuradio/random.h
index e95f36d..c5761d7 100644
--- a/gnuradio-runtime/include/gnuradio/random.h
+++ b/gnuradio-runtime/include/gnuradio/random.h
@@ -45,10 +45,12 @@ namespace gr {
 
     boost::mt19937 *d_rng; // mersenne twister as random number generator
     boost::uniform_real<float> *d_uniform; // choose uniform distribution, 
default is [0,1)
+    boost::random::uniform_int_distribution<> *d_integer_dis;
     boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > 
*d_generator;
+    boost::variate_generator<boost::mt19937&, 
boost::random::uniform_int_distribution<> > *d_integer_generator;
 
   public:
-    random(unsigned int seed=0);
+    random(unsigned int seed=0, int min_integer = 0, int max_integer = 2);
     ~random();
 
     /*!
@@ -57,6 +59,18 @@ namespace gr {
     void reseed(unsigned int seed);
 
     /*!
+     * set minimum and maximum for integer random number generator.
+     * Limits are [minimum, maximum)
+     * Default: [0, std::numeric_limits< IntType >::max)]
+     */
+    void set_integer_limits(const int minimum, const int maximum);
+
+    /*!
+     * Uniform random integers in the range set by 'set_integer_limits'.
+     */
+    int ran_int();
+
+    /*!
      * \brief Uniform random numbers in the range [0.0, 1.0)
      */
     float ran1();
diff --git a/gnuradio-runtime/lib/math/random.cc 
b/gnuradio-runtime/lib/math/random.cc
index a2b2621..b35dfa1 100644
--- a/gnuradio-runtime/lib/math/random.cc
+++ b/gnuradio-runtime/lib/math/random.cc
@@ -44,7 +44,7 @@
 
 namespace gr {
 
-  random::random(unsigned int seed)
+  random::random(unsigned int seed, int min_integer, int max_integer)
   {
     d_gauss_stored = false; // set gasdev (gauss distributed numbers) on 
calculation state
 
@@ -52,14 +52,18 @@ namespace gr {
     d_rng = new boost::mt19937;
     reseed(seed); // set seed for random number generator
     d_uniform = new boost::uniform_real<float>;
+    d_integer_dis = new boost::random::uniform_int_distribution<>(min_integer, 
max_integer - 1);
     d_generator = new boost::variate_generator<boost::mt19937&, 
boost::uniform_real<float> > (*d_rng,*d_uniform); // create number generator in 
[0,1) from boost.random
+    d_integer_generator = new boost::variate_generator<boost::mt19937&, 
boost::random::uniform_int_distribution<> >(*d_rng, *d_integer_dis);
   }
 
   random::~random()
   {
       delete d_rng;
       delete d_uniform;
+      delete d_integer_dis;
       delete d_generator;
+      delete d_integer_generator;
   }
 
   /*
@@ -68,11 +72,26 @@ namespace gr {
   void
   random::reseed(unsigned int seed)
   {
+    //FIXME: method without effect after c'tor.
     if(seed==0) d_seed = static_cast<unsigned int>(std::time(0));
     else d_seed = seed;
     d_rng->seed(d_seed);
   }
 
+  void
+  random::set_integer_limits(const int minimum, const int maximum){
+    // boost expects integer limits defined as [minimum, maximum] which is 
unintuitive.
+    boost::random::uniform_int_distribution<>::param_type dis_params(minimum, 
maximum - 1);
+    d_integer_dis->param(dis_params);
+    delete d_integer_generator;
+    d_integer_generator = new boost::variate_generator<boost::mt19937&, 
boost::random::uniform_int_distribution<> >(*d_rng, *d_integer_dis);
+  }
+
+  int
+  random::ran_int(){
+    return (*d_integer_generator)();
+  }
+
   /*
    * Returns uniformly distributed numbers in [0,1) taken from boost.random 
using a Mersenne twister
    */
diff --git a/gnuradio-runtime/python/gnuradio/gr/qa_random.py 
b/gnuradio-runtime/python/gnuradio/gr/qa_random.py
index 83fee56..6e32189 100644
--- a/gnuradio-runtime/python/gnuradio/gr/qa_random.py
+++ b/gnuradio-runtime/python/gnuradio/gr/qa_random.py
@@ -23,8 +23,8 @@
 from gnuradio import gr, gr_unittest
 import numpy as np
 
-class test_random(gr_unittest.TestCase):
 
+class test_random(gr_unittest.TestCase):
     # NOTE: For tests on the output distribution of the random numbers, see 
gnuradio-runtime/apps/evaluation_random_numbers.py.
 
     # Check for range [0,1) of uniform distributed random numbers
@@ -42,23 +42,38 @@ class test_random(gr_unittest.TestCase):
     def test_2(self):
         num = 5
 
-        rndm0 = gr.random(42); # init with time
-        rndm1 = gr.random(42); # init with fix seed
+        rndm0 = gr.random(42)  # init with time
+        rndm1 = gr.random(42)  # init with fix seed
         for k in range(num):
-            x = rndm0.ran1();
-            y = rndm1.ran1();
-            self.assertEqual(x,y)
+            x = rndm0.ran1()
+            y = rndm1.ran1()
+            self.assertEqual(x, y)
 
         x = np.zeros(num)
         y = np.zeros(num)
-        rndm0 = gr.random(42); # init with fix seed 1
+        rndm0 = gr.random(42)  # init with fix seed 1
         for k in range(num):
-            x[k] = rndm0.ran1();
-        rndm1.reseed(43); # init with fix seed 2
+            x[k] = rndm0.ran1()
+        rndm1.reseed(43)  # init with fix seed 2
         for k in range(num):
-            y[k] = rndm0.ran1();
+            y[k] = rndm0.ran1()
         for k in range(num):
-            self.assertNotEqual(x[k],y[k])
+            self.assertNotEqual(x[k], y[k])
+
+    def test_003_integer(self):
+        nitems = 100000
+        minimum = 2
+        maximum = 42
+
+        rng = gr.random(1, minimum, maximum)
+
+        rnd_vals = np.zeros(nitems, dtype=int)
+        for i in range(nitems):
+            rnd_vals[i] = rng.ran_int()
+
+        self.assertGreaterEqual(minimum, np.min(rnd_vals))
+        self.assertLess(np.max(rnd_vals), maximum)
+
 
 if __name__ == '__main__':
     gr_unittest.run(test_random, "test_random.xml")
diff --git a/gr-analog/grc/CMakeLists.txt b/gr-analog/grc/CMakeLists.txt
index 908ef0e..a699d7c 100644
--- a/gr-analog/grc/CMakeLists.txt
+++ b/gr-analog/grc/CMakeLists.txt
@@ -19,3 +19,4 @@
 
 file(GLOB xml_files "*.xml")
 install(FILES ${xml_files} DESTINATION ${GRC_BLOCKS_DIR} COMPONENT 
"analog_python")
+
diff --git a/gr-analog/grc/analog_random_uniform_source_x.xml 
b/gr-analog/grc/analog_random_uniform_source_x.xml
new file mode 100644
index 0000000..614b800
--- /dev/null
+++ b/gr-analog/grc/analog_random_uniform_source_x.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0"?>
+<block>
+  <name>Random Uniform Source</name>
+  <key>analog_random_uniform_source_x</key>
+  <category>analog</category>
+  <import>from gnuradio import analog</import>
+  <make>analog.random_uniform_source_$(type.fcn)($minimum, $maximum, 
$seed)</make>
+
+  <param>
+    <name>Output Type</name>
+    <key>type</key>
+    <type>enum</type>
+    <option>
+      <name>Int</name>
+      <key>int</key>
+      <opt>fcn:i</opt>
+      <opt>offset_type:int</opt>
+    </option>
+    <option>
+      <name>Short</name>
+      <key>short</key>
+      <opt>fcn:s</opt>
+      <opt>offset_type:int</opt>
+    </option>
+    <option>
+      <name>Byte</name>
+      <key>byte</key>
+      <opt>fcn:b</opt>
+      <opt>offset_type:int</opt>
+    </option>
+  </param>
+
+  <param>
+    <name>Minimum</name>
+    <key>minimum</key>
+    <type>int</type>
+  </param>
+
+  <param>
+    <name>Maximum</name>
+    <key>maximum</key>
+    <type>int</type>
+  </param>
+
+  <param>
+    <name>Seed</name>
+    <key>seed</key>
+    <value>0</value>
+    <type>int</type>
+  </param>
+
+  <source>
+    <name>out</name>
+    <type>$type</type>
+  </source>
+</block>
diff --git a/gr-analog/include/gnuradio/analog/CMakeLists.txt 
b/gr-analog/include/gnuradio/analog/CMakeLists.txt
index 430e643..b533363 100644
--- a/gr-analog/include/gnuradio/analog/CMakeLists.txt
+++ b/gr-analog/include/gnuradio/analog/CMakeLists.txt
@@ -24,6 +24,7 @@ include(GrMiscUtils)
 GR_EXPAND_X_H(analog noise_source_X     s i f c)
 GR_EXPAND_X_H(analog fastnoise_source_X s i f c)
 GR_EXPAND_X_H(analog sig_source_X       s i f c)
+GR_EXPAND_X_H(analog random_uniform_source_X b s i)
 
 add_custom_target(analog_generated_includes DEPENDS
     ${generated_includes}
diff --git a/gr-analog/include/gnuradio/analog/random_uniform_source_X.h.t 
b/gr-analog/include/gnuradio/analog/random_uniform_source_X.h.t
new file mode 100644
index 0000000..d2f7cbd
--- /dev/null
+++ b/gr-analog/include/gnuradio/analog/random_uniform_source_X.h.t
@@ -0,0 +1,61 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2015 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+/* @WARNING@ */
+
+#ifndef @GUARD_NAME@
+#define @GUARD_NAME@
+
+#include <gnuradio/analog/api.h>
+#include <gnuradio/sync_block.h>
+
+namespace gr {
+  namespace analog {
+
+    /*!
+     * \brief Uniform Random Number Generator with @TYPE@ output.
+     * \ingroup waveform_generators_blk
+     */
+    class ANALOG_API @BASE_NAME@ : virtual public sync_block
+    {
+    public:
+      // gr::analog::@BASE_NAME@::sptr
+      typedef boost::shared_ptr<@BASE_NAME@> sptr;
+
+      /*!
+       * \brief Return a shared_ptr to a new instance of 
analog::random_uniform_source_X.
+       *
+       * To avoid accidental use of raw pointers, 
analog::random_uniform_source_b's
+       * constructor is in a private implementation
+       * class. analog::random_uniform_source_b::make is the public interface 
for
+       * creating new instances.
+       * \param minimum defines minimal integer value output.
+       * \param maximum output values are below this value
+       * \param seed for Pseudo Random Number Generator. Defaults to 0. In 
this case current time is used.
+       */
+      static sptr make(int minimum, int maximum, int seed);
+    };
+
+  } /* namespace analog */
+} /* namespace gr */
+
+#endif /* @GUARD_NAME@ */
diff --git a/gr-analog/lib/CMakeLists.txt b/gr-analog/lib/CMakeLists.txt
index 054fa13..918f894 100644
--- a/gr-analog/lib/CMakeLists.txt
+++ b/gr-analog/lib/CMakeLists.txt
@@ -46,6 +46,7 @@ include(GrMiscUtils)
 GR_EXPAND_X_CC_H(analog noise_source_X_impl     s i f c)
 GR_EXPAND_X_CC_H(analog fastnoise_source_X_impl s i f c)
 GR_EXPAND_X_CC_H(analog sig_source_X_impl     s i f c)
+GR_EXPAND_X_CC_H(analog random_uniform_source_X_impl b s i)
 
 ########################################################################
 # Setup library
diff --git a/gr-analog/lib/random_uniform_source_X_impl.cc.t 
b/gr-analog/lib/random_uniform_source_X_impl.cc.t
new file mode 100644
index 0000000..a93f887
--- /dev/null
+++ b/gr-analog/lib/random_uniform_source_X_impl.cc.t
@@ -0,0 +1,83 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2015 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+/* @WARNING@ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "@address@hidden"
+#include <gnuradio/io_signature.h>
+
+namespace gr {
+  namespace analog {
+
+    @BASE_NAME@::sptr
+    @BASE_NAME@::make(int minimum, int maximum, int seed)
+    {
+      return gnuradio::get_initial_sptr(new @IMPL_NAME@(minimum, maximum, 
seed));
+    }
+
+    @IMPL_NAME@::@IMPL_NAME@(int minimum, int maximum, int seed)
+    : sync_block("@BASE_NAME@",
+            io_signature::make(0, 0, 0),
+            io_signature::make(1, 1, sizeof(@TYPE@)))
+    {
+      d_rng = new gr::random(seed, minimum, maximum);
+    }
+
+    @IMPL_NAME@::address@hidden@()
+    {
+      delete d_rng;
+    }
+
+    int
+    @IMPL_NAME@::random_int()
+    {
+      return d_rng->ran_int();
+    }
+
+    int
+    @IMPL_NAME@::work(int noutput_items,
+              gr_vector_const_void_star &input_items,
+              gr_vector_void_star &output_items)
+    {
+      @TYPE@ *out = (@address@hidden)output_items[0];
+
+      for(int i = 0; i < noutput_items; i++){
+        *out++ = (@TYPE@) random_int();
+      }
+
+      // Tell runtime system how many output items we produced.
+      return noutput_items;
+    }
+
+
+
+
+
+
+
+
+  } /* namespace analog */
+} /* namespace gr */
diff --git a/gr-analog/lib/random_uniform_source_X_impl.h.t 
b/gr-analog/lib/random_uniform_source_X_impl.h.t
new file mode 100644
index 0000000..289ee3c
--- /dev/null
+++ b/gr-analog/lib/random_uniform_source_X_impl.h.t
@@ -0,0 +1,54 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2015 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+// @WARNING@
+
+#ifndef @GUARD_NAME@
+#define @GUARD_NAME@
+
+#include <gnuradio/analog/@address@hidden>
+#include <gnuradio/random.h>
+
+namespace gr {
+  namespace analog {
+
+    class @IMPL_NAME@ : public @BASE_NAME@
+    {
+    private:
+      gr::random *d_rng;
+
+    public:
+      @IMPL_NAME@(int minimum, int maximum, int seed);
+      address@hidden@();
+
+      // Where all the action really happens
+      int work(int noutput_items,
+           gr_vector_const_void_star &input_items,
+           gr_vector_void_star &output_items);
+
+      int random_int();
+    };
+
+  } /* namespace filter */
+} /* namespace gr */
+
+#endif /* @GUARD_NAME@ */
\ No newline at end of file
diff --git a/gr-analog/python/analog/qa_random_uniform_source.py 
b/gr-analog/python/analog/qa_random_uniform_source.py
new file mode 100755
index 0000000..6a6fb82
--- /dev/null
+++ b/gr-analog/python/analog/qa_random_uniform_source.py
@@ -0,0 +1,90 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# 
+# Copyright 2015 Free Software Foundation, Inc.
+# 
+# This file is part of GNU Radio
+# 
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+# 
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+# 
+
+from gnuradio import gr, gr_unittest
+from gnuradio import blocks
+import analog_swig as analog
+import numpy as np
+
+
+class qa_random_uniform_source(gr_unittest.TestCase):
+    def setUp(self):
+        self.tb = gr.top_block()
+
+    def tearDown(self):
+        self.tb = None
+
+    def test_001_byte(self):
+        minimum = 0
+        maximum = 5
+        seed = 3
+        n_items = 10000
+        rnd_src = analog.random_uniform_source_b(minimum, maximum, seed)
+        head = blocks.head(1, n_items)
+        snk = blocks.vector_sink_b(1)
+        self.tb.connect(rnd_src, head, snk)
+        # set up fg
+        self.tb.run()
+        # check data
+        res = snk.data()
+        self.assertGreaterEqual(minimum, np.min(res))
+        self.assertLess(np.max(res), maximum)
+
+    def test_002_short(self):
+        minimum = 42
+        maximum = 1025
+        seed = 3
+        n_items = 10000
+        rnd_src = analog.random_uniform_source_s(minimum, maximum, seed)
+        head = blocks.head(2, n_items)
+        snk = blocks.vector_sink_s(1)
+        self.tb.connect(rnd_src, head, snk)
+        # set up fg
+        self.tb.run()
+        # check data
+        res = snk.data()
+        self.assertGreaterEqual(minimum, np.min(res))
+        self.assertLess(np.max(res), maximum)
+
+    def test_003_int(self):
+        minimum = 2 ** 12 - 2
+        maximum = 2 ** 17 + 5
+        seed = 3
+        n_items = 10000
+        rnd_src = analog.random_uniform_source_i(minimum, maximum, seed)
+        head = blocks.head(4, n_items)
+        snk = blocks.vector_sink_i(1)
+        self.tb.connect(rnd_src, head, snk)
+        # set up fg
+        self.tb.run()
+        # check data
+        res = snk.data()
+        # plt.hist(res)
+        # plt.show()
+
+        self.assertGreaterEqual(np.min(res), minimum)
+        self.assertLess(np.max(res), maximum)
+
+
+if __name__ == '__main__':
+    gr_unittest.run(qa_random_uniform_source, "qa_random_uniform_source.xml")
diff --git a/gr-analog/swig/analog_swig.i b/gr-analog/swig/analog_swig.i
index 84ac148..7f064f5 100644
--- a/gr-analog/swig/analog_swig.i
+++ b/gr-analog/swig/analog_swig.i
@@ -74,6 +74,9 @@
 #include "gnuradio/analog/simple_squelch_cc.h"
 #include "gnuradio/analog/squelch_base_cc.h"
 #include "gnuradio/analog/squelch_base_ff.h"
+#include "gnuradio/analog/random_uniform_source_b.h"
+#include "gnuradio/analog/random_uniform_source_s.h"
+#include "gnuradio/analog/random_uniform_source_i.h"
 %}
 
 %include "gnuradio/analog/cpm.h"
@@ -116,6 +119,9 @@
 %include "gnuradio/analog/simple_squelch_cc.h"
 %include "gnuradio/analog/squelch_base_cc.h"
 %include "gnuradio/analog/squelch_base_ff.h"
+%include "gnuradio/analog/random_uniform_source_b.h"
+%include "gnuradio/analog/random_uniform_source_s.h"
+%include "gnuradio/analog/random_uniform_source_i.h"
 
 GR_SWIG_BLOCK_MAGIC2(analog, agc_cc);
 GR_SWIG_BLOCK_MAGIC2(analog, agc_ff);
@@ -152,3 +158,6 @@ GR_SWIG_BLOCK_MAGIC2(analog, sig_source_i);
 GR_SWIG_BLOCK_MAGIC2(analog, sig_source_f);
 GR_SWIG_BLOCK_MAGIC2(analog, sig_source_c);
 GR_SWIG_BLOCK_MAGIC2(analog, simple_squelch_cc);
+GR_SWIG_BLOCK_MAGIC2(analog, random_uniform_source_b);
+GR_SWIG_BLOCK_MAGIC2(analog, random_uniform_source_s);
+GR_SWIG_BLOCK_MAGIC2(analog, random_uniform_source_i);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]