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_sig_source (gr_unittest.TestCase):
29 self.tb = gr.top_block ()
34 def test_const_f (self):
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 ()
43 dst_data = dst1.data ()
44 self.assertEqual (expected_result, dst_data)
46 def test_const_i (self):
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 ()
55 dst_data = dst1.data ()
56 self.assertEqual (expected_result, dst_data)
58 def test_sine_f (self):
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 ()
68 dst_data = dst1.data ()
69 self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
71 def test_cosine_f (self):
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 ()
81 dst_data = dst1.data ()
82 self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
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 ()
93 dst_data = dst1.data ()
94 self.assertEqual (expected_result, dst_data)
96 def test_tri_c (self):
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)
105 dst_data = dst1.data ()
106 self.assertComplexTuplesAlmostEqual (expected_result, dst_data, 5)
108 def test_saw_c (self):
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)
117 dst_data = dst1.data ()
118 self.assertComplexTuplesAlmostEqual (expected_result, dst_data, 5)
120 def test_sqr_f (self):
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)
129 dst_data = dst1.data ()
130 self.assertEqual (expected_result, dst_data)
132 def test_tri_f (self):
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)
141 dst_data = dst1.data ()
142 self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
144 def test_saw_f (self):
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)
153 dst_data = dst1.data ()
154 self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 5)
156 if __name__ == '__main__':