1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#!/usr/bin/env python
#
# Copyright 2007,2010,2012 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
from gnuradio import gr, gr_unittest, analog, blocks
import numpy
class test_fastnoise_source(gr_unittest.TestCase):
def setUp (self):
self.num = 2**22
self.num_items = 10**6
self.default_args = {"samples": self.num, "seed": 43, "ampl": 1}
def tearDown (self):
pass
def run_test_real(self, form):
""" Run test case with float input/output
"""
tb = gr.top_block()
src = analog.fastnoise_source_f(type=form, **self.default_args)
head = blocks.head(nitems=self.num_items, sizeof_stream_item=gr.sizeof_float)
sink = blocks.vector_sink_f()
tb.connect(src, head, sink)
tb.run()
return numpy.array(sink.data())
def run_test_complex(self, form):
""" Run test case with complex input/output
"""
tb = gr.top_block()
src = analog.fastnoise_source_c(type=form, **self.default_args)
head = blocks.head(nitems=self.num_items, sizeof_stream_item=gr.sizeof_gr_complex)
sink = blocks.vector_sink_c()
tb.connect(src, head, sink)
tb.run()
return numpy.array(sink.data())
def test_001_real_uniform_moments(self):
data = self.run_test_real(analog.GR_UNIFORM)
self.assertAlmostEqual(min(data), -1, places=4)
self.assertAlmostEqual(max(data), 1, places=4)
# mean, variance
self.assertAlmostEqual(data.mean(), 0, places=2)
self.assertAlmostEqual(data.var(), (1-(-1))**2./12, places=3)
def test_001_real_gaussian_moments(self):
data = self.run_test_real(analog.GR_GAUSSIAN)
# mean, variance
self.assertAlmostEqual(data.mean(), 0, places=2)
self.assertAlmostEqual(data.var(), 1, places=2)
def test_001_real_laplacian_moments(self):
data = self.run_test_real(analog.GR_LAPLACIAN)
# mean, variance
self.assertAlmostEqual(data.mean(), 0, places=2)
self.assertAlmostEqual(data.var(), 2, places=2)
def test_001_complex_uniform_moments(self):
data = self.run_test_complex(analog.GR_UNIFORM)
# mean, variance
self.assertAlmostEqual(data.real.mean(), 0, places=2)
self.assertAlmostEqual(data.real.var(), 0.5*(1-(-1))**2./12, places=3)
self.assertAlmostEqual(data.imag.mean(), 0, places=2)
self.assertAlmostEqual(data.imag.var(), 0.5*(1-(-1))**2./12, places=3)
def test_001_complex_gaussian_moments(self):
data = self.run_test_complex(analog.GR_GAUSSIAN)
# mean, variance
self.assertAlmostEqual(data.real.mean(), 0, places=2)
self.assertAlmostEqual(data.real.var(), 0.5, places=2)
self.assertAlmostEqual(data.imag.mean(), 0, places=2)
self.assertAlmostEqual(data.imag.var(), 0.5, places=2)
def test_002_reproducibility(self):
data1 = self.run_test_real(analog.GR_UNIFORM)
data2 = self.run_test_real(analog.GR_UNIFORM)
# It's pseudoramdo thus must be equal
self.assertTrue(numpy.array_equal(data1, data2))
data1 = self.run_test_real(analog.GR_GAUSSIAN)
data2 = self.run_test_real(analog.GR_GAUSSIAN)
self.assertTrue(numpy.array_equal(data1, data2))
if __name__ == '__main__':
gr_unittest.run(test_fastnoise_source, "test_fastnoise_source.xml")
|