3 # Copyright 2004,2007 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 3, 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., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
23 from gnuradio import gr, gr_unittest
26 class test_complex_ops (gr_unittest.TestCase):
29 self.tb = gr.top_block ()
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.tb.connect (src, op)
41 self.tb.connect (op, dst)
42 self.tb.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.tb.connect (src, op)
55 self.tb.connect ((op, 0), dst0)
56 self.tb.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.tb.connect (src, op)
70 self.tb.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.tb.connect (src, op)
82 self.tb.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.tb.connect (src, op)
94 self.tb.connect (op, dst)
96 actual_result = dst.data ()
97 self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
99 def test_complex_to_mag_squared (self):
100 src_data = (0, 1, -1, 3+4j, -3-4j, -3+4j)
101 expected_result = (0, 1, 1, 25, 25, 25)
102 src = gr.vector_source_c (src_data)
103 op = gr.complex_to_mag_squared ()
104 dst = gr.vector_sink_f ()
105 self.tb.connect (src, op)
106 self.tb.connect (op, dst)
108 actual_result = dst.data ()
109 self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
111 def test_complex_to_arg (self):
113 input_data = (0, pi/6, pi/4, pi/2, 3*pi/4, 7*pi/8,
114 -pi/6, -pi/4, -pi/2, -3*pi/4, -7*pi/8)
116 expected_result = (0.0, # 0
117 0.52382522821426392, # pi/6
118 0.78539806604385376, # pi/4
119 1.5707963705062866, # pi/2
120 2.3561947345733643, # 3pi/4
121 2.7491819858551025, # 7pi/8
122 -0.52382522821426392, # -pi/6
123 -0.78539806604385376, # -pi/4
124 -1.5707963705062866, # -pi/2
125 -2.3561947345733643, # -3pi/4
126 -2.7491819858551025) # -7pi/8
128 src_data = tuple ([math.cos (x) + math.sin (x) * 1j for x in input_data])
129 src = gr.vector_source_c (src_data)
130 op = gr.complex_to_arg ()
131 dst = gr.vector_sink_f ()
132 self.tb.connect (src, op)
133 self.tb.connect (op, dst)
135 actual_result = dst.data ()
137 self.assertFloatTuplesAlmostEqual (expected_result, actual_result, 5)
140 if __name__ == '__main__':