From bbd91d7a08bfc0c229267e5dc848d1f57d629373 Mon Sep 17 00:00:00 2001
From: Johnathan Corgan
Date: Thu, 19 Jul 2012 06:27:19 -0700
Subject: blocks: added gr::blocks::keep_m_in_n

---
 gr-blocks/grc/blocks_block_tree.xml     |  1 +
 gr-blocks/grc/blocks_keep_m_in_n.xml    | 76 ++++++++++++++++++++++++++++
 gr-blocks/include/blocks/CMakeLists.txt |  1 +
 gr-blocks/include/blocks/keep_m_in_n.h  | 53 ++++++++++++++++++++
 gr-blocks/lib/CMakeLists.txt            |  1 +
 gr-blocks/lib/keep_m_in_n_impl.cc       | 87 +++++++++++++++++++++++++++++++++
 gr-blocks/lib/keep_m_in_n_impl.h        | 58 ++++++++++++++++++++++
 gr-blocks/python/qa_keep_m_in_n.py      | 59 ++++++++++++++++++++++
 gr-blocks/swig/blocks_swig.i            |  3 ++
 9 files changed, 339 insertions(+)
 create mode 100644 gr-blocks/grc/blocks_keep_m_in_n.xml
 create mode 100644 gr-blocks/include/blocks/keep_m_in_n.h
 create mode 100644 gr-blocks/lib/keep_m_in_n_impl.cc
 create mode 100644 gr-blocks/lib/keep_m_in_n_impl.h
 create mode 100755 gr-blocks/python/qa_keep_m_in_n.py

(limited to 'gr-blocks')

diff --git a/gr-blocks/grc/blocks_block_tree.xml b/gr-blocks/grc/blocks_block_tree.xml
index 37cd2fc4d..c2e45b9eb 100644
--- a/gr-blocks/grc/blocks_block_tree.xml
+++ b/gr-blocks/grc/blocks_block_tree.xml
@@ -75,6 +75,7 @@
 	        <name>Stream Operations (New) </name>
 		<block>blocks_deinterleave.xml</block>
 		<block>blocks_interleave.xml</block>
+		<block>blocks_keep_m_in_n.xml</block>
 		<block>blocks_keep_one_in_n.xml</block>
 	</cat>
 </cat>
