f0b52c3255e0318d8091c0404a3e2befc0941e74
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_bpsk_mapper.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
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_bpsk_mapper.h>
28 #include <gr_io_signature.h>
29 #include <vector>
30
31 gr_ofdm_bpsk_mapper_sptr
32 gr_make_ofdm_bpsk_mapper (unsigned int msgq_limit, 
33                           unsigned int occupied_carriers, unsigned int fft_length,
34                           const std::vector<gr_complex> &known_symbol1, 
35                           const std::vector<gr_complex> &known_symbol2)
36 {
37   return gr_ofdm_bpsk_mapper_sptr (new gr_ofdm_bpsk_mapper (msgq_limit, occupied_carriers, fft_length, 
38                                                             known_symbol1, known_symbol2));
39 }
40
41 // Consumes 1 packet and produces as many OFDM symbols of fft_length to hold the full packet
42 gr_ofdm_bpsk_mapper::gr_ofdm_bpsk_mapper (unsigned int msgq_limit, 
43                                           unsigned int occupied_carriers, unsigned int fft_length,
44                                           const std::vector<gr_complex> &known_symbol1, 
45                                           const std::vector<gr_complex> &known_symbol2)
46   : gr_sync_block ("ofdm_bpsk_mapper",
47                    gr_make_io_signature (0, 0, 0),
48                    gr_make_io_signature2 (1, 2, sizeof(gr_complex)*fft_length, sizeof(char))),
49     d_msgq(gr_make_msg_queue(msgq_limit)), d_msg_offset(0), d_eof(false),
50     d_occupied_carriers(occupied_carriers),
51     d_fft_length(fft_length),
52     d_bit_offset(0),
53     d_header_sent(0),
54     d_known_symbol1(known_symbol1),
55     d_known_symbol2(known_symbol2)
56 {
57   assert(d_occupied_carriers <= d_fft_length);
58   assert(d_occupied_carriers == d_known_symbol1.size());
59   assert(d_occupied_carriers == d_known_symbol2.size());
60 }
61
62 gr_ofdm_bpsk_mapper::~gr_ofdm_bpsk_mapper(void)
63 {
64 }
65
66 float randombit()
67 {
68   int r = rand()&1;
69   return (float)(-1 + 2*r);
70 }
71
72 int
73 gr_ofdm_bpsk_mapper::work(int noutput_items,
74                           gr_vector_const_void_star &input_items,
75                           gr_vector_void_star &output_items)
76 {
77   gr_complex *out = (gr_complex *)output_items[0];
78   
79   unsigned int i=0;
80   unsigned int unoccupied_carriers = d_fft_length - d_occupied_carriers;
81   unsigned int zeros_on_left = (unsigned)ceil(unoccupied_carriers/2.0);
82
83   //printf("OFDM BPSK Mapper:  ninput_items: %d   noutput_items: %d\n", ninput_items[0], noutput_items);
84
85   if(d_eof) {
86     return -1;
87   }
88   
89   if(!d_msg) {
90     d_msg = d_msgq->delete_head();         // block, waiting for a message
91     d_msg_offset = 0;
92     d_bit_offset = 0;
93     d_header_sent = 0;
94     
95     if((d_msg->length() == 0) && (d_msg->type() == 1)) {
96       d_msg.reset();
97       return -1;
98     }
99   }
100
101   if(output_items.size() == 2) {
102     char *sig = (char *)output_items[1];
103     if(d_header_sent == 1) {
104       sig[0] = 1;
105     }
106     else {
107       sig[0] = 0;
108     }
109   }
110   
111   // Build a single symbol:
112
113   // Initialize all bins to 0 to set unused carriers
114   memset(out, 0, d_fft_length*sizeof(gr_complex));
115   
116   if(d_header_sent == 0) {
117      for(i=0; i < d_occupied_carriers; i++) {
118        out[i+zeros_on_left] = d_known_symbol1[i];
119      }
120     d_header_sent++;
121
122     return 1;
123   }
124   
125   if(d_header_sent == 1) {
126     for(i=0; i < d_occupied_carriers; i++) {
127       out[i+zeros_on_left] = d_known_symbol2[i];
128     }
129     d_header_sent++;
130
131     return 1;
132   }
133
134   i = 0;
135   while((d_msg_offset < d_msg->length()) && (i < d_occupied_carriers)) {
136     unsigned char bit = (d_msg->msg()[d_msg_offset] >> (d_bit_offset)) & 0x01;
137     out[i + zeros_on_left] = gr_complex(-1+2*(bit));
138     i++;
139     d_bit_offset++;
140     if(d_bit_offset == 8) {
141       d_bit_offset = 0;
142       d_msg_offset++;
143     }
144   }
145
146   // Ran out of data to put in symbol
147   if (d_msg_offset == d_msg->length()) {
148     while(i < d_occupied_carriers) {   // finish filling out the symbol
149       out[i + zeros_on_left] = gr_complex(randombit(),0);
150       i++;
151     }
152
153     if (d_msg->type() == 1)                // type == 1 sets EOF
154       d_eof = true;
155     d_msg.reset();   // finished packet, free message
156  
157     assert(d_bit_offset == 0);
158     return 1;          // produced one symbol
159   }
160   
161   return 1;  // produced symbol
162 }
163