44a6847af3ffb106a297ef38ff438bdd5f94a738
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_correlator.h
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 2, 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 #ifndef INCLUDED_GR_OFDM_CORRELATOR_H
24 #define INCLUDED_GR_OFDM_CORRELATOR_H
25
26
27 #include <gr_block.h>
28 #include <vector>
29
30 class gr_ofdm_correlator;
31 typedef boost::shared_ptr<gr_ofdm_correlator> gr_ofdm_correlator_sptr;
32
33 gr_ofdm_correlator_sptr 
34 gr_make_ofdm_correlator (unsigned int occupied_carriers, unsigned int fft_length,
35                          unsigned int cplen,
36                          const std::vector<gr_complex> &known_symbol1, 
37                          const std::vector<gr_complex> &known_symbol2);
38
39 /*!
40  * \brief take a vector of complex constellation points in from an FFT
41  * and performs a correlation and equalization.
42  * \inblock blocks
43  *
44  * This block takes the output of an FFT of a received OFDM symbol and finds the 
45  * start of a frame based on two known symbols. It also looks at the surrounding
46  * bins in the FFT output for the correlation in case there is a large frequency
47  * shift in the data. This block assumes that the fine frequency shift has already
48  * been corrected and that the samples fall in the middle of one FFT bin.
49  *
50  * It then uses one of those known
51  * symbols to estimate the channel response over all subcarriers and does a simple 
52  * 1-tap equalization on all subcarriers. This corrects for the phase and amplitude
53  * distortion caused by the channel.
54  */
55
56 class gr_ofdm_correlator : public gr_block
57 {
58   /*! 
59    * \brief Build an OFDM correlator and equalizer.
60    * \param occupied_carriers   The number of subcarriers with data in the received symbol
61    * \param fft_length          The size of the FFT vector (occupied_carriers + unused carriers)
62    * \param known_symbol1       A vector of complex numbers representing a known symbol at the
63    *                            start of a frame (usually a BPSK PN sequence)
64    * \param known_symbol2       A vector of complex numbers representing a known symbol at the
65    *                            start of a frame after known_symbol1 (usually a BPSK PN sequence). 
66    *                            Both of these start symbols are differentially correlated to compensate
67    *                            for phase changes between symbols. 
68    */
69   friend gr_ofdm_correlator_sptr
70   gr_make_ofdm_correlator (unsigned int occupied_carriers, unsigned int fft_length,
71                            unsigned int cplen,
72                            const std::vector<gr_complex> &known_symbol1, 
73                            const std::vector<gr_complex> &known_symbol2);
74   
75  protected:
76   gr_ofdm_correlator (unsigned int occupied_carriers, unsigned int fft_length,
77                       unsigned int cplen,
78                       const std::vector<gr_complex> &known_symbol1, 
79                       const std::vector<gr_complex> &known_symbol2);
80   
81  private:
82   unsigned char slicer(gr_complex x);
83   bool correlate(const gr_complex *previous, const gr_complex *current, int zeros_on_left);
84   void calculate_equalizer(const gr_complex *previous, 
85                            const gr_complex *current, int zeros_on_left);
86   gr_complex coarse_freq_comp(int freq_delta, int count);
87   
88   unsigned int d_occupied_carriers;  // !< \brief number of subcarriers with data
89   unsigned int d_fft_length;         // !< \brief length of FFT vector
90   unsigned int d_cplen;              // !< \brief length of cyclic prefix in samples
91   unsigned int d_freq_shift_len;     // !< \brief number of surrounding bins to look at for correlation
92   std::vector<gr_complex> d_known_symbol1, d_known_symbol2; // !< \brief known symbols at start of frame
93   std::vector<gr_complex> d_diff_corr_factor; // !< \brief factor used in correlation
94   std::vector<gr_complex> d_hestimate;  // !< channel estimate
95   signed int d_coarse_freq;             // !< \brief search distance in number of bins
96   unsigned int d_phase_count;           // !< \brief accumulator for coarse freq correction
97   float d_snr_est;                      // !< an estimation of the signal to noise ratio
98
99   void forecast(int noutput_items, gr_vector_int &ninput_items_required);
100
101  public:
102   /*!
103    * \brief Return an estimate of the SNR of the channel
104    */
105   float snr() { return d_snr_est; }
106
107   ~gr_ofdm_correlator(void);
108   int general_work(int noutput_items,
109                    gr_vector_int &ninput_items,
110                    gr_vector_const_void_star &input_items,
111                    gr_vector_void_star &output_items);
112 };
113
114
115 #endif