probe hier wrappers
[debian/gnuradio] / grc / src / grc_gnuradio / blks2 / probe.py
1 #
2 # Copyright 2008 Free Software Foundation, Inc.
3 #
4 # This file is part of GNU Radio
5 #
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10 #
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20 #
21
22 from gnuradio import gr
23 import threading
24 import numpy
25 import time
26
27 class _probe_base(gr.hier_block2, threading.Thread):
28         """
29         A hier2 block with float output and probe input.
30         The thread polls the prope for values and writes to a message source.
31         """
32
33         def __init__(self, probe_block, probe_callback, probe_rate):
34                 #init hier block
35                 gr.hier_block2.__init__(
36                         self, 'probe',
37                         gr.io_signature(1, 1, probe_block.input_signature().sizeof_stream_items()[0]),
38                         gr.io_signature(1, 1, gr.sizeof_float),
39                 )
40                 self._probe_callback = probe_callback
41                 self.set_probe_rate(probe_rate)
42                 #create message source
43                 message_source = gr.message_source(gr.sizeof_float, 1)
44                 self._msgq = message_source.msgq()
45                 #connect
46                 self.connect(self, probe_block)
47                 self.connect(message_source, self)
48                 #setup thread
49                 threading.Thread.__init__(self)
50                 self.setDaemon(True)
51                 self.start()
52
53         def run(self):
54                 """
55                 Infinite polling loop.
56                 """
57                 while True:
58                         time.sleep(1.0/self._probe_rate)
59                         arr = numpy.array(self._probe_callback(), numpy.float32)
60                         msg = gr.message_from_string(arr.tostring(), 0, gr.sizeof_float, 1)
61                         self._msgq.insert_tail(msg)
62
63         def set_probe_rate(self, probe_rate):
64                 self._probe_rate = probe_rate
65
66 #######################################################################################
67 ## Probe: Average Magnitude Squared
68 #######################################################################################
69 class _probe_avg_mag_sqrd_base(_probe_base):
70         def __init__(self, threshold, alpha, probe_rate):
71                 #create block
72                 probe_block = self._probe_block_contructor[0](threshold, alpha)
73                 #forward callbacks
74                 self.set_alpha = probe_block.set_alpha
75                 self.set_threshold = probe_block.set_threshold
76                 #init
77                 _probe_base.__init__(self, probe_block, probe_block.level, probe_rate)
78
79 class probe_avg_mag_sqrd_c(_probe_avg_mag_sqrd_base): _probe_block_contructor = (gr.probe_avg_mag_sqrd_c,)
80 class probe_avg_mag_sqrd_f(_probe_avg_mag_sqrd_base): _probe_block_contructor = (gr.probe_avg_mag_sqrd_f,)
81
82 #######################################################################################
83 ## Probe: Density
84 #######################################################################################
85 class probe_density_b(_probe_base):
86         def __init__(self, alpha, probe_rate):
87                 #create block
88                 probe_block = gr.probe_density_b(alpha)
89                 #forward callbacks
90                 self.set_alpha = probe_block.set_alpha
91                 #init
92                 _probe_base.__init__(self, probe_block, probe_block.density, probe_rate)
93
94 #######################################################################################
95 ## Probe: MPSK SNR
96 #######################################################################################
97 class probe_mpsk_snr_c(_probe_base):
98         def __init__(self, type, alpha, probe_rate):
99                 """
100                 Type can be "snr", "signal_mean", or "noise_variance" 
101                 """
102                 #create block
103                 probe_block = gr.probe_mpsk_snr_c(alpha)
104                 #forward callbacks
105                 self.set_alpha = probe_block.set_alpha
106                 #init
107                 _probe_base.__init__(self, probe_block, getattr(probe_block, type), probe_rate)