summaryrefslogtreecommitdiff
path: root/gr-blocks/lib
diff options
context:
space:
mode:
authorTom Rondeau <trondeau@vt.edu>2013-02-24 18:55:04 -0500
committerTom Rondeau <trondeau@vt.edu>2013-02-24 18:55:04 -0500
commita54dc5bab6cc38513e9732be9f5c2670145b160f (patch)
tree50c711e17105a00f16486e51fd41d1e3b7786d8d /gr-blocks/lib
parentffcaa3436cc8e608cb51bad78ca669b90bd110f2 (diff)
blocks: adding threshold, strech, and throttle to gr-blocks.
Diffstat (limited to 'gr-blocks/lib')
-rw-r--r--gr-blocks/lib/CMakeLists.txt3
-rw-r--r--gr-blocks/lib/stretch_ff_impl.cc84
-rw-r--r--gr-blocks/lib/stretch_ff_impl.h53
-rw-r--r--gr-blocks/lib/threshold_ff_impl.cc78
-rw-r--r--gr-blocks/lib/threshold_ff_impl.h57
-rw-r--r--gr-blocks/lib/throttle_impl.cc97
-rw-r--r--gr-blocks/lib/throttle_impl.h54
7 files changed, 426 insertions, 0 deletions
diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt
index aa87d9c64a..e1b9b4f008 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 0000000000..90bbc7ee5a
--- /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 0000000000..af69d835a5
--- /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 0000000000..477f2b1c89
--- /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 0000000000..41afaa52aa
--- /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 0000000000..49743e3f26
--- /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 0000000000..86dbef2ac9
--- /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 */