Added vector smoothing, removed record reversal
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blksimpl / ofdm_sync.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 gnuradio import gr
25
26 class ofdm_sync(gr.hier_block):
27     def __init__(self, fg, fft_length, cp_length, snr):
28         self.fg = fg
29
30         self.input = gr.add_const_cc(0)
31
32         SNR = 10.0**(snr/10.0)
33         rho = SNR / (SNR + 1.0)
34         symbol_length = fft_length + cp_length
35
36         # ML Sync
37
38         # Energy Detection from ML Sync
39
40         # Create a delay line
41         self.delay = gr.delay(gr.sizeof_gr_complex, fft_length)
42         self.fg.connect(self.input, self.delay)
43
44         # magnitude squared blocks
45         self.magsqrd1 = gr.complex_to_mag_squared()
46         self.magsqrd2 = gr.complex_to_mag_squared()
47         self.adder = gr.add_ff()
48
49         moving_sum_taps = [rho/2 for i in range(cp_length)]
50         self.moving_sum_filter = gr.fir_filter_fff(1,moving_sum_taps)
51         
52         self.fg.connect(self.input,self.magsqrd1)
53         self.fg.connect(self.delay,self.magsqrd2)
54         self.fg.connect(self.magsqrd1,(self.adder,0))
55         self.fg.connect(self.magsqrd2,(self.adder,1))
56         self.fg.connect(self.adder,self.moving_sum_filter)
57         
58
59         # Correlation from ML Sync
60         self.conjg = gr.conjugate_cc();
61         self.mixer = gr.multiply_cc();
62
63         movingsum2_taps = [1.0 for i in range(cp_length)]
64         self.movingsum2 = gr.fir_filter_ccf(1,movingsum2_taps)
65         
66         # Correlator data handler
67         self.c2mag = gr.complex_to_mag()
68         self.angle = gr.complex_to_arg()
69         self.fg.connect(self.input,(self.mixer,1))
70         self.fg.connect(self.delay,self.conjg,(self.mixer,0))
71         self.fg.connect(self.mixer,self.movingsum2,self.c2mag)
72         self.fg.connect(self.movingsum2,self.angle)
73
74         # ML Sync output arg, need to find maximum point of this
75         self.diff = gr.sub_ff()
76         self.fg.connect(self.c2mag,(self.diff,0))
77         self.fg.connect(self.moving_sum_filter,(self.diff,1))
78
79         #ML measurements input to sampler block and detect
80         nco_sensitivity = 1.0/fft_length
81         self.f2c = gr.float_to_complex()
82         self.sampler = gr.ofdm_sampler(fft_length,symbol_length)
83         self.pk_detect = gr.peak_detector_fb(0.2, 0.25, 30, 0.0005)
84         self.sample_and_hold = gr.sample_and_hold_ff()
85         self.nco = gr.frequency_modulator_fc(nco_sensitivity)
86         self.inv = gr.multiply_const_ff(-1)
87         self.sigmix = gr.multiply_cc()
88
89         # Mix the signal with an NCO controlled by the sync loop
90         self.fg.connect(self.input, (self.sigmix,0))
91         self.fg.connect(self.nco, (self.sigmix,1))
92         self.fg.connect(self.sigmix, (self.sampler,0))
93
94         # use the sync loop values to set the sampler and the NCO
95         #     self.diff = theta
96         #     self.angle = epsilon
97                           
98         self.fg.connect(self.diff, self.pk_detect)
99         use_dpll = 1
100
101         fixed_timing = 0
102         if fixed_timing:
103             # Use a fixed trigger point instead of sync block
104             peak_null = gr.null_sink(gr.sizeof_char)
105             data = 640*[0,]
106             data[639] = 1
107             peak_trigger = gr.vector_source_b(data, True)
108
109             self.fg.connect(self.pk_detect, peak_null)
110             self.fg.connect(peak_trigger, (self.sampler,1))
111             self.fg.connect(peak_trigger, (self.sample_and_hold,1))
112         else:
113             self.dpll = gr.dpll_bb(float(symbol_length),0.01)
114             if use_dpll:
115                 self.fg.connect(self.pk_detect, self.dpll)
116                 self.fg.connect(self.dpll, (self.sampler,1))
117                 self.fg.connect(self.dpll, (self.sample_and_hold,1))
118             else:
119                 self.fg.connect(self.pk_detect, (self.sampler,1))
120                 self.fg.connect(self.pk_detect, (self.sample_and_hold,1))
121             
122         self.fg.connect(self.angle, (self.sample_and_hold,0))
123         self.fg.connect(self.sample_and_hold, self.inv, self.nco)
124
125         if 1:
126             self.fg.connect(self.diff, gr.file_sink(gr.sizeof_float, "theta_f.dat"))
127             self.fg.connect(self.angle, gr.file_sink(gr.sizeof_float, "epsilon_f.dat"))
128             if fixed_timing:
129                 self.fg.connect(peak_trigger, gr.file_sink(gr.sizeof_char, "peaks_b.dat"))
130             else:
131                 self.fg.connect(self.pk_detect, gr.file_sink(gr.sizeof_char, "peaks_b.dat"))
132                 if use_dpll:
133                     self.fg.connect(self.dpll, gr.file_sink(gr.sizeof_char, "dpll_b.dat"))
134
135             self.fg.connect(self.sigmix, gr.file_sink(gr.sizeof_gr_complex, "sigmix_c.dat"))
136             self.fg.connect(self.sampler, gr.file_sink(gr.sizeof_gr_complex*fft_length, "sampler_c.dat"))
137             self.fg.connect(self.sample_and_hold, gr.file_sink(gr.sizeof_float, "sample_and_hold_f.dat"))
138             self.fg.connect(self.nco, gr.file_sink(gr.sizeof_gr_complex, "nco_c.dat"))
139             self.fg.connect(self.input, gr.file_sink(gr.sizeof_gr_complex, "input_c.dat"))
140
141         gr.hier_block.__init__(self, fg, self.input, self.sampler)