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