Imported Upstream version 3.2.2
[debian/gnuradio] / gr-wxgui / src / python / common.py
1 #
2 # Copyright 2008 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 #A macro to apply an index to a key
23 index_key = lambda key, i: "%s_%d"%(key, i+1)
24
25 def _register_access_method(destination, controller, key):
26         """
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.
30         """
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) 
35
36 def register_access_methods(destination, controller):
37         """
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
41         """
42         for key in controller.keys(): _register_access_method(destination, controller, key)
43
44 ##################################################
45 # Input Watcher Thread
46 ##################################################
47 from gnuradio import gru
48
49 class input_watcher(gru.msgq_runner):
50         """
51         Input watcher thread runs forever.
52         Read messages from the message queue.
53         Forward messages to the message handler.
54         """
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)
61
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()
66
67
68 ##################################################
69 # Shared Functions
70 ##################################################
71 import numpy
72 import math
73
74 def get_exp(num):
75         """
76         Get the exponent of the number in base 10.
77         @param num the floating point number
78         @return the exponent as an integer
79         """
80         if num == 0: return 0
81         return int(math.floor(math.log10(abs(num))))
82
83 def get_clean_num(num):
84         """
85         Get the closest clean number match to num with bases 1, 2, 5.
86         @param num the number
87         @return the closest number
88         """
89         if num == 0: return 0
90         sign = num > 0 and 1 or -1
91         exp = get_exp(num)
92         nums = numpy.array((1, 2, 5, 10))*(10**exp)
93         return sign*nums[numpy.argmin(numpy.abs(nums - abs(num)))]
94
95 def get_clean_incr(num):
96         """
97         Get the next higher clean number with bases 1, 2, 5.
98         @param num the number
99         @return the next higher number
100         """
101         num = get_clean_num(num)
102         exp = get_exp(num)
103         coeff = int(round(num/10**exp))
104         return {
105                 -5: -2,
106                 -2: -1,
107                 -1: -.5,
108                 1: 2,
109                 2: 5,
110                 5: 10,
111         }[coeff]*(10**exp)
112
113 def get_clean_decr(num):
114         """
115         Get the next lower clean number with bases 1, 2, 5.
116         @param num the number
117         @return the next lower number
118         """
119         num = get_clean_num(num)
120         exp = get_exp(num)
121         coeff = int(round(num/10**exp))
122         return {
123                 -5: -10,
124                 -2: -5,
125                 -1: -2,
126                 1: .5,
127                 2: 1,
128                 5: 2,
129         }[coeff]*(10**exp)
130
131 def get_min_max(samples):
132         """
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
136         """
137         scale_factor = 3
138         mean = numpy.average(samples)
139         rms = numpy.max([scale_factor*((numpy.sum((samples-mean)**2)/len(samples))**.5), .1])
140         min = mean - rms
141         max = mean + rms
142         return min, max