Merging OFDM features branch r5661:5759 into trunk. OFDM works over the air with...
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blksimpl / ofdm_sync_pn.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2007 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 2, 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
27 class ofdm_sync_pn(gr.hier_block):
28     def __init__(self, fg, fft_length, cp_length, logging=False):
29         ''' OFDM synchronization using PN Correlation:
30         T. M. Schmidl and D. C. Cox, "Robust Frequency and Timing
31         Synchonization for OFDM," IEEE Trans. Communications, vol. 45,
32         no. 12, 1997.
33         '''
34         
35         self.fg = fg
36
37         # FIXME: when converting to hier_block2's, the output signature
38         # should be the output of the divider (the normalized peaks) and
39         # the angle value out of the sample and hold block
40             
41         self.input = gr.add_const_cc(0)
42
43         symbol_length = fft_length + cp_length
44
45         # PN Sync
46
47         # Create a delay line
48         self.delay = gr.delay(gr.sizeof_gr_complex, fft_length/2)
49
50         # Correlation from ML Sync
51         self.conjg = gr.conjugate_cc();
52         self.corr = gr.multiply_cc();
53
54         # Create a moving sum filter for the corr output
55         if 1:
56             moving_sum_taps = [1.0 for i in range(fft_length//2)]
57             self.moving_sum_filter = gr.fir_filter_ccf(1,moving_sum_taps)
58         else:
59             moving_sum_taps = [complex(1.0,0.0) for i in range(fft_length//2)]
60             self.moving_sum_filter = gr.fft_filter_ccc(1,moving_sum_taps)
61
62         # Create a moving sum filter for the input
63         self.inputmag2 = gr.complex_to_mag_squared()
64         movingsum2_taps = [1.0 for i in range(fft_length//2)]
65
66         if 1:
67             self.inputmovingsum = gr.fir_filter_fff(1,movingsum2_taps)
68         else:
69             self.inputmovingsum = gr.fft_filter_fff(1,movingsum2_taps)
70
71         self.square = gr.multiply_ff()
72         self.normalize = gr.divide_ff()
73      
74         # Get magnitude (peaks) and angle (phase/freq error)
75         self.c2mag = gr.complex_to_mag_squared()
76         self.angle = gr.complex_to_arg()
77
78         self.sample_and_hold = gr.sample_and_hold_ff()
79
80         # Mix the signal with an NCO controlled by the sync loop
81         nco_sensitivity = -2.0/fft_length
82         self.nco = gr.frequency_modulator_fc(nco_sensitivity)
83         self.sigmix = gr.multiply_cc()
84
85         #ML measurements input to sampler block and detect
86         self.sub1 = gr.add_const_ff(-1)
87         self.pk_detect = gr.peak_detector_fb(0.2, 0.25, 30, 0.0005)
88         self.regen = gr.regenerate_bb(symbol_length)
89
90         self.sampler = gr.ofdm_sampler(fft_length,symbol_length)
91         
92         self.fg.connect(self.input, self.delay)
93         self.fg.connect(self.input, (self.corr,0))
94         self.fg.connect(self.delay, self.conjg)
95         self.fg.connect(self.conjg, (self.corr,1))
96         self.fg.connect(self.corr, self.moving_sum_filter)
97         self.fg.connect(self.moving_sum_filter, self.c2mag)
98         self.fg.connect(self.moving_sum_filter, self.angle)
99         self.fg.connect(self.angle, (self.sample_and_hold,0))
100         self.fg.connect(self.sample_and_hold, self.nco)
101
102         self.fg.connect(self.input, (self.sigmix,0))
103         self.fg.connect(self.nco, (self.sigmix,1))
104         self.fg.connect(self.sigmix, (self.sampler,0))
105
106         self.fg.connect(self.input, self.inputmag2, self.inputmovingsum)
107         self.fg.connect(self.inputmovingsum, (self.square,0))
108         self.fg.connect(self.inputmovingsum, (self.square,1))
109         self.fg.connect(self.square, (self.normalize,1))
110         self.fg.connect(self.c2mag, (self.normalize,0))
111
112         # Create a moving sum filter for the corr output
113         matched_filter_taps = [1.0/cp_length for i in range(cp_length)]
114         self.matched_filter = gr.fir_filter_fff(1,matched_filter_taps)
115         self.fg.connect(self.normalize, self.matched_filter)
116         
117         self.fg.connect(self.matched_filter, self.sub1, self.pk_detect)
118         self.fg.connect(self.pk_detect, self.regen)
119         self.fg.connect(self.regen, (self.sampler,1))
120         self.fg.connect(self.pk_detect, (self.sample_and_hold,1))
121
122
123         if logging:
124             self.fg.connect(self.matched_filter, gr.file_sink(gr.sizeof_float, "ofdm_sync_pn-mf_f.dat"))
125             self.fg.connect(self.normalize, gr.file_sink(gr.sizeof_float, "ofdm_sync_pn-theta_f.dat"))
126             self.fg.connect(self.angle, gr.file_sink(gr.sizeof_float, "ofdm_sync_pn-epsilon_f.dat"))
127             self.fg.connect(self.pk_detect, gr.file_sink(gr.sizeof_char, "ofdm_sync_pn-peaks_b.dat"))
128             self.fg.connect(self.regen, gr.file_sink(gr.sizeof_char, "ofdm_sync_pn-regen_b.dat"))
129             self.fg.connect(self.sigmix, gr.file_sink(gr.sizeof_gr_complex, "ofdm_sync_pn-sigmix_c.dat"))
130             self.fg.connect(self.sampler, gr.file_sink(gr.sizeof_gr_complex*fft_length, "ofdm_sync_pn-sampler_c.dat"))
131             self.fg.connect(self.sample_and_hold, gr.file_sink(gr.sizeof_float, "ofdm_sync_pn-sample_and_hold_f.dat"))
132             self.fg.connect(self.nco, gr.file_sink(gr.sizeof_gr_complex, "ofdm_sync_pn-nco_c.dat"))
133             self.fg.connect(self.input, gr.file_sink(gr.sizeof_gr_complex, "ofdm_sync_pn-input_c.dat"))
134
135         gr.hier_block.__init__(self, fg, self.input, self.sampler)