merging branch trondeau/ofdm r7971:8143. Fixing subcarriers and other issues to impro...
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_sampler.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 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_sampler.h>
28 #include <gr_io_signature.h>
29 #include <gr_expj.h>
30
31 gr_ofdm_sampler_sptr
32 gr_make_ofdm_sampler (unsigned int fft_length, 
33                       unsigned int symbol_length,
34                       unsigned int timeout)
35 {
36   return gr_ofdm_sampler_sptr (new gr_ofdm_sampler (fft_length, symbol_length, timeout));
37 }
38
39 gr_ofdm_sampler::gr_ofdm_sampler (unsigned int fft_length, 
40                                   unsigned int symbol_length,
41                                   unsigned int timeout)
42   : gr_block ("ofdm_sampler",
43               gr_make_io_signature2 (2, 2, sizeof (gr_complex), sizeof(char)),
44               gr_make_io_signature2 (2, 2, sizeof (gr_complex)*fft_length, sizeof(char)*fft_length)),
45     d_state(STATE_NO_SIG), d_timeout_max(timeout), d_fft_length(fft_length), d_symbol_length(symbol_length)
46 {
47 }
48
49 void
50 gr_ofdm_sampler::forecast (int noutput_items, gr_vector_int &ninput_items_required)
51 {
52   // FIXME do we need more
53   //int nreqd  = (noutput_items-1) * d_symbol_length + d_fft_length;
54   int nreqd  = d_symbol_length + d_fft_length;
55   unsigned ninputs = ninput_items_required.size ();
56   for (unsigned i = 0; i < ninputs; i++)
57     ninput_items_required[i] = nreqd;
58 }
59
60
61 int
62 gr_ofdm_sampler::general_work (int noutput_items,
63                                gr_vector_int &ninput_items,
64                                gr_vector_const_void_star &input_items,
65                                gr_vector_void_star &output_items)
66 {
67   const gr_complex *iptr = (const gr_complex *) input_items[0];
68   const char *trigger = (const char *) input_items[1];
69
70   gr_complex *optr = (gr_complex *) output_items[0];
71   char *outsig = (char *) output_items[1];
72
73   //FIXME: we only process a single OFDM symbol at a time; after the preamble, we can 
74   // process a few at a time as long as we always look out for the next preamble.
75
76   unsigned int index=d_fft_length;  // start one fft length into the input so we can always look back this far
77
78   outsig[0] = 0; // set output to no signal by default
79
80   // Search for a preamble trigger signal during the next symbol length
81   while((d_state != STATE_PREAMBLE) && (index <= (d_symbol_length+d_fft_length))) {
82     if(trigger[index]) {
83       outsig[0] = 1; // tell the next block there is a preamble coming
84       d_state = STATE_PREAMBLE;
85     }
86     else
87       index++;
88   }
89   
90   unsigned int i, pos, ret;
91   switch(d_state) {
92   case(STATE_PREAMBLE):
93     // When we found a preamble trigger, get it and set the symbol boundary here
94     for(i = (index - d_fft_length + 1); i <= index; i++) {
95       *optr++ = iptr[i];
96     }
97     
98     d_timeout = d_timeout_max; // tell the system to expect at least this many symbols for a frame
99     d_state = STATE_FRAME;
100     consume_each(index - d_fft_length + 1); // consume up to one fft_length away to keep the history
101     ret = 1;
102     break;
103     
104   case(STATE_FRAME):
105     // use this state when we have processed a preamble and are getting the rest of the frames
106     //FIXME: we could also have a power squelch system here to enter STATE_NO_SIG if no power is received
107
108     // skip over fft length history and cyclic prefix
109     pos = d_symbol_length;         // keeps track of where we are in the input buffer
110     while(pos < d_symbol_length + d_fft_length) {
111       *optr++ = iptr[pos++];
112     }
113
114     if(d_timeout-- == 0) {
115       printf("TIMEOUT\n");
116       d_state = STATE_NO_SIG;
117     }
118
119     consume_each(d_symbol_length); // jump up by 1 fft length and the cyclic prefix length
120     ret = 1;
121     break;
122
123   case(STATE_NO_SIG):
124   default:
125     consume_each(index-d_fft_length); // consume everything we've gone through so far leaving the fft length history
126     ret = 0;
127     break;
128   }
129
130   return ret;
131 }