diff options
author | Tim O'Shea <tim.oshea753@gmail.com> | 2014-02-15 22:21:44 -0500 |
---|---|---|
committer | Tom Rondeau <tom@trondeau.com> | 2014-02-16 14:35:12 -0500 |
commit | 246ec97757c69d5296ebf56a273bf8e0e782a0b4 (patch) | |
tree | 878f08052223c54338a00f4526ca4bab3795a194 | |
parent | abdc4fcb6cade9442aceb36e5ec7e0c93210b9f1 (diff) |
blocks: random_pdu (speedup, also allow a byte mask), message_strobe_random (new block, send messages with a known timing distribution)
-rw-r--r-- | gr-blocks/grc/blocks_message_strobe_random.xml | 69 | ||||
-rw-r--r-- | gr-blocks/grc/blocks_random_pdu.xml | 8 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/message_strobe_random.h | 113 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/random_pdu.h | 2 | ||||
-rw-r--r-- | gr-blocks/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-blocks/lib/message_strobe_random_impl.cc | 121 | ||||
-rw-r--r-- | gr-blocks/lib/message_strobe_random_impl.h | 73 | ||||
-rw-r--r-- | gr-blocks/lib/random_pdu_impl.cc | 13 | ||||
-rw-r--r-- | gr-blocks/lib/random_pdu_impl.h | 5 | ||||
-rw-r--r-- | gr-blocks/swig/blocks_swig0.i | 3 |
11 files changed, 399 insertions, 10 deletions
diff --git a/gr-blocks/grc/blocks_message_strobe_random.xml b/gr-blocks/grc/blocks_message_strobe_random.xml new file mode 100644 index 0000000000..71c51322c5 --- /dev/null +++ b/gr-blocks/grc/blocks_message_strobe_random.xml @@ -0,0 +1,69 @@ +<?xml version="1.0"?> +<!-- +################################################### +##Message Strobe +################################################### + --> +<block> + <name>Message Strobe Random-Delay</name> + <key>blocks_message_strobe_random</key> + <import>from gnuradio import blocks</import> + <import>import pmt</import> + <make>blocks.message_strobe_random($msg, $dist, $mean, $std)</make> + <callback>set_msg($msg)</callback> + <callback>set_dist($period)</callback> + <callback>set_mean($mean)</callback> + <callback>set_std($std)</callback> + <param> + <name>Message PMT</name> + <key>msg</key> + <value>pmt.intern("TEST")</value> + <type>raw</type> + </param> + <param> + <name>Distribution</name> + <key>dist</key> + <type>enum</type> + <option> + <name>Poisson</name> + <key>blocks.STROBE_POISSON</key> + </option> + <option> + <name>Uniform</name> + <key>blocks.STROBE_UNIFORM</key> + </option> + <option> + <name>Gaussian</name> + <key>blocks.STROBE_GAUSSIAN</key> + </option> + </param> + <param> + <name>Mean (ms)</name> + <key>mean</key> + <value>1000</value> + <type>real</type> + </param> + <param> + <name>Std (ms)</name> + <key>std</key> + <value>100</value> + <type>real</type> + </param> + <sink> + <name>set_msg</name> + <type>message</type> + <optional>1</optional> + </sink> + <source> + <name>strobe</name> + <type>message</type> + <optional>1</optional> + </source> + + <doc> + Please note some peculiarities below: + - poisson does not care about your std + - gaussian operates as expected + - uniform is actually of the range (mean-std, mean+std) - thus we are lieing and it is not actually an std here + </doc> +</block> diff --git a/gr-blocks/grc/blocks_random_pdu.xml b/gr-blocks/grc/blocks_random_pdu.xml index 83e3e65fd6..bd484d05d2 100644 --- a/gr-blocks/grc/blocks_random_pdu.xml +++ b/gr-blocks/grc/blocks_random_pdu.xml @@ -9,7 +9,7 @@ <key>blocks_random_pdu</key> <import>from gnuradio import blocks</import> <import>import pmt</import> - <make>blocks.random_pdu($minsize, $maxsize)</make> + <make>blocks.random_pdu($minsize, $maxsize, chr($mask))</make> <param> <name>Min Bytes</name> <key>minsize</key> @@ -22,6 +22,12 @@ <value>2000</value> <type>int</type> </param> + <param> + <name>Byte Mask</name> + <key>mask</key> + <value>0xFF</value> + <type>int</type> + </param> <sink> <name>generate</name> <type>message</type> diff --git a/gr-blocks/include/gnuradio/blocks/CMakeLists.txt b/gr-blocks/include/gnuradio/blocks/CMakeLists.txt index 6d39d98c54..c120242c01 100644 --- a/gr-blocks/include/gnuradio/blocks/CMakeLists.txt +++ b/gr-blocks/include/gnuradio/blocks/CMakeLists.txt @@ -154,6 +154,7 @@ install(FILES message_sink.h message_source.h message_strobe.h + message_strobe_random.h message_burst_source.h multiply_cc.h multiply_ff.h diff --git a/gr-blocks/include/gnuradio/blocks/message_strobe_random.h b/gr-blocks/include/gnuradio/blocks/message_strobe_random.h new file mode 100644 index 0000000000..f399f637d1 --- /dev/null +++ b/gr-blocks/include/gnuradio/blocks/message_strobe_random.h @@ -0,0 +1,113 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012-2013 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. + */ + +#ifndef INCLUDED_GR_MESSAGE_STROBE_RANDOM_H +#define INCLUDED_GR_MESSAGE_STROBE_RANDOM_H + +#include <gnuradio/blocks/api.h> +#include <gnuradio/block.h> + +namespace gr { + namespace blocks { + + /* + * strobing models + */ + typedef enum { + STROBE_POISSON=1, + STROBE_GAUSSIAN=2, + STROBE_UNIFORM=3 + } message_strobe_random_distribution_t; + + /*! + * \brief Send message at defined interval + * \ingroup message_tools_blk + * + * \details + * Takes a PMT message and sends it out every \p period_ms + * milliseconds. Useful for testing/debugging the message system. + */ + class BLOCKS_API message_strobe_random : virtual public block + { + public: + // gr::blocks::message_strobe_random::sptr + typedef boost::shared_ptr<message_strobe_random> sptr; + + /*! + * Make a message stobe block to send message \p msg every \p + * period_ms milliseconds. + * + * \param msg The message to send as a PMT. + * \param period_ms the time period in milliseconds in which to + * send \p msg. + */ + static sptr make(pmt::pmt_t msg, message_strobe_random_distribution_t dist, float mean_ms, float std_ms); + + /*! + * Reset the message being sent. + * \param msg The message to send as a PMT. + */ + virtual void set_msg(pmt::pmt_t msg) = 0; + + /*! + * Get the value of the message being sent. + */ + virtual pmt::pmt_t msg() const = 0; + + /*! + * \param dist new distribution. + */ + virtual void set_dist(message_strobe_random_distribution_t dist) = 0; + + /*! + * get the current distribution. + */ + virtual message_strobe_random_distribution_t dist() const = 0; + + /*! + * Reset the sending interval. + * \param mean delay in milliseconds. + */ + virtual void set_mean(float mean) = 0; + + /*! + * Get the time interval of the strobe_random. + */ + virtual float mean() const = 0; + + /*! + * Reset the sending interval. + * \param std delay in milliseconds. + */ + virtual void set_std(float std) = 0; + + /*! + * Get the std of strobe_random. + */ + virtual float std() const = 0; + + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_MESSAGE_STROBE_H */ diff --git a/gr-blocks/include/gnuradio/blocks/random_pdu.h b/gr-blocks/include/gnuradio/blocks/random_pdu.h index e9d0b8afd6..fe6bca7615 100644 --- a/gr-blocks/include/gnuradio/blocks/random_pdu.h +++ b/gr-blocks/include/gnuradio/blocks/random_pdu.h @@ -43,7 +43,7 @@ namespace gr { /*! * \brief Construct a random PDU generator */ - static sptr make(int mintime, int maxtime); + static sptr make(int mintime, int maxtime, char byte_mask = 0xFF); }; } /* namespace blocks */ diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt index 6ca6c1744b..fba0e4b771 100644 --- a/gr-blocks/lib/CMakeLists.txt +++ b/gr-blocks/lib/CMakeLists.txt @@ -194,6 +194,7 @@ list(APPEND gr_blocks_sources message_sink_impl.cc message_source_impl.cc message_strobe_impl.cc + message_strobe_random_impl.cc message_burst_source_impl.cc multiply_cc_impl.cc multiply_ff_impl.cc diff --git a/gr-blocks/lib/message_strobe_random_impl.cc b/gr-blocks/lib/message_strobe_random_impl.cc new file mode 100644 index 0000000000..3d15574ba0 --- /dev/null +++ b/gr-blocks/lib/message_strobe_random_impl.cc @@ -0,0 +1,121 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012-2013 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "message_strobe_random_impl.h" +#include <gnuradio/io_signature.h> +#include <cstdio> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdexcept> +#include <string.h> +#include <iostream> + +namespace gr { + namespace blocks { + + message_strobe_random::sptr + message_strobe_random::make(pmt::pmt_t msg, message_strobe_random_distribution_t dist, float mean_ms, float std_ms) + { + return gnuradio::get_initial_sptr + (new message_strobe_random_impl(msg, dist, mean_ms, std_ms)); + } + + message_strobe_random_impl::message_strobe_random_impl(pmt::pmt_t msg, message_strobe_random_distribution_t dist, float mean_ms, float std_ms) + : block("message_strobe_random", + io_signature::make(0, 0, 0), + io_signature::make(0, 0, 0)), + d_finished(false), + d_mean_ms(mean_ms), + d_std_ms(std_ms), + d_dist(dist), + d_msg(msg), + d_rng() + { + // allocate RNGs + update_dist(); + + // set up ports + message_port_register_out(pmt::mp("strobe")); + d_thread = boost::shared_ptr<boost::thread> + (new boost::thread(boost::bind(&message_strobe_random_impl::run, this))); + + message_port_register_in(pmt::mp("set_msg")); + set_msg_handler(pmt::mp("set_msg"), + boost::bind(&message_strobe_random_impl::set_msg, this, _1)); + } + + float message_strobe_random_impl::next_delay(){ + switch(d_dist){ + case STROBE_POISSON: + //return d_variate_poisson->operator()(); + return d_variate_poisson->operator()(); + case STROBE_GAUSSIAN: + return d_variate_normal->operator()(); + case STROBE_UNIFORM: + return d_variate_uniform->operator()(); + default: + throw std::runtime_error("message_strobe_random_impl::d_distribution is very unhappy with you"); + } + } + + void message_strobe_random_impl::update_dist(){ + boost::poisson_distribution<> pd(d_mean_ms); + d_variate_poisson = boost::shared_ptr< boost::variate_generator<boost::mt19937, boost::poisson_distribution<> > > ( + new boost::variate_generator <boost::mt19937, boost::poisson_distribution<> >(d_rng,pd) ); + + boost::normal_distribution<> nd(d_mean_ms, d_std_ms); + d_variate_normal = boost::shared_ptr< boost::variate_generator<boost::mt19937, boost::normal_distribution<> > > ( + new boost::variate_generator <boost::mt19937, boost::normal_distribution<> >(d_rng,nd) ); + + boost::uniform_real<> ud(d_mean_ms-d_std_ms, d_mean_ms+d_std_ms); + d_variate_uniform = boost::shared_ptr< boost::variate_generator<boost::mt19937, boost::uniform_real<> > > ( + new boost::variate_generator <boost::mt19937, boost::uniform_real<> >(d_rng,ud) ); + } + + + message_strobe_random_impl::~message_strobe_random_impl() + { + d_finished = true; + d_thread->interrupt(); + d_thread->join(); + } + + void message_strobe_random_impl::run() + { + while(!d_finished) { + boost::this_thread::sleep(boost::posix_time::milliseconds(std::max(0.0f,next_delay()))); + if(d_finished) { + return; + } + + message_port_pub(pmt::mp("strobe"), d_msg); + } + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/message_strobe_random_impl.h b/gr-blocks/lib/message_strobe_random_impl.h new file mode 100644 index 0000000000..a91c6604a4 --- /dev/null +++ b/gr-blocks/lib/message_strobe_random_impl.h @@ -0,0 +1,73 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012-2013 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. + */ + +#ifndef INCLUDED_GR_MESSAGE_STROBE_IMPL_H +#define INCLUDED_GR_MESSAGE_STROBE_IMPL_H + +#include <gnuradio/blocks/message_strobe_random.h> +#include <boost/random/variate_generator.hpp> +#include <boost/random/mersenne_twister.hpp> +#include <boost/random/poisson_distribution.hpp> +#include <boost/random/normal_distribution.hpp> +#include <boost/random/uniform_real.hpp> + +namespace gr { + namespace blocks { + + class BLOCKS_API message_strobe_random_impl : public message_strobe_random + { + private: + boost::shared_ptr<boost::thread> d_thread; + bool d_finished; + float d_mean_ms; + float d_std_ms; + message_strobe_random_distribution_t d_dist; + pmt::pmt_t d_msg; + void run(); + float next_delay(); + + boost::random::mt19937 d_rng; + boost::shared_ptr< boost::variate_generator <boost::mt19937, boost::poisson_distribution<> > > d_variate_poisson; + boost::shared_ptr< boost::variate_generator <boost::mt19937, boost::normal_distribution<> > > d_variate_normal; + boost::shared_ptr< boost::variate_generator <boost::mt19937, boost::uniform_real<> > > d_variate_uniform; + + void update_dist(); + + public: + message_strobe_random_impl(pmt::pmt_t msg, message_strobe_random_distribution_t dist, float mean_ms, float std_ms); + ~message_strobe_random_impl(); + + void set_msg(pmt::pmt_t msg) { d_msg = msg; } + pmt::pmt_t msg() const { return d_msg; } + void set_mean(float mean_ms) { d_mean_ms = mean_ms; update_dist(); } + float mean() const { return d_mean_ms; } + void set_std(float std_ms) { d_std_ms = std_ms; update_dist(); } + float std() const { return d_std_ms; } + void set_dist(message_strobe_random_distribution_t dist){ d_dist = dist; } + message_strobe_random_distribution_t dist() const { return d_dist; } + + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_MESSAGE_STROBE_IMPL_H */ diff --git a/gr-blocks/lib/random_pdu_impl.cc b/gr-blocks/lib/random_pdu_impl.cc index 6941beae85..0ae800a167 100644 --- a/gr-blocks/lib/random_pdu_impl.cc +++ b/gr-blocks/lib/random_pdu_impl.cc @@ -32,19 +32,20 @@ namespace gr { namespace blocks { random_pdu::sptr - random_pdu::make(int min_items, int max_items) + random_pdu::make(int min_items, int max_items, char byte_mask) { - return gnuradio::get_initial_sptr(new random_pdu_impl(min_items, max_items)); + return gnuradio::get_initial_sptr(new random_pdu_impl(min_items, max_items, byte_mask)); } - random_pdu_impl::random_pdu_impl(int min_items, int max_items) + random_pdu_impl::random_pdu_impl(int min_items, int max_items, char byte_mask) : block("random_pdu", io_signature::make (0, 0, 0), io_signature::make (0, 0, 0)), d_urange(min_items, max_items), d_brange(0, 255), d_rvar(d_rng, d_urange), - d_bvar(d_rng, d_brange) + d_bvar(d_rng, d_brange), + d_mask(byte_mask) { message_port_register_out(PDU_PORT_ID); message_port_register_in(pmt::mp("generate")); @@ -65,9 +66,9 @@ namespace gr { int len = d_rvar(); // fill it with random bytes - std::vector<unsigned char> vec; + std::vector<unsigned char> vec(len); for (int i=0; i<len; i++) - vec.push_back((unsigned char) d_bvar()); + vec[i] = ((unsigned char) d_bvar()) & d_mask; // send the vector pmt::pmt_t vecpmt(pmt::make_blob(&vec[0], len)); diff --git a/gr-blocks/lib/random_pdu_impl.h b/gr-blocks/lib/random_pdu_impl.h index 7d8aa23fbf..9cb34c775d 100644 --- a/gr-blocks/lib/random_pdu_impl.h +++ b/gr-blocks/lib/random_pdu_impl.h @@ -38,9 +38,10 @@ namespace gr { boost::uniform_int<> d_brange; boost::variate_generator< boost::mt19937, boost::uniform_int<> > d_rvar; // pdu length boost::variate_generator< boost::mt19937, boost::uniform_int<> > d_bvar; // pdu contents - + char d_mask; + public: - random_pdu_impl(int min_items, int max_items); + random_pdu_impl(int min_items, int max_items, char byte_mask); bool start(); void output_random(); diff --git a/gr-blocks/swig/blocks_swig0.i b/gr-blocks/swig/blocks_swig0.i index 28e14fea35..37b8ef6b0b 100644 --- a/gr-blocks/swig/blocks_swig0.i +++ b/gr-blocks/swig/blocks_swig0.i @@ -46,6 +46,7 @@ #include "gnuradio/blocks/message_sink.h" #include "gnuradio/blocks/message_source.h" #include "gnuradio/blocks/message_strobe.h" +#include "gnuradio/blocks/message_strobe_random.h" #include "gnuradio/blocks/message_burst_source.h" #include "gnuradio/blocks/nop.h" #include "gnuradio/blocks/null_sink.h" @@ -71,6 +72,7 @@ %include "gnuradio/blocks/message_sink.h" %include "gnuradio/blocks/message_source.h" %include "gnuradio/blocks/message_strobe.h" +%include "gnuradio/blocks/message_strobe_random.h" %include "gnuradio/blocks/message_burst_source.h" %include "gnuradio/blocks/nop.h" %include "gnuradio/blocks/null_sink.h" @@ -93,6 +95,7 @@ GR_SWIG_BLOCK_MAGIC2(blocks, message_debug); GR_SWIG_BLOCK_MAGIC2(blocks, message_sink); GR_SWIG_BLOCK_MAGIC2(blocks, message_source); GR_SWIG_BLOCK_MAGIC2(blocks, message_strobe); +GR_SWIG_BLOCK_MAGIC2(blocks, message_strobe_random); GR_SWIG_BLOCK_MAGIC2(blocks, message_burst_source); GR_SWIG_BLOCK_MAGIC2(blocks, nop); GR_SWIG_BLOCK_MAGIC2(blocks, null_sink); |