Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gr-error-correcting-codes / src / lib / ecc_syms_to_metrics.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 <ecc_syms_to_metrics.h>
28 #include <gr_io_signature.h>
29 #include <assert.h>
30
31 ecc_syms_to_metrics_sptr 
32 ecc_make_syms_to_metrics (gr_feval_ff* pdf_fcn_0_bit,
33                           gr_feval_ff* pdf_fcn_1_bit,
34                           int n_samples,
35                           float min_sample,
36                           float max_sample,
37                           int sample_precision)
38 {
39   return ecc_syms_to_metrics_sptr
40     (new ecc_syms_to_metrics (pdf_fcn_0_bit,
41                               pdf_fcn_1_bit,
42                               n_samples,
43                               min_sample,
44                               max_sample,
45                               sample_precision));
46 }
47
48 /*
49  * dummy functions and variables to get the float(*)(float) function
50  * to work properly with the gr_feval_XX stuff.
51  */
52
53 static gr_feval_ff* l_pdf_fcn_0_bit;
54 static gr_feval_ff* l_pdf_fcn_1_bit;
55
56 static float pdf_fcn_0 (float x)
57 {
58   return (l_pdf_fcn_0_bit->eval (x));
59 }
60
61 static float pdf_fcn_1 (float x)
62 {
63   return (l_pdf_fcn_1_bit->eval (x));
64 }
65
66 ecc_syms_to_metrics::ecc_syms_to_metrics
67 (gr_feval_ff* pdf_fcn_0_bit,
68  gr_feval_ff* pdf_fcn_1_bit,
69  int n_samples,
70  float min_sample,
71  float max_sample,
72  int sample_precision)
73   : gr_block ("syms_to_metrics",
74               gr_make_io_signature (1, -1, sizeof (float)),
75               gr_make_io_signature (0, 0, 0))
76 {
77   // setup the dummy functions to do the conversion from the
78   // python-provided feval classes to that which is needed by the
79   // libecc's code_metrics classes.
80
81   l_pdf_fcn_0_bit = pdf_fcn_0_bit;
82   l_pdf_fcn_1_bit = pdf_fcn_1_bit;
83
84   // use the static "create" member function to create the actual
85   // code_metrics to use.
86
87   d_code_metrics_table = libecc_code_metrics_create_table<float>
88     (&pdf_fcn_0,
89      &pdf_fcn_1,
90      n_samples,
91      min_sample,
92      max_sample,
93      sample_precision);
94
95   // get the output item size in bytes from the new code_metrics.
96
97   d_out_item_size_bytes = d_code_metrics_table->out_item_size_bytes ();
98
99   // set the output signature to match that which the code_metrics
100   // will generate.
101
102   set_output_signature (gr_make_io_signature (1, -1, d_out_item_size_bytes));
103 }
104
105 bool ecc_syms_to_metrics::check_topology (int ninputs, int noutputs)
106 {
107   // there are 2 output streams per input stream; the first is ~to
108   // log(p(0|x)) = log-probability of a 0-bit given the input symbol
109   // 'x'; the second is ~to log(p(1|x)).
110
111   return ((noutputs == (2*ninputs)) ? true : false);
112 }
113
114 void
115 ecc_syms_to_metrics::forecast
116 (int noutput_items,
117  gr_vector_int &ninput_items_required)
118 {
119   // always 1:1, for all streams
120   for (size_t n = 0; n < ninput_items_required.size(); n++) {
121     ninput_items_required[n] = noutput_items;
122   }
123 }
124
125 int
126 ecc_syms_to_metrics::general_work
127 (int noutput_items,
128  gr_vector_int &ninput_items,
129  gr_vector_const_void_star &input_items,
130  gr_vector_void_star &output_items)
131 {
132   size_t l_n_output_items = noutput_items;
133
134   for (size_t n = 0; n < input_items.size(); n++) {
135     float* t_in_buf = (float*)(&input_items[n]);
136     void* t_out_buf_0_bit = (void*)(&(output_items[2*n]));
137     void* t_out_buf_1_bit = (void*)(&(output_items[(2*n)+1]));
138     d_code_metrics_table->convert (l_n_output_items, t_in_buf,
139                                    t_out_buf_0_bit, t_out_buf_1_bit);
140   }
141
142   // consume the number of used input items on all input streams
143
144   consume_each (noutput_items);
145
146   // returns number of items written to each output stream
147
148   return (noutput_items);
149 }