3 * Copyright 2006,2007,2008 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 3, 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.
27 #include <gr_ofdm_frame_acquisition.h>
28 #include <gr_io_signature.h>
34 #define M_TWOPI (2*M_PI)
35 #define MAX_NUM_SYMBOLS 1000
37 gr_ofdm_frame_acquisition_sptr
38 gr_make_ofdm_frame_acquisition (unsigned int occupied_carriers, unsigned int fft_length,
40 const std::vector<gr_complex> &known_symbol,
41 unsigned int max_fft_shift_len)
43 return gr_ofdm_frame_acquisition_sptr (new gr_ofdm_frame_acquisition (occupied_carriers, fft_length, cplen,
44 known_symbol, max_fft_shift_len));
47 gr_ofdm_frame_acquisition::gr_ofdm_frame_acquisition (unsigned occupied_carriers, unsigned int fft_length,
49 const std::vector<gr_complex> &known_symbol,
50 unsigned int max_fft_shift_len)
51 : gr_block ("ofdm_frame_acquisition",
52 gr_make_io_signature2 (2, 2, sizeof(gr_complex)*fft_length, sizeof(char)*fft_length),
53 gr_make_io_signature2 (2, 2, sizeof(gr_complex)*occupied_carriers, sizeof(char))),
54 d_occupied_carriers(occupied_carriers),
55 d_fft_length(fft_length),
57 d_freq_shift_len(max_fft_shift_len),
58 d_known_symbol(known_symbol),
62 d_symbol_phase_diff.resize(d_fft_length);
63 d_known_phase_diff.resize(d_occupied_carriers);
64 d_hestimate.resize(d_occupied_carriers);
66 unsigned int i = 0, j = 0;
68 std::fill(d_known_phase_diff.begin(), d_known_phase_diff.end(), 0);
69 for(i = 0; i < d_known_symbol.size()-2; i+=2) {
70 d_known_phase_diff[i] = norm(d_known_symbol[i] - d_known_symbol[i+2]);
73 d_phase_lut = new gr_complex[(2*d_freq_shift_len+1) * MAX_NUM_SYMBOLS];
74 for(i = 0; i <= 2*d_freq_shift_len; i++) {
75 for(j = 0; j < MAX_NUM_SYMBOLS; j++) {
76 d_phase_lut[j + i*MAX_NUM_SYMBOLS] = gr_expj(-M_TWOPI*d_cplen/d_fft_length*(i-d_freq_shift_len)*j);
81 gr_ofdm_frame_acquisition::~gr_ofdm_frame_acquisition(void)
83 delete [] d_phase_lut;
87 gr_ofdm_frame_acquisition::forecast (int noutput_items, gr_vector_int &ninput_items_required)
89 unsigned ninputs = ninput_items_required.size ();
90 for (unsigned i = 0; i < ninputs; i++)
91 ninput_items_required[i] = 1;
95 gr_ofdm_frame_acquisition::coarse_freq_comp(int freq_delta, int symbol_count)
97 // return gr_complex(cos(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count),
98 // sin(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count));
100 return gr_expj(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count);
102 //return d_phase_lut[MAX_NUM_SYMBOLS * (d_freq_shift_len + freq_delta) + symbol_count];
106 gr_ofdm_frame_acquisition::correlate(const gr_complex *symbol, int zeros_on_left)
110 std::fill(d_symbol_phase_diff.begin(), d_symbol_phase_diff.end(), 0);
111 for(i = 0; i < d_fft_length-2; i++) {
112 d_symbol_phase_diff[i] = norm(symbol[i] - symbol[i+2]);
115 // sweep through all possible/allowed frequency offsets and select the best
117 float max = 0, sum=0;
118 for(i = zeros_on_left - d_freq_shift_len; i < zeros_on_left + d_freq_shift_len; i++) {
120 for(j = 0; j < d_occupied_carriers; j++) {
121 sum += (d_known_phase_diff[j] * d_symbol_phase_diff[i+j]);
129 // set the coarse frequency offset relative to the edge of the occupied tones
130 d_coarse_freq = index - zeros_on_left;
134 gr_ofdm_frame_acquisition::calculate_equalizer(const gr_complex *symbol, int zeros_on_left)
138 // Set first tap of equalizer
139 d_hestimate[0] = d_known_symbol[0] /
140 (coarse_freq_comp(d_coarse_freq,1)*symbol[zeros_on_left+d_coarse_freq]);
142 // set every even tap based on known symbol
143 // linearly interpolate between set carriers to set zero-filled carriers
144 // FIXME: is this the best way to set this?
145 for(i = 2; i < d_occupied_carriers; i+=2) {
146 d_hestimate[i] = d_known_symbol[i] /
147 (coarse_freq_comp(d_coarse_freq,1)*(symbol[i+zeros_on_left+d_coarse_freq]));
148 d_hestimate[i-1] = (d_hestimate[i] + d_hestimate[i-2]) / gr_complex(2.0, 0.0);
151 // with even number of carriers; last equalizer tap is wrong
152 if(!(d_occupied_carriers & 1)) {
153 d_hestimate[d_occupied_carriers-1] = d_hestimate[d_occupied_carriers-2];
157 fprintf(stderr, "Equalizer setting:\n");
158 for(i = 0; i < d_occupied_carriers; i++) {
159 gr_complex sym = coarse_freq_comp(d_coarse_freq,1)*symbol[i+zeros_on_left+d_coarse_freq];
160 gr_complex output = sym * d_hestimate[i];
161 fprintf(stderr, "sym: %+.4f + j%+.4f ks: %+.4f + j%+.4f eq: %+.4f + j%+.4f ==> %+.4f + j%+.4f\n",
162 sym .real(), sym.imag(),
163 d_known_symbol[i].real(), d_known_symbol[i].imag(),
164 d_hestimate[i].real(), d_hestimate[i].imag(),
165 output.real(), output.imag());
167 fprintf(stderr, "\n");
172 gr_ofdm_frame_acquisition::general_work(int noutput_items,
173 gr_vector_int &ninput_items,
174 gr_vector_const_void_star &input_items,
175 gr_vector_void_star &output_items)
177 const gr_complex *symbol = (const gr_complex *)input_items[0];
178 const char *signal_in = (const char *)input_items[1];
180 gr_complex *out = (gr_complex *) output_items[0];
181 char *signal_out = (char *) output_items[1];
183 int unoccupied_carriers = d_fft_length - d_occupied_carriers;
184 int zeros_on_left = (int)ceil(unoccupied_carriers/2.0);
188 correlate(symbol, zeros_on_left);
189 calculate_equalizer(symbol, zeros_on_left);
196 for(unsigned int i = 0; i < d_occupied_carriers; i++) {
197 out[i] = d_hestimate[i]*coarse_freq_comp(d_coarse_freq,d_phase_count)
198 *symbol[i+zeros_on_left+d_coarse_freq];
202 if(d_phase_count == MAX_NUM_SYMBOLS) {