Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / modulation_utils.py
1 #
2 # Copyright 2006 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 2, 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 along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #
20
21 """
22 Miscellaneous utilities for managing mods and demods, as well as other items
23 useful in dealing with generalized handling of different modulations and demods.
24 """
25
26 import inspect
27
28
29 # Type 1 modulators accept a stream of bytes on their input and produce complex baseband output
30 _type_1_modulators = {}
31
32 def type_1_mods():
33     return _type_1_modulators
34
35 def add_type_1_mod(name, mod_class):
36     _type_1_modulators[name] = mod_class
37
38
39 # Type 1 demodulators accept complex baseband input and produce a stream of bits, packed
40 # 1 bit / byte as their output.  Their output is completely unambiguous.  There is no need
41 # to resolve phase or polarity ambiguities.
42 _type_1_demodulators = {}
43
44 def type_1_demods():
45     return _type_1_demodulators
46
47 def add_type_1_demod(name, demod_class):
48     _type_1_demodulators[name] = demod_class
49
50
51 def extract_kwargs_from_options(function, excluded_args, options):
52     """
53     Given a function, a list of excluded arguments and the result of
54     parsing command line options, create a dictionary of key word
55     arguments suitable for passing to the function.  The dictionary
56     will be populated with key/value pairs where the keys are those
57     that are common to the function's argument list (minus the
58     excluded_args) and the attributes in options.  The values are the
59     corresponding values from options unless that value is None.
60     In that case, the corresponding dictionary entry is not populated.
61
62     (This allows different modulations that have the same parameter
63     names, but different default values to coexist.  The downside is
64     that --help in the option parser will list the default as None,
65     but in that case the default provided in the __init__ argument
66     list will be used since there is no kwargs entry.)
67
68     @param function: the function whose parameter list will be examined
69     @param excluded_args: function arguments that are NOT to be added to the dictionary
70     @type excluded_args: sequence of strings
71     @param options: result of command argument parsing
72     @type options: optparse.Values
73     """
74     # Try this in C++ ;)
75     args, varargs, varkw, defaults = inspect.getargspec(function)
76     d = {}
77     for kw in [a for a in args if a not in excluded_args]:
78         if hasattr(options, kw):
79             if getattr(options, kw) is not None:
80                 d[kw] = getattr(options, kw)
81     return d