Enhanced_GMSK_Demodulator: gmskenhanced.py

Line 
1 #
2 # GMSK modulation and demodulation. 
3 #
4 #
5 # Copyright 2005,2006,2007 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 from gnuradio import gr
26 from gnuradio import modulation_utils
27 from math import pi
28
29 # default values (used in __init__ and add_options)
30 _def_samples_per_symbol = 2
31 _def_verbose = False
32 _def_log = False
33
34 _def_gain_mu = None
35 _def_mu = 0.5
36 _def_freq_error = 0.0
37 _def_omega_relative_limit = 0.005
38
39 # /////////////////////////////////////////////////////////////////////////////
40 #                            GMSK demodulator
41 # /////////////////////////////////////////////////////////////////////////////
42
43 class gmsk_demod(gr.hier_block2):
44
45     def __init__(self,
46                  samples_per_symbol=_def_samples_per_symbol,
47                  gain_mu=_def_gain_mu,
48                  mu=_def_mu,
49                  omega_relative_limit=_def_omega_relative_limit,
50                  freq_error=_def_freq_error,
51                  verbose=_def_verbose,
52                  log=_def_log,
53                  log_fn_prefix='',
54                  sym_per_sec=1e6,
55                  pre_cr_filt_bt=1.0,
56                  pre_cr_filt_tr=0.1,
57                  decision_threshold=0.1):
58         """
59     Hierarchical block for Gaussian Minimum Shift Key (GMSK)
60     demodulation.
61
62     The input is the complex modulated signal at baseband.
63     The output is a stream of bits packed 1 bit per byte (the LSB)
64
65     @param samples_per_symbol: samples per baud
66     @type samples_per_symbol: integer
67         @param verbose: Print information about modulator?
68         @type verbose: bool
69         @param log: Print modualtion data to files?
70         @type log: bool
71
72         Clock recovery parameters.  These all have reasonble defaults.
73         
74         @param gain_mu: controls rate of mu adjustment
75         @type gain_mu: float
76         @param mu: fractional delay [0.0, 1.0]
77         @type mu: float
78         @param omega_relative_limit: sets max variation in omega
79         @type omega_relative_limit: float, typically 0.000200 (200 ppm)
80         @param freq_error: bit rate error as a fraction
81         @param float
82     """
83        
84         gr.hier_block2.__init__(self, "gmsk_demod",
85                 gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
86                 gr.io_signature(1, 1, gr.sizeof_char))       # Output signature
87
88
89         self._samples_per_symbol = samples_per_symbol
90         self._gain_mu = gain_mu
91         self._mu = mu
92         self._omega_relative_limit = omega_relative_limit
93         self._freq_error = freq_error
94         self._log_fn_prefix = log_fn_prefix
95         self._decision_threshold = decision_threshold
96        
97         if samples_per_symbol < 2:
98             raise TypeError, "samples_per_symbol >= 2, is %f" % samples_per_symbol
99
100         self._omega = samples_per_symbol*(1+self._freq_error)
101
102         if not self._gain_mu:
103             self._gain_mu = 0.175
104            
105         self._gain_omega = .25 * self._gain_mu * self._gain_mu        # critically damped
106
107        
108
109         self.kc = gr.kludge_copy(gr.sizeof_gr_complex)
110         self.delay = gr.delay(gr.sizeof_gr_complex, 2*self._samples_per_symbol) #2T delay
111         self.conj = gr.conjugate_cc()
112         self.mult = gr.multiply_cc() #This is producing our differential phasor
113         self.c2mag = gr.complex_to_mag()
114         self.safety_add = gr.add_const_ff(0.0000001)
115         self.c2f = gr.complex_to_float()
116         self.rescaler = gr.divide_ff() #This is used to normalize the signal
117         self.sub = gr.add_const_ff(-self._decision_threshold)
118         samp_per_sec = samples_per_symbol * sym_per_sec
119         pre_cr_filt_bw = sym_per_sec*pre_cr_filt_bt
120         pre_cr_filt_taps = gr.firdes.low_pass(1.0, samp_per_sec, pre_cr_filt_bw, pre_cr_filt_tr*samp_per_sec, gr.firdes.WIN_HAMMING)
121
122
123         self.pre_cr_filt = gr.fir_filter_fff(1, pre_cr_filt_taps)
124
125         # the clock recovery block tracks the symbol clock and resamples as needed.
126         # the output of the block is a stream of soft symbols (float)
127         self.clock_recovery = gr.clock_recovery_mm_ff(self._omega, self._gain_omega,
128                                                       self._mu, self._gain_mu,
129                                                       self._omega_relative_limit)
130
131         # slice the floats at 0, outputting 1 bit (the LSB of the output byte) per sample
132         self.slicer = gr.binary_slicer_fb()
133
134         if verbose:
135             self._print_verbage()
136          
137         if log:
138             self._setup_logging()
139
140         # Connect & Initialize base class
141         self.connect(self, self.kc, self.delay, self.conj, (self.mult, 0))
142         self.connect(self.kc, (self.mult, 1))
143         self.connect(self.mult, self.c2f, (self.rescaler, 0))
144         self.connect(self.mult, self.c2mag, self.safety_add, (self.rescaler, 1))
145         self.connect(self.rescaler, self.pre_cr_filt, self.sub,
146                 self.clock_recovery, self.slicer, self)
147
148     def samples_per_symbol(self):
149         return self._samples_per_symbol
150
151     def bits_per_symbol(self=None):   # staticmethod that's also callable on an instance
152         return 1
153     bits_per_symbol = staticmethod(bits_per_symbol)      # make it a static method.
154
155
156     def _print_verbage(self):
157         print "bits per symbol = %d" % self.bits_per_symbol()
158         print "M&M clock recovery omega = %f" % self._omega
159         print "M&M clock recovery gain mu = %f" % self._gain_mu
160         print "M&M clock recovery mu = %f" % self._mu
161         print "M&M clock recovery omega rel. limit = %f" % self._omega_relative_limit
162         print "frequency error = %f" % self._freq_error
163
164
165     def _setup_logging(self):
166         print "Demodulation logging turned on."
167         self.connect(self.mult,
168                     gr.file_sink(gr.sizeof_gr_complex, self._log_fn_prefix+"mult_output.dat"))
169         self.connect(self.rescaler,
170                     gr.file_sink(gr.sizeof_float, self._log_fn_prefix+"rescaler_output.dat"))
171         self.connect((self.c2f, 0),
172                     gr.file_sink(gr.sizeof_float, self._log_fn_prefix+"rescaler_input.dat"))
173         self.connect(self.pre_cr_filt,
174                     gr.file_sink(gr.sizeof_float, self._log_fn_prefix+"pre_cr_filt_output.dat"))
175         self.connect(self.sub,
176                     gr.file_sink(gr.sizeof_float, self._log_fn_prefix+"cr_input.dat"))
177         self.connect(self.clock_recovery,
178                     gr.file_sink(gr.sizeof_float, self._log_fn_prefix+"cr_output.dat"))
179         self.connect(self.slicer,
180                     gr.file_sink(gr.sizeof_char, self._log_fn_prefix+"slicer.dat"))
181
182     def add_options(parser):
183         """
184         Adds GMSK demodulation-specific options to the standard parser
185         """
186         parser.add_option("", "--gain-mu", type="float", default=_def_gain_mu,
187                           help="M&M clock recovery gain mu [default=%default] (GMSK/PSK)")
188         parser.add_option("", "--mu", type="float", default=_def_mu,
189                           help="M&M clock recovery mu [default=%default] (GMSK/PSK)")
190         parser.add_option("", "--omega-relative-limit", type="float", default=_def_omega_relative_limit,
191                           help="M&M clock recovery omega relative limit [default=%default] (GMSK/PSK)")
192         parser.add_option("", "--freq-error", type="float", default=_def_freq_error,
193                           help="M&M clock recovery frequency error [default=%default] (GMSK)")
194     add_options=staticmethod(add_options)
195
196