2 # Copyright 2008 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 #A macro to apply an index to a key
23 index_key = lambda key, i: "%s_%d"%(key, i+1)
25 def _register_access_method(destination, controller, key):
27 Helper function for register access methods.
28 This helper creates distinct set and get methods for each key
29 and adds them to the destination object.
31 def set(value): controller[key] = value
32 setattr(destination, 'set_'+key, set)
33 def get(): return controller[key]
34 setattr(destination, 'get_'+key, get)
36 def register_access_methods(destination, controller):
38 Register setter and getter functions in the destination object for all keys in the controller.
39 @param destination the object to get new setter and getter methods
40 @param controller the pubsub controller
42 for key in controller.keys(): _register_access_method(destination, controller, key)
44 ##################################################
45 # Input Watcher Thread
46 ##################################################
47 from gnuradio import gru
49 class input_watcher(gru.msgq_runner):
51 Input watcher thread runs forever.
52 Read messages from the message queue.
53 Forward messages to the message handler.
55 def __init__ (self, msgq, controller, msg_key, arg1_key='', arg2_key=''):
56 self._controller = controller
57 self._msg_key = msg_key
58 self._arg1_key = arg1_key
59 self._arg2_key = arg2_key
60 gru.msgq_runner.__init__(self, msgq, self.handle_msg)
62 def handle_msg(self, msg):
63 if self._arg1_key: self._controller[self._arg1_key] = msg.arg1()
64 if self._arg2_key: self._controller[self._arg2_key] = msg.arg2()
65 self._controller[self._msg_key] = msg.to_string()
68 ##################################################
70 ##################################################
76 Get the exponent of the number in base 10.
77 @param num the floating point number
78 @return the exponent as an integer
81 return int(math.floor(math.log10(abs(num))))
83 def get_clean_num(num):
85 Get the closest clean number match to num with bases 1, 2, 5.
87 @return the closest number
90 sign = num > 0 and 1 or -1
92 nums = numpy.array((1, 2, 5, 10))*(10**exp)
93 return sign*nums[numpy.argmin(numpy.abs(nums - abs(num)))]
95 def get_clean_incr(num):
97 Get the next higher clean number with bases 1, 2, 5.
99 @return the next higher number
101 num = get_clean_num(num)
103 coeff = int(round(num/10**exp))
113 def get_clean_decr(num):
115 Get the next lower clean number with bases 1, 2, 5.
116 @param num the number
117 @return the next lower number
119 num = get_clean_num(num)
121 coeff = int(round(num/10**exp))
131 def get_min_max(samples):
133 Get the minimum and maximum bounds for an array of samples.
134 @param samples the array of real values
135 @return a tuple of min, max
138 mean = numpy.average(samples)
139 rms = numpy.max([scale_factor*((numpy.sum((samples-mean)**2)/len(samples))**.5), .1])