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