Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_sig_source.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_sig_source (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_const_f (self):
35         fg = self.fg
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         fg.connect (src1, op)
41         fg.connect (op, dst1)
42         fg.run ()
43         dst_data = dst1.data ()
44         self.assertEqual (expected_result, dst_data)
45     
46     def test_const_i (self):
47         fg = self.fg
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         fg.connect (src1, op)
53         fg.connect (op, dst1)
54         fg.run ()
55         dst_data = dst1.data ()
56         self.assertEqual (expected_result, dst_data)
57     
58     def test_sine_f (self):
59         fg = self.fg
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         fg.connect (src1, op)
66         fg.connect (op, dst1)
67         fg.run ()
68         dst_data = dst1.data ()
69         self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
70
71     def test_cosine_f (self):
72         fg = self.fg
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         fg.connect (src1, op)
79         fg.connect (op, dst1)
80         fg.run ()
81         dst_data = dst1.data ()
82         self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
83
84 if __name__ == '__main__':
85     gr_unittest.main ()