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 RUN_ALWAYS = gr.prefs().get_bool ('wxgui', 'run_always', False)
30 class wxgui_hb(object):
32 The wxgui hier block helper/wrapper class:
33 A hier block should inherit from this class to make use of the wxgui connect method.
34 To use, call wxgui_connect in place of regular connect; self.win must be defined.
35 The implementation will conditionally enable the copy block after the source (self).
36 This condition depends on weather or not the window is visible with the parent notebooks.
37 This condition will be re-checked on every ui update event.
40 def wxgui_connect(self, *points):
42 Use wxgui connect when the first point is the self source of the hb.
43 The win property of this object should be set to the wx window.
44 When this method tries to connect self to the next point,
45 it will conditionally make this connection based on the visibility state.
46 All other points will be connected normally.
49 assert points[0] == self or points[0][0] == self
50 copy = gr.copy(self._hb.input_signature().sizeof_stream_item(0))
51 handler = self._handler_factory(copy.set_enabled)
52 if RUN_ALWAYS == False:
53 handler(False) #initially disable the copy block
55 handler(True) #initially enable the copy block
56 self._bind_to_visible_event(win=self.win, handler=handler)
58 points.insert(1, copy) #insert the copy block into the chain
59 except (AssertionError, IndexError): pass
60 self.connect(*points) #actually connect the blocks
63 def _handler_factory(handler):
65 Create a function that will cache the visibility flag,
66 and only call the handler when that flag changes.
67 @param handler the function to call on a change
68 @return a function of 1 argument
71 def callback(visible):
72 if cache[0] == visible: return
74 #print visible, handler
75 if RUN_ALWAYS == False:
82 def _bind_to_visible_event(win, handler):
84 Bind a handler to a window when its visibility changes.
85 Specifically, call the handler when the window visibility changes.
86 This condition is checked on every update ui event.
87 @param win the wx window
88 @param handler a function of 1 param
90 #is the window visible in the hierarchy
91 def is_wx_window_visible(my_win):
93 parent = my_win.GetParent()
94 if not parent: return True #reached the top of the hierarchy
95 #if we are hidden, then finish, otherwise keep traversing up
96 if isinstance(parent, wx.Notebook) and parent.GetCurrentPage() != my_win: return False
98 #call the handler, the arg is shown or not
99 def handler_factory(my_win, my_handler):
101 my_handler(is_wx_window_visible(my_win))
102 evt.Skip() #skip so all bound handlers are called
104 handler = handler_factory(win, handler)
105 #bind the handler to all the parent notebooks
106 win.Bind(wx.EVT_UPDATE_UI, handler)
108 ##################################################
110 ##################################################
112 #A macro to apply an index to a key
113 index_key = lambda key, i: "%s_%d"%(key, i+1)
115 def _register_access_method(destination, controller, key):
117 Helper function for register access methods.
118 This helper creates distinct set and get methods for each key
119 and adds them to the destination object.
121 def set(value): controller[key] = value
122 setattr(destination, 'set_'+key, set)
123 def get(): return controller[key]
124 setattr(destination, 'get_'+key, get)
126 def register_access_methods(destination, controller):
128 Register setter and getter functions in the destination object for all keys in the controller.
129 @param destination the object to get new setter and getter methods
130 @param controller the pubsub controller
132 for key in controller.keys(): _register_access_method(destination, controller, key)
134 ##################################################
135 # Input Watcher Thread
136 ##################################################
137 from gnuradio import gru
139 class input_watcher(gru.msgq_runner):
141 Input watcher thread runs forever.
142 Read messages from the message queue.
143 Forward messages to the message handler.
145 def __init__ (self, msgq, controller, msg_key, arg1_key='', arg2_key=''):
146 self._controller = controller
147 self._msg_key = msg_key
148 self._arg1_key = arg1_key
149 self._arg2_key = arg2_key
150 gru.msgq_runner.__init__(self, msgq, self.handle_msg)
152 def handle_msg(self, msg):
153 if self._arg1_key: self._controller[self._arg1_key] = msg.arg1()
154 if self._arg2_key: self._controller[self._arg2_key] = msg.arg2()
155 self._controller[self._msg_key] = msg.to_string()
158 ##################################################
160 ##################################################
166 Get the exponent of the number in base 10.
167 @param num the floating point number
168 @return the exponent as an integer
170 if num == 0: return 0
171 return int(math.floor(math.log10(abs(num))))
173 def get_clean_num(num):
175 Get the closest clean number match to num with bases 1, 2, 5.
176 @param num the number
177 @return the closest number
179 if num == 0: return 0
180 sign = num > 0 and 1 or -1
182 nums = numpy.array((1, 2, 5, 10))*(10**exp)
183 return sign*nums[numpy.argmin(numpy.abs(nums - abs(num)))]
185 def get_clean_incr(num):
187 Get the next higher clean number with bases 1, 2, 5.
188 @param num the number
189 @return the next higher number
191 num = get_clean_num(num)
193 coeff = int(round(num/10**exp))
203 def get_clean_decr(num):
205 Get the next lower clean number with bases 1, 2, 5.
206 @param num the number
207 @return the next lower number
209 num = get_clean_num(num)
211 coeff = int(round(num/10**exp))
221 def get_min_max(samples):
223 Get the minimum and maximum bounds for an array of samples.
224 @param samples the array of real values
225 @return a tuple of min, max
228 mean = numpy.average(samples)
229 std = numpy.std(samples)
230 fft = numpy.abs(numpy.fft.fft(samples - mean))
231 envelope = 2*numpy.max(fft)/len(samples)
232 ampl = max(std, envelope) or 0.1
233 return mean - factor*ampl, mean + factor*ampl
235 def get_min_max_fft(fft_samps):
237 Get the minimum and maximum bounds for an array of fft samples.
238 @param samples the array of real values
239 @return a tuple of min, max
241 #get the peak level (max of the samples)
242 peak_level = numpy.max(fft_samps)
243 #separate noise samples
244 noise_samps = numpy.sort(fft_samps)[:len(fft_samps)/2]
246 noise_floor = numpy.average(noise_samps)
247 #get the noise deviation
248 noise_dev = numpy.std(noise_samps)
249 #determine the maximum and minimum levels
250 max_level = peak_level
251 min_level = noise_floor - abs(2*noise_dev)
252 return min_level, max_level