Merging ofdm2 branch -r7047:7321 into trunk. This updates the OFDM code to hier_block...
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_sampler.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 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_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 {
35   return gr_ofdm_sampler_sptr (new gr_ofdm_sampler (fft_length, symbol_length));
36 }
37
38 gr_ofdm_sampler::gr_ofdm_sampler (unsigned int fft_length, 
39                                   unsigned int symbol_length)
40   : gr_block ("ofdm_sampler",
41               gr_make_io_signature2 (2, 2, sizeof (gr_complex), sizeof(char)),
42               gr_make_io_signature (1, 1, sizeof (gr_complex)*fft_length)),
43     d_fft_length(fft_length), d_symbol_length(symbol_length)
44 {
45 }
46
47 void
48 gr_ofdm_sampler::forecast (int noutput_items, gr_vector_int &ninput_items_required)
49 {
50   // FIXME do we need more
51   int nreqd  = (noutput_items-1) * d_symbol_length + d_fft_length;
52   unsigned ninputs = ninput_items_required.size ();
53   for (unsigned i = 0; i < ninputs; i++)
54     ninput_items_required[i] = nreqd;
55 }
56
57 int
58 gr_ofdm_sampler::general_work (int noutput_items,
59                                gr_vector_int &ninput_items,
60                                gr_vector_const_void_star &input_items,
61                                gr_vector_void_star &output_items)
62 {
63   gr_complex *iptr = (gr_complex *) input_items[0];
64   char *trigger = (char *) input_items[1];
65
66   gr_complex *optr = (gr_complex *) output_items[0];
67
68   int found=0, index=0;
69
70   int i=d_fft_length-1;
71
72   // FIXME: This is where we miss if the regeneration happens too soon.
73   //while(!found && i<std::min(ninput_items[0],ninput_items[1]) ) {
74   while(i<std::min(ninput_items[0],ninput_items[1]) ) {
75     if(trigger[i]) {
76       found = 1;
77       index = i++;
78     }
79     else
80       i++;
81   }
82   
83   if(found) {
84     assert(index-d_fft_length+1 >= 0);
85     for(int j=index - d_fft_length + 1; j <= index; j++)
86       *optr++ = iptr[j];
87     consume_each(index - d_fft_length + 2);
88     //printf("OFDM Sampler found:  ninput_items: %d/%d   noutput_items: %d  consumed: %d   found: %d\n", 
89     //   ninput_items[0], ninput_items[1], noutput_items, (i-d_fft_length+2), found);
90   }
91   else {
92     consume_each(i-d_fft_length+1);
93     //printf("OFDM Sampler not found:  ninput_items: %d/%d   noutput_items: %d  consumed: %d   found: %d\n", 
94     //  ninput_items[0], ninput_items[1], noutput_items, (i-d_fft_length+1), found);
95  }
96
97   return found;
98 }