summaryrefslogtreecommitdiff
path: root/gnuradio-core/src/lib/general
diff options
context:
space:
mode:
authorBen Reynwar2012-05-17 11:37:20 -0700
committerBen Reynwar2012-05-17 11:37:20 -0700
commitae75ab750c2210a880684c3ce82bc152dd591ab3 (patch)
treecf59e015d9bf46dc1e6eec42c945fa2a2ebbc143 /gnuradio-core/src/lib/general
parent99f8f19166bf00b22bfafec26be8715b0fe291dd (diff)
downloadgnuradio-ae75ab750c2210a880684c3ce82bc152dd591ab3.tar.gz
gnuradio-ae75ab750c2210a880684c3ce82bc152dd591ab3.tar.bz2
gnuradio-ae75ab750c2210a880684c3ce82bc152dd591ab3.zip
gr: Added vector_map block.
Diffstat (limited to 'gnuradio-core/src/lib/general')
-rw-r--r--gnuradio-core/src/lib/general/CMakeLists.txt1
-rw-r--r--gnuradio-core/src/lib/general/general.i2
-rw-r--r--gnuradio-core/src/lib/general/gr_vector_map.cc112
-rw-r--r--gnuradio-core/src/lib/general/gr_vector_map.h77
-rw-r--r--gnuradio-core/src/lib/general/gr_vector_map.i28
5 files changed, 220 insertions, 0 deletions
diff --git a/gnuradio-core/src/lib/general/CMakeLists.txt b/gnuradio-core/src/lib/general/CMakeLists.txt
index 207d85c4c..0ad55e38a 100644
--- a/gnuradio-core/src/lib/general/CMakeLists.txt
+++ b/gnuradio-core/src/lib/general/CMakeLists.txt
@@ -281,6 +281,7 @@ set(gr_core_general_triple_threats
gr_transcendental
gr_uchar_to_float
gr_vco_f
+ gr_vector_map
gr_vector_to_stream
gr_vector_to_streams
gr_unpack_k_bits_bb
diff --git a/gnuradio-core/src/lib/general/general.i b/gnuradio-core/src/lib/general/general.i
index cd8c279c9..54d9a8670 100644
--- a/gnuradio-core/src/lib/general/general.i
+++ b/gnuradio-core/src/lib/general/general.i
@@ -137,6 +137,7 @@
#include <gr_cpm.h>
#include <gr_correlate_access_code_tag_bb.h>
#include <gr_add_ff.h>
+#include <gr_vector_map.h>
%}
%include "gri_control_loop.i"
@@ -254,3 +255,4 @@
%include "gr_cpm.i"
%include "gr_correlate_access_code_tag_bb.i"
%include "gr_add_ff.i"
+%include "gr_vector_map.i"
diff --git a/gnuradio-core/src/lib/general/gr_vector_map.cc b/gnuradio-core/src/lib/general/gr_vector_map.cc
new file mode 100644
index 000000000..361daa490
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_vector_map.cc
@@ -0,0 +1,112 @@
+/* -*- 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 <gr_vector_map.h>
+#include <gr_io_signature.h>
+#include <string.h>
+
+std::vector<int>
+get_in_sizeofs(size_t item_size, std::vector<size_t> in_vlens) {
+ std::vector<int> in_sizeofs;
+ for (unsigned int i; i < in_vlens.size(); i++) {
+ in_sizeofs.push_back(in_vlens[i]*item_size);
+ }
+ return in_sizeofs;
+}
+
+std::vector<int>
+get_out_sizeofs(size_t item_size,
+ std::vector< std::vector< std::vector<size_t> > > mapping) {
+ std::vector<int> out_sizeofs;
+ for (unsigned int i; i < mapping.size(); i++) {
+ out_sizeofs.push_back(mapping[i].size()*item_size);
+ }
+ return out_sizeofs;
+}
+
+gr_vector_map_sptr
+gr_make_vector_map (size_t item_size, std::vector<size_t> in_vlens,
+ std::vector< std::vector< std::vector<size_t> > > mapping)
+{
+ return gnuradio::get_initial_sptr(new gr_vector_map (item_size, in_vlens, mapping));
+}
+
+gr_vector_map::gr_vector_map (size_t item_size, std::vector<size_t> in_vlens,
+ std::vector< std::vector< std::vector<size_t> > > mapping)
+ : gr_sync_block ("vector_map",
+ gr_make_io_signaturev (in_vlens.size(), in_vlens.size(),
+ get_in_sizeofs(item_size, in_vlens)),
+ gr_make_io_signaturev (mapping.size(), mapping.size(),
+ get_out_sizeofs(item_size, mapping))),
+ d_item_size(item_size), d_in_vlens(in_vlens)
+{
+ set_mapping(mapping);
+}
+
+void
+gr_vector_map::set_mapping(std::vector< std::vector< std::vector<size_t> > > mapping) {
+ // Make sure the contents of the mapping vectors are possible.
+ for (unsigned int i=0; i<mapping.size(); i++) {
+ for (unsigned int j=0; j<mapping[i].size(); j++) {
+ if (mapping[i][j].size() != 2) {
+ throw std::runtime_error("Mapping must be of the form (out_mapping_stream1, out_mapping_stream2, ...), where out_mapping_stream1 is of the form (mapping_element1, mapping_element2, ...), where mapping_element1 is of the form (input_stream, input_element). This error is raised because a mapping_element vector does not contain exactly 2 items.");
+ }
+ unsigned int s = mapping[i][j][0];
+ unsigned int index = mapping[i][j][1];
+ if (s >= d_in_vlens.size()) {
+ throw std::runtime_error("Stream numbers in mapping must be less than the number of input streams.");
+ }
+ if ((index < 0) || (index >= d_in_vlens[s])) {
+ throw std::runtime_error ("Indices in mapping must be greater than 0 and less than the input vector lengths.");
+ }
+ }
+ }
+ gruel::scoped_lock guard(d_mutex);
+ d_mapping = mapping;
+}
+
+int
+gr_vector_map::work (int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
+{
+
+ const char **inv = (const char **) &input_items[0];
+ char **outv = (char **) &output_items[0];
+
+ for (unsigned int n=0; n<(unsigned int)(noutput_items); n++) {
+ for (unsigned int i=0; i<d_mapping.size(); i++) {
+ unsigned int out_vlen = d_mapping[i].size();
+ for (unsigned int j=0; j<out_vlen; j++) {
+ unsigned int s = d_mapping[i][j][0];
+ unsigned int k = d_mapping[i][j][1];
+ memcpy(outv[i] + out_vlen*d_item_size*n + d_item_size*j, inv[s] + d_in_vlens[s]*d_item_size*n + k*d_item_size, d_item_size);
+ }
+ }
+ }
+
+ return noutput_items;
+}
diff --git a/gnuradio-core/src/lib/general/gr_vector_map.h b/gnuradio-core/src/lib/general/gr_vector_map.h
new file mode 100644
index 000000000..ccb05bc53
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_vector_map.h
@@ -0,0 +1,77 @@
+/* -*- 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_GR_VECTOR_MAP_H
+#define INCLUDED_GR_VECTOR_MAP_H
+
+#include <vector>
+#include <gr_core_api.h>
+#include <gr_sync_interpolator.h>
+#include <gruel/thread.h>
+
+class gr_vector_map;
+typedef boost::shared_ptr<gr_vector_map> gr_vector_map_sptr;
+
+GR_CORE_API gr_vector_map_sptr
+gr_make_vector_map (size_t item_size, std::vector<size_t> in_vlens,
+ std::vector< std::vector< std::vector<size_t> > > mapping);
+
+/*!
+ * \brief Maps elements from a set of input vectors to a set of output vectors.
+ *
+ * If in[i] is the input vector in the i'th stream then the output vector in the j'th stream is:
+ * out[j][k] = in[mapping[j][k][0]][mapping[j][k][1]]
+ * That is mapping is of the form (out_stream1_mapping, out_stream2_mapping, ...)
+ * and out_stream1_mapping is of the form (element1_mapping, element2_mapping, ...)
+ * and element1_mapping is of the form (in_stream, in_element).
+ *
+ * \param item_size (integer) size of vector elements
+ *
+ * \param in_vlens (vector of integers) number of elements in each input vector
+ *
+ * \param mapping (vector of vectors of vectors of integers) how to map elements from input to output vectors
+ *
+ * \ingroup slicedice_blk
+ */
+class GR_CORE_API gr_vector_map : public gr_sync_block
+{
+ friend GR_CORE_API gr_vector_map_sptr
+ gr_make_vector_map (size_t item_size, std::vector<size_t> in_vlens,
+ std::vector< std::vector< std::vector<size_t> > > mapping);
+ size_t d_item_size;
+ std::vector<size_t> d_in_vlens;
+ std::vector< std::vector< std::vector<size_t> > > d_mapping;
+ gruel::mutex d_mutex; // mutex to protect set/work access
+
+ protected:
+ gr_vector_map (size_t item_size, std::vector<size_t> in_vlens,
+ std::vector< std::vector< std::vector<size_t> > > mapping);
+
+ public:
+ int work (int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items);
+
+ void set_mapping(std::vector< std::vector< std::vector<size_t> > > mapping);
+};
+
+#endif /* INCLUDED_GR_VECTOR_MAP_H */
diff --git a/gnuradio-core/src/lib/general/gr_vector_map.i b/gnuradio-core/src/lib/general/gr_vector_map.i
new file mode 100644
index 000000000..e9fa3f27e
--- /dev/null
+++ b/gnuradio-core/src/lib/general/gr_vector_map.i
@@ -0,0 +1,28 @@
+/* -*- 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.
+ */
+
+GR_SWIG_BLOCK_MAGIC(gr, vector_map);
+
+%template() std::vector<size_t>;
+%template() std::vector< std::vector< std::vector<size_t> > >;
+
+%include "gr_vector_map.h"