2 # Copyright 2008, 2009 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
22 ##################################################
23 # conditional disconnections of wx flow graph
24 ##################################################
26 from gnuradio import gr
28 class wxgui_hb(object):
30 The wxgui hier block helper/wrapper class:
31 A hier block should inherit from this class to make use of the wxgui connect method.
32 To use, call wxgui_connect in place of regular connect; self.win must be defined.
33 The implementation will conditionally enable the copy block after the source (self).
34 This condition depends on weather or not the window is visible with the parent notebooks.
35 This condition will be re-checked on every ui update event.
38 def wxgui_connect(self, *points):
40 Use wxgui connect when the first point is the self source of the hb.
41 The win property of this object should be set to the wx window.
42 When this method tries to connect self to the next point,
43 it will conditionally make this connection based on the visibility state.
44 All other points will be connected normally.
47 assert points[0] == self or points[0][0] == self
48 copy = gr.copy(self._hb.input_signature().sizeof_stream_item(0))
49 handler = self._handler_factory(copy.set_enabled)
50 handler(False) #initially disable the copy block
51 self._bind_to_visible_event(win=self.win, handler=handler)
53 points.insert(1, copy) #insert the copy block into the chain
54 except (AssertionError, IndexError): pass
55 self.connect(*points) #actually connect the blocks
58 def _handler_factory(handler):
60 Create a function that will cache the visibility flag,
61 and only call the handler when that flag changes.
62 @param handler the function to call on a change
63 @return a function of 1 argument
66 def callback(visible):
67 if cache[0] == visible: return
69 #print visible, handler
74 def _bind_to_visible_event(win, handler):
76 Bind a handler to a window when its visibility changes.
77 Specifically, call the handler when the window visibility changes.
78 This condition is checked on every update ui event.
79 @param win the wx window
80 @param handler a function of 1 param
82 #is the window visible in the hierarchy
83 def is_wx_window_visible(my_win):
85 parent = my_win.GetParent()
86 if not parent: return True #reached the top of the hierarchy
87 #if we are hidden, then finish, otherwise keep traversing up
88 if isinstance(parent, wx.Notebook) and parent.GetCurrentPage() != my_win: return False
90 #call the handler, the arg is shown or not
91 def handler_factory(my_win, my_handler):
93 my_handler(is_wx_window_visible(my_win))
94 evt.Skip() #skip so all bound handlers are called
96 handler = handler_factory(win, handler)
97 #bind the handler to all the parent notebooks
98 win.Bind(wx.EVT_UPDATE_UI, handler)
100 ##################################################
102 ##################################################
104 #A macro to apply an index to a key
105 index_key = lambda key, i: "%s_%d"%(key, i+1)
107 def _register_access_method(destination, controller, key):
109 Helper function for register access methods.
110 This helper creates distinct set and get methods for each key
111 and adds them to the destination object.
113 def set(value): controller[key] = value
114 setattr(destination, 'set_'+key, set)
115 def get(): return controller[key]
116 setattr(destination, 'get_'+key, get)
118 def register_access_methods(destination, controller):
120 Register setter and getter functions in the destination object for all keys in the controller.
121 @param destination the object to get new setter and getter methods
122 @param controller the pubsub controller
124 for key in controller.keys(): _register_access_method(destination, controller, key)
126 ##################################################
127 # Input Watcher Thread
128 ##################################################
129 from gnuradio import gru
131 class input_watcher(gru.msgq_runner):
133 Input watcher thread runs forever.
134 Read messages from the message queue.
135 Forward messages to the message handler.
137 def __init__ (self, msgq, controller, msg_key, arg1_key='', arg2_key=''):
138 self._controller = controller
139 self._msg_key = msg_key
140 self._arg1_key = arg1_key
141 self._arg2_key = arg2_key
142 gru.msgq_runner.__init__(self, msgq, self.handle_msg)
144 def handle_msg(self, msg):
145 if self._arg1_key: self._controller[self._arg1_key] = msg.arg1()
146 if self._arg2_key: self._controller[self._arg2_key] = msg.arg2()
147 self._controller[self._msg_key] = msg.to_string()
150 ##################################################
152 ##################################################
158 Get the exponent of the number in base 10.
159 @param num the floating point number
160 @return the exponent as an integer
162 if num == 0: return 0
163 return int(math.floor(math.log10(abs(num))))
165 def get_clean_num(num):
167 Get the closest clean number match to num with bases 1, 2, 5.
168 @param num the number
169 @return the closest number
171 if num == 0: return 0
172 sign = num > 0 and 1 or -1
174 nums = numpy.array((1, 2, 5, 10))*(10**exp)
175 return sign*nums[numpy.argmin(numpy.abs(nums - abs(num)))]
177 def get_clean_incr(num):
179 Get the next higher clean number with bases 1, 2, 5.
180 @param num the number
181 @return the next higher number
183 num = get_clean_num(num)
185 coeff = int(round(num/10**exp))
195 def get_clean_decr(num):
197 Get the next lower clean number with bases 1, 2, 5.
198 @param num the number
199 @return the next lower number
201 num = get_clean_num(num)
203 coeff = int(round(num/10**exp))
213 def get_min_max(samples):
215 Get the minimum and maximum bounds for an array of samples.
216 @param samples the array of real values
217 @return a tuple of min, max
220 mean = numpy.average(samples)
221 std = numpy.std(samples)
222 fft = numpy.abs(numpy.fft.fft(samples - mean))
223 envelope = 2*numpy.max(fft)/len(samples)
224 ampl = max(std, envelope) or 0.1
225 return mean - factor*ampl, mean + factor*ampl
227 def get_min_max_fft(fft_samps):
229 Get the minimum and maximum bounds for an array of fft samples.
230 @param samples the array of real values
231 @return a tuple of min, max
233 #get the peak level (max of the samples)
234 peak_level = numpy.max(fft_samps)
235 #separate noise samples
236 noise_samps = numpy.sort(fft_samps)[:len(fft_samps)/2]
238 noise_floor = numpy.average(noise_samps)
239 #get the noise deviation
240 noise_dev = numpy.std(noise_samps)
241 #determine the maximum and minimum levels
242 max_level = peak_level
243 min_level = noise_floor - abs(2*noise_dev)
244 return min_level, max_level