From 0b7655a76e5c73b8f5a8310909cf038f12cbb869 Mon Sep 17 00:00:00 2001 From: Johnathan Corgan Date: Tue, 26 Jun 2012 07:06:53 -0700 Subject: blocks: added gr::blocks::float_to_uchar --- gr-blocks/grc/blocks_block_tree.xml | 1 + gr-blocks/grc/blocks_float_uchar.xml | 20 +++++++++++ gr-blocks/include/blocks/CMakeLists.txt | 1 + gr-blocks/include/blocks/float_to_uchar.h | 49 +++++++++++++++++++++++++ gr-blocks/lib/CMakeLists.txt | 2 ++ gr-blocks/lib/float_array_to_uchar.cc | 45 +++++++++++++++++++++++ gr-blocks/lib/float_array_to_uchar.h | 33 +++++++++++++++++ gr-blocks/lib/float_to_uchar_impl.cc | 60 +++++++++++++++++++++++++++++++ gr-blocks/lib/float_to_uchar_impl.h | 45 +++++++++++++++++++++++ gr-blocks/python/qa_type_conversions.py | 10 ++++++ gr-blocks/swig/blocks_swig.i | 3 ++ 11 files changed, 269 insertions(+) create mode 100644 gr-blocks/grc/blocks_float_uchar.xml create mode 100644 gr-blocks/include/blocks/float_to_uchar.h create mode 100644 gr-blocks/lib/float_array_to_uchar.cc create mode 100644 gr-blocks/lib/float_array_to_uchar.h create mode 100644 gr-blocks/lib/float_to_uchar_impl.cc create mode 100644 gr-blocks/lib/float_to_uchar_impl.h (limited to 'gr-blocks') diff --git a/gr-blocks/grc/blocks_block_tree.xml b/gr-blocks/grc/blocks_block_tree.xml index f73c634a7..32a81c07a 100644 --- a/gr-blocks/grc/blocks_block_tree.xml +++ b/gr-blocks/grc/blocks_block_tree.xml @@ -52,5 +52,6 @@ blocks_float_to_complex blocks_float_to_int blocks_float_to_short + blocks_float_to_uchar diff --git a/gr-blocks/grc/blocks_float_uchar.xml b/gr-blocks/grc/blocks_float_uchar.xml new file mode 100644 index 000000000..d190eafcf --- /dev/null +++ b/gr-blocks/grc/blocks_float_uchar.xml @@ -0,0 +1,20 @@ + + + + Float To UChar + blocks_float_to_uchar + from gnuradio import blocks + blocks.float_to_uchar() + + in + float + + + out + byte + + diff --git a/gr-blocks/include/blocks/CMakeLists.txt b/gr-blocks/include/blocks/CMakeLists.txt index bf7ea230a..a688168d0 100644 --- a/gr-blocks/include/blocks/CMakeLists.txt +++ b/gr-blocks/include/blocks/CMakeLists.txt @@ -97,6 +97,7 @@ install(FILES float_to_complex.h float_to_int.h float_to_short.h + float_to_uchar.h multiply_cc.h multiply_ff.h multiply_const_cc.h diff --git a/gr-blocks/include/blocks/float_to_uchar.h b/gr-blocks/include/blocks/float_to_uchar.h new file mode 100644 index 000000000..b5d0d08f7 --- /dev/null +++ b/gr-blocks/include/blocks/float_to_uchar.h @@ -0,0 +1,49 @@ +/* -*- 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_FLOAT_TO_UCHAR_H +#define INCLUDED_BLOCKS_FLOAT_TO_UCHAR_H + +#include +#include + +namespace gr { + namespace blocks { + + /*! + * \brief Convert stream of floats to a stream of unsigned chars + * \ingroup converter_blk + */ + class BLOCKS_API float_to_uchar : virtual public gr_sync_block + { + public: + + // gr::blocks::float_to_uchar_ff::sptr + typedef boost::shared_ptr sptr; + + static sptr make(); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_BLOCKS_FLOAT_TO_UCHAR_H */ diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt index f0f20fb5d..a6894f256 100644 --- a/gr-blocks/lib/CMakeLists.txt +++ b/gr-blocks/lib/CMakeLists.txt @@ -132,6 +132,8 @@ list(APPEND gr_blocks_sources float_array_to_int.cc float_to_int_impl.cc float_to_short_impl.cc + float_array_to_uchar.cc + float_to_uchar_impl.cc multiply_cc_impl.cc multiply_ff_impl.cc multiply_const_cc_impl.cc diff --git a/gr-blocks/lib/float_array_to_uchar.cc b/gr-blocks/lib/float_array_to_uchar.cc new file mode 100644 index 000000000..06688e7b0 --- /dev/null +++ b/gr-blocks/lib/float_array_to_uchar.cc @@ -0,0 +1,45 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,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 +#endif + +#define _ISOC9X_SOURCE +#include +#include + +static const int MIN_UCHAR = 0; +static const int MAX_UCHAR = 255; + +void +float_array_to_uchar(const float *in, unsigned char *out, int nsamples) +{ + for (int i = 0; i < nsamples; i++){ + long int r = (long int) rint (in[i]); + if (r < MIN_UCHAR) + r = MIN_UCHAR; + else if (r > MAX_UCHAR) + r = MAX_UCHAR; + out[i] = r; + } +} diff --git a/gr-blocks/lib/float_array_to_uchar.h b/gr-blocks/lib/float_array_to_uchar.h new file mode 100644 index 000000000..defed8e3e --- /dev/null +++ b/gr-blocks/lib/float_array_to_uchar.h @@ -0,0 +1,33 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,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_FLOAT_ARRAY_TO_UCHAR_H +#define INCLUDED_FLOAT_ARRAY_TO_UCHAR_H + +#include + +/*! + * convert array of floats to unsigned chars with rounding and saturation. + */ +BLOCKS_API void float_array_to_uchar (const float *in, unsigned char *out, int nsamples); + +#endif /* INCLUDED_FLOAT_ARRAY_TO_UCHAR_H */ diff --git a/gr-blocks/lib/float_to_uchar_impl.cc b/gr-blocks/lib/float_to_uchar_impl.cc new file mode 100644 index 000000000..22d0527b1 --- /dev/null +++ b/gr-blocks/lib/float_to_uchar_impl.cc @@ -0,0 +1,60 @@ +/* -*- 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 "float_to_uchar_impl.h" +#include "float_array_to_uchar.h" +#include + +namespace gr { + namespace blocks { + + float_to_uchar::sptr float_to_uchar::make() + { + return gnuradio::get_initial_sptr(new float_to_uchar_impl()); + } + + float_to_uchar_impl::float_to_uchar_impl() + : gr_sync_block("float_to_uchar", + gr_make_io_signature (1, 1, sizeof(float)), + gr_make_io_signature (1, 1, sizeof(unsigned char))) + { + } + + int + float_to_uchar_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]; + unsigned char *out = (unsigned char *)output_items[0]; + + float_array_to_uchar(in, out, noutput_items); + + return noutput_items; + } + + } /* namespace blocks */ +}/* namespace gr */ diff --git a/gr-blocks/lib/float_to_uchar_impl.h b/gr-blocks/lib/float_to_uchar_impl.h new file mode 100644 index 000000000..effd66c5c --- /dev/null +++ b/gr-blocks/lib/float_to_uchar_impl.h @@ -0,0 +1,45 @@ +/* -*- 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_FLOAT_TO_UCHAR_IMPL_H +#define INCLUDED_FLOAT_TO_UCHAR_IMPL_H + +#include + +namespace gr { + namespace blocks { + + class BLOCKS_API float_to_uchar_impl : public float_to_uchar + { + public: + float_to_uchar_impl(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + + +#endif /* INCLUDED_FLOAT_TO_UCHAR_IMPL_H */ diff --git a/gr-blocks/python/qa_type_conversions.py b/gr-blocks/python/qa_type_conversions.py index d41fe690d..a97203566 100755 --- a/gr-blocks/python/qa_type_conversions.py +++ b/gr-blocks/python/qa_type_conversions.py @@ -231,6 +231,16 @@ class test_type_conversions(gr_unittest.TestCase): self.tb.run() self.assertEqual(expected_data, dst.data()) + def test_float_to_uchar(self): + src_data = (1.0, -2.0, 3.0, -4.0, 256.0) + expected_data = (1, 0, 3, 0, 255) + src = gr.vector_source_f(src_data) + op = blocks_swig.float_to_uchar() + dst = gr.vector_sink_b() + self.tb.connect(src, op, dst) + self.tb.run() + self.assertEqual(expected_data, dst.data()) + if __name__ == '__main__': gr_unittest.run(test_type_conversions, "test_type_conversions.xml") diff --git a/gr-blocks/swig/blocks_swig.i b/gr-blocks/swig/blocks_swig.i index a0f3039bc..acf0fe26f 100644 --- a/gr-blocks/swig/blocks_swig.i +++ b/gr-blocks/swig/blocks_swig.i @@ -57,6 +57,7 @@ #include "blocks/float_to_complex.h" #include "blocks/float_to_int.h" #include "blocks/float_to_short.h" +#include "blocks/float_to_uchar.h" #include "blocks/multiply_ss.h" #include "blocks/multiply_ii.h" #include "blocks/multiply_ff.h" @@ -104,6 +105,7 @@ %include "blocks/float_to_complex.h" %include "blocks/float_to_int.h" %include "blocks/float_to_short.h" +%include "blocks/float_to_uchar.h" %include "blocks/multiply_ss.h" %include "blocks/multiply_ii.h" %include "blocks/multiply_ff.h" @@ -150,6 +152,7 @@ GR_SWIG_BLOCK_MAGIC2(blocks, float_to_char); GR_SWIG_BLOCK_MAGIC2(blocks, float_to_complex); GR_SWIG_BLOCK_MAGIC2(blocks, float_to_int); GR_SWIG_BLOCK_MAGIC2(blocks, float_to_short); +GR_SWIG_BLOCK_MAGIC2(blocks, float_to_uchar); GR_SWIG_BLOCK_MAGIC2(blocks, multiply_ss); GR_SWIG_BLOCK_MAGIC2(blocks, multiply_ii); GR_SWIG_BLOCK_MAGIC2(blocks, multiply_ff); -- cgit