2 # GMSK modulation and demodulation.
5 # Copyright 2005,2006,2007 Free Software Foundation, Inc.
7 # This file is part of GNU Radio
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)
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.
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.
25 # See gnuradio-examples/python/digital for examples
27 from gnuradio import gr
28 from gnuradio import modulation_utils
31 from pprint import pprint
34 # default values (used in __init__ and add_options)
35 _def_samples_per_symbol = 2
43 _def_omega_relative_limit = 0.005
46 # /////////////////////////////////////////////////////////////////////////////
48 # /////////////////////////////////////////////////////////////////////////////
50 class gmsk2_mod(gr.hier_block2):
53 samples_per_symbol=_def_samples_per_symbol,
58 Hierarchical block for Gaussian Minimum Shift Key (GMSK)
61 The input is a byte stream (unsigned char) and the
62 output is the complex modulated signal at baseband.
64 @param samples_per_symbol: samples per baud >= 2
65 @type samples_per_symbol: integer
66 @param bt: Gaussian filter bandwidth * symbol time
68 @param verbose: Print information about modulator?
70 @param debug: Print modualtion data to files?
73 gr.hier_block2.__init__(self,
74 "gmsk2_mod", # Block typename
75 gr.io_signature(1,1,gr.sizeof_char), # Input signature
76 gr.io_signature(1,1,gr.sizeof_gr_complex)) # Output signature
78 self._samples_per_symbol = samples_per_symbol
81 if not isinstance(samples_per_symbol, int) or samples_per_symbol < 2:
82 raise TypeError, ("samples_per_symbol must be an integer >= 2, is %r" % (samples_per_symbol,))
84 ntaps = 4 * samples_per_symbol # up to 3 bits in filter at once
85 sensitivity = (pi / 2) / samples_per_symbol # phase change per bit = pi / 2
87 # Turn it into NRZ data.
88 self.nrz = gr.bytes_to_syms()
90 # Form Gaussian filter
91 # Generate Gaussian response (Needs to be convolved with window below).
92 self.gaussian_taps = gr.firdes.gaussian(
94 samples_per_symbol, # symbol_rate
95 bt, # bandwidth * symbol time
96 ntaps # number of taps
99 self.sqwave = (1,) * samples_per_symbol # rectangular window
100 self.taps = numpy.convolve(numpy.array(self.gaussian_taps),numpy.array(self.sqwave))
101 self.gaussian_filter = gr.interp_fir_filter_fff(samples_per_symbol, self.taps)
104 self.fmmod = gr.frequency_modulator_fc(sensitivity)
107 self._print_verbage()
109 self.connect(self, self.nrz, self.gaussian_filter, self.fmmod, self)
112 self._setup_logging()
114 def samples_per_symbol(self):
115 return self._samples_per_symbol
117 def bits_per_symbol(self=None): # staticmethod that's also callable on an instance
119 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method.
121 def _print_verbage(self):
122 print "bits per symbol = %d" % self.bits_per_symbol()
123 print "Gaussian filter bt = %.2f" % self._bt
126 def _setup_logging(self):
127 print "Modulation logging turned on."
128 self.connect(self.nrz, gr.file_sink(gr.sizeof_float, "nrz.dat"))
129 self.connect(self.gaussian_filter, gr.file_sink(gr.sizeof_float, "gaussian_filter.dat"))
130 self.connect(self.fmmod, gr.file_sink(gr.sizeof_gr_complex, "fmmod.dat"))
132 def add_options(parser):
134 Adds GMSK modulation-specific options to the standard parser
136 parser.add_option("", "--bt", type="float", default=_def_bt,
137 help="set bandwidth-time product [default=%default] (GMSK)")
138 add_options=staticmethod(add_options)
140 # FIXME: figure out what has to change for gr.hier_block2 version
141 #def extract_kwargs_from_options(options):
143 # Given command line options, create dictionary suitable for passing to __init__
145 # return modulation_utils.extract_kwargs_from_options(gmsk_mod.__init__,
146 # ('self', 'fg'), options)
147 #extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
150 # /////////////////////////////////////////////////////////////////////////////
152 # /////////////////////////////////////////////////////////////////////////////
154 class gmsk2_demod(gr.hier_block2):
157 samples_per_symbol=_def_samples_per_symbol,
158 gain_mu=_def_gain_mu,
160 omega_relative_limit=_def_omega_relative_limit,
161 freq_error=_def_freq_error,
162 verbose=_def_verbose,
165 Hierarchical block for Gaussian Minimum Shift Key (GMSK)
168 The input is the complex modulated signal at baseband.
169 The output is a stream of bits packed 1 bit per byte (the LSB)
171 @param samples_per_symbol: samples per baud
172 @type samples_per_symbol: integer
173 @param verbose: Print information about modulator?
175 @param log: Print modualtion data to files?
178 Clock recovery parameters. These all have reasonble defaults.
180 @param gain_mu: controls rate of mu adjustment
182 @param mu: fractional delay [0.0, 1.0]
184 @param omega_relative_limit: sets max variation in omega
185 @type omega_relative_limit: float, typically 0.000200 (200 ppm)
186 @param freq_error: bit rate error as a fraction
190 gr.hier_block2.__init__(self,
191 "gmsk2_demod", # Block typename
192 gr.io_signature(1,1,gr.sizeof_gr_complex), # Input signature
193 gr.io_signature(1,1,gr.sizeof_char)) # Output signature
195 self._samples_per_symbol = samples_per_symbol
196 self._gain_mu = gain_mu
198 self._omega_relative_limit = omega_relative_limit
199 self._freq_error = freq_error
201 if samples_per_symbol < 2:
202 raise TypeError, "samples_per_symbol >= 2, is %f" % samples_per_symbol
204 self._omega = samples_per_symbol*(1+self._freq_error)
206 self._gain_omega = .25 * self._gain_mu * self._gain_mu # critically damped
209 sensitivity = (pi / 2) / samples_per_symbol
210 self.fmdemod = gr.quadrature_demod_cf(1.0 / sensitivity)
212 # the clock recovery block tracks the symbol clock and resamples as needed.
213 # the output of the block is a stream of soft symbols (float)
214 self.clock_recovery = gr.clock_recovery_mm_ff(self._omega, self._gain_omega,
215 self._mu, self._gain_mu,
216 self._omega_relative_limit)
218 # slice the floats at 0, outputting 1 bit (the LSB of the output byte) per sample
219 self.slicer = gr.binary_slicer_fb()
222 self._print_verbage()
224 self.connect(self, self.fmdemod, self.clock_recovery, self.slicer, self)
227 self._setup_logging()
229 def samples_per_symbol(self):
230 return self._samples_per_symbol
232 def bits_per_symbol(self=None): # staticmethod that's also callable on an instance
234 bits_per_symbol = staticmethod(bits_per_symbol) # make it a static method.
236 def _print_verbage(self):
237 print "bits per symbol = %d" % self.bits_per_symbol()
238 print "M&M clock recovery omega = %f" % self._omega
239 print "M&M clock recovery gain mu = %f" % self._gain_mu
240 print "M&M clock recovery mu = %f" % self._mu
241 print "M&M clock recovery omega rel. limit = %f" % self._omega_relative_limit
242 print "frequency error = %f" % self._freq_error
245 def _setup_logging(self):
246 print "Demodulation logging turned on."
247 self.connect(fmdemod, gr.file_sink(gr.sizeof_float, "fmdemod.dat"))
248 self.connect(clock_recovery, gr.file_sink(gr.sizeof_float, "clock_recovery.dat"))
249 self.connect(slicer, gr.file_sink(gr.sizeof_char, "slicer.dat"))
251 def add_options(parser):
253 Adds GMSK demodulation-specific options to the standard parser
255 parser.add_option("", "--gain-mu", type="float", default=_def_gain_mu,
256 help="M&M clock recovery gain mu [default=%default] (GMSK/PSK)")
257 parser.add_option("", "--mu", type="float", default=_def_mu,
258 help="M&M clock recovery mu [default=%default] (GMSK/PSK)")
259 parser.add_option("", "--omega-relative-limit", type="float", default=_def_omega_relative_limit,
260 help="M&M clock recovery omega relative limit [default=%default] (GMSK/PSK)")
261 parser.add_option("", "--freq-error", type="float", default=_def_freq_error,
262 help="M&M clock recovery frequency error [default=%default] (GMSK)")
263 add_options=staticmethod(add_options)
265 # FIXME: figure out what this is for gr.hier_block2 version
266 #def extract_kwargs_from_options(options):
268 # Given command line options, create dictionary suitable for passing to __init__
270 # return modulation_utils.extract_kwargs_from_options(gmsk_demod.__init__,
271 # ('self', 'fg'), options)
272 #extract_kwargs_from_options=staticmethod(extract_kwargs_from_options)
275 # Add these to the mod/demod registry
277 modulation_utils.add_type_1_mod('gmsk2', gmsk2_mod)
278 modulation_utils.add_type_1_demod('gmsk2', gmsk2_demod)