diff --git a/gr-blocks/grc/blocks_keep_m_in_n.xml b/gr-blocks/grc/blocks_keep_m_in_n.xml
new file mode 100644
index 000000000..9e861749b
--- /dev/null
+++ b/gr-blocks/grc/blocks_keep_m_in_n.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0"?>
+<!--
+###################################################
+##Keep M in N
+###################################################
+ -->
+<block>
+	<name>Keep M in N</name>
+	<key>blocks_keep_m_in_n</key>
+	<import>from gnuradio import blocks</import>
+	<make>blocks.keep_m_in_n($type.size, $m, $n, $offset)</make>
+    <callback>set_offset($offset)</callback>
+    <callback>set_m($m)</callback>
+    <callback>set_n($n)</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>M</name>
+		<key>m</key>
+		<value>1</value>
+		<type>int</type>
+	</param>
+	<param>
+		<name>N</name>
+		<key>n</key>
+		<value>2</value>
+		<type>int</type>
+	</param>
+	<param>
+		<name>initial offset</name>
+		<key>offset</key>
+		<value>0</value>
+		<type>int</type>
+	</param>
+	<check>$n &gt; 0</check>
+	<check>$m &gt; 0</check>
+	<check>$m &lt; $n</check>
+	<sink>
+		<name>in</name>
+		<type>$type</type>
+		<vlen>1</vlen>
+	</sink>
+	<source>
+		<name>out</name>
+		<type>$type</type>
+		<vlen>1</vlen>
+	</source>
+</block>
diff --git a/gr-blocks/include/blocks/CMakeLists.txt b/gr-blocks/include/blocks/CMakeLists.txt
index 5b4c0e437..0db0eebd2 100644
--- a/gr-blocks/include/blocks/CMakeLists.txt
+++ b/gr-blocks/include/blocks/CMakeLists.txt
@@ -109,6 +109,7 @@ install(FILES
     int_to_float.h
     interleave.h
     interleaved_short_to_complex.h
+    keep_m_in_n.h
     keep_one_in_n.h
     multiply_cc.h
     multiply_ff.h
diff --git a/gr-blocks/include/blocks/keep_m_in_n.h b/gr-blocks/include/blocks/keep_m_in_n.h
new file mode 100644
index 000000000..806ec3de5
--- /dev/null
+++ b/gr-blocks/include/blocks/keep_m_in_n.h
@@ -0,0 +1,53 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2012 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_BLOCKS_KEEP_M_IN_N_H
+#define INCLUDED_BLOCKS_KEEP_M_IN_N_H
+
+#include <blocks/api.h>
+#include <gr_block.h>
+
+namespace gr {
+  namespace blocks {
+
+    /*!
+     * \brief decimate a stream, keeping one item out of every n.
+     * \ingroup slicedice_blk
+     */
+    class BLOCKS_API keep_m_in_n : virtual public gr_block
+    {
+    public:
+      
+      // gr::blocks::keep_m_in_n::sptr
+      typedef boost::shared_ptr<keep_m_in_n> sptr;
+
+      static sptr make(size_t itemsize, int m, int n, int offset);
+
+      virtual void set_m(int m) = 0;
+      virtual void set_n(int n) = 0;
+      virtual void set_offset(int offset) = 0;
+    };
+
+  } /* namespace blocks */
+} /* namespace gr */
+
+#endif /* INCLUDED_BLOCKS_KEEP_M_IN_N_H */
diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt
index 1e34b544f..e4aca48f0 100644
--- a/gr-blocks/lib/CMakeLists.txt
+++ b/gr-blocks/lib/CMakeLists.txt
@@ -146,6 +146,7 @@ list(APPEND gr_blocks_sources
     interleave_impl.cc
     interleaved_short_array_to_complex.cc
     interleaved_short_to_complex_impl.cc
+    keep_m_in_n_impl.cc
     keep_one_in_n_impl.cc
     multiply_cc_impl.cc
     multiply_ff_impl.cc
diff --git a/gr-blocks/lib/keep_m_in_n_impl.cc b/gr-blocks/lib/keep_m_in_n_impl.cc
new file mode 100644
index 000000000..b3efdd962
--- /dev/null
+++ b/gr-blocks/lib/keep_m_in_n_impl.cc
@@ -0,0 +1,87 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2012 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 "keep_m_in_n_impl.h"
+#include <gr_io_signature.h>
+
+namespace gr {
+  namespace blocks {
+
+    keep_m_in_n::sptr keep_m_in_n::make(size_t itemsize, int m, int n, int offset)
+    {
+      return gnuradio::get_initial_sptr(new keep_m_in_n_impl(itemsize, m, n, offset));
+    }
+
+    keep_m_in_n_impl::keep_m_in_n_impl(size_t itemsize, int m, int n, int offset)
+      : gr_block("keep_m_in_n",
+		 gr_make_io_signature (1, 1, itemsize),
+		 gr_make_io_signature (1, 1, itemsize)),
+	d_itemsize(itemsize),
+	d_m(m),
+	d_n(n),
+	d_offset(offset)
+    {
+      // sanity checking
+      assert(d_m > 0);
+      assert(d_n > 0);
+      assert(d_m <= d_n);
+      assert(d_offset <= (d_n-d_m));
+      
+      set_output_multiple(m);
+    }
+
+    void
+    keep_m_in_n_impl::forecast(int noutput_items, gr_vector_int &ninput_items_required)
+    {
+      ninput_items_required[0] = d_n*(noutput_items/d_m);
+    }
+
+    int
+    keep_m_in_n_impl::general_work(int noutput_items,
+				   gr_vector_int &ninput_items,
+				   gr_vector_const_void_star &input_items,
+				   gr_vector_void_star &output_items)
+    {
+      uint8_t* out = (uint8_t*)output_items[0];
+      const uint8_t* in = (const uint8_t*)input_items[0];
+      
+      // iterate over data blocks of size {n, input : m, output}
+      int blks = std::min(noutput_items/d_m, ninput_items[0]/d_n);
+
+      for(int i=0; i<blks; i++) {
+	// set up copy pointers
+	const uint8_t* iptr = &in[(i*d_n + d_offset)*d_itemsize];
+	uint8_t* optr = &out[i*d_m*d_itemsize];
+	// perform copy
+	memcpy( optr, iptr, d_m*d_itemsize );
+      }
+      
+      consume_each(d_n);
+      return d_m;
+    }
+    
+  } /* namespace blocks */
+} /* namespace gr */
diff --git a/gr-blocks/lib/keep_m_in_n_impl.h b/gr-blocks/lib/keep_m_in_n_impl.h
new file mode 100644
index 000000000..5e264dba0
--- /dev/null
+++ b/gr-blocks/lib/keep_m_in_n_impl.h
@@ -0,0 +1,58 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2012 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_KEEP_M_IN_N_IMPL_H
+#define INCLUDED_KEEP_M_IN_N_IMPL_H
+
+#include <blocks/keep_m_in_n.h>
+
+namespace gr {
+  namespace blocks {
+
+    class BLOCKS_API keep_m_in_n_impl : public keep_m_in_n
+    {
+      int d_n;
+      int d_m;
+      int d_count;
+      int d_offset;
+      int d_itemsize;
+
+      void forecast(int noutput_items, gr_vector_int &ninput_items_required);
+      
+    public:
+      keep_m_in_n_impl(size_t itemsize, int m, int n, int offset);
+
+      int general_work(int noutput_items,
+		       gr_vector_int &ninput_items,
+		       gr_vector_const_void_star &input_items,
+		       gr_vector_void_star &output_items);
+      
+      void set_m(int m) { d_m = m; }
+      void set_n(int n) { d_n = n; }
+      void set_offset(int offset) { d_offset = offset; }
+    };
+
+  } /* namespace blocks */
+} /* namespace gr */
+   
+
+#endif /* INCLUDED_KEEP_M_IN_N_IMPL_H */
diff --git a/gr-blocks/python/qa_keep_m_in_n.py b/gr-blocks/python/qa_keep_m_in_n.py
new file mode 100755
index 000000000..0898217ba
--- /dev/null
+++ b/gr-blocks/python/qa_keep_m_in_n.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+#
+# Copyright 2008,2010,2012 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 this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+
+from gnuradio import gr, gr_unittest
+import blocks_swig
+import sys
+import random
+
+class test_keep_m_in_n(gr_unittest.TestCase):
+
+    def setUp(self):
+	pass
+
+    def tearDown(self):
+	pass
+
+    def test_001(self):
+        self.maxDiff = None;
+        tb = gr.top_block()
+        src = gr.vector_source_b( range(0,100) )
+
+        # itemsize, M, N, offset
+        km2 = blocks_swig.keep_m_in_n( 1, 1, 2, 0 );
+        km3 = blocks_swig.keep_m_in_n( 1, 1, 3, 1 );
+        km7 = blocks_swig.keep_m_in_n( 1, 1, 7, 2 );
+        snk2 = gr.vector_sink_b();
+        snk3 = gr.vector_sink_b();
+        snk7 = gr.vector_sink_b();
+        tb.connect(src,km2,snk2);
+        tb.connect(src,km3,snk3);
+        tb.connect(src,km7,snk7);
+        tb.run();
+
+        self.assertEqual(range(0,100,2), list(snk2.data()));
+        self.assertEqual(range(1,100,3), list(snk3.data()));
+        self.assertEqual(range(2,100,7), list(snk7.data()));
+
+
+if __name__ == '__main__':
+    gr_unittest.run(test_keep_m_in_n, "test_keep_m_in_n.xml")
+
diff --git a/gr-blocks/swig/blocks_swig.i b/gr-blocks/swig/blocks_swig.i
index ea3071805..41c5081bb 100644
--- a/gr-blocks/swig/blocks_swig.i
+++ b/gr-blocks/swig/blocks_swig.i
@@ -73,6 +73,7 @@
 #include "blocks/integrate_cc.h"
 #include "blocks/interleave.h"
 #include "blocks/interleaved_short_to_complex.h"
+#include "blocks/keep_m_in_n.h"
 #include "blocks/keep_one_in_n.h"
 #include "blocks/multiply_ss.h"
 #include "blocks/multiply_ii.h"
@@ -151,6 +152,7 @@
 %include "blocks/integrate_cc.h"
 %include "blocks/interleave.h"
 %include "blocks/interleaved_short_to_complex.h"
+%include "blocks/keep_m_in_n.h"
 %include "blocks/keep_one_in_n.h"
 %include "blocks/multiply_ss.h"
 %include "blocks/multiply_ii.h"
@@ -228,6 +230,7 @@ GR_SWIG_BLOCK_MAGIC2(blocks, integrate_ff);
 GR_SWIG_BLOCK_MAGIC2(blocks, integrate_cc);
 GR_SWIG_BLOCK_MAGIC2(blocks, interleave);
 GR_SWIG_BLOCK_MAGIC2(blocks, interleaved_short_to_complex);
+GR_SWIG_BLOCK_MAGIC2(blocks, keep_m_in_n);
 GR_SWIG_BLOCK_MAGIC2(blocks, keep_one_in_n);
 GR_SWIG_BLOCK_MAGIC2(blocks, multiply_ss);
 GR_SWIG_BLOCK_MAGIC2(blocks, multiply_ii);
-- 
cgit