Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gr-error-correcting-codes / src / lib / libecc / code_metrics.h
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 #ifndef INCLUDED_CODE_METRICS_H
24 #define INCLUDED_CODE_METRICS_H
25
26 #include "code_types.h"
27 #include <vector>
28
29 template<typename pdf_fcn_io_t>
30 class code_metrics_table;
31
32 template<typename pdf_fcn_io_t>
33 code_metrics_table<pdf_fcn_io_t>*
34 libecc_code_metrics_create_table
35 (pdf_fcn_io_t (*pdf_fcn_0_bit) (pdf_fcn_io_t),
36  pdf_fcn_io_t (*pdf_fcn_1_bit) (pdf_fcn_io_t),
37  size_t n_samples,
38  pdf_fcn_io_t min_sample,
39  pdf_fcn_io_t max_sample,
40  int sample_precision = 0);
41
42 #if 0
43 template<typename pdf_fcn_io_t>
44 class code_metrics_decoder;
45
46 template<typename pdf_fcn_io_t>
47 libecc_code_metrics_create_decoder
48 (code_convolutional_trellis* trellis,
49  code_metrics_table<pdf_fcn_io_t>* table = 0,
50  int sample_precision = 0);
51 #endif
52
53 template<typename pdf_fcn_io_t>
54 class code_metrics_table
55 {
56 /*
57  * class code_metrics_table: metrics table for convolutional codes.
58  * Pre-compute a lookup table upon instantiation, which makes for
59  * quick conversion from soft-float symbol value ('sym') to metric
60  * value.  The symbol value probability is defined by the arguments
61  * 'pdf_fcn_0_bit' and 'pdf_fcn_1_bit' for p(0|sym) and p(1|sym)
62  * respectively.  Internally, the PDF's are sampled and normalized to
63  * always have a minimum value of 0.  For float-precision, the maximum
64  * value is 1.0, while for integer M-bits the maximum value is
65  * (2^M)-1.  Smaller metric values indicate that the received symbols
66  * are closer to the given output bits; larger values indicate greater
67  * differences.  The only constraint on the PDF functions are that
68  * they are piecewise continuous; otherwise, they don't even have to
69  * be a true PDF in terms of the integral from -inf to +inf being
70  * equal to 1 due to the normalization.
71  *
72  * Storage type for the tables is determined by the "sample_precision"
73  * argument.  When the precision equals 0, 32-bit float storage is
74  * used; otherwise, the next largest standard integer type is used
75  * unsigned (char for 1 to 8 bits, short for 9 to 16 bits, and long
76  * for 17 to 32 bits).  For the purposes of coding gain, any
77  * sample_precision larger than about 9 will have minimal added
78  * benefit under most conditions where communications are possible.
79  * Trellis computations are performed in either 32-bit float (for
80  * float storage) or 32-bit unsigned long (for all integer storage).
81  *
82  * The number of samples to store is determined by the "n_samples"
83  * argument, which must be at least 2 but is otherwise not limited
84  * except by the memory of the host computer.
85  *
86  * Samples of the PDF functions are taken from "min_sample" to
87  * "max_sample", which represent the floor and ceiling on input symbol
88  * values below and above which symbol values are truncated.
89  * Internally, a sub-n_samples value is determined and used to "sum"
90  * the PDF functions to divide the probabilities into "bins".
91  */
92
93 public:
94   typedef pdf_fcn_io_t (*pdf_fcn_t) (pdf_fcn_io_t);
95
96   virtual ~code_metrics_table () {};
97
98   // lookup() returns either a float, or a sign-extended
99   // 'sample_precision'-bit integer value.
100
101   virtual void lookup (pdf_fcn_io_t sym,
102                        void* bit_0,
103                        void* bit_1) = 0;
104
105   // convert does a lookup on 'n_syms' input symbols
106
107   virtual void convert (size_t n_syms,
108                         pdf_fcn_io_t* syms,
109                         void* bit_0,
110                         void* bit_1) = 0;
111
112   inline const unsigned char out_item_size_bytes ()
113   {return(d_out_item_size_bytes);};
114
115 protected:
116   code_metrics_table (pdf_fcn_t pdf_fcn_0_bit,
117                       pdf_fcn_t pdf_fcn_1_bit,
118                       size_t n_samples,
119                       pdf_fcn_io_t min_sample,
120                       pdf_fcn_io_t max_sample);
121
122   unsigned char d_out_item_size_bytes, d_sample_precision;
123   size_t d_n_samples;
124   pdf_fcn_io_t d_max_sample, d_min_sample, d_delta;
125   pdf_fcn_t d_pdf_fcn_0_bit, d_pdf_fcn_1_bit;
126   std::vector<pdf_fcn_io_t> d_pdf_fcn_0_samples, d_pdf_fcn_1_samples;
127 };
128
129 template<typename pdf_fcn_io_t, typename metric_t>
130 class code_metrics_table_work : public code_metrics_table<pdf_fcn_io_t>
131 {
132 public:
133   typedef metric_t *metric_ptr_t;
134   typedef pdf_fcn_io_t (*pdf_fcn_t) (pdf_fcn_io_t);
135
136   ~code_metrics_table_work () {};
137
138   void lookup (pdf_fcn_io_t sym, void* bit_0, void* bit_1);
139   void convert (size_t n_syms, pdf_fcn_io_t* sym, void* bit_0, void* bit_1);
140
141 protected:
142   code_metrics_table_work (pdf_fcn_t pdf_fcn_0_bit,
143                            pdf_fcn_t pdf_fcn_1_bit,
144                            size_t n_samples,
145                            pdf_fcn_io_t min_sample,
146                            pdf_fcn_io_t max_sample,
147                            int sample_precision = 0);
148
149   friend code_metrics_table<pdf_fcn_io_t>*
150   libecc_code_metrics_create_table<pdf_fcn_io_t>
151   (pdf_fcn_io_t (*pdf_fcn_0_bit) (pdf_fcn_io_t),
152    pdf_fcn_io_t (*pdf_fcn_1_bit) (pdf_fcn_io_t),
153    size_t n_samples,
154    pdf_fcn_io_t min_sample,
155    pdf_fcn_io_t max_sample,
156    int sample_precision);
157
158   std::vector<metric_t> d_metric_table_0_bit, d_metric_table_1_bit;
159 };
160
161 #if 0
162   // compute all output-bit combinations of the incoming symbols' metrics
163
164   void compute_all_outputs (pdf_fcn_io_t* syms, std::vector<unsigned long>& out);
165   void compute_all_outputs (pdf_fcn_io_t* syms, std::vector<float>& out);
166
167   size_t d_n_code_outputs;
168   std::vector<unsigned long> in_l[2];
169   std::vector<float> in_f[2];
170 #endif
171
172 #endif /* INCLUDED_CODE_METRICS_H */