3 # Copyright 2004 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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 2, or (at your option)
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.
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., 59 Temple Place - Suite 330,
20 # Boston, MA 02111-1307, USA.
23 from gnuradio import gr, gr_unittest
26 class test_complex_ops (gr_unittest.TestCase):
29 self.fg = gr.flow_graph ()
34 def test_complex_to_float_1 (self):
35 src_data = (0, 1, -1, 3+4j, -3-4j, -3+4j)
36 expected_result = (0, 1, -1, 3, -3, -3)
37 src = gr.vector_source_c (src_data)
38 op = gr.complex_to_float ()
39 dst = gr.vector_sink_f ()
40 self.fg.connect (src, op)
41 self.fg.connect (op, dst)
42 self.fg.run () # run the graph and wait for it to finish
43 actual_result = dst.data () # fetch the contents of the sink
44 self.assertFloatTuplesAlmostEqual (expected_result, actual_result)
46 def test_complex_to_float_2 (self):
47 src_data = (0, 1, -1, 3+4j, -3-4j, -3+4j)
48 expected_result0 = (0, 1, -1, 3, -3, -3)
49 expected_result1 = (0, 0, 0, 4, -4, 4)
50 src = gr.vector_source_c (src_data)
51 op = gr.complex_to_float ()
52 dst0 = gr.vector_sink_f ()
53 dst1 = gr.vector_sink_f ()
54 self.fg.connect (src, op)
55 self.fg.connect ((op, 0), dst0)
56 self.fg.connect ((op, 1), dst1)
58 actual_result = dst0.data ()
59 self.assertFloatTuplesAlmostEqual (expected_result0, actual_result)
60 actual_result = dst1.data ()
61 self.assertFloatTuplesAlmostEqual (expected_result1, actual_result)
63 def test_complex_to_real (self):
64 src_data = (0, 1, -1, 3+4j, -3-4j, -3+4j)
65 expected_result = (0, 1, -1, 3, -3, -3)
66 src = gr.vector_source_c (src_data)
67 op = gr.complex_to_real ()
68 dst = gr.vector_sink_f ()
69 self.fg.connect (src, op)
70 self.fg.connect (op, dst)
72 actual_result = dst.data ()
73 self.assertFloatTuplesAlmostEqual (expected_result, actual_result)
75 def test_complex_to_imag (self):
76 src_data = (0, 1, -1, 3+4j, -3-4j, -3+4j)
77 expected_result = (0, 0, 0, 4, -4, 4)
78 src = gr.vector_source_c (src_data)
79 op = gr.complex_to_imag ()
80 dst = gr.vector_sink_f ()
81 self.fg.connect (src, op)
82 self.fg.connect (op, dst)
84 actual_result = dst.data ()
85 self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
87 def test_complex_to_mag (self):
88 src_data = (0, 1, -1, 3+4j, -3-4j, -3+4j)
89 expected_result = (0, 1, 1, 5, 5, 5)
90 src = gr.vector_source_c (src_data)
91 op = gr.complex_to_mag ()
92 dst = gr.vector_sink_f ()
93 self.fg.connect (src, op)
94 self.fg.connect (op, dst)
96 actual_result = dst.data ()
97 self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
99 def test_complex_to_arg (self):
101 expected_result = (0, pi/6, pi/4, pi/2, 3*pi/4, 7*pi/8,
102 -pi/6, -pi/4, -pi/2, -3*pi/4, -7*pi/8)
103 src_data = tuple ([math.cos (x) + math.sin (x) * 1j for x in expected_result])
104 src = gr.vector_source_c (src_data)
105 op = gr.complex_to_arg ()
106 dst = gr.vector_sink_f ()
107 self.fg.connect (src, op)
108 self.fg.connect (op, dst)
110 actual_result = dst.data ()
111 self.assertFloatTuplesAlmostEqual (expected_result, actual_result, 5)
114 if __name__ == '__main__':