diff options
-rw-r--r-- | gr-blocks/grc/blocks_block_tree.xml | 8 | ||||
-rw-r--r-- | gr-blocks/grc/blocks_regenerate_bb.xml | 2 | ||||
-rw-r--r-- | gr-blocks/grc/blocks_stretch_ff.xml | 34 | ||||
-rw-r--r-- | gr-blocks/grc/blocks_threshold_ff.xml | 40 | ||||
-rw-r--r-- | gr-blocks/grc/blocks_throttle.xml | 67 | ||||
-rw-r--r-- | gr-blocks/include/blocks/CMakeLists.txt | 3 | ||||
-rw-r--r-- | gr-blocks/include/blocks/stretch_ff.h | 59 | ||||
-rw-r--r-- | gr-blocks/include/blocks/threshold_ff.h | 66 | ||||
-rw-r--r-- | gr-blocks/include/blocks/throttle.h | 62 | ||||
-rw-r--r-- | gr-blocks/lib/CMakeLists.txt | 3 | ||||
-rw-r--r-- | gr-blocks/lib/stretch_ff_impl.cc | 84 | ||||
-rw-r--r-- | gr-blocks/lib/stretch_ff_impl.h | 53 | ||||
-rw-r--r-- | gr-blocks/lib/threshold_ff_impl.cc | 78 | ||||
-rw-r--r-- | gr-blocks/lib/threshold_ff_impl.h | 57 | ||||
-rw-r--r-- | gr-blocks/lib/throttle_impl.cc | 97 | ||||
-rw-r--r-- | gr-blocks/lib/throttle_impl.h | 54 | ||||
-rwxr-xr-x | gr-blocks/python/qa_stretch.py | 67 | ||||
-rw-r--r-- | gr-blocks/python/qa_threshold.py | 54 | ||||
-rwxr-xr-x | gr-blocks/python/qa_throttle.py | 39 | ||||
-rw-r--r-- | gr-blocks/swig/blocks_swig.i | 9 |
20 files changed, 934 insertions, 2 deletions
diff --git a/gr-blocks/grc/blocks_block_tree.xml b/gr-blocks/grc/blocks_block_tree.xml index 6b17078cd..6ab84c94e 100644 --- a/gr-blocks/grc/blocks_block_tree.xml +++ b/gr-blocks/grc/blocks_block_tree.xml @@ -98,5 +98,13 @@ <block>blocks_streams_to_vector</block> <block>blocks_vector_to_stream</block> <block>blocks_vector_to_streams</block> + <block>blocks_peak_detector2_fb</block> + <block>blocks_regenerate_bb</block> + <block>blocks_stretch_ff</block> + <block>blocks_threadhold_ff</block> + </cat> + <cat> + <name>Misc (New) </name> + <block>blocks_throttle</block> </cat> </cat> diff --git a/gr-blocks/grc/blocks_regenerate_bb.xml b/gr-blocks/grc/blocks_regenerate_bb.xml index 6e6f46188..bbee53387 100644 --- a/gr-blocks/grc/blocks_regenerate_bb.xml +++ b/gr-blocks/grc/blocks_regenerate_bb.xml @@ -24,11 +24,9 @@ <sink> <name>in</name> <type>byte</type> - <vlen>$vlen</vlen> </sink> <source> <name>out</name> <type>byte</type> - <vlen>$vlen</vlen> </source> </block> diff --git a/gr-blocks/grc/blocks_stretch_ff.xml b/gr-blocks/grc/blocks_stretch_ff.xml new file mode 100644 index 000000000..00a67602b --- /dev/null +++ b/gr-blocks/grc/blocks_stretch_ff.xml @@ -0,0 +1,34 @@ +<?xml version="1.0"?> +<!-- +################################################### +##Stretch Block: +## float in / float out +################################################### + --> +<block> + <name>Stretch</name> + <key>blocks_stretch_ff</key> + <import>from gnuradio import blocks</import> + <make>blocks.stretch_ff($lo, $vlen)</make> + <param> + <name>Low</name> + <key>lo</key> + <type>real</type> + </param> + <param> + <name>Vec. Length</name> + <key>vlen</key> + <value>1</value> + <type>int</type> + </param> + <sink> + <name>in</name> + <type>float</type> + <vlen>$vlen</vlen> + </sink> + <source> + <name>out</name> + <type>float</type> + <vlen>$vlen</vlen> + </source> +</block> diff --git a/gr-blocks/grc/blocks_threshold_ff.xml b/gr-blocks/grc/blocks_threshold_ff.xml new file mode 100644 index 000000000..0da4157fc --- /dev/null +++ b/gr-blocks/grc/blocks_threshold_ff.xml @@ -0,0 +1,40 @@ +<?xml version="1.0"?> +<!-- +################################################### +##Threshold +################################################### + --> +<block> + <name>Threshold</name> + <key>blocks_threshold_ff</key> + <import>from gnuradio import blocks</import> + <make>blocks.threshold_ff($low, $high, $init)</make> + <callback>set_hi($high)</callback> + <callback>set_lo($low)</callback> + <param> + <name>Low</name> + <key>low</key> + <value>-100</value> + <type>real</type> + </param> + <param> + <name>High</name> + <key>high</key> + <value>100</value> + <type>real</type> + </param> + <param> + <name>Initial State</name> + <key>init</key> + <value>0</value> + <type>real</type> + </param> + <sink> + <name>in</name> + <type>float</type> + </sink> + <source> + <name>out</name> + <type>float</type> + </source> +</block> diff --git a/gr-blocks/grc/blocks_throttle.xml b/gr-blocks/grc/blocks_throttle.xml new file mode 100644 index 000000000..8293cbde9 --- /dev/null +++ b/gr-blocks/grc/blocks_throttle.xml @@ -0,0 +1,67 @@ +<?xml version="1.0"?> +<!-- +################################################### +##Throttle +################################################### + --> +<block> + <name>Throttle</name> + <key>blocks_throttle</key> + <throttle>1</throttle> + <import>from gnuradio import blocks</import> + <make>blocks.throttle($type.size*$vlen, $samples_per_second)</make> + <callback>set_sample_rate($samples_per_second)</callback> + <param> + <name>Type</name> + <key>type</key> + <type>enum</type> + <option> + <name>Complex</name> + <key>complex</key> + <opt>size:gr.sizeof_gr_complex</opt> + </option> + <option> + <name>Float</name> + <key>float</key> + <opt>size:gr.sizeof_float</opt> + </option> + <option> + <name>Int</name> + <key>int</key> + <opt>size:gr.sizeof_int</opt> + </option> + <option> + <name>Short</name> + <key>short</key> + <opt>size:gr.sizeof_short</opt> + </option> + <option> + <name>Byte</name> + <key>byte</key> + <opt>size:gr.sizeof_char</opt> + </option> + </param> + <param> + <name>Sample Rate</name> + <key>samples_per_second</key> + <value>samp_rate</value> + <type>real</type> + </param> + <param> + <name>Vec Length</name> + <key>vlen</key> + <value>1</value> + <type>int</type> + </param> + <check>$vlen > 0</check> + <sink> + <name>in</name> + <type>$type</type> + <vlen>$vlen</vlen> + </sink> + <source> + <name>out</name> + <type>$type</type> + <vlen>$vlen</vlen> + </source> +</block> diff --git a/gr-blocks/include/blocks/CMakeLists.txt b/gr-blocks/include/blocks/CMakeLists.txt index cd2fea672..8d5fe9b13 100644 --- a/gr-blocks/include/blocks/CMakeLists.txt +++ b/gr-blocks/include/blocks/CMakeLists.txt @@ -138,6 +138,9 @@ install(FILES stream_to_vector.h streams_to_stream.h streams_to_vector.h + stretch_ff.h + threshold_ff.h + throttle.h uchar_to_float.h vector_to_stream.h vector_to_streams.h diff --git a/gr-blocks/include/blocks/stretch_ff.h b/gr-blocks/include/blocks/stretch_ff.h new file mode 100644 index 000000000..5f98452a4 --- /dev/null +++ b/gr-blocks/include/blocks/stretch_ff.h @@ -0,0 +1,59 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,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_STRETCH_FF_H +#define INCLUDED_GR_STRETCH_FF_H + +#include <blocks/api.h> +#include <gr_sync_block.h> + +namespace gr { + namespace blocks { + + /*! + * \brief adjust y-range of an input vector by mapping to range + * (max-of-input, stipulated-min). Primarily for spectral + * signature matching by normalizing spectrum dynamic ranges. + * \ingroup misc_blk + */ + class BLOCKS_API stretch_ff : virtual public gr_sync_block + { + public: + // gr::blocks::stretch_ff::sptr + typedef boost::shared_ptr<stretch_ff> sptr; + + /*! + * \brief Make a stretch block. + * \param lo Set low value for range. + * \param vlen vector length of input stream. + */ + static sptr make(float lo, size_t vlen=1); + + virtual float lo() const = 0; + virtual void set_lo(float lo) = 0; + virtual size_t vlen() const = 0; + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_STRETCH_FF_H */ diff --git a/gr-blocks/include/blocks/threshold_ff.h b/gr-blocks/include/blocks/threshold_ff.h new file mode 100644 index 000000000..900e5c5bd --- /dev/null +++ b/gr-blocks/include/blocks/threshold_ff.h @@ -0,0 +1,66 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,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_THRESHOLD_FF_H +#define INCLUDED_GR_THRESHOLD_FF_H + +#include <blocks/api.h> +#include <gr_sync_block.h> + +namespace gr { + namespace blocks { + + /*! + * \brief Output a 1 or zero based on a threshold value. + * \ingroup misc_blk + * + * Test the incoming signal against a threshold. If the signal + * excedes the \p hi value, it will output a 1 until the signal + * falls below the \p lo value. + */ + class BLOCKS_API threshold_ff : virtual public gr_sync_block + { + public: + // gr::blocks::threshold_ff::sptr + typedef boost::shared_ptr<threshold_ff> sptr; + + /* \brief Create a threadshold block. + * \param lo Threshold input signal needs to drop below to + * change state to 0. + * \param hi Threshold input signal needs to rise above to + * change state to 1. + * \param initial_state Initial state of the block (0 or 1). + */ + static sptr make(float lo, float hi, float initial_state=0); + + virtual float lo () const = 0; + virtual void set_lo (float lo) = 0; + virtual float hi () const = 0; + virtual void set_hi (float hi) = 0; + virtual float last_state () const = 0; + virtual void set_last_state (float last_state) = 0; + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_THRESHOLD_FF_H */ diff --git a/gr-blocks/include/blocks/throttle.h b/gr-blocks/include/blocks/throttle.h new file mode 100644 index 000000000..20d8037e1 --- /dev/null +++ b/gr-blocks/include/blocks/throttle.h @@ -0,0 +1,62 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011,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_THROTTLE_H +#define INCLUDED_GR_THROTTLE_H + +#include <blocks/api.h> +#include <gr_sync_block.h> + +namespace gr { + namespace blocks { + + /*! + * \brief throttle flow of samples such that the average rate does + * not exceed samples_per_sec. + * \ingroup misc_blk + * + * input: one stream of itemsize; output: one stream of itemsize + * + * N.B. this should only be used in GUI apps where there is no + * other rate limiting block. It is not intended nor effective at + * precisely controlling the rate of samples. That should be + * controlled by a source or sink tied to sample clock. E.g., a + * USRP or audio card. + */ + class BLOCKS_API throttle : virtual public gr_sync_block + { + public: + typedef boost::shared_ptr<throttle> sptr; + + static sptr make(size_t itemsize, double samples_per_sec); + + //! Sets the sample rate in samples per second. + virtual void set_sample_rate(double rate) = 0; + + //! Get the sample rate in samples per second. + virtual double sample_rate() const = 0; + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_THROTTLE_H */ diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt index aa87d9c64..e1b9b4f00 100644 --- a/gr-blocks/lib/CMakeLists.txt +++ b/gr-blocks/lib/CMakeLists.txt @@ -177,6 +177,9 @@ list(APPEND gr_blocks_sources stream_to_vector_impl.cc streams_to_stream_impl.cc streams_to_vector_impl.cc + stretch_ff_impl.cc + threshold_ff_impl.cc + throttle_impl.cc uchar_array_to_float.cc uchar_to_float_impl.cc vector_to_stream_impl.cc diff --git a/gr-blocks/lib/stretch_ff_impl.cc b/gr-blocks/lib/stretch_ff_impl.cc new file mode 100644 index 000000000..90bbc7ee5 --- /dev/null +++ b/gr-blocks/lib/stretch_ff_impl.cc @@ -0,0 +1,84 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2010,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 "stretch_ff_impl.h" +#include <gr_io_signature.h> + +namespace gr { + namespace blocks { + + stretch_ff::sptr + stretch_ff::make(float lo, size_t vlen) + { + return gnuradio::get_initial_sptr + (new stretch_ff_impl(lo, vlen)); + } + + stretch_ff_impl::stretch_ff_impl(float lo, size_t vlen) + : gr_sync_block("stretch_ff", + gr_make_io_signature(1, 1, vlen * sizeof(float)), + gr_make_io_signature(1, 1, vlen * sizeof(float))), + d_lo(lo), d_vlen(vlen) + { + } + + stretch_ff_impl::~stretch_ff_impl() + { + } + + int + stretch_ff_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const float *in = (const float *)input_items[0]; + float *out = (float *)output_items[0]; + + for(int count = 0; count < noutput_items; count++) { + float vmax = in[0] - d_lo; + + for(unsigned int i = 1; i < d_vlen; i++) { + float vtmp = in[i] - d_lo; + if(vtmp > vmax) + vmax = vtmp; + } + + if(vmax != 0.0) + for(unsigned int i = 0; i < d_vlen; i++) + out[i] = d_lo * (1.0 - (in[i] - d_lo) / vmax); + else + for(unsigned int i = 0; i < d_vlen; i++) + out[i] = in[i]; + + in += d_vlen; + out += d_vlen; + } + + return noutput_items; + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/stretch_ff_impl.h b/gr-blocks/lib/stretch_ff_impl.h new file mode 100644 index 000000000..af69d835a --- /dev/null +++ b/gr-blocks/lib/stretch_ff_impl.h @@ -0,0 +1,53 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,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_STRETCH_FF_IMPL_H +#define INCLUDED_GR_STRETCH_FF_IMPL_H + +#include <blocks/stretch_ff.h> + +namespace gr { + namespace blocks { + + class stretch_ff_impl : public stretch_ff + { + private: + float d_lo; // the constant + size_t d_vlen; + + public: + stretch_ff_impl(float lo, size_t vlen); + ~stretch_ff_impl(); + + float lo() const { return d_lo; } + void set_lo(float lo) { d_lo = lo; } + size_t vlen() const { return d_vlen; } + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_STRETCH_FF_IMPL_H */ diff --git a/gr-blocks/lib/threshold_ff_impl.cc b/gr-blocks/lib/threshold_ff_impl.cc new file mode 100644 index 000000000..477f2b1c8 --- /dev/null +++ b/gr-blocks/lib/threshold_ff_impl.cc @@ -0,0 +1,78 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2010,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 "threshold_ff_impl.h" +#include <gr_io_signature.h> + +namespace gr { + namespace blocks { + + threshold_ff::sptr + threshold_ff::make(float lo, float hi, float initial_state) + { + return gnuradio::get_initial_sptr + (new threshold_ff_impl(lo, hi, initial_state)); + } + + threshold_ff_impl::threshold_ff_impl(float lo, float hi, + float initial_state) + : gr_sync_block("threshold_ff", + gr_make_io_signature(1, 1, sizeof(float)), + gr_make_io_signature(1, 1, sizeof(float))), + d_lo(lo), d_hi(hi), d_last_state(initial_state) + { + } + + threshold_ff_impl::~threshold_ff_impl() + { + } + + int + threshold_ff_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const float *in = (const float *)input_items[0]; + float *out = (float *)output_items[0]; + + for(int i = 0; i < noutput_items; i++) { + if(in[i] > d_hi) { + out[i] = 1.0; + d_last_state = 1.0; + } + else if(in[i] < d_lo) { + out[i] = 0.0; + d_last_state = 0.0; + } + else + out[i] = d_last_state; + } + + return noutput_items; + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/threshold_ff_impl.h b/gr-blocks/lib/threshold_ff_impl.h new file mode 100644 index 000000000..41afaa52a --- /dev/null +++ b/gr-blocks/lib/threshold_ff_impl.h @@ -0,0 +1,57 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,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_THRESHOLD_FF_IMPL_H +#define INCLUDED_GR_THRESHOLD_FF_IMPL_H + +#include <blocks/threshold_ff.h> + +namespace gr { + namespace blocks { + + class threshold_ff_impl : public threshold_ff + { + private: + float d_lo, d_hi; // the constant + float d_last_state; + + public: + threshold_ff_impl(float lo, float hi, + float initial_state=0); + ~threshold_ff_impl(); + + float lo() const { return d_lo; } + void set_lo(float lo) { d_lo = lo; } + float hi() const { return d_hi; } + void set_hi(float hi) { d_hi = hi; } + float last_state() const { return d_last_state; } + void set_last_state(float last_state) { d_last_state = last_state; } + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_THRESHOLD_FF_IMPL_H */ diff --git a/gr-blocks/lib/throttle_impl.cc b/gr-blocks/lib/throttle_impl.cc new file mode 100644 index 000000000..49743e3f2 --- /dev/null +++ b/gr-blocks/lib/throttle_impl.cc @@ -0,0 +1,97 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011 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 "throttle_impl.h" +#include <gr_io_signature.h> +#include <cstring> +#include <boost/thread/thread.hpp> + +namespace gr { + namespace blocks { + + throttle::sptr + throttle::make(size_t itemsize, double samples_per_sec) + { + return gnuradio::get_initial_sptr + (new throttle_impl(itemsize, samples_per_sec)); + } + + throttle_impl::throttle_impl(size_t itemsize, + double samples_per_second) + : gr_sync_block("throttle", + gr_make_io_signature(1, 1, itemsize), + gr_make_io_signature(1, 1, itemsize)), + d_itemsize(itemsize) + { + set_sample_rate(samples_per_second); + } + + throttle_impl::~throttle_impl() + { + } + + void + throttle_impl::set_sample_rate(double rate) + { + //changing the sample rate performs a reset of state params + d_start = boost::get_system_time(); + d_total_samples = 0; + d_samps_per_tick = rate/boost::posix_time::time_duration::ticks_per_second(); + d_samps_per_us = rate/1e6; + } + + double + throttle_impl::sample_rate() const + { + return d_samps_per_us * 1e6; + } + + int + throttle_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + //calculate the expected number of samples to have passed through + boost::system_time now = boost::get_system_time(); + boost::int64_t ticks = (now - d_start).ticks(); + uint64_t expected_samps = uint64_t(d_samps_per_tick*ticks); + + //if the expected samples was less, we need to throttle back + if(d_total_samples > expected_samps) { + boost::this_thread::sleep(boost::posix_time::microseconds + (long((d_total_samples - expected_samps)/d_samps_per_us))); + } + + //copy all samples output[i] <= input[i] + const char *in = (const char *)input_items[0]; + char *out = (char *)output_items[0]; + std::memcpy(out, in, noutput_items * d_itemsize); + d_total_samples += noutput_items; + return noutput_items; + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/throttle_impl.h b/gr-blocks/lib/throttle_impl.h new file mode 100644 index 000000000..86dbef2ac --- /dev/null +++ b/gr-blocks/lib/throttle_impl.h @@ -0,0 +1,54 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011,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_THROTTLE_IMPL_H +#define INCLUDED_GR_THROTTLE_IMPL_H + +#include <blocks/throttle.h> + +namespace gr { + namespace blocks { + + class throttle_impl : public throttle + { + private: + boost::system_time d_start; + size_t d_itemsize; + uint64_t d_total_samples; + double d_samps_per_tick, d_samps_per_us; + + public: + throttle_impl(size_t itemsize, double samples_per_sec); + ~throttle_impl(); + + void set_sample_rate(double rate); + double sample_rate() const; + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_THROTTLE_IMPL_H */ diff --git a/gr-blocks/python/qa_stretch.py b/gr-blocks/python/qa_stretch.py new file mode 100755 index 000000000..013d878a8 --- /dev/null +++ b/gr-blocks/python/qa_stretch.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# +# Copyright 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. +# + +from gnuradio import gr, gr_unittest +import blocks_swig as blocks + +class test_stretch(gr_unittest.TestCase): + + def setUp(self): + self.tb = gr.top_block() + + def tearDown(self): + self.tb = None + + def test_stretch_01(self): + tb = self.tb + + data = 10*[1,] + data0 = map(lambda x: x/20.0, data) + data1 = map(lambda x: x/10.0, data) + + expected_result0 = 10*[0.05,] + expected_result1 = 10*[0.1,] + + src0 = gr.vector_source_f(data0, False) + src1 = gr.vector_source_f(data1, False) + inter = gr.streams_to_vector(gr.sizeof_float, 2) + op = blocks.stretch_ff(0.1, 2) + deinter = gr.vector_to_streams(gr.sizeof_float, 2) + dst0 = gr.vector_sink_f() + dst1 = gr.vector_sink_f() + + tb.connect(src0, (inter,0)) + tb.connect(src1, (inter,1)) + tb.connect(inter, op) + tb.connect(op, deinter) + tb.connect((deinter,0), dst0) + tb.connect((deinter,1), dst1) + tb.run() + + dst0_data = dst0.data() + dst1_data = dst1.data() + + self.assertFloatTuplesAlmostEqual(expected_result0, dst0_data, 4) + self.assertFloatTuplesAlmostEqual(expected_result1, dst1_data, 4) + +if __name__ == '__main__': + gr_unittest.run(test_stretch, "test_stretch.xml") diff --git a/gr-blocks/python/qa_threshold.py b/gr-blocks/python/qa_threshold.py new file mode 100644 index 000000000..f91af739a --- /dev/null +++ b/gr-blocks/python/qa_threshold.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# +# Copyright 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. +# + +from gnuradio import gr, gr_unittest +import blocks_swig as blocks + +class test_threshold(gr_unittest.TestCase): + + def setUp(self): + self.tb = gr.top_block() + + def tearDown(self): + self.tb = None + + def test_01(self): + tb = self.tb + + data = [0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 2, 2, 2] + + expected_result = (0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1) + + src = gr.vector_source_f(data, False) + op = blocks.threshold_ff(1, 1) + dst = gr.vector_sink_f() + + tb.connect(src, op) + tb.connect(op, dst) + tb.run() + + dst_data = dst.data() + + self.assertEqual(expected_result, dst_data) + +if __name__ == '__main__': + gr_unittest.run(test_threshold, "test_threshold.xml") diff --git a/gr-blocks/python/qa_throttle.py b/gr-blocks/python/qa_throttle.py new file mode 100755 index 000000000..5d462f2c9 --- /dev/null +++ b/gr-blocks/python/qa_throttle.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# +# Copyright 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. +# + +from gnuradio import gr, gr_unittest +import blocks_swig as blocks + +class test_throttle(gr_unittest.TestCase): + + def setUp(self): + self.tb = gr.top_block() + + def tearDown(self): + self.tb = None + + def test_01(self): + # Test that we can make the block + op = blocks.throttle(gr.sizeof_gr_complex, 1) + +if __name__ == '__main__': + gr_unittest.run(test_throttle, "test_throttle.xml") diff --git a/gr-blocks/swig/blocks_swig.i b/gr-blocks/swig/blocks_swig.i index 114142cbf..6407421b1 100644 --- a/gr-blocks/swig/blocks_swig.i +++ b/gr-blocks/swig/blocks_swig.i @@ -117,10 +117,13 @@ #include "blocks/stream_to_vector.h" #include "blocks/streams_to_stream.h" #include "blocks/streams_to_vector.h" +#include "blocks/stretch_ff.h" #include "blocks/sub_ff.h" #include "blocks/sub_ss.h" #include "blocks/sub_ii.h" #include "blocks/sub_cc.h" +#include "blocks/threshold_ff.h" +#include "blocks/throttle.h" #include "blocks/uchar_to_float.h" #include "blocks/unpacked_to_packed_bb.h" #include "blocks/unpacked_to_packed_ss.h" @@ -219,10 +222,13 @@ %include "blocks/stream_to_vector.h" %include "blocks/streams_to_stream.h" %include "blocks/streams_to_vector.h" +%include "blocks/stretch_ff.h" %include "blocks/sub_ff.h" %include "blocks/sub_ss.h" %include "blocks/sub_ii.h" %include "blocks/sub_cc.h" +%include "blocks/threshold_ff.h" +%include "blocks/throttle.h" %include "blocks/uchar_to_float.h" %include "blocks/unpacked_to_packed_bb.h" %include "blocks/unpacked_to_packed_ss.h" @@ -320,10 +326,13 @@ GR_SWIG_BLOCK_MAGIC2(blocks, stream_to_streams); GR_SWIG_BLOCK_MAGIC2(blocks, stream_to_vector); GR_SWIG_BLOCK_MAGIC2(blocks, streams_to_stream); GR_SWIG_BLOCK_MAGIC2(blocks, streams_to_vector); +GR_SWIG_BLOCK_MAGIC2(blocks, stretch_ff); GR_SWIG_BLOCK_MAGIC2(blocks, sub_ff); GR_SWIG_BLOCK_MAGIC2(blocks, sub_ss); GR_SWIG_BLOCK_MAGIC2(blocks, sub_ii); GR_SWIG_BLOCK_MAGIC2(blocks, sub_cc); +GR_SWIG_BLOCK_MAGIC2(blocks, threshold_ff); +GR_SWIG_BLOCK_MAGIC2(blocks, throttle); GR_SWIG_BLOCK_MAGIC2(blocks, uchar_to_float); GR_SWIG_BLOCK_MAGIC2(blocks, unpacked_to_packed_bb); GR_SWIG_BLOCK_MAGIC2(blocks, unpacked_to_packed_ss); |