summaryrefslogtreecommitdiff
path: root/gnuradio-examples/python/gmsk2/bpsk.py
blob: 14cea7b102c75a59d60fbbfa5cd3a2cca064cccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#
# Copyright 2005,2006 Free Software Foundation, Inc.
# 
# This file is part of GNU Radio
# 
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# 

# See gnuradio-examples/python/gmsk2 for examples

"""
BPSK modulation and demodulation.
"""

from gnuradio import gr, gru
from math import pi, sqrt
import cmath
import Numeric
from pprint import pprint

_use_gray_code = True

def make_constellation(m):
    return [cmath.exp(i * 2 * pi / m * 1j) for i in range(m)]
        
# Common definition of constellations for Tx and Rx
constellation = {
    2 : make_constellation(2),           # BPSK
    4 : make_constellation(4),           # QPSK
    8 : make_constellation(8)            # 8PSK
    }

if 0:
    print "const(2) ="
    pprint(constellation[2])
    print "const(4) ="
    pprint(constellation[4])
    print "const(8) ="
    pprint(constellation[8])


if _use_gray_code:
    # -----------------------
    # Do Gray code
    # -----------------------
    # binary to gray coding
    binary_to_gray = {
        2 : (0, 1),
        4 : (0, 1, 3, 2),
        8 : (0, 1, 3, 2, 7, 6, 4, 5)
        }
    
    # gray to binary
    gray_to_binary = {
        2 : (0, 1),
        4 : (0, 1, 3, 2),
        8 : (0, 1, 3, 2, 6, 7, 5, 4)
        }
else:
    # -----------------------
    # Don't Gray code
    # -----------------------
    # identity mapping
    binary_to_gray = {
        2 : (0, 1),
        4 : (0, 1, 2, 3),
        8 : (0, 1, 2, 3, 4, 5, 6, 7)
        }
    
    # identity mapping
    gray_to_binary = {
        2 : (0, 1),
        4 : (0, 1, 2, 3),
        8 : (0, 1, 2, 3, 4, 5, 6, 7)
        }


# /////////////////////////////////////////////////////////////////////////////
#            mPSK mod/demod with steams of bytes as data i/o
# /////////////////////////////////////////////////////////////////////////////


class bpsk_mod(gr.hier_block):

    def __init__(self, fg, spb, excess_bw):
        """
	Hierarchical block for RRC-filtered BPSK modulation.

	The input is a byte stream (unsigned char) and the
	output is the complex modulated signal at baseband.

	@param fg: flow graph
	@type fg: flow graph
	@param spb: samples per baud >= 2
	@type spb: integer
	@param excess_bw: Root-raised cosine filter excess bandwidth
	@type excess_bw: float
	"""
        if not isinstance(spb, int) or spb < 2:
            raise TypeError, "sbp must be an integer >= 2"
        self.spb = spb

	ntaps = 11 * spb

        bits_per_symbol = self.bits_per_baud()
        arity = pow(2,bits_per_symbol)
        print "bits_per_symbol =", bits_per_symbol
        
        # turn bytes into k-bit vectors
        self.bytes2chunks = \
          gr.packed_to_unpacked_bb(bits_per_symbol, gr.GR_MSB_FIRST)

        self.chunks2symbols = gr.chunks_to_symbols_bc(constellation[arity])

        # pulse shaping filter
	self.rrc_taps = gr.firdes.root_raised_cosine(
		spb,		# gain  (spb since we're interpolating by spb)
		spb,            # sampling rate
		1.0,		# symbol rate
		excess_bw,      # excess bandwidth (roll-off factor)
                ntaps)

	self.rrc_filter = gr.interp_fir_filter_ccf(spb, self.rrc_taps)

	# Connect
        fg.connect(self.bytes2chunks, self.chunks2symbols, self.rrc_filter)

        if 1:
            fg.connect(self.rrc_filter,
                       gr.file_sink(gr.sizeof_gr_complex, "rrc.dat"))
            
	# Initialize base class
	gr.hier_block.__init__(self, fg, self.bytes2chunks, self.rrc_filter)

    def samples_per_baud(self):
        return self.spb

    def bits_per_baud(self=None):   # staticmethod that's also callable on an instance
        return 1
    bits_per_baud = staticmethod(bits_per_baud)      # make it a static method.  RTFM


class bpsk_demod__coherent_detection_of_psk(gr.hier_block):
    def __init__(self, fg, spb, excess_bw, costas_alpha=0.005, gain_mu=0.05):
        """
	Hierarchical block for RRC-filtered BPSK demodulation

	The input is the complex modulated signal at baseband.
	The output is a stream of bits packed 1 bit per byte (LSB)

	@param fg: flow graph
	@type fg: flow graph
	@param spb: samples per baud >= 2
	@type spb: float
	@param excess_bw: Root-raised cosine filter excess bandwidth
	@type excess_bw: float
        @param costas_alpha: loop filter gain
        @type costas_alphas: float
        @param gain_mu:
        @type gain_mu: float
	"""
        if spb < 2:
            raise TypeError, "sbp must be >= 2"
        self.spb = spb

        bits_per_symbol = self.bits_per_baud()
        arity = pow(2,bits_per_symbol)
        print "bits_per_symbol =", bits_per_symbol

        # Automatic gain control
        self.preamp = gr.multiply_const_cc(10e-5)
        self.agc = gr.agc_cc(1e-3, 1, 1)
        
        # Costas loop (carrier tracking)
        # FIXME: need to decide how to handle this more generally; do we pull it from higher layer?
        costas_order = 2
        costas_alpha *= 15   # 2nd order loop needs more gain
        beta = .25 * costas_alpha * costas_alpha
        self.costas_loop = gr.costas_loop_cc(costas_alpha, beta, 0.05, -0.05, costas_order)

        # RRC data filter
        ntaps = 11 * spb
        self.rrc_taps = gr.firdes.root_raised_cosine(
            1.0,                # gain 
            spb,                # sampling rate
            1.0,                # symbol rate
            excess_bw,          # excess bandwidth (roll-off factor)
            ntaps)

        self.rrc_filter=gr.fir_filter_ccf(1, self.rrc_taps)

        # symbol clock recovery
        omega = spb
        gain_omega = .25 * gain_mu * gain_mu
        omega_rel_limit = 0.5
        mu = 0.05
        gain_mu = 0.1
        self.clock_recovery=gr.clock_recovery_mm_cc(omega, gain_omega,
                                                    mu, gain_mu, omega_rel_limit)

        # find closest constellation point
        #rot = .707 + .707j
        rot = 1
        rotated_const = map(lambda pt: pt * rot, constellation[arity])
        print "rotated_const =", rotated_const

        self.slicer = gr.constellation_decoder_cb(rotated_const, range(arity))
        self.gray_decoder = gr.map_bb(gray_to_binary[arity])
        
        # unpack the k bit vector into a stream of bits
        self.unpack = gr.unpack_k_bits_bb(bits_per_symbol)

        fg.connect(self.preamp, self.agc, self.costas_loop, self.rrc_filter, self.clock_recovery,
                   self.slicer, self.gray_decoder, self.unpack)
        
        # Debug sinks
        if 1:
            fg.connect(self.agc,
                       gr.file_sink(gr.sizeof_gr_complex, "agc.dat"))
            fg.connect(self.costas_loop,
                       gr.file_sink(gr.sizeof_gr_complex, "costas_loop.dat"))
            fg.connect(self.rrc_filter,
                       gr.file_sink(gr.sizeof_gr_complex, "rrc.dat"))
            fg.connect(self.clock_recovery,
                       gr.file_sink(gr.sizeof_gr_complex, "clock_recovery.dat"))
            fg.connect(self.slicer,
                       gr.file_sink(gr.sizeof_char, "slicer.dat"))
            fg.connect(self.unpack,
                       gr.file_sink(gr.sizeof_char, "unpack.dat"))

        # Initialize base class
        gr.hier_block.__init__(self, fg, self.preamp, self.unpack)

    def samples_per_baud(self):
        return self.spb

    def bits_per_baud(self=None):   # staticmethod that's also callable on an instance
        return 1
    bits_per_baud = staticmethod(bits_per_baud)      # make it a static method.  RTFM


bpsk_demod = bpsk_demod__coherent_detection_of_psk