Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_constellation_decoder_cb.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 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_constellation_decoder_cb.h>
28 #include <gr_io_signature.h>
29 #include <stdexcept>
30
31 #include <iostream>
32 using std::cout;
33 using std::endl;
34
35 static const bool compute_EVM = false;
36
37 gr_constellation_decoder_cb_sptr
38 gr_make_constellation_decoder_cb (const std::vector<gr_complex> &sym_position, 
39                                   const std::vector<unsigned char> &sym_value_out)
40 {
41   return gr_constellation_decoder_cb_sptr 
42                 (new gr_constellation_decoder_cb(sym_position, sym_value_out));
43 }
44
45 gr_constellation_decoder_cb::
46 gr_constellation_decoder_cb (const std::vector<gr_complex> &sym_position, 
47                              const std::vector<unsigned char> &sym_value_out)
48   : gr_sync_block ("constellation_decoder_cb",
49                    gr_make_io_signature (1, 1, sizeof (gr_complex)),
50                    gr_make_io_signature (1, 1, sizeof (unsigned char))) 
51 {
52   if (!set_constellation(sym_position,sym_value_out))
53     throw std::invalid_argument("constellation_decoder_cb");
54 }
55
56
57 gr_constellation_decoder_cb::~gr_constellation_decoder_cb(){}
58
59
60 bool
61 gr_constellation_decoder_cb::set_constellation(const std::vector<gr_complex> &sym_position, 
62                                                const std::vector<unsigned char> &sym_value_out)
63 {
64   if (sym_position.size() != sym_value_out.size())
65     return false;
66
67   if (sym_position.size()<1)
68     return false;
69
70   d_sym_position  = sym_position;
71   d_sym_value_out = sym_value_out;      
72   return true;
73 }
74
75
76 int
77 gr_constellation_decoder_cb::work(int noutput_items,
78                                   gr_vector_const_void_star &input_items,
79                                   gr_vector_void_star &output_items)
80 {
81   gr_complex const *in = (const gr_complex *) input_items[0];
82   unsigned char *out = (unsigned char *) output_items[0];
83   unsigned int table_size = d_sym_value_out.size();
84   unsigned int min_index = 0;
85   float min_euclid_dist = 0;
86   float euclid_dist = 0;
87   double total_error = 0;
88     
89   for(int i = 0; i < noutput_items; i++){
90     min_euclid_dist = norm(in[i] - d_sym_position[0]); 
91     min_index = 0; 
92     for (unsigned int j = 1; j < table_size; j++){
93       euclid_dist = norm(in[i] - d_sym_position[j]);
94       if (euclid_dist < min_euclid_dist){
95         min_euclid_dist = euclid_dist;
96         min_index = j;
97       }
98     }
99
100     out[i] = d_sym_value_out[min_index];
101
102     if (compute_EVM)
103       total_error += sqrtf(min_euclid_dist);
104   }
105
106   if (compute_EVM){
107     double mean = total_error / noutput_items;
108     double rms = sqrt(mean * mean);
109     fprintf(stderr, "EVM = %8.4f\n", rms);
110   }
111
112   return noutput_items;
113 }