3 # Copyright 2007 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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)
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.
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.
25 from gnuradio import gr
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,
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
41 self.input = gr.add_const_cc(0)
43 symbol_length = fft_length + cp_length
48 self.delay = gr.delay(gr.sizeof_gr_complex, fft_length/2)
50 # Correlation from ML Sync
51 self.conjg = gr.conjugate_cc();
52 self.corr = gr.multiply_cc();
54 # Create a moving sum filter for the corr output
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)
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)
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)]
67 self.inputmovingsum = gr.fir_filter_fff(1,movingsum2_taps)
69 self.inputmovingsum = gr.fft_filter_fff(1,movingsum2_taps)
71 self.square = gr.multiply_ff()
72 self.normalize = gr.divide_ff()
74 # Get magnitude (peaks) and angle (phase/freq error)
75 self.c2mag = gr.complex_to_mag_squared()
76 self.angle = gr.complex_to_arg()
78 self.sample_and_hold = gr.sample_and_hold_ff()
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()
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)
90 self.sampler = gr.ofdm_sampler(fft_length,symbol_length)
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)
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))
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))
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)
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))
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"))
135 gr.hier_block.__init__(self, fg, self.input, self.sampler)