Merging ofdm2 branch -r7047:7321 into trunk. This updates the OFDM code to hier_block...
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_frame_acquisition.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006, 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 #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
32 #define VERBOSE 0
33 #define M_TWOPI (2*M_PI)
34 #define MAX_NUM_SYMBOLS 1000
35
36 gr_ofdm_frame_acquisition_sptr
37 gr_make_ofdm_frame_acquisition (unsigned int occupied_carriers, unsigned int fft_length, 
38                                 unsigned int cplen,
39                                 const std::vector<gr_complex> &known_symbol,
40                                 unsigned int max_fft_shift_len)
41 {
42   return gr_ofdm_frame_acquisition_sptr (new gr_ofdm_frame_acquisition (occupied_carriers, fft_length, cplen,
43                                                                         known_symbol, max_fft_shift_len));
44 }
45
46 gr_ofdm_frame_acquisition::gr_ofdm_frame_acquisition (unsigned occupied_carriers, unsigned int fft_length, 
47                                                       unsigned int cplen,
48                                                       const std::vector<gr_complex> &known_symbol,
49                                                       unsigned int max_fft_shift_len)
50   : gr_block ("ofdm_frame_acquisition",
51               gr_make_io_signature2 (2, 2, sizeof(gr_complex)*fft_length, sizeof(char)),
52               gr_make_io_signature2 (2, 2, sizeof(gr_complex)*occupied_carriers, sizeof(char))),
53     d_occupied_carriers(occupied_carriers),
54     d_fft_length(fft_length),
55     d_cplen(cplen),
56     d_freq_shift_len(max_fft_shift_len),
57     d_known_symbol(known_symbol),
58     d_coarse_freq(0),
59     d_phase_count(0)
60 {
61   d_symbol_phase_diff.resize(d_fft_length);
62   d_known_phase_diff.resize(d_occupied_carriers);
63   d_hestimate.resize(d_occupied_carriers);
64
65   unsigned int i = 0, j = 0;
66
67   std::fill(d_known_phase_diff.begin(), d_known_phase_diff.end(), 0);
68   for(i = 0; i < d_known_symbol.size()-2; i+=2) {
69     d_known_phase_diff[i] = fabs(gr_fast_atan2f(d_known_symbol[i]) - gr_fast_atan2f(d_known_symbol[i+2]));
70   }
71   
72   d_phase_lut = new gr_complex[(2*d_freq_shift_len+1) * MAX_NUM_SYMBOLS];
73   for(i = 0; i <= 2*d_freq_shift_len; i++) {
74     for(j = 0; j < MAX_NUM_SYMBOLS; j++) {
75       d_phase_lut[j + i*MAX_NUM_SYMBOLS] =  gr_expj(-M_TWOPI*d_cplen/d_fft_length*(i-d_freq_shift_len)*j);
76     }
77   }
78 }
79
80 gr_ofdm_frame_acquisition::~gr_ofdm_frame_acquisition(void)
81 {
82   delete [] d_phase_lut;
83 }
84
85 void
86 gr_ofdm_frame_acquisition::forecast (int noutput_items, gr_vector_int &ninput_items_required)
87 {
88   unsigned ninputs = ninput_items_required.size ();
89   for (unsigned i = 0; i < ninputs; i++)
90     ninput_items_required[i] = 1;
91 }
92
93 gr_complex
94 gr_ofdm_frame_acquisition::coarse_freq_comp(int freq_delta, int symbol_count)
95 {
96   //  return gr_complex(cos(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count),
97   //        sin(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count));
98
99   return gr_expj(-M_TWOPI*freq_delta*d_cplen/d_fft_length*symbol_count);
100
101   //return d_phase_lut[MAX_NUM_SYMBOLS * (d_freq_shift_len + freq_delta) + symbol_count];
102 }
103
104
105
106 bool
107 gr_ofdm_frame_acquisition::correlate(const gr_complex *symbol, int zeros_on_left)
108 {
109   unsigned int i = 0, j = 0;
110
111   std::fill(d_symbol_phase_diff.begin(), d_symbol_phase_diff.end(), 0);
112   for(i = 0; i < d_fft_length-2; i++) {
113     d_symbol_phase_diff[i] = fabs(gr_fast_atan2f(symbol[i]) - gr_fast_atan2f(symbol[i+2]));
114   }
115
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+=2) {
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(fabs(sum) > max) {
124       max = sum;
125       index = i;
126     }
127   }
128
129   d_coarse_freq = index - zeros_on_left;
130
131   if(VERBOSE) {
132     fprintf(stderr, "Coarse Freq Offset: %d\n", d_coarse_freq);
133     for(i = 0; i < 40; i++) {
134       fprintf(stderr, "%+.4f   %+.4f\n", d_known_phase_diff[i], 
135               d_symbol_phase_diff[zeros_on_left+d_coarse_freq+i]);
136     }
137   }
138
139   return true;  //FIXME: don't need ot return anything now
140 }
141
142 void
143 gr_ofdm_frame_acquisition::calculate_equalizer(const gr_complex *symbol, int zeros_on_left)
144 {
145   unsigned int i=0;
146
147   // Set first tap of equalizer
148   d_hestimate[0] = d_known_symbol[0] / 
149     (coarse_freq_comp(d_coarse_freq,1)*symbol[zeros_on_left+d_coarse_freq]);
150
151   // set every even tap based on known symbol
152   // linearly interpolate between set carriers to set zero-filled carriers
153   // FIXME: is this the best way to set this?
154   for(i = 2; i < d_occupied_carriers; i+=2) {
155     d_hestimate[i] = d_known_symbol[i] / 
156       (coarse_freq_comp(d_coarse_freq,1)*(symbol[i+zeros_on_left+d_coarse_freq]));
157     d_hestimate[i-1] = (d_hestimate[i] + d_hestimate[i-2]) / gr_complex(2.0, 0.0);    
158   }
159
160   // with even number of carriers; last equalizer tap is wrong
161   if(!(d_occupied_carriers & 1)) {
162     d_hestimate[d_occupied_carriers-1] = d_hestimate[d_occupied_carriers-2];
163   }
164
165   if(VERBOSE) {
166     fprintf(stderr, "Equalizer setting:\n");
167     for(i = 0; i < d_occupied_carriers; i++) {
168       gr_complex sym = coarse_freq_comp(d_coarse_freq,1)*symbol[i+zeros_on_left+d_coarse_freq];
169       gr_complex output = sym * d_hestimate[i];
170       fprintf(stderr, "sym: %+.4f + j%+.4f  ks: %+.4f + j%+.4f  eq: %+.4f + j%+.4f  ==>  %+.4f + j%+.4f\n", 
171               sym .real(), sym.imag(),
172               d_known_symbol[i].real(), d_known_symbol[i].imag(),
173               d_hestimate[i].real(), d_hestimate[i].imag(),
174               output.real(), output.imag());
175     }
176     fprintf(stderr, "\n");
177   }
178 }
179
180 int
181 gr_ofdm_frame_acquisition::general_work(int noutput_items,
182                                         gr_vector_int &ninput_items,
183                                         gr_vector_const_void_star &input_items,
184                                         gr_vector_void_star &output_items)
185 {
186   const gr_complex *symbol = (const gr_complex *)input_items[0];
187   const char *trigger = (const char *)input_items[1];
188
189   gr_complex *out = (gr_complex *) output_items[0];
190   char *sig = (char *) output_items[1];
191   
192   int unoccupied_carriers = d_fft_length - d_occupied_carriers;
193   int zeros_on_left = (int)ceil(unoccupied_carriers/2.0);
194
195   int found = 0;
196   for(int i = 0; i < ninput_items[1]; i++) {
197     found += trigger[i];
198   }
199
200   if(found) {
201     d_phase_count = 1;
202     correlate(symbol, zeros_on_left);
203     calculate_equalizer(symbol, zeros_on_left);
204     sig[0] = 1;
205   }
206   else {
207     sig[0] = 0;
208   }
209
210   for(unsigned int i = 0; i < d_occupied_carriers; i++) {
211     out[i] = d_hestimate[i]*coarse_freq_comp(d_coarse_freq,d_phase_count)
212       *symbol[i+zeros_on_left+d_coarse_freq];
213   }
214   
215   d_phase_count++;
216   if(d_phase_count == MAX_NUM_SYMBOLS) {
217     d_phase_count = 1;
218   }
219
220   consume(0,1);
221   consume(1,ninput_items[1]);
222   return 1;
223 }