switch source package format to 3.0 quilt
[debian/gnuradio] / gr-wxgui / src / python / common.py
1 #
2 # Copyright 2008, 2009 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 ##################################################
23 # conditional disconnections of wx flow graph
24 ##################################################
25 import wx
26 from gnuradio import gr
27
28 class wxgui_hb(object):
29         """
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.
36         """
37
38         def wxgui_connect(self, *points):
39                 """
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.
45                 """
46                 try:
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)
52                         points = list(points)
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
56
57         @staticmethod
58         def _handler_factory(handler):
59                 """
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
64                 """
65                 cache = [None]
66                 def callback(visible):
67                         if cache[0] == visible: return
68                         cache[0] = visible
69                         #print visible, handler
70                         handler(visible)
71                 return callback
72
73         @staticmethod
74         def _bind_to_visible_event(win, handler):
75                 """
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
81                 """
82                 #is the window visible in the hierarchy
83                 def is_wx_window_visible(my_win):
84                         while True:
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
89                                 my_win = parent
90                 #call the handler, the arg is shown or not
91                 def handler_factory(my_win, my_handler):
92                         def callback(evt):
93                                 my_handler(is_wx_window_visible(my_win))
94                                 evt.Skip() #skip so all bound handlers are called
95                         return callback
96                 handler = handler_factory(win, handler)
97                 #bind the handler to all the parent notebooks
98                 win.Bind(wx.EVT_UPDATE_UI, handler)
99
100 ##################################################
101 # Helpful Functions
102 ##################################################
103
104 #A macro to apply an index to a key
105 index_key = lambda key, i: "%s_%d"%(key, i+1)
106
107 def _register_access_method(destination, controller, key):
108         """
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.
112         """
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) 
117
118 def register_access_methods(destination, controller):
119         """
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
123         """
124         for key in controller.keys(): _register_access_method(destination, controller, key)
125
126 ##################################################
127 # Input Watcher Thread
128 ##################################################
129 from gnuradio import gru
130
131 class input_watcher(gru.msgq_runner):
132         """
133         Input watcher thread runs forever.
134         Read messages from the message queue.
135         Forward messages to the message handler.
136         """
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)
143
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()
148
149
150 ##################################################
151 # Shared Functions
152 ##################################################
153 import numpy
154 import math
155
156 def get_exp(num):
157         """
158         Get the exponent of the number in base 10.
159         @param num the floating point number
160         @return the exponent as an integer
161         """
162         if num == 0: return 0
163         return int(math.floor(math.log10(abs(num))))
164
165 def get_clean_num(num):
166         """
167         Get the closest clean number match to num with bases 1, 2, 5.
168         @param num the number
169         @return the closest number
170         """
171         if num == 0: return 0
172         sign = num > 0 and 1 or -1
173         exp = get_exp(num)
174         nums = numpy.array((1, 2, 5, 10))*(10**exp)
175         return sign*nums[numpy.argmin(numpy.abs(nums - abs(num)))]
176
177 def get_clean_incr(num):
178         """
179         Get the next higher clean number with bases 1, 2, 5.
180         @param num the number
181         @return the next higher number
182         """
183         num = get_clean_num(num)
184         exp = get_exp(num)
185         coeff = int(round(num/10**exp))
186         return {
187                 -5: -2,
188                 -2: -1,
189                 -1: -.5,
190                 1: 2,
191                 2: 5,
192                 5: 10,
193         }[coeff]*(10**exp)
194
195 def get_clean_decr(num):
196         """
197         Get the next lower clean number with bases 1, 2, 5.
198         @param num the number
199         @return the next lower number
200         """
201         num = get_clean_num(num)
202         exp = get_exp(num)
203         coeff = int(round(num/10**exp))
204         return {
205                 -5: -10,
206                 -2: -5,
207                 -1: -2,
208                 1: .5,
209                 2: 1,
210                 5: 2,
211         }[coeff]*(10**exp)
212
213 def get_min_max(samples):
214         """
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
218         """
219         factor = 2.0
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
226
227 def get_min_max_fft(fft_samps):
228         """
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
232         """
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]
237         #get the noise floor
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