diff options
author | Tom Rondeau <tom@trondeau.com> | 2014-05-16 12:17:25 -0400 |
---|---|---|
committer | Tom Rondeau <tom@trondeau.com> | 2014-05-17 17:45:16 -0400 |
commit | 0db24ffd533cc4b0e22f82c5d6a4713892bb5ef7 (patch) | |
tree | 8d5166be35b9f947825900a8d5ecf2055b0e6eb3 | |
parent | f23b3106cd2c73be3708c29dc85afb37298d4731 (diff) |
blocks: adds kernels for pack_k_bits and unpack_k_bits.
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/CMakeLists.txt | 4 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/pack_k_bits.h | 80 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/unpack_k_bits.h | 81 | ||||
-rw-r--r-- | gr-blocks/lib/CMakeLists.txt | 12 | ||||
-rw-r--r-- | gr-blocks/lib/pack_k_bits.cc | 59 | ||||
-rw-r--r-- | gr-blocks/lib/pack_k_bits_bb_impl.cc | 26 | ||||
-rw-r--r-- | gr-blocks/lib/pack_k_bits_bb_impl.h | 14 | ||||
-rw-r--r-- | gr-blocks/lib/unpack_k_bits.cc | 66 | ||||
-rw-r--r-- | gr-blocks/lib/unpack_k_bits_bb_impl.cc | 23 | ||||
-rw-r--r-- | gr-blocks/lib/unpack_k_bits_bb_impl.h | 5 | ||||
-rw-r--r-- | gr-blocks/swig/blocks_swig5.i | 2 |
11 files changed, 327 insertions, 45 deletions
diff --git a/gr-blocks/include/gnuradio/blocks/CMakeLists.txt b/gr-blocks/include/gnuradio/blocks/CMakeLists.txt index 7b8a0d6dc4..a0c5dc80de 100644 --- a/gr-blocks/include/gnuradio/blocks/CMakeLists.txt +++ b/gr-blocks/include/gnuradio/blocks/CMakeLists.txt @@ -47,7 +47,7 @@ macro(expand_h root) string(REGEX REPLACE "X+" ${sig} name ${root}) list(APPEND expanded_files_h ${CMAKE_CURRENT_BINARY_DIR}/${name}.h) endforeach(sig) - + #create a command to generate the files add_custom_command( OUTPUT ${expanded_files_h} @@ -109,7 +109,9 @@ install(FILES lfsr_15_1_0.h lfsr_32k.h log2_const.h + pack_k_bits.h rotator.h + unpack_k_bits.h wavfile.h add_ff.h annotator_1to1.h diff --git a/gr-blocks/include/gnuradio/blocks/pack_k_bits.h b/gr-blocks/include/gnuradio/blocks/pack_k_bits.h new file mode 100644 index 0000000000..f8c154d692 --- /dev/null +++ b/gr-blocks/include/gnuradio/blocks/pack_k_bits.h @@ -0,0 +1,80 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_BLOCKS_PACK_K_BITS_H +#define INCLUDED_GR_BLOCKS_PACK_K_BITS_H + +#include <gnuradio/blocks/api.h> +#include <vector> + +namespace gr { + namespace blocks { + namespace kernel { + + /*! + * \brief Converts a vector of bytes with 1 bit in the LSB to a + * byte with k relevent bits. + * + * Example: + * k = 4 + * in = [0,1,0,1, 0x81,0x00,0x00,0x00] + * out = [0x05, 0x08] + * + * k = 8 + * in = [1,1,1,1, 0,1,0,1, 0,0,0,0, 1,0,0,0] + * out = [0xf5, 0x08] + * \ingroup byte_operators_blk + */ + class BLOCKS_API pack_k_bits + { + public: + /*! + * \brief Make a pack_k_bits object. + * \param k number of bits to be packed. + */ + pack_k_bits(unsigned k); + ~pack_k_bits(); + + /*! + * \brief Perform the packing. + * + * This block performs no bounds checking. It assumes that the + * input, \p in, has of length k*nbytes and that the output + * vector, \p out, has \p nbytes available for writing. + * + * \param bytes output vector (k-bits per byte) of the unpacked data + * \param bits The input vector of bits to pack + * \param nbytes The number of output bytes + */ + void pack(unsigned char *bytes, const unsigned char *bits, int nbytes) const; + + int k() const; + + private: + unsigned d_k; + }; + + } /* namespace kernel */ + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_BLOCKS_PACK_K_BITS_H */ diff --git a/gr-blocks/include/gnuradio/blocks/unpack_k_bits.h b/gr-blocks/include/gnuradio/blocks/unpack_k_bits.h new file mode 100644 index 0000000000..6f982b4725 --- /dev/null +++ b/gr-blocks/include/gnuradio/blocks/unpack_k_bits.h @@ -0,0 +1,81 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_BLOCKS_UNPACK_K_BITS_H +#define INCLUDED_GR_BLOCKS_UNPACK_K_BITS_H + +#include <gnuradio/blocks/api.h> +#include <vector> + +namespace gr { + namespace blocks { + namespace kernel { + + /*! + * \brief Converts a byte with k relevent bits to k output bytes with 1 bit in the LSB. + * + * This is the algorithm kernel for the gr::blocks::unpack_k_bits_bb block. + * + * Example: + * k = 4 + * in = [0xf5, 0x08] + * out = [0,1,0,1, 1,0,0,0] + * + * k = 8 + * in = [0xf5, 0x08] + * out = [1,1,1,1, 0,1,0,1, 0,0,0,0, 1,0,0,0] + * \ingroup byte_operators_blk + */ + class BLOCKS_API unpack_k_bits + { + public: + /*! + * \brief Make an unpack_k_bits object. + * \param k number of bits to unpack. + */ + unpack_k_bits(unsigned k); + ~unpack_k_bits(); + + /*! + * \brief Perform the unpacking. + * + * This block performs no bounds checking. It assumes that the + * input, \p in, has of length \p nbytes and that the output + * vector, \p out, has k*nbytes available for writing. + * + * \param bits output vector (1-bit per byte) of the unpacked data + * \param bytes The input vector of bytes to unpack + * \param nbytes The number of input bytes + */ + void unpack(unsigned char *bits, const unsigned char *bytes, int nbytes) const; + + int k() const; + + private: + unsigned d_k; + }; + + } /* namespace kernel */ + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_BLOCKS_UNPACK_K_BITS_BB_H */ diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt index 0e7aab5e7c..416c2c5c2d 100644 --- a/gr-blocks/lib/CMakeLists.txt +++ b/gr-blocks/lib/CMakeLists.txt @@ -54,7 +54,7 @@ macro(expand_cc_h_impl root) list(APPEND expanded_files_h_impl ${CMAKE_CURRENT_BINARY_DIR}/${name}_impl.h) list(APPEND expanded_files_h ${CMAKE_CURRENT_BINARY_DIR}/../include/${name}.h) endforeach(sig) - + #create a command to generate the _impl.cc files add_custom_command( OUTPUT ${expanded_files_cc_impl} @@ -63,7 +63,7 @@ macro(expand_cc_h_impl root) ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py ${root} ${root}_impl.cc.t ${ARGN} ) - + #create a command to generate the _impl.h files add_custom_command( OUTPUT ${expanded_files_h_impl} @@ -72,19 +72,19 @@ macro(expand_cc_h_impl root) ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py ${root} ${root}_impl.h.t ${ARGN} ) - + #make _impl.cc source files depend on headers to force generation set_source_files_properties(${expanded_files_cc_impl} PROPERTIES OBJECT_DEPENDS "${expanded_files_h_impl}" ) - + #make _impl.h source files depend on headers to force generation set_source_files_properties(${expanded_files_h_impl} PROPERTIES OBJECT_DEPENDS "${expanded_files_h}" ) #install rules for the generated cc files - list(APPEND generated_sources ${expanded_files_cc_impl}) + list(APPEND generated_sources ${expanded_files_cc_impl}) endmacro(expand_cc_h_impl) ######################################################################## @@ -147,6 +147,8 @@ list(APPEND gr_blocks_sources control_loop.cc count_bits.cc file_sink_base.cc + pack_k_bits.cc + unpack_k_bits.cc wavfile.cc add_ff_impl.cc annotator_1to1_impl.cc diff --git a/gr-blocks/lib/pack_k_bits.cc b/gr-blocks/lib/pack_k_bits.cc new file mode 100644 index 0000000000..d0123f5a09 --- /dev/null +++ b/gr-blocks/lib/pack_k_bits.cc @@ -0,0 +1,59 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gnuradio/blocks/pack_k_bits.h> +#include <stdexcept> +#include <iostream> + +namespace gr { + namespace blocks { + namespace kernel { + + pack_k_bits::pack_k_bits(unsigned k) + : d_k(k) + { + if(d_k == 0) + throw std::out_of_range("pack_k_bits: k must be > 0"); + } + + pack_k_bits::~pack_k_bits() + { + } + + void + pack_k_bits::pack(unsigned char *bytes, const unsigned char *bits, int nbytes) const + { + for(int i = 0; i < nbytes; i++) { + bytes[i] = 0x00; + for(unsigned int j = 0; j < d_k; j++) { + bytes[i] |= (0x01 & bits[i*d_k+j])<<(d_k-j-1); + } + } + } + + } /* namespace kernel */ + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/pack_k_bits_bb_impl.cc b/gr-blocks/lib/pack_k_bits_bb_impl.cc index 9009c89ab7..889e0d29f4 100644 --- a/gr-blocks/lib/pack_k_bits_bb_impl.cc +++ b/gr-blocks/lib/pack_k_bits_bb_impl.cc @@ -1,19 +1,19 @@ /* -*- c++ -*- */ /* - * Copyright 2012-2013 Free Software Foundation, Inc. - * + * Copyright 2012-2014 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, @@ -43,15 +43,14 @@ namespace gr { : sync_decimator("pack_k_bits_bb", io_signature::make(1, 1, sizeof(unsigned char)), io_signature::make(1, 1, sizeof(unsigned char)), - k), - d_k(k) + k) { - if(d_k == 0) - throw std::out_of_range("interpolation must be > 0"); + d_pack = new kernel::pack_k_bits(k); } - + pack_k_bits_bb_impl::~pack_k_bits_bb_impl() { + delete d_pack; } int @@ -62,12 +61,7 @@ namespace gr { const unsigned char *in = (const unsigned char *)input_items[0]; unsigned char *out = (unsigned char *)output_items[0]; - for(int i = 0; i < noutput_items; i++) { - out[i] = 0x00; - for(unsigned int j = 0; j < d_k; j++) { - out[i] |= (0x01 & in[i*d_k+j])<<(d_k-j-1); - } - } + d_pack->pack(out, in, noutput_items); return noutput_items; } diff --git a/gr-blocks/lib/pack_k_bits_bb_impl.h b/gr-blocks/lib/pack_k_bits_bb_impl.h index dfe859478c..38cf1e7517 100644 --- a/gr-blocks/lib/pack_k_bits_bb_impl.h +++ b/gr-blocks/lib/pack_k_bits_bb_impl.h @@ -1,19 +1,19 @@ /* -*- c++ -*- */ /* - * Copyright 2012-2013 Free Software Foundation, Inc. - * + * Copyright 2012-2014 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, @@ -24,6 +24,7 @@ #define INCLUDED_GR_PACK_K_BITS_BB_IMPL_H #include <gnuradio/blocks/pack_k_bits_bb.h> +#include <gnuradio/blocks/pack_k_bits.h> namespace gr { namespace blocks { @@ -32,7 +33,8 @@ namespace gr { { private: unsigned d_k; // number of relevent bits to pack from k input bytes - + kernel::pack_k_bits *d_pack; + public: pack_k_bits_bb_impl(unsigned k); ~pack_k_bits_bb_impl(); diff --git a/gr-blocks/lib/unpack_k_bits.cc b/gr-blocks/lib/unpack_k_bits.cc new file mode 100644 index 0000000000..f274cf5b60 --- /dev/null +++ b/gr-blocks/lib/unpack_k_bits.cc @@ -0,0 +1,66 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gnuradio/blocks/unpack_k_bits.h> +#include <gnuradio/io_signature.h> +#include <stdexcept> +#include <iostream> + +namespace gr { + namespace blocks { + namespace kernel { + + unpack_k_bits::unpack_k_bits(unsigned k) + : d_k(k) + { + if(d_k == 0) + throw std::out_of_range("unpack_k_bits: k must be > 0"); + } + + unpack_k_bits::~unpack_k_bits() + { + } + + void + unpack_k_bits::unpack(unsigned char *bits, const unsigned char *bytes, int nbytes) const + { + int n = 0; + for(int i = 0; i < nbytes; i++) { + unsigned int t = bytes[i]; + for(int j = d_k - 1; j >= 0; j--) + bits[n++] = (t >> j) & 0x01; + } + } + + int + unpack_k_bits::k() const + { + return d_k; + } + + } /* namespace kernel */ + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/unpack_k_bits_bb_impl.cc b/gr-blocks/lib/unpack_k_bits_bb_impl.cc index 70055f9381..caf3d059d6 100644 --- a/gr-blocks/lib/unpack_k_bits_bb_impl.cc +++ b/gr-blocks/lib/unpack_k_bits_bb_impl.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2005,2010,2013 Free Software Foundation, Inc. + * Copyright 2005,2010,2013-2014 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -41,17 +41,16 @@ namespace gr { unpack_k_bits_bb_impl::unpack_k_bits_bb_impl(unsigned k) : sync_interpolator("unpack_k_bits_bb", - io_signature::make(1, 1, sizeof(unsigned char)), - io_signature::make(1, 1, sizeof(unsigned char)), - k), - d_k(k) + io_signature::make(1, 1, sizeof(unsigned char)), + io_signature::make(1, 1, sizeof(unsigned char)), + k) { - if(d_k == 0) - throw std::out_of_range("interpolation must be > 0"); + d_unpack = new kernel::unpack_k_bits(k); } unpack_k_bits_bb_impl::~unpack_k_bits_bb_impl() { + delete d_unpack; } int @@ -61,15 +60,9 @@ namespace gr { { const unsigned char *in = (const unsigned char *)input_items[0]; unsigned char *out = (unsigned char *)output_items[0]; - - int n = 0; - for(unsigned int i = 0; i < noutput_items/d_k; i++) { - unsigned int t = in[i]; - for(int j = d_k - 1; j >= 0; j--) - out[n++] = (t >> j) & 0x01; - } - assert(n == noutput_items); + d_unpack->unpack(out, in, noutput_items/d_unpack->k()); + return noutput_items; } diff --git a/gr-blocks/lib/unpack_k_bits_bb_impl.h b/gr-blocks/lib/unpack_k_bits_bb_impl.h index 7355d235c5..246c4ea724 100644 --- a/gr-blocks/lib/unpack_k_bits_bb_impl.h +++ b/gr-blocks/lib/unpack_k_bits_bb_impl.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2006,2013 Free Software Foundation, Inc. + * Copyright 2006,2013-2014 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -24,6 +24,7 @@ #define INCLUDED_GR_UNPACK_K_BITS_BB_IMPL_H #include <gnuradio/blocks/unpack_k_bits_bb.h> +#include <gnuradio/blocks/unpack_k_bits.h> namespace gr { namespace blocks { @@ -31,7 +32,7 @@ namespace gr { class unpack_k_bits_bb_impl : public unpack_k_bits_bb { private: - unsigned d_k; // number of relevent bits to unpack into k output bytes + kernel::unpack_k_bits *d_unpack; public: unpack_k_bits_bb_impl(unsigned k); diff --git a/gr-blocks/swig/blocks_swig5.i b/gr-blocks/swig/blocks_swig5.i index 0d68dc7819..497388c7ba 100644 --- a/gr-blocks/swig/blocks_swig5.i +++ b/gr-blocks/swig/blocks_swig5.i @@ -61,6 +61,7 @@ #include "gnuradio/blocks/uchar_to_float.h" #include "gnuradio/blocks/udp_sink.h" #include "gnuradio/blocks/udp_source.h" +#include "gnuradio/blocks/unpack_k_bits.h" #include "gnuradio/blocks/unpack_k_bits_bb.h" #include "gnuradio/blocks/unpacked_to_packed_bb.h" #include "gnuradio/blocks/unpacked_to_packed_ss.h" @@ -102,6 +103,7 @@ %include "gnuradio/blocks/uchar_to_float.h" %include "gnuradio/blocks/udp_sink.h" %include "gnuradio/blocks/udp_source.h" +%include "gnuradio/blocks/unpack_k_bits.h" %include "gnuradio/blocks/unpack_k_bits_bb.h" %include "gnuradio/blocks/unpacked_to_packed_bb.h" %include "gnuradio/blocks/unpacked_to_packed_ss.h" |