Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blksimpl / gmsk.py
1 #
2 # GMSK modulation and demodulation.  
3 #
4 #
5 # Copyright 2005,2006 Free Software Foundation, Inc.
6
7 # This file is part of GNU Radio
8
9 # GNU Radio is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3, or (at your option)
12 # any later version.
13
14 # GNU Radio is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with GNU Radio; see the file COPYING.  If not, write to
21 # the Free Software Foundation, Inc., 51 Franklin Street,
22 # Boston, MA 02110-1301, USA.
23
24
25 # See gnuradio-examples/python/gmsk2 for examples
26
27 from gnuradio import gr
28 from gnuradio import modulation_utils
29 from math import pi
30 import Numeric
31 from pprint import pprint
32 import inspect
33
34 # default values (used in __init__ and add_options)
35 _def_samples_per_symbol = 2
36 _def_bt = 0.35
37 _def_verbose = False
38 _def_log = False
39
40 _def_gain_mu = 0.05
41 _def_mu = 0.5
42 _def_freq_error = 0.0
43 _def_omega_relative_limit = 0.005
44
45
46 # /////////////////////////////////////////////////////////////////////////////
47 #                              GMSK modulator
48 # /////////////////////////////////////////////////////////////////////////////
49
50 class gmsk_mod(gr.hier_block):
51
52     def __init__(self, fg,
53                  samples_per_symbol=_def_samples_per_symbol,
54                  bt=_def_bt,
55                  verbose=_def_verbose,
56                  log=_def_log):
57         """
58         Hierarchical block for Gaussian Minimum Shift Key (GMSK)
59         modulation.
60
61         The input is a byte stream (unsigned char) and the
62         output is the complex modulated signal at baseband.
63
64         @param fg: flow graph
65         @type fg: flow graph
66         @param samples_per_symbol: samples per baud >= 2
67         @type samples_per_symbol: integer
68         @param bt: Gaussian filter bandwidth * symbol time
69         @type bt: float
70         @param verbose: Print information about modulator?
71         @type verbose: bool
72         @param debug: Print modualtion data to files?
73         @type debug: bool       
74         """
75
76         self._fg = fg
77         self._samples_per_symbol = samples_per_symbol
78         self._bt = bt
79
80         if not isinstance(samples_per_symbol, int) or samples_per_symbol < 2:
81             raise TypeError, ("samples_per_symbol must be an integer >= 2, is %r" % (samples_per_symbol,))
82
83         ntaps = 4 * samples_per_symbol                  # up to 3 bits in filter at once
84         sensitivity = (pi / 2) / samples_per_symbol     # phase change per bit = pi / 2
85
86         # Turn it into NRZ data.
87         self.nrz = gr.bytes_to_syms()
88
89         # Form Gaussian filter
90         # Generate Gaussian response (Needs to be convolved with window below).
91         self.gaussian_taps = gr.firdes.gaussian(
92                 1,                     # gain
93                 samples_per_symbol,    # symbol_rate
94                 bt,                    # bandwidth * symbol time
95                 ntaps                  # number of taps
96                 )
97
98         self.sqwave = (1,) * samples_per_symbol       # rectangular window
99         self.taps = Numeric.convolve(Numeric.array(self.gaussian_taps),Numeric.array(self.sqwave))
100         self.gaussian_filter = gr.interp_fir_filter_fff(samples_per_symbol, self.taps)
101
102         # FM modulation
103         self.fmmod = gr.frequency_modulator_fc(sensitivity)
104                 
105         if verbose:
106             self._print_verbage()
107          
108         if log:
109             self._setup_logging()
110
111         # Connect & Initialize base class
112         self._fg.connect(self.nrz, self.gaussian_filter, self.fmmod)
113         gr.hier_block.__init__(self, self._fg, self.nrz, self.fmmod)
114
115     def samples_per_symbol(self):
116         return self._samples_per_symbol
117
118     def bits_per_symbol(self=None):     # staticmethod that's also callable on an instance
119         return 1
120     bits_per_symbol = staticmethod(bits_per_symbol)      # make it a static method.
121
122
123     def _print_verbage(self):
124         print "bits per symbol = %d" % self.bits_per_symbol()
125         print "Gaussian filter bt = %.2f" % self._bt
126
127
128     def _setup_logging(self):
129         print "Modulation logging turned on."
130         self._fg.connect(self.nrz,
131                          gr.file_sink(gr.sizeof_float, "nrz.dat"))
132         self._fg.connect(self.gaussian_filter,
133                          gr.file_sink(gr.sizeof_float, "gaussian_filter.dat"))
134         self._fg.connect(self.fmmod,
135                          gr.file_sink(gr.sizeof_gr_complex, "fmmod.dat"))
136
137
138     def add_options(parser):
139         """
140         Adds GMSK modulation-specific options to the standard parser
141         """
142         parser.add_option("", "--bt", type="float", default=_def_bt,
143                           help="set bandwidth-time product [default=%default] (GMSK)")
144     add_options=staticmethod(add_options)
145
146
147     def extract_kwargs_from_options(options):
148         """
149         Given command line options, create dictionary suitable for passing to __init__
150         """
151         return modulation_utils.extract_kwargs_from_options(gmsk_mod.__init__,
152                                                             ('self', 'fg'), options)
153     extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
154
155
156
157 # /////////////////////////////////////////////////////////////////////////////
158 #                            GMSK demodulator
159 # /////////////////////////////////////////////////////////////////////////////
160
161 class gmsk_demod(gr.hier_block):
162
163     def __init__(self, fg,
164                  samples_per_symbol=_def_samples_per_symbol,
165                  gain_mu=_def_gain_mu,
166                  mu=_def_mu,
167                  omega_relative_limit=_def_omega_relative_limit,
168                  freq_error=_def_freq_error,
169                  verbose=_def_verbose,
170                  log=_def_log):
171         """
172         Hierarchical block for Gaussian Minimum Shift Key (GMSK)
173         demodulation.
174
175         The input is the complex modulated signal at baseband.
176         The output is a stream of bits packed 1 bit per byte (the LSB)
177
178         @param fg: flow graph
179         @type fg: flow graph
180         @param samples_per_symbol: samples per baud
181         @type samples_per_symbol: integer
182         @param verbose: Print information about modulator?
183         @type verbose: bool
184         @param log: Print modualtion data to files?
185         @type log: bool 
186
187         Clock recovery parameters.  These all have reasonble defaults.
188         
189         @param gain_mu: controls rate of mu adjustment
190         @type gain_mu: float
191         @param mu: fractional delay [0.0, 1.0]
192         @type mu: float
193         @param omega_relative_limit: sets max variation in omega
194         @type omega_relative_limit: float, typically 0.000200 (200 ppm)
195         @param freq_error: bit rate error as a fraction
196         @param float
197         """
198
199         self._fg = fg
200         self._samples_per_symbol = samples_per_symbol
201         self._gain_mu = gain_mu
202         self._mu = mu
203         self._omega_relative_limit = omega_relative_limit
204         self._freq_error = freq_error
205         
206         if samples_per_symbol < 2:
207             raise TypeError, "samples_per_symbol >= 2, is %f" % samples_per_symbol
208
209         self._omega = samples_per_symbol*(1+self._freq_error)
210
211         self._gain_omega = .25 * self._gain_mu * self._gain_mu        # critically damped
212
213         # Demodulate FM
214         sensitivity = (pi / 2) / samples_per_symbol
215         self.fmdemod = gr.quadrature_demod_cf(1.0 / sensitivity)
216
217         # the clock recovery block tracks the symbol clock and resamples as needed.
218         # the output of the block is a stream of soft symbols (float)
219         self.clock_recovery = gr.clock_recovery_mm_ff(self._omega, self._gain_omega,
220                                                       self._mu, self._gain_mu,
221                                                       self._omega_relative_limit)
222
223         # slice the floats at 0, outputting 1 bit (the LSB of the output byte) per sample
224         self.slicer = gr.binary_slicer_fb()
225
226         if verbose:
227             self._print_verbage()
228          
229         if log:
230             self._setup_logging()
231
232         # Connect & Initialize base class
233         self._fg.connect(self.fmdemod, self.clock_recovery, self.slicer)
234         gr.hier_block.__init__(self, self._fg, self.fmdemod, self.slicer)
235
236     def samples_per_symbol(self):
237         return self._samples_per_symbol
238
239     def bits_per_symbol(self=None):   # staticmethod that's also callable on an instance
240         return 1
241     bits_per_symbol = staticmethod(bits_per_symbol)      # make it a static method.
242
243
244     def _print_verbage(self):
245         print "bits per symbol = %d" % self.bits_per_symbol()
246         print "M&M clock recovery omega = %f" % self._omega
247         print "M&M clock recovery gain mu = %f" % self._gain_mu
248         print "M&M clock recovery mu = %f" % self._mu
249         print "M&M clock recovery omega rel. limit = %f" % self._omega_relative_limit
250         print "frequency error = %f" % self._freq_error
251
252
253     def _setup_logging(self):
254         print "Demodulation logging turned on."
255         self._fg.connect(self.fmdemod,
256                         gr.file_sink(gr.sizeof_float, "fmdemod.dat"))
257         self._fg.connect(self.clock_recovery,
258                         gr.file_sink(gr.sizeof_float, "clock_recovery.dat"))
259         self._fg.connect(self.slicer,
260                         gr.file_sink(gr.sizeof_char, "slicer.dat"))
261
262     def add_options(parser):
263         """
264         Adds GMSK demodulation-specific options to the standard parser
265         """
266         parser.add_option("", "--gain-mu", type="float", default=_def_gain_mu,
267                           help="M&M clock recovery gain mu [default=%default] (GMSK/PSK)")
268         parser.add_option("", "--mu", type="float", default=_def_mu,
269                           help="M&M clock recovery mu [default=%default] (GMSK/PSK)")
270         parser.add_option("", "--omega-relative-limit", type="float", default=_def_omega_relative_limit,
271                           help="M&M clock recovery omega relative limit [default=%default] (GMSK/PSK)")
272         parser.add_option("", "--freq-error", type="float", default=_def_freq_error,
273                           help="M&M clock recovery frequency error [default=%default] (GMSK)")
274     add_options=staticmethod(add_options)
275
276     def extract_kwargs_from_options(options):
277         """
278         Given command line options, create dictionary suitable for passing to __init__
279         """
280         return modulation_utils.extract_kwargs_from_options(gmsk_demod.__init__,
281                                                             ('self', 'fg'), options)
282     extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
283
284
285 #
286 # Add these to the mod/demod registry
287 #
288 modulation_utils.add_type_1_mod('gmsk', gmsk_mod)
289 modulation_utils.add_type_1_demod('gmsk', gmsk_demod)