Merging OFDM features branch r5661:5759 into trunk. OFDM works over the air with...
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_insert_preamble.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 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <gr_ofdm_insert_preamble.h>
27 #include <gr_io_signature.h>
28 #include <stdexcept>
29 #include <iostream>
30
31 gr_ofdm_insert_preamble_sptr
32 gr_make_ofdm_insert_preamble(int fft_length,
33                              const std::vector<std::vector<gr_complex> > &preamble)
34 {
35   return gr_ofdm_insert_preamble_sptr(new gr_ofdm_insert_preamble(fft_length,
36                                                                   preamble));
37 }
38
39 gr_ofdm_insert_preamble::gr_ofdm_insert_preamble
40        (int fft_length,
41         const std::vector<std::vector<gr_complex> > &preamble)
42   : gr_block("ofdm_insert_preamble",
43              gr_make_io_signature2(2, 2,
44                                    sizeof(gr_complex)*fft_length,
45                                    sizeof(char)),
46              gr_make_io_signature2(1, 2,
47                                    sizeof(gr_complex)*fft_length,
48                                    sizeof(char))),
49     d_fft_length(fft_length),
50     d_preamble(preamble),
51     d_state(ST_IDLE),
52     d_nsymbols_output(0),
53     d_pending_flag(0)
54 {
55   // sanity check preamble symbols
56   for (size_t i = 0; i < d_preamble.size(); i++){
57     if (d_preamble[i].size() != (size_t) d_fft_length)
58       throw std::invalid_argument("gr_ofdm_insert_preamble: invalid length for preamble symbol");
59   }
60
61   enter_idle();
62 }
63
64
65 gr_ofdm_insert_preamble::~gr_ofdm_insert_preamble()
66 {
67 }
68
69 int
70 gr_ofdm_insert_preamble::general_work (int noutput_items,
71                                        gr_vector_int &ninput_items_v,
72                                        gr_vector_const_void_star &input_items,
73                                        gr_vector_void_star &output_items)
74 {
75   int ninput_items = std::min(ninput_items_v[0], ninput_items_v[1]);
76   const gr_complex *in_sym = (const gr_complex *) input_items[0];
77   const unsigned char *in_flag = (const unsigned char *) input_items[1];
78
79   gr_complex *out_sym = (gr_complex *) output_items[0];
80   unsigned char *out_flag = 0;
81   if (output_items.size() == 2)
82     out_flag = (unsigned char *) output_items[1];
83
84
85   int no = 0;   // number items output
86   int ni = 0;   // number items read from input
87
88
89 #define write_out_flag()                        \
90   do { if (out_flag)                            \
91           out_flag[no] = d_pending_flag;        \
92        d_pending_flag = 0;                      \
93   } while(0)
94
95
96   while (no < noutput_items && ni < ninput_items){
97     switch(d_state){
98     case ST_IDLE:
99       if (in_flag[ni] & 0x1)    // this is first symbol of new payload
100         enter_preamble();
101       else
102         ni++;                   // eat one input symbol
103       break;
104       
105     case ST_PREAMBLE:
106       assert(in_flag[ni] & 0x1);
107       if (d_nsymbols_output >= (int) d_preamble.size()){
108         // we've output all the preamble
109         enter_first_payload();
110       }
111       else {
112         memcpy(&out_sym[no * d_fft_length],
113                &d_preamble[d_nsymbols_output][0],
114                d_fft_length*sizeof(gr_complex));
115
116         write_out_flag();
117         no++;
118         d_nsymbols_output++;
119       }
120       break;
121       
122     case ST_FIRST_PAYLOAD:
123       // copy first payload symbol from input to output
124       memcpy(&out_sym[no * d_fft_length],
125              &in_sym[ni * d_fft_length],
126              d_fft_length * sizeof(gr_complex));
127
128       write_out_flag();
129       no++;
130       ni++;
131       enter_payload();
132       break;
133       
134     case ST_PAYLOAD:
135       if (in_flag[ni] & 0x1){   // this is first symbol of a new payload
136         enter_preamble();
137         break;
138       }
139
140       // copy a symbol from input to output
141       memcpy(&out_sym[no * d_fft_length],
142              &in_sym[ni * d_fft_length],
143              d_fft_length * sizeof(gr_complex));
144
145       write_out_flag();
146       no++;
147       ni++;
148       break;
149
150     default:
151       std::cerr << "gr_ofdm_insert_preamble: (can't happen) invalid state, resetting\n";
152       enter_idle();
153     }
154   }
155
156   consume_each(ni);
157   return no;
158 }
159
160 void
161 gr_ofdm_insert_preamble::enter_idle()
162 {
163   d_state = ST_IDLE;
164   d_nsymbols_output = 0;
165   d_pending_flag = 0;
166 }
167
168 void
169 gr_ofdm_insert_preamble::enter_preamble()
170 {
171   d_state = ST_PREAMBLE;
172   d_nsymbols_output = 0;
173   d_pending_flag = 1;
174 }
175
176 void
177 gr_ofdm_insert_preamble::enter_first_payload()
178 {
179   d_state = ST_FIRST_PAYLOAD;
180 }
181
182 void
183 gr_ofdm_insert_preamble::enter_payload()
184 {
185   d_state = ST_PAYLOAD;
186 }