19
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
##################################################
22
112
#A macro to apply an index to a key
23
113
index_key = lambda key, i: "%s_%d"%(key, i+1)
134
224
@param samples the array of real values
135
225
@return a tuple of min, max
138
228
mean = numpy.average(samples)
139
rms = numpy.max([scale_factor*((numpy.sum((samples-mean)**2)/len(samples))**.5), .1])
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