Fix compiler warnings across the tree. Adds --enable-warnings-as-errors configure...
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_qam_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 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_qam_mapper.h>
28 #include <gr_io_signature.h>
29 #include <stdexcept>
30 #include <string.h>
31
32 gr_ofdm_qam_mapper_sptr
33 gr_make_ofdm_qam_mapper (unsigned int msgq_limit, 
34                          unsigned int occupied_carriers, unsigned int fft_length,
35                          int m)
36 {
37   return gr_ofdm_qam_mapper_sptr (new gr_ofdm_qam_mapper (msgq_limit, occupied_carriers, fft_length, m));
38 }
39
40 // Consumes 1 packet and produces as many OFDM symbols of fft_length to hold the full packet
41 gr_ofdm_qam_mapper::gr_ofdm_qam_mapper (unsigned int msgq_limit, 
42                                         unsigned int occupied_carriers, unsigned int fft_length,
43                                         int m)
44   : gr_sync_block ("ofdm_qam_mapper",
45                    gr_make_io_signature (0, 0, 0),
46                    gr_make_io_signature2 (1, 2, sizeof(gr_complex)*fft_length, sizeof(char))),
47     d_msgq(gr_make_msg_queue(msgq_limit)), d_msg_offset(0), d_eof(false),
48     d_occupied_carriers(occupied_carriers),
49     d_fft_length(fft_length),
50     d_bit_offset(0),
51     d_pending_flag(0),
52     d_mod_order(m)
53 {
54   if (!(d_occupied_carriers <= d_fft_length))
55     throw std::invalid_argument("gr_ofdm_qam_mapper: occupied carriers must be <= fft_length");
56
57   bool ok = false;
58   if(m == 2) {
59     ok = true;
60   }
61   if(m == 4) {
62     ok = true;
63   }
64   if(m == 16) {
65     ok = true;
66   }
67   if(m == 64) {
68     ok = true;
69   }
70   if(m == 256) {
71     ok = true;
72   }
73   
74   if(!ok)
75     throw std::invalid_argument("Order M must be [2, 4, 16, 64, 256]");  
76
77   make_constellation();
78 }
79
80 gr_ofdm_qam_mapper::~gr_ofdm_qam_mapper(void)
81 {
82 }
83
84 void gr_ofdm_qam_mapper::make_constellation()
85 {
86   int i = 0, j = 0, ii = 0, jj = 0;
87   // number of bits/symbol (log2(M))
88   int k = (int)(log10(d_mod_order) / log10(2.0));
89
90   int a, b;
91   float re, im;
92   float coeff = 1;
93   std::vector<int> bits_i, bits_q;
94
95   int rr = 0;
96   int ss = 0;
97   int ll = bits_i.size();
98
99   for(i=0; i < d_mod_order; i++) {
100     a = (i & (0x01 << (k-1))) >> (k-1);
101     b = (i & (0x01 << (k-2))) >> (k-2);
102     for(j=2; j < k; j+=2) {
103       bits_i.push_back( (i & (0x01 << (k-j-1))) >> (k-j-1));
104       bits_q.push_back( (i & (0x01 << (k-(j+1)-1))) >> (k-(j+1)-1));
105     }
106     
107     ss = 0;
108     ll = bits_i.size();
109     for(ii = 0; ii < ll; ii++) {
110       rr = 0;
111       for(jj = 0; jj < (ll-ii); jj++) {
112         rr = abs(bits_i[jj] - rr);
113       }
114       ss += rr * pow(2.0, ii+1.0);
115     }
116     re = (2.0*a-1.0)*(ss+1.0);
117     
118     ss = 0;
119     ll = bits_q.size();
120     for(ii = 0; ii < ll; ii++) {
121       rr = 0;
122       for(jj = 0; jj < (ll-ii); jj++) {
123         rr = abs(bits_q[jj] - rr);
124       }
125       ss += rr*pow(2.0, ii+1.0);
126     }
127     im = (2.0*b-1.0)*(ss+1.0);
128       
129     a = std::max(re, im);
130     if(a > coeff) {
131       coeff = a;
132     }
133     d_constellation_map.push_back(gr_complex(re, im));
134   }
135   
136   d_constellation_map[0] = gr_complex(-3, -3);
137   d_constellation_map[1] = gr_complex(-3, -1);
138   d_constellation_map[2] = gr_complex(-3, 1);
139   d_constellation_map[3] = gr_complex(-3, 3);
140   d_constellation_map[4] = gr_complex(-1, -3);
141   d_constellation_map[5] = gr_complex(-1, -1);
142   d_constellation_map[6] = gr_complex(-1, 1);
143   d_constellation_map[7] = gr_complex(-1, 3);
144   d_constellation_map[8] = gr_complex(1, -3);
145   d_constellation_map[9] = gr_complex(1, -1);
146   d_constellation_map[10] = gr_complex(1, 1);
147   d_constellation_map[11] = gr_complex(1, 3);
148   d_constellation_map[12] = gr_complex(3, -3);
149   d_constellation_map[13] = gr_complex(3, -1);
150   d_constellation_map[14] = gr_complex(3, 1);
151   d_constellation_map[15] = gr_complex(3, 3);
152
153   coeff = sqrt(31.0)/2.0;
154   for(i = 0; i < static_cast<int>(d_constellation_map.size()); i++) {
155     d_constellation_map[i] /= coeff;
156     printf("const[%d]: %f + j%f\n", i, d_constellation_map[i].real(), d_constellation_map[i].imag());
157   }
158 }
159
160 #if 0
161 static float
162 randombit()
163 {
164   int r = rand()&1;
165   return (float)(-1 + 2*r);
166 }
167 #endif
168
169 int
170 gr_ofdm_qam_mapper::work(int noutput_items,
171                           gr_vector_const_void_star &input_items,
172                           gr_vector_void_star &output_items)
173 {
174   gr_complex *out = (gr_complex *)output_items[0];
175   
176   unsigned int i=0;
177   unsigned int unoccupied_carriers = d_fft_length - d_occupied_carriers;
178   unsigned int zeros_on_left = (unsigned)ceil(unoccupied_carriers/2.0);
179
180   //printf("OFDM QAM Mapper:  ninput_items: %d   noutput_items: %d\n", ninput_items[0], noutput_items);
181
182   if(d_eof) {
183     return -1;
184   }
185   
186   if(!d_msg) {
187     d_msg = d_msgq->delete_head();         // block, waiting for a message
188     d_msg_offset = 0;
189     d_bit_offset = 0;
190     d_pending_flag = 1;                    // new packet, write start of packet flag
191     
192     if((d_msg->length() == 0) && (d_msg->type() == 1)) {
193       d_msg.reset();
194       return -1;                // We're done; no more messages coming.
195     }
196   }
197
198   char *out_flag = 0;
199   if(output_items.size() == 2)
200     out_flag = (char *) output_items[1];
201   
202
203   // Build a single symbol:
204   
205
206   // Initialize all bins to 0 to set unused carriers
207   memset(out, 0, d_fft_length*sizeof(gr_complex));
208   
209   i = 0;
210   while((d_msg_offset < d_msg->length()) && (i < d_occupied_carriers)) {
211     unsigned char bit0 = (d_msg->msg()[d_msg_offset] >> (d_bit_offset)) & 0x01;
212     d_bit_offset++;
213     
214     unsigned char bit1 = (d_msg->msg()[d_msg_offset] >> (d_bit_offset)) & 0x01;
215     d_bit_offset++;
216     
217     unsigned char bit2 = (d_msg->msg()[d_msg_offset] >> (d_bit_offset)) & 0x01;
218     d_bit_offset++;
219     
220     unsigned char bit3 = (d_msg->msg()[d_msg_offset] >> (d_bit_offset)) & 0x01;
221     d_bit_offset++;
222
223     unsigned char bit = (bit0 << 3) | (bit1 << 2) | (bit2 << 1) | (bit3 << 0);
224     
225     out[i + zeros_on_left] = d_constellation_map[bit];
226     i++;
227     if(d_bit_offset == 8) {
228       d_bit_offset = 0;
229       d_msg_offset++;
230     }
231   }
232
233   // Ran out of data to put in symbol
234   if (d_msg_offset == d_msg->length()) {
235     while(i < d_occupied_carriers) {   // finish filling out the symbol
236       out[i + zeros_on_left] = d_constellation_map[rand() & 0x0F];
237       i++;
238     }
239
240     if (d_msg->type() == 1)             // type == 1 sets EOF
241       d_eof = true;
242     d_msg.reset();                      // finished packet, free message
243     assert(d_bit_offset == 0);
244   }
245
246   if (out_flag)
247     out_flag[0] = d_pending_flag;
248   d_pending_flag = 0;
249
250   return 1;  // produced symbol
251 }
252