Skip to content

Commit 1b4fb3f

Browse files
committedOct 11, 2015
blocks: Add Complex to IChar block
1 parent b1883cb commit 1b4fb3f

8 files changed

+224
-0
lines changed
 

‎gr-blocks/grc/blocks_block_tree.xml

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
<name>Type Converters</name>
200200
<block>blocks_char_to_float</block>
201201
<block>blocks_char_to_short</block>
202+
<block>blocks_complex_to_interleaved_char</block>
202203
<block>blocks_complex_to_interleaved_short</block>
203204
<block>blocks_complex_to_float</block>
204205
<block>blocks_complex_to_imag</block>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
###################################################
4+
##Complex to Interleaved Char:
5+
###################################################
6+
-->
7+
<block>
8+
<name>Complex To IChar</name>
9+
<key>blocks_complex_to_interleaved_char</key>
10+
<import>from gnuradio import blocks</import>
11+
<make>blocks.complex_to_interleaved_char($vector_output)</make>
12+
<param>
13+
<name>Vector Output</name>
14+
<key>vector_output</key>
15+
<value>False</value>
16+
<type>enum</type>
17+
<option>
18+
<name>No</name>
19+
<key>False</key>
20+
<opt>vlen:1</opt>
21+
</option>
22+
<option>
23+
<name>Yes</name>
24+
<key>True</key>
25+
<opt>vlen:2</opt>
26+
</option>
27+
</param>
28+
<sink>
29+
<name>in</name>
30+
<type>complex</type>
31+
</sink>
32+
<source>
33+
<name>out</name>
34+
<type>byte</type>
35+
<vlen>$vector_output.vlen</vlen>
36+
</source>
37+
</block>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* -*- c++ -*- */
2+
/*
3+
* Copyright 2015 Free Software Foundation, Inc.
4+
*
5+
* This file is part of GNU Radio
6+
*
7+
* GNU Radio is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3, or (at your option)
10+
* any later version.
11+
*
12+
* GNU Radio is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with GNU Radio; see the file COPYING. If not, write to
19+
* the Free Software Foundation, Inc., 51 Franklin Street,
20+
* Boston, MA 02110-1301, USA.
21+
*/
22+
23+
#ifndef INCLUDED_BLOCKS_COMPLEX_TO_INTERLEAVED_CHAR_H
24+
#define INCLUDED_BLOCKS_COMPLEX_TO_INTERLEAVED_CHAR_H
25+
26+
#include <gnuradio/blocks/api.h>
27+
#include <gnuradio/sync_interpolator.h>
28+
29+
namespace gr {
30+
namespace blocks {
31+
32+
/*!
33+
* \brief Convert stream of complex to a stream of interleaved chars.
34+
* \ingroup type_converters_blk
35+
*
36+
* \details
37+
* The output stream contains chars with twice as many output
38+
* items as input items. For every complex input item, we produce
39+
* two output chars that contain the real part and imaginary part
40+
* converted to chars:
41+
*
42+
* \li output[0][n] = static_cast<char>(input[0][m].real());
43+
* \li output[0][n+1] = static_cast<char>(input[0][m].imag());
44+
*/
45+
class BLOCKS_API complex_to_interleaved_char : virtual public sync_interpolator
46+
{
47+
public:
48+
// gr::blocks::complex_to_interleaved_char::sptr
49+
typedef boost::shared_ptr<complex_to_interleaved_char> sptr;
50+
51+
/*!
52+
* Build a complex to interleaved chars block.
53+
*/
54+
static sptr make(bool vector=false);
55+
};
56+
57+
} /* namespace blocks */
58+
} /* namespace gr */
59+
60+
#endif /* INCLUDED_BLOCKS_COMPLEX_TO_INTERLEAVED_CHAR_H */

‎gr-blocks/lib/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ list(APPEND gr_blocks_sources
9696
char_to_float_impl.cc
9797
char_to_short_impl.cc
9898
check_lfsr_32k_s_impl.cc
99+
complex_to_interleaved_char_impl.cc
99100
complex_to_interleaved_short_impl.cc
100101
complex_to_float_impl.cc
101102
complex_to_real_impl.cc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* -*- c++ -*- */
2+
/*
3+
* Copyright 2015 Free Software Foundation, Inc.
4+
*
5+
* This file is part of GNU Radio
6+
*
7+
* GNU Radio is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3, or (at your option)
10+
* any later version.
11+
*
12+
* GNU Radio is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with GNU Radio; see the file COPYING. If not, write to
19+
* the Free Software Foundation, Inc., 51 Franklin Street,
20+
* Boston, MA 02110-1301, USA.
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
#include "config.h"
25+
#endif
26+
27+
#include "complex_to_interleaved_char_impl.h"
28+
#include <gnuradio/io_signature.h>
29+
30+
namespace gr {
31+
namespace blocks {
32+
33+
complex_to_interleaved_char::sptr complex_to_interleaved_char::make(bool vector)
34+
{
35+
return gnuradio::get_initial_sptr(new complex_to_interleaved_char_impl(vector));
36+
}
37+
38+
complex_to_interleaved_char_impl::complex_to_interleaved_char_impl(bool vector)
39+
: sync_interpolator("complex_to_interleaved_char",
40+
io_signature::make (1, 1, sizeof(gr_complex)),
41+
io_signature::make (1, 1, vector?2*sizeof(char):sizeof(char)),
42+
vector?1:2),
43+
d_vector(vector)
44+
{
45+
}
46+
47+
int
48+
complex_to_interleaved_char_impl::work(int noutput_items,
49+
gr_vector_const_void_star &input_items,
50+
gr_vector_void_star &output_items)
51+
{
52+
const gr_complex *in = (const gr_complex *) input_items[0];
53+
char *out = (char *) output_items[0];
54+
55+
int npairs = (d_vector?noutput_items:noutput_items/2);
56+
for (int i = 0; i < npairs; i++){
57+
*out++ = (char) lrintf(in[i].real()); // FIXME saturate?
58+
*out++ = (char) lrintf(in[i].imag());
59+
}
60+
61+
return noutput_items;
62+
}
63+
64+
} /* namespace blocks */
65+
}/* namespace gr */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* -*- c++ -*- */
2+
/*
3+
* Copyright 2015 Free Software Foundation, Inc.
4+
*
5+
* This file is part of GNU Radio
6+
*
7+
* GNU Radio is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3, or (at your option)
10+
* any later version.
11+
*
12+
* GNU Radio is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with GNU Radio; see the file COPYING. If not, write to
19+
* the Free Software Foundation, Inc., 51 Franklin Street,
20+
* Boston, MA 02110-1301, USA.
21+
*/
22+
23+
#ifndef INCLUDED_COMPLEX_TO_INTERLEAVED_CHAR_IMPL_H
24+
#define INCLUDED_COMPLEX_TO_INTERLEAVED_CHAR_IMPL_H
25+
26+
#include <gnuradio/blocks/complex_to_interleaved_char.h>
27+
28+
namespace gr {
29+
namespace blocks {
30+
31+
class BLOCKS_API complex_to_interleaved_char_impl : public complex_to_interleaved_char
32+
{
33+
private:
34+
bool d_vector;
35+
public:
36+
complex_to_interleaved_char_impl(bool vector);
37+
38+
int work(int noutput_items,
39+
gr_vector_const_void_star &input_items,
40+
gr_vector_void_star &output_items);
41+
};
42+
43+
} /* namespace blocks */
44+
} /* namespace gr */
45+
46+
47+
#endif /* INCLUDED_COMPLEX_TO_INTERLEAVED_CHAR_IMPL_H */

‎gr-blocks/python/blocks/qa_type_conversions.py

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ def test_char_to_short(self):
6262
self.tb.run()
6363
self.assertEqual(expected_data, dst.data())
6464

65+
def test_complex_to_interleaved_char(self):
66+
src_data = (1+2j, 3+4j, 5+6j, 7+8j, 9+10j)
67+
expected_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
68+
src = blocks.vector_source_c(src_data)
69+
op = blocks.complex_to_interleaved_char()
70+
dst = blocks.vector_sink_b()
71+
self.tb.connect(src, op, dst)
72+
self.tb.run()
73+
self.assertEqual(expected_data, dst.data())
74+
6575
def test_complex_to_interleaved_short(self):
6676
src_data = (1+2j, 3+4j, 5+6j, 7+8j, 9+10j)
6777
expected_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

‎gr-blocks/swig/blocks_swig2.i

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "gnuradio/blocks/char_to_float.h"
6363
#include "gnuradio/blocks/char_to_short.h"
6464
#include "gnuradio/blocks/check_lfsr_32k_s.h"
65+
#include "gnuradio/blocks/complex_to_interleaved_char.h"
6566
#include "gnuradio/blocks/complex_to_interleaved_short.h"
6667
#include "gnuradio/blocks/complex_to_float.h"
6768
#include "gnuradio/blocks/complex_to_real.h"
@@ -108,6 +109,7 @@
108109
%include "gnuradio/blocks/burst_tagger.h"
109110
%include "gnuradio/blocks/char_to_short.h"
110111
%include "gnuradio/blocks/check_lfsr_32k_s.h"
112+
%include "gnuradio/blocks/complex_to_interleaved_char.h"
111113
%include "gnuradio/blocks/complex_to_interleaved_short.h"
112114
%include "gnuradio/blocks/complex_to_float.h"
113115
%include "gnuradio/blocks/complex_to_real.h"
@@ -153,6 +155,7 @@ GR_SWIG_BLOCK_MAGIC2(blocks, burst_tagger);
153155
GR_SWIG_BLOCK_MAGIC2(blocks, char_to_float);
154156
GR_SWIG_BLOCK_MAGIC2(blocks, char_to_short);
155157
GR_SWIG_BLOCK_MAGIC2(blocks, check_lfsr_32k_s);
158+
GR_SWIG_BLOCK_MAGIC2(blocks, complex_to_interleaved_char);
156159
GR_SWIG_BLOCK_MAGIC2(blocks, complex_to_interleaved_short);
157160
GR_SWIG_BLOCK_MAGIC2(blocks, complex_to_float);
158161
GR_SWIG_BLOCK_MAGIC2(blocks, complex_to_real);

0 commit comments

Comments
 (0)
Please sign in to comment.