summaryrefslogtreecommitdiff
path: root/gr-blocks/lib
diff options
context:
space:
mode:
Diffstat (limited to 'gr-blocks/lib')
-rw-r--r--gr-blocks/lib/CMakeLists.txt1
-rw-r--r--gr-blocks/lib/message_strobe_random_impl.cc121
-rw-r--r--gr-blocks/lib/message_strobe_random_impl.h73
-rw-r--r--gr-blocks/lib/random_pdu_impl.cc13
-rw-r--r--gr-blocks/lib/random_pdu_impl.h5
5 files changed, 205 insertions, 8 deletions
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();