summaryrefslogtreecommitdiff
path: root/gr-blocks/python/blocks/qa_bin_statistics.py
blob: e6bdfaca5b99071d4306523e5ac98b7d93d33bfc (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python
#
# Copyright 2006,2007,2010,2013 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.
#

"""
Note: There has been an issue with this block in the past, see Issue
#199. This test is being enabled only on the 'next' branch for version
v3.7 for now. TWR
"""

from __future__ import print_function

import struct

import six

from gnuradio import gr, gr_unittest, blocks


class counter(gr.feval_dd):
    def __init__(self, step_size=1):
        gr.feval_dd.__init__(self)
        self.step_size = step_size
        self.count = 0

    def eval(self, input):
        #print "eval: self.count =", self.count
        t = self.count
        self.count = self.count + self.step_size
        return t


class counter3(gr.feval_dd):
    def __init__(self, f, step_size):
        gr.feval_dd.__init__(self)
        self.f = f
        self.step_size = step_size
        self.count = 0

    def eval(self, input):
        try:
            #print "eval: self.count =", self.count
            t = self.count
            self.count = self.count + self.step_size
            self.f(self.count)
        except Exception as e:
            print("Exception: ", e)
        return t

def foobar3(new_t):
    #print "foobar3: new_t =", new_t
    pass


class counter4(gr.feval_dd):
    def __init__(self, obj_instance, step_size):
        gr.feval_dd.__init__(self)
        self.obj_instance = obj_instance
        self.step_size = step_size
        self.count = 0

    def eval(self, input):
        try:
            #print "eval: self.count =", self.count
            t = self.count
            self.count = self.count + self.step_size
            self.obj_instance.foobar4(self.count)
        except Exception as e:
            print("Exception: ", e)
        return t


class parse_msg(object):
    def __init__(self, msg):
        self.center_freq = msg.arg1()
        self.vlen = int(msg.arg2())
        assert(msg.length() == self.vlen * gr.sizeof_float)
        self.data = struct.unpack(b'%df' % self.vlen, six.b(msg.to_string()))


class test_bin_statistics(gr_unittest.TestCase):

    def setUp(self):
        self.tb = gr.top_block ()

    def tearDown(self):
        self.tb = None

    def test_001(self):
        vlen = 4
        tune = counter(1)
        tune_delay = 0
        dwell_delay = 1
        msgq = gr.msg_queue()

        src_data = tuple([float(x) for x in
                          ( 1,  2,  3,  4,
                            5,  6,  7,  8,
                            9, 10, 11, 12,
                            13, 14, 15, 16
                            )])

        expected_results = tuple([float(x) for x in
                                  ( 1,  2,  3,  4,
                                    5,  6,  7,  8,
                                    9, 10, 11, 12,
                                    13, 14, 15, 16
                                    )])

        src = blocks.vector_source_f(src_data, False)
        s2v = blocks.stream_to_vector(gr.sizeof_float, vlen)
        stats = blocks.bin_statistics_f(vlen, msgq, tune, tune_delay, dwell_delay)
        self.tb.connect(src, s2v, stats)
        self.tb.run()
        self.assertEqual(4, msgq.count())
        for i in range(4):
            m = parse_msg(msgq.delete_head())
            #print "m =", m.center_freq, m.data
            self.assertEqual(expected_results[vlen*i:vlen*i + vlen], m.data)

    def test_002(self):
        vlen = 4
        tune = counter(1)
        tune_delay = 1
        dwell_delay = 2
        msgq = gr.msg_queue()

        src_data = tuple([float(x) for x in
                          ( 1,  2,  3,  4,
                            9,  6, 11,  8,
                            5, 10,  7, 12,
                            13, 14, 15, 16
                            )])

        expected_results = tuple([float(x) for x in
                                  ( 9, 10, 11, 12)])

        src = blocks.vector_source_f(src_data, False)
        s2v = blocks.stream_to_vector(gr.sizeof_float, vlen)
        stats = blocks.bin_statistics_f(vlen, msgq, tune, tune_delay, dwell_delay)
        self.tb.connect(src, s2v, stats)
        self.tb.run()
        self.assertEqual(1, msgq.count())
        for i in range(1):
            m = parse_msg(msgq.delete_head())
            #print "m =", m.center_freq, m.data
            self.assertEqual(expected_results[vlen*i:vlen*i + vlen], m.data)



    def test_003(self):
        vlen = 4
        tune = counter3(foobar3, 1)
        tune_delay = 1
        dwell_delay = 2
        msgq = gr.msg_queue()

        src_data = tuple([float(x) for x in
                          ( 1,  2,  3,  4,
                            9,  6, 11,  8,
                            5, 10,  7, 12,
                            13, 14, 15, 16
                            )])

        expected_results = tuple([float(x) for x in
                                  ( 9, 10, 11, 12)])

        src = blocks.vector_source_f(src_data, False)
        s2v = blocks.stream_to_vector(gr.sizeof_float, vlen)
        stats = blocks.bin_statistics_f(vlen, msgq, tune, tune_delay, dwell_delay)
        self.tb.connect(src, s2v, stats)
        self.tb.run()
        self.assertEqual(1, msgq.count())
        for i in range(1):
            m = parse_msg(msgq.delete_head())
            #print "m =", m.center_freq, m.data
            self.assertEqual(expected_results[vlen*i:vlen*i + vlen], m.data)

    def foobar4(self, new_t):
        #print "foobar4: new_t =", new_t
        pass

    def test_004(self):
        vlen = 4
        tune = counter4(self, 1)
        tune_delay = 1
        dwell_delay = 2
        msgq = gr.msg_queue()

        src_data = tuple([float(x) for x in
                          ( 1,  2,  3,  4,
                            9,  6, 11,  8,
                            5, 10,  7, 12,
                            13, 14, 15, 16
                            )])

        expected_results = tuple([float(x) for x in
                                  ( 9, 10, 11, 12)])

        src = blocks.vector_source_f(src_data, False)
        s2v = blocks.stream_to_vector(gr.sizeof_float, vlen)
        stats = blocks.bin_statistics_f(vlen, msgq, tune, tune_delay, dwell_delay)
        self.tb.connect(src, s2v, stats)
        self.tb.run()
        self.assertEqual(1, msgq.count())
        for i in range(1):
            m = parse_msg(msgq.delete_head())
            #print "m =", m.center_freq, m.data
            self.assertEqual(expected_results[vlen*i:vlen*i + vlen], m.data)


if __name__ == '__main__':
   gr_unittest.run(test_bin_statistics, "test_bin_statistics.xml")