Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blks2impl / ofdm_receiver.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 import math
24 from numpy import fft
25 from gnuradio import gr
26 from gnuradio.blks2impl.ofdm_sync_ml import ofdm_sync_ml
27 from gnuradio.blks2impl.ofdm_sync_pn import ofdm_sync_pn
28 from gnuradio.blks2impl.ofdm_sync_pnac import ofdm_sync_pnac
29 from gnuradio.blks2impl.ofdm_sync_fixed import ofdm_sync_fixed
30
31 class ofdm_receiver(gr.hier_block2):
32     """
33     Performs receiver synchronization on OFDM symbols.
34
35     The receiver performs channel filtering as well as symbol, frequency, and phase synchronization.
36     The synchronization routines are available in three flavors: preamble correlator (Schmidl and Cox),
37     modifid preamble correlator with autocorrelation (not yet working), and cyclic prefix correlator
38     (Van de Beeks).
39     """
40
41     def __init__(self, fft_length, cp_length, occupied_tones, snr, ks, logging=False):
42         """
43         Hierarchical block for receiving OFDM symbols.
44
45         The input is the complex modulated signal at baseband.
46         Synchronized packets are sent back to the demodulator.
47
48         @param fft_length: total number of subcarriers
49         @type  fft_length: int
50         @param cp_length: length of cyclic prefix as specified in subcarriers (<= fft_length)
51         @type  cp_length: int
52         @param occupied_tones: number of subcarriers used for data
53         @type  occupied_tones: int
54         @param snr: estimated signal to noise ratio used to guide cyclic prefix synchronizer
55         @type  snr: float
56         @param ks: known symbols used as preambles to each packet
57         @type  ks: list of lists
58         @param logging: turn file logging on or off
59         @type  logging: bool
60         """
61
62         gr.hier_block2.__init__(self, "ofdm_receiver",
63                                 gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
64                                 gr.io_signature2(2, 2, gr.sizeof_gr_complex*occupied_tones, gr.sizeof_char)) # Output signature
65         
66         bw = (float(occupied_tones) / float(fft_length)) / 2.0
67         tb = bw*0.08
68         chan_coeffs = gr.firdes.low_pass (1.0,                     # gain
69                                           1.0,                     # sampling rate
70                                           bw+tb,                   # midpoint of trans. band
71                                           tb,                      # width of trans. band
72                                           gr.firdes.WIN_HAMMING)   # filter type
73         self.chan_filt = gr.fft_filter_ccc(1, chan_coeffs)
74         
75         win = [1 for i in range(fft_length)]
76
77         zeros_on_left = int(math.ceil((fft_length - occupied_tones)/2.0))
78         ks0 = fft_length*[0,]
79         ks0[zeros_on_left : zeros_on_left + occupied_tones] = ks[0]
80         
81         ks0 = fft.ifftshift(ks0)
82         ks0time = fft.ifft(ks0)
83         # ADD SCALING FACTOR
84         ks0time = ks0time.tolist()
85
86         SYNC = "pn"
87         if SYNC == "ml":
88             nco_sensitivity = -1.0/fft_length                             # correct for fine frequency
89             self.ofdm_sync = ofdm_sync_ml(fft_length, cp_length, snr, ks0time, logging)
90         elif SYNC == "pn":
91             nco_sensitivity = -2.0/fft_length                             # correct for fine frequency
92             self.ofdm_sync = ofdm_sync_pn(fft_length, cp_length, logging)
93         elif SYNC == "pnac":
94             nco_sensitivity = -2.0/fft_length                             # correct for fine frequency
95             self.ofdm_sync = ofdm_sync_pnac(fft_length, cp_length, ks0time, logging)
96         elif SYNC == "fixed":                                             # for testing only; do not user over the air
97             self.chan_filt = gr.multiply_const_cc(1.0)                    # remove filter and filter delay for this
98             nsymbols = 18                                                 # enter the number of symbols per packet
99             freq_offset = 0.0                                             # if you use a frequency offset, enter it here
100             nco_sensitivity = -2.0/fft_length                             # correct for fine frequency
101             self.ofdm_sync = ofdm_sync_fixed(fft_length, cp_length, nsymbols, freq_offset, logging)
102
103         # Set up blocks
104
105         self.nco = gr.frequency_modulator_fc(nco_sensitivity)         # generate a signal proportional to frequency error of sync block
106         self.sigmix = gr.multiply_cc()
107         self.sampler = gr.ofdm_sampler(fft_length, fft_length+cp_length)
108         self.fft_demod = gr.fft_vcc(fft_length, True, win, True)
109         self.ofdm_frame_acq = gr.ofdm_frame_acquisition(occupied_tones, fft_length,
110                                                         cp_length, ks[0])
111
112         self.connect(self, self.chan_filt)                            # filter the input channel
113         self.connect(self.chan_filt, self.ofdm_sync)                  # into the synchronization alg.
114         self.connect((self.ofdm_sync,0), self.nco, (self.sigmix,1))   # use sync freq. offset output to derotate input signal
115         self.connect(self.chan_filt, (self.sigmix,0))                 # signal to be derotated
116         self.connect(self.sigmix, (self.sampler,0))                   # sample off timing signal detected in sync alg
117         self.connect((self.ofdm_sync,1), (self.sampler,1))            # timing signal to sample at
118
119         self.connect((self.sampler,0), self.fft_demod)                # send derotated sampled signal to FFT
120         self.connect(self.fft_demod, (self.ofdm_frame_acq,0))         # find frame start and equalize signal
121         self.connect((self.sampler,1), (self.ofdm_frame_acq,1))       # send timing signal to signal frame start
122         self.connect((self.ofdm_frame_acq,0), (self,0))               # finished with fine/coarse freq correction,
123         self.connect((self.ofdm_frame_acq,1), (self,1))               # frame and symbol timing, and equalization
124
125         if logging:
126             self.connect(self.chan_filt, gr.file_sink(gr.sizeof_gr_complex, "ofdm_receiver-chan_filt_c.dat"))
127             self.connect(self.fft_demod, gr.file_sink(gr.sizeof_gr_complex*fft_length, "ofdm_receiver-fft_out_c.dat"))
128             self.connect(self.ofdm_frame_acq,
129                          gr.file_sink(gr.sizeof_gr_complex*occupied_tones, "ofdm_receiver-frame_acq_c.dat"))
130             self.connect((self.ofdm_frame_acq,1), gr.file_sink(1, "ofdm_receiver-found_corr_b.dat"))
131             self.connect(self.sampler, gr.file_sink(gr.sizeof_gr_complex*fft_length, "ofdm_receiver-sampler_c.dat"))
132             self.connect(self.sigmix, gr.file_sink(gr.sizeof_gr_complex, "ofdm_receiver-sigmix_c.dat"))
133             self.connect(self.nco, gr.file_sink(gr.sizeof_gr_complex, "ofdm_receiver-nco_c.dat"))