Merging OFDM features branch r5661:5759 into trunk. OFDM works over the air with...
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blksimpl / ofdm_sync_pnac.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_pnac(gr.hier_block):
28     def __init__(self, fg, fft_length, cp_length, ks):
29         self.fg = fg
30
31         # FIXME: when converting to hier_block2's, the output signature
32         # should be the output of the divider (the normalized peaks) and
33         # the angle value out of the sample and hold block
34             
35         self.input = gr.add_const_cc(0)
36
37         symbol_length = fft_length + cp_length
38
39         # PN Sync
40
41         # autocorrelate with the known symbol
42         ks = ks[0:fft_length//2]
43         ks.reverse()
44         self.crosscorr_filter = gr.fir_filter_ccc(1, ks)
45         self.fg.connect(self.crosscorr_filter, gr.file_sink(gr.sizeof_gr_complex, "crosscorr.dat"))
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         moving_sum_taps = [1.0 for i in range(fft_length//2)]
56         self.moving_sum_filter = gr.fir_filter_ccf(1,moving_sum_taps)
57
58         # Create a moving sum filter for the input
59         self.inputmag2 = gr.complex_to_mag_squared()
60         movingsum2_taps = [1.0 for i in range(fft_length/2)]
61         self.inputmovingsum = gr.fir_filter_fff(1,movingsum2_taps)
62         self.square = gr.multiply_ff()
63         self.normalize = gr.divide_ff()
64      
65         # Get magnitude (peaks) and angle (phase/freq error)
66         self.c2mag = gr.complex_to_mag_squared()
67         self.angle = gr.complex_to_arg()
68
69         self.sample_and_hold = gr.sample_and_hold_ff()
70
71         # Mix the signal with an NCO controlled by the sync loop
72         nco_sensitivity = -1.0/fft_length
73         self.nco = gr.frequency_modulator_fc(nco_sensitivity)
74         self.sigmix = gr.multiply_cc()
75
76         #ML measurements input to sampler block and detect
77         self.sub1 = gr.add_const_ff(-1)
78         self.pk_detect = gr.peak_detector_fb(0.2, 0.25, 30, 0.0005)
79
80         self.sampler = gr.ofdm_sampler(fft_length,symbol_length)
81         
82         self.fg.connect(self.input, self.crosscorr_filter)
83         self.fg.connect(self.crosscorr_filter, self.delay)
84         self.fg.connect(self.crosscorr_filter, (self.corr,0))
85         self.fg.connect(self.delay, self.conjg)
86         self.fg.connect(self.conjg, (self.corr,1))
87         self.fg.connect(self.corr, self.moving_sum_filter)
88         self.fg.connect(self.moving_sum_filter, self.c2mag)
89         self.fg.connect(self.moving_sum_filter, self.angle)
90         self.fg.connect(self.angle, (self.sample_and_hold,0))
91         self.fg.connect(self.sample_and_hold, self.nco)
92
93         self.fg.connect(self.input, (self.sigmix,0))
94         self.fg.connect(self.nco, (self.sigmix,1))
95         self.fg.connect(self.sigmix, (self.sampler,0))
96
97         self.fg.connect(self.input, self.inputmag2, self.inputmovingsum)
98         self.fg.connect(self.inputmovingsum, (self.square,0))
99         self.fg.connect(self.inputmovingsum, (self.square,1))
100         self.fg.connect(self.square, (self.normalize,1))
101         self.fg.connect(self.c2mag, (self.normalize,0))
102         self.fg.connect(self.normalize, self.sub1, self.pk_detect)
103
104         self.fg.connect(self.pk_detect, (self.sampler,1))
105         self.fg.connect(self.pk_detect, (self.sample_and_hold,1))
106             
107
108         if 1:
109             self.fg.connect(self.normalize, gr.file_sink(gr.sizeof_float,
110                                                          "ofdm_sync_pnac-theta_f.dat"))
111             self.fg.connect(self.angle, gr.file_sink(gr.sizeof_float,
112                                                      "ofdm_sync_pnac-epsilon_f.dat"))
113             self.fg.connect(self.pk_detect, gr.file_sink(gr.sizeof_char,
114                                                          "ofdm_sync_pnac-peaks_b.dat"))
115             self.fg.connect(self.sigmix, gr.file_sink(gr.sizeof_gr_complex,
116                                                       "ofdm_sync_pnac-sigmix_c.dat"))
117             self.fg.connect(self.sampler, gr.file_sink(gr.sizeof_gr_complex*fft_length,
118                                                        "ofdm_sync_pnac-sampler_c.dat"))
119             self.fg.connect(self.sample_and_hold, gr.file_sink(gr.sizeof_float,
120                                                                "ofdm_sync_pnac-sample_and_hold_f.dat"))
121             self.fg.connect(self.nco, gr.file_sink(gr.sizeof_gr_complex,
122                                                    "ofdm_sync_pnac-nco_c.dat"))
123             self.fg.connect(self.input, gr.file_sink(gr.sizeof_gr_complex,
124                                                      "ofdm_sync_pnac-input_c.dat"))
125
126         gr.hier_block.__init__(self, fg, self.input, self.sampler)