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