Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_frame_acquisition.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007,2008 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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_ofdm_frame_acquisition.h>
28 #include <gr_io_signature.h>
29 #include <gr_expj.h>
30 #include <gr_math.h>
31 #include <cstdio>
32
33 #define VERBOSE 0
34 #define M_TWOPI (2*M_PI)
35 #define MAX_NUM_SYMBOLS 1000
36
37 gr_ofdm_frame_acquisition_sptr
38 gr_make_ofdm_frame_acquisition (unsigned int occupied_carriers, unsigned int fft_length, 
39                                 unsigned int cplen,
40                                 const std::vector<gr_complex> &known_symbol,
41                                 unsigned int max_fft_shift_len)
42 {
43   return gr_ofdm_frame_acquisition_sptr (new gr_ofdm_frame_acquisition (occupied_carriers, fft_length, cplen,
44                                                                         known_symbol, max_fft_shift_len));
45 }
46
47 gr_ofdm_frame_acquisition::gr_ofdm_frame_acquisition (unsigned occupied_carriers, unsigned int fft_length, 
48                                                       unsigned int cplen,
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),
56     d_cplen(cplen),
57     d_freq_shift_len(max_fft_shift_len),
58     d_known_symbol(known_symbol),
59     d_coarse_freq(0),
60     d_phase_count(0)
61 {
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);
65
66   unsigned int i = 0, j = 0;
67
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]);
71   }
72   
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);
77     }
78   }
79 }
80
81 gr_ofdm_frame_acquisition::~gr_ofdm_frame_acquisition(void)
82 {
83   delete [] d_phase_lut;
84 }
85
86 void
87 gr_ofdm_frame_acquisition::forecast (int noutput_items, gr_vector_int &ninput_items_required)
88 {
89   unsigned ninputs = ninput_items_required.size ();
90   for (unsigned i = 0; i < ninputs; i++)
91     ninput_items_required[i] = 1;
92 }
93
94 gr_complex
95 gr_ofdm_frame_acquisition::coarse_freq_comp(int freq_delta, int symbol_count)
96 {
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));
99
100   return gr_expj(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count);
101
102   //return d_phase_lut[MAX_NUM_SYMBOLS * (d_freq_shift_len + freq_delta) + symbol_count];
103 }
104
105 void
106 gr_ofdm_frame_acquisition::correlate(const gr_complex *symbol, int zeros_on_left)
107 {
108   unsigned int i,j;
109   
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]);
113   }
114
115   // sweep through all possible/allowed frequency offsets and select the best
116   int index = 0;
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++) {
119     sum = 0;
120     for(j = 0; j < d_occupied_carriers; j++) {
121       sum += (d_known_phase_diff[j] * d_symbol_phase_diff[i+j]);
122     }
123     if(sum > max) {
124       max = sum;
125       index = i;
126     }
127   }
128   
129   // set the coarse frequency offset relative to the edge of the occupied tones
130   d_coarse_freq = index - zeros_on_left;
131 }
132
133 void
134 gr_ofdm_frame_acquisition::calculate_equalizer(const gr_complex *symbol, int zeros_on_left)
135 {
136   unsigned int i=0;
137
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]);
141
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);    
149   }
150
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];
154   }
155
156   if(VERBOSE) {
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());
166     }
167     fprintf(stderr, "\n");
168   }
169 }
170
171 int
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)
176 {
177   const gr_complex *symbol = (const gr_complex *)input_items[0];
178   const char *signal_in = (const char *)input_items[1];
179
180   gr_complex *out = (gr_complex *) output_items[0];
181   char *signal_out = (char *) output_items[1];
182   
183   int unoccupied_carriers = d_fft_length - d_occupied_carriers;
184   int zeros_on_left = (int)ceil(unoccupied_carriers/2.0);
185
186   if(signal_in[0]) {
187     d_phase_count = 1;
188     correlate(symbol, zeros_on_left);
189     calculate_equalizer(symbol, zeros_on_left);
190     signal_out[0] = 1;
191   }
192   else {
193     signal_out[0] = 0;
194   } 
195
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];
199   }
200   
201   d_phase_count++;
202   if(d_phase_count == MAX_NUM_SYMBOLS) {
203     d_phase_count = 1;
204   }
205
206   consume_each(1);
207   return 1;
208 }