Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_bpsk_demapper.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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_bpsk_demapper.h>
28 #include <gr_io_signature.h>
29
30 gr_ofdm_bpsk_demapper_sptr
31 gr_make_ofdm_bpsk_demapper (unsigned int occupied_carriers)
32 {
33   return gr_ofdm_bpsk_demapper_sptr (new gr_ofdm_bpsk_demapper (occupied_carriers));
34 }
35
36 gr_ofdm_bpsk_demapper::gr_ofdm_bpsk_demapper (unsigned occupied_carriers)
37   : gr_block ("ofdm_bpsk_demapper",
38               gr_make_io_signature (1, 1, sizeof(gr_complex)*occupied_carriers),
39               gr_make_io_signature (1, 1, sizeof(unsigned char))),
40     d_occupied_carriers(occupied_carriers),
41     d_byte_offset(0), d_partial_byte(0)
42 {
43 }
44
45 gr_ofdm_bpsk_demapper::~gr_ofdm_bpsk_demapper(void)
46 {
47 }
48
49 unsigned char gr_ofdm_bpsk_demapper::slicer(gr_complex x)
50 {
51   return (unsigned char)(x.real() > 0 ? 1 : 0);
52 }
53
54 void
55 gr_ofdm_bpsk_demapper::forecast (int noutput_items, gr_vector_int &ninput_items_required)
56 {
57   unsigned ninputs = ninput_items_required.size ();
58   for (unsigned i = 0; i < ninputs; i++)
59     ninput_items_required[i] = 1;
60 }
61
62 int
63 gr_ofdm_bpsk_demapper::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 *in = (const gr_complex *)input_items[0];
69   unsigned char *out = (unsigned char *) output_items[0];
70   
71   unsigned int i=0, bytes_produced=0;
72
73   while(i < d_occupied_carriers) {
74
75     while((d_byte_offset < 8) && (i < d_occupied_carriers)) {
76       //fprintf(stderr, "%f+j%f\n", in[i].real(), in[i].imag()); 
77       d_partial_byte |= slicer(in[i++]) << (d_byte_offset++);
78     }
79
80     if(d_byte_offset == 8) {
81       out[bytes_produced++] = d_partial_byte;
82       d_byte_offset = 0;
83       d_partial_byte = 0;
84     }
85   }
86
87 #if 0
88 printf("demod out: ");
89   for(i = 0; i < bytes_produced; i++) {
90     printf("%4x", out[i]);
91   }
92   printf(" \tlen: %d\n", i);
93 #endif
94
95   consume_each(1);
96   return bytes_produced;
97 }