Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_sig_source.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2007 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 from gnuradio import gr, gr_unittest
24 import math
25
26 class test_sig_source (gr_unittest.TestCase):
27
28     def setUp (self):
29         self.tb = gr.top_block ()
30
31     def tearDown (self):
32         self.tb = None
33
34     def test_const_f (self):
35         tb = self.tb
36         expected_result = (1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5)
37         src1 = gr.sig_source_f (1e6, gr.GR_CONST_WAVE, 0, 1.5)
38         op = gr.head (gr.sizeof_float, 10)
39         dst1 = gr.vector_sink_f ()
40         tb.connect (src1, op)
41         tb.connect (op, dst1)
42         tb.run ()
43         dst_data = dst1.data ()
44         self.assertEqual (expected_result, dst_data)
45     
46     def test_const_i (self):
47         tb = self.tb
48         expected_result = (1, 1, 1, 1)
49         src1 = gr.sig_source_i (1e6, gr.GR_CONST_WAVE, 0, 1)
50         op = gr.head (gr.sizeof_int, 4)
51         dst1 = gr.vector_sink_i ()
52         tb.connect (src1, op)
53         tb.connect (op, dst1)
54         tb.run ()
55         dst_data = dst1.data ()
56         self.assertEqual (expected_result, dst_data)
57     
58     def test_sine_f (self):
59         tb = self.tb
60         sqrt2 = math.sqrt(2) / 2
61         expected_result = (0, sqrt2, 1, sqrt2, 0, -sqrt2, -1, -sqrt2, 0)
62         src1 = gr.sig_source_f (8, gr.GR_SIN_WAVE, 1.0, 1.0)
63         op = gr.head (gr.sizeof_float, 9)
64         dst1 = gr.vector_sink_f ()
65         tb.connect (src1, op)
66         tb.connect (op, dst1)
67         tb.run ()
68         dst_data = dst1.data ()
69         self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
70
71     def test_cosine_f (self):
72         tb = self.tb
73         sqrt2 = math.sqrt(2) / 2
74         expected_result = (1, sqrt2, 0, -sqrt2, -1, -sqrt2, 0, sqrt2, 1)
75         src1 = gr.sig_source_f (8, gr.GR_COS_WAVE, 1.0, 1.0)
76         op = gr.head (gr.sizeof_float, 9)
77         dst1 = gr.vector_sink_f ()
78         tb.connect (src1, op)
79         tb.connect (op, dst1)
80         tb.run ()
81         dst_data = dst1.data ()
82         self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
83         
84     def test_sqr_c (self):
85         tb = self.tb                                            #arg6 is a bit before -PI/2
86         expected_result = (1j, 1j, 0, 0, 1, 1, 1+0j, 1+1j, 1j)
87         src1 = gr.sig_source_c (8, gr.GR_SQR_WAVE, 1.0, 1.0)
88         op = gr.head (gr.sizeof_gr_complex, 9)
89         dst1 = gr.vector_sink_c ()
90         tb.connect (src1, op)
91         tb.connect (op, dst1)
92         tb.run ()
93         dst_data = dst1.data ()
94         self.assertEqual (expected_result, dst_data)
95         
96     def test_tri_c (self):
97         tb = self.tb
98         expected_result = (1+.5j, .75+.75j, .5+1j, .25+.75j, 0+.5j, .25+.25j, .5+0j, .75+.25j, 1+.5j)
99         src1 = gr.sig_source_c (8, gr.GR_TRI_WAVE, 1.0, 1.0)
100         op = gr.head (gr.sizeof_gr_complex, 9)
101         dst1 = gr.vector_sink_c ()
102         tb.connect (src1, op)
103         tb.connect (op, dst1)
104         tb.run ()
105         dst_data = dst1.data ()
106         self.assertComplexTuplesAlmostEqual (expected_result, dst_data, 5)
107         
108     def test_saw_c (self):
109         tb = self.tb
110         expected_result = (.5+.25j, .625+.375j, .75+.5j, .875+.625j, 0+.75j, .125+.875j, .25+1j, .375+.125j, .5+.25j)
111         src1 = gr.sig_source_c (8, gr.GR_SAW_WAVE, 1.0, 1.0)
112         op = gr.head (gr.sizeof_gr_complex, 9)
113         dst1 = gr.vector_sink_c ()
114         tb.connect (src1, op)
115         tb.connect (op, dst1)
116         tb.run ()
117         dst_data = dst1.data ()
118         self.assertComplexTuplesAlmostEqual (expected_result, dst_data, 5)
119     
120     def test_sqr_f (self):
121         tb = self.tb
122         expected_result = (0, 0, 0, 0, 1, 1, 1, 1, 0)
123         src1 = gr.sig_source_f (8, gr.GR_SQR_WAVE, 1.0, 1.0)
124         op = gr.head (gr.sizeof_float, 9)
125         dst1 = gr.vector_sink_f ()
126         tb.connect (src1, op)
127         tb.connect (op, dst1)
128         tb.run ()
129         dst_data = dst1.data ()
130         self.assertEqual (expected_result, dst_data)
131         
132     def test_tri_f (self):
133         tb = self.tb
134         expected_result = (1, .75, .5, .25, 0, .25, .5, .75, 1)
135         src1 = gr.sig_source_f (8, gr.GR_TRI_WAVE, 1.0, 1.0)
136         op = gr.head (gr.sizeof_float, 9)
137         dst1 = gr.vector_sink_f ()
138         tb.connect (src1, op)
139         tb.connect (op, dst1)
140         tb.run ()
141         dst_data = dst1.data ()
142         self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
143         
144     def test_saw_f (self):
145         tb = self.tb
146         expected_result = (.5, .625, .75, .875, 0, .125, .25, .375, .5)
147         src1 = gr.sig_source_f (8, gr.GR_SAW_WAVE, 1.0, 1.0)
148         op = gr.head (gr.sizeof_float, 9)
149         dst1 = gr.vector_sink_f ()
150         tb.connect (src1, op)
151         tb.connect (op, dst1)
152         tb.run ()
153         dst_data = dst1.data ()
154         self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
155
156 if __name__ == '__main__':
157     gr_unittest.main ()