Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_complex_to_xxx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004 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 2, 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 from gnuradio import gr, gr_unittest
24 import math
25
26 class test_complex_ops (gr_unittest.TestCase):
27
28     def setUp (self):
29         self.fg = gr.flow_graph ()
30
31     def tearDown (self):
32         self.fg = None
33
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)
45
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)
57         self.fg.run ()
58         actual_result = dst0.data ()
59         self.assertFloatTuplesAlmostEqual (expected_result0, actual_result)
60         actual_result = dst1.data ()
61         self.assertFloatTuplesAlmostEqual (expected_result1, actual_result)
62
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)
71         self.fg.run ()
72         actual_result = dst.data ()
73         self.assertFloatTuplesAlmostEqual (expected_result, actual_result)
74
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)
83         self.fg.run ()
84         actual_result = dst.data ()
85         self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
86
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)
95         self.fg.run ()
96         actual_result = dst.data ()
97         self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
98
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.fg.connect (src, op)
106         self.fg.connect (op, dst)
107         self.fg.run ()
108         actual_result = dst.data ()
109         self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
110
111     def test_complex_to_arg (self):
112         pi = math.pi
113         expected_result = (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)
115         src_data = tuple ([math.cos (x) + math.sin (x) * 1j for x in expected_result])
116         src = gr.vector_source_c (src_data)
117         op = gr.complex_to_arg ()
118         dst = gr.vector_sink_f ()
119         self.fg.connect (src, op)
120         self.fg.connect (op, dst)
121         self.fg.run ()
122         actual_result = dst.data ()
123         self.assertFloatTuplesAlmostEqual (expected_result, actual_result, 5)
124
125
126 if __name__ == '__main__':
127     gr_unittest.main ()
128