GNU Radio Manual and C++ API Reference  3.7.4.1
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
constellation.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2010-2012 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 #ifndef INCLUDED_DIGITAL_CONSTELLATION_H
24 #define INCLUDED_DIGITAL_CONSTELLATION_H
25 
26 #include <gnuradio/digital/api.h>
28 #include <boost/enable_shared_from_this.hpp>
29 #include <boost/any.hpp>
30 #include <gnuradio/gr_complex.h>
31 #include <pmt/pmt.h>
32 #include <vector>
33 
34 namespace gr {
35  namespace digital {
36 
37  /************************************************************/
38  /* constellation */
39  /* */
40  /* Base class defining interface. */
41  /************************************************************/
42 
43  class constellation;
44  typedef boost::shared_ptr<constellation> constellation_sptr;
45 
46  /*!
47  * \brief An abstracted constellation object
48  * \ingroup symbol_coding_blk
49  *
50  * \details
51  * The constellation objects hold the necessary information to pass
52  * around constellation information for modulators and
53  * demodulators. These objects contain the mapping between the bits
54  * and the constellation points used to represent them as well as
55  * methods for slicing the symbol space. Various implementations are
56  * possible for efficiency and ease of use.
57  *
58  * Standard constellations (BPSK, QPSK, QAM, etc) can be inherited
59  * from this class and overloaded to perform optimized slicing and
60  * constellation mappings.
61  */
63  : public boost::enable_shared_from_this<constellation>
64  {
65  public:
66  constellation(std::vector<gr_complex> constell,
67  std::vector<int> pre_diff_code,
68  unsigned int rotational_symmetry,
69  unsigned int dimensionality);
70  constellation();
71  virtual ~constellation();
72 
73  //! Returns the constellation points for a symbol value
74  void map_to_points(unsigned int value, gr_complex *points);
75  std::vector<gr_complex> map_to_points_v(unsigned int value);
76 
77  //! Returns the constellation point that matches best.
78  virtual unsigned int decision_maker(const gr_complex *sample) = 0;
79  //! Takes a vector rather than a pointer. Better for SWIG wrapping.
80  unsigned int decision_maker_v(std::vector<gr_complex> sample);
81  //! Also calculates the phase error.
82  unsigned int decision_maker_pe(const gr_complex *sample, float *phase_error);
83  //! Calculates distance.
84  //unsigned int decision_maker_e(const gr_complex *sample, float *error);
85 
86  //! Calculates metrics for all points in the constellation.
87  //! For use with the viterbi algorithm.
88  virtual void calc_metric(const gr_complex *sample, float *metric, gr::digital::trellis_metric_type_t type);
89  virtual void calc_euclidean_metric(const gr_complex *sample, float *metric);
90  virtual void calc_hard_symbol_metric(const gr_complex *sample, float *metric);
91 
92  //! Returns the set of points in this constellation.
93  std::vector<gr_complex> points() { return d_constellation;}
94  //! Returns the vector of points in this constellation.
95  //! Raise error if dimensionality is not one.
96  std::vector<gr_complex> s_points();
97  //! Returns a vector of vectors of points.
98  std::vector<std::vector<gr_complex> > v_points();
99  //! Whether to apply an encoding before doing differential encoding. (e.g. gray coding)
100  bool apply_pre_diff_code() { return d_apply_pre_diff_code;}
101  //! Whether to apply an encoding before doing differential encoding. (e.g. gray coding)
102  void set_pre_diff_code(bool a) { d_apply_pre_diff_code = a;}
103  //! Returns the encoding to apply before differential encoding.
104  std::vector<int> pre_diff_code() { return d_pre_diff_code;}
105  //! Returns the order of rotational symmetry.
106  unsigned int rotational_symmetry() { return d_rotational_symmetry;}
107  //! Returns the number of complex numbers in a single symbol.
108  unsigned int dimensionality() {return d_dimensionality;}
109 
110  unsigned int bits_per_symbol()
111  {
112  return floor(log(double(d_constellation.size()))/d_dimensionality/log(2.0));
113  }
114 
115  unsigned int arity()
116  {
117  return d_arity;
118  }
119 
121  {
122  return shared_from_this();
123  }
124 
126  {
127  return pmt::make_any(boost::any(base()));
128  }
129 
130  /*! \brief Generates the soft decision LUT based on
131  * constellation and symbol map.
132  *
133  * \details Generates the soft decision LUT based on
134  * constellation and symbol map. It can be given a estimate of
135  * the noise power in the channel as \p npwr.
136  *
137  * \param precision Number of bits of precision on each axis.
138  * \param npwr Estimate of the noise power (if known).
139  *
140  * This is expensive to compute.
141  */
142  void gen_soft_dec_lut(int precision, float npwr=1.0);
143 
144  /*! \brief Calculate soft decisions for the given \p sample.
145  *
146  * \details Calculate the soft decisions from the given \p sample
147  * at the given noise power \p npwr.
148  *
149  * This is a very costly algorithm (especially for higher order
150  * modulations) and should be used sparingly. It uses the
151  * #gen_soft_dec_lut function to generate the LUT, which
152  * should be done once or if a large change in the noise floor
153  * is detected.
154  *
155  * Instead of using this function, generate the LUT using the
156  * #gen_soft_dec_lut after creating the constellation object
157  * and then use the #soft_decision_maker function to return the
158  * answer from the LUT.
159  *
160  * \param sample The complex sample to get the soft decisions.
161  * \param npwr Estimate of the noise power (if known).
162  */
163  virtual std::vector<float> calc_soft_dec(gr_complex sample, float npwr=1.0);
164 
165  /*! \brief Define a soft decision look-up table.
166  *
167  * \details Define a soft decision look-up table (LUT). Because
168  * soft decisions can be calculated in various ways with various
169  * levels of accuracy and complexity, this function allows
170  * users to create a LUT in their own way.
171  *
172  * Setting the LUT here means that #has_soft_dec_lut will return
173  * true. Decision vectors returned by #soft_decision_maker will
174  * be calculated using this LUT.
175  *
176  * \param soft_dec_lut The soft decision LUT as a vector of
177  * tuples (vectors in C++) of soft decisions. Each
178  * element of the LUT is a vector of k-bit floats (where
179  * there are k bits/sample in the constellation).
180  * \param precision The number of bits of precision used when
181  * generating the LUT.
182  */
183  void set_soft_dec_lut(const std::vector< std::vector<float> > &soft_dec_lut,
184  int precision);
185 
186  //! Returns True if the soft decision LUT has been defined, False otherwise.
187  bool has_soft_dec_lut();
188 
189  /*! \brief Returns the soft decisions for the given \p sample.
190  *
191  * \details Returns the soft decisions for the given \p
192  * sample. If a LUT is defined for the object, the decisions
193  * will be calculated from there. Otherwise, this function will
194  * call calc_soft_dec directly to calculate the soft decisions.
195  *
196  * \param sample The complex sample to get the soft decisions.
197  */
198  std::vector<float> soft_decision_maker(gr_complex sample);
199 
200 
201  protected:
202  std::vector<gr_complex> d_constellation;
203  std::vector<int> d_pre_diff_code;
205  unsigned int d_rotational_symmetry;
206  unsigned int d_dimensionality;
207  unsigned int d_arity;
208  //! The factor by which the user given constellation points were
209  //! scaled by to achieve an average amplitude of 1.
211  float d_re_min, d_re_max, d_im_min, d_im_max;
212 
213  std::vector< std::vector<float> > d_soft_dec_lut;
215  float d_lut_scale;
216 
217  float get_distance(unsigned int index, const gr_complex *sample);
218  unsigned int get_closest_point(const gr_complex *sample);
219  void calc_arity();
220 
221  void max_min_axes();
222  };
223 
224  /************************************************************/
225  /* constellation_calcdist */
226  /* */
227  /************************************************************/
228 
229  /*! \brief Calculate Euclidian distance for any constellation
230  * \ingroup digital
231  *
232  * \details
233  * Constellation which calculates the distance to each point in the
234  * constellation for decision making. Inefficient for large
235  * constellations.
236  */
238  : public constellation
239  {
240  public:
242 
243  /*!
244  * Make a general constellation object that calculates the Euclidean distance for hard decisions.
245  *
246  * \param constell List of constellation points (order of list matches pre_diff_code)
247  * \param pre_diff_code List of alphabet symbols (before applying any differential
248  * coding) (order of list matches constell)
249  * \param rotational_symmetry Number of rotations around unit circle that have the same representation.
250  * \param dimensionality Number of dimensions to the constellation.
251  */
252  static sptr make(std::vector<gr_complex> constell,
253  std::vector<int> pre_diff_code,
254  unsigned int rotational_symmetry,
255  unsigned int dimensionality);
256 
257  unsigned int decision_maker(const gr_complex *sample);
258  // void calc_metric(gr_complex *sample, float *metric, trellis_metric_type_t type);
259  // void calc_euclidean_metric(gr_complex *sample, float *metric);
260  // void calc_hard_symbol_metric(gr_complex *sample, float *metric);
261 
262  protected:
263  constellation_calcdist(std::vector<gr_complex> constell,
264  std::vector<int> pre_diff_code,
265  unsigned int rotational_symmetry,
266  unsigned int dimensionality);
267  };
268 
269 
270  /************************************************************/
271  /*! constellation_sector */
272  /************************************************************/
273 
274  /*!
275  * \brief Sectorized digital constellation
276  * \ingroup digital
277  *
278  * \details
279  * Constellation space is divided into sectors. Each sector is
280  * associated with the nearest constellation point.
281  */
283  {
284  public:
285 
286  /*!
287  * Make a sectorized constellation object.
288  *
289  * \param constell List of constellation points (order of list matches pre_diff_code)
290  * \param pre_diff_code List of alphabet symbols (before applying any differential
291  * coding) (order of list matches constell)
292  * \param rotational_symmetry Number of rotations around unit circle that have the same representation.
293  * \param dimensionality Number of z-axis dimensions to the constellation
294  * \param n_sectors Number of sectors in the constellation.
295  */
296  constellation_sector(std::vector<gr_complex> constell,
297  std::vector<int> pre_diff_code,
298  unsigned int rotational_symmetry,
299  unsigned int dimensionality,
300  unsigned int n_sectors);
301 
303 
304  unsigned int decision_maker(const gr_complex *sample);
305 
306  protected:
307  virtual unsigned int get_sector(const gr_complex *sample) = 0;
308  virtual unsigned int calc_sector_value(unsigned int sector) = 0;
309  void find_sector_values();
310 
311  unsigned int n_sectors;
312 
313  private:
314  std::vector<int> sector_values;
315  };
316 
317  /************************************************************/
318  /* constellation_rect */
319  /************************************************************/
320 
321  /*!
322  * \brief Rectangular digital constellation
323  * \ingroup digital
324  *
325  * Only implemented for 1-(complex)dimensional constellation.
326  *
327  * Constellation space is divided into rectangular sectors. Each
328  * sector is associated with the nearest constellation point.
329  *
330  * Works well for square QAM.
331  *
332  * Works for any generic constellation provided sectors are not
333  * too large.
334  */
336  : public constellation_sector
337  {
338  public:
340 
341  /*!
342  * Make a rectangular constellation object.
343  *
344  * \param constell List of constellation points (order of list matches pre_diff_code)
345  * \param pre_diff_code List of alphabet symbols (before applying any differential
346  * coding) (order of list matches constell)
347  * \param rotational_symmetry Number of rotations around unit circle that have the same representation.
348  * \param real_sectors Number of sectors the real axis is split in to.
349  * \param imag_sectors Number of sectors the imag axis is split in to.
350  * \param width_real_sectors width of each real sector to calculate decision boundaries.
351  * \param width_imag_sectors width of each imag sector to calculate decision boundaries.
352  */
353  static constellation_rect::sptr make(std::vector<gr_complex> constell,
354  std::vector<int> pre_diff_code,
355  unsigned int rotational_symmetry,
356  unsigned int real_sectors,
357  unsigned int imag_sectors,
358  float width_real_sectors,
359  float width_imag_sectors);
361 
362  protected:
363 
364  constellation_rect(std::vector<gr_complex> constell,
365  std::vector<int> pre_diff_code,
366  unsigned int rotational_symmetry,
367  unsigned int real_sectors,
368  unsigned int imag_sectors,
369  float width_real_sectors,
370  float width_imag_sectors);
371 
372  unsigned int get_sector(const gr_complex *sample);
373  gr_complex calc_sector_center(unsigned int sector);
374  unsigned int calc_sector_value(unsigned int sector);
375 
376  private:
377  unsigned int n_real_sectors;
378  unsigned int n_imag_sectors;
379  float d_width_real_sectors;
380  float d_width_imag_sectors;
381  };
382 
383 
384  /************************************************************/
385  /* constellation_expl_rect */
386  /************************************************************/
387 
388  /*!
389  * \brief Rectangular digital constellation.
390  * \ingroup digital
391  *
392  * \details
393  * Only implemented for 1-(complex)dimensional constellation.
394  *
395  * Constellation space is divided into rectangular sectors. Each
396  * sector is associated with the nearest constellation point.
397  *
398  * This class is different from constellation_rect in that the
399  * mapping from sector to constellation point is explicitly passed
400  * into the constructor as sector_values. Usually we do not need
401  * this, since we want each sector to be automatically mapped to
402  * the closest constellation point, however sometimes it's nice to
403  * have the flexibility.
404  */
406  : public constellation_rect
407  {
408  public:
410 
411  static sptr make(std::vector<gr_complex> constellation,
412  std::vector<int> pre_diff_code,
413  unsigned int rotational_symmetry,
414  unsigned int real_sectors,
415  unsigned int imag_sectors,
416  float width_real_sectors,
417  float width_imag_sectors,
418  std::vector<unsigned int> sector_values);
420 
421  protected:
422  constellation_expl_rect(std::vector<gr_complex> constellation,
423  std::vector<int> pre_diff_code,
424  unsigned int rotational_symmetry,
425  unsigned int real_sectors,
426  unsigned int imag_sectors,
427  float width_real_sectors,
428  float width_imag_sectors,
429  std::vector<unsigned int> sector_values);
430 
431  unsigned int calc_sector_value (unsigned int sector) {
432  return d_sector_values[sector];
433  }
434 
435  private:
436  std::vector<unsigned int> d_sector_values;
437  };
438 
439  /************************************************************/
440  /* constellation_psk */
441  /************************************************************/
442 
443  /*!
444  * \brief constellation_psk
445  * \ingroup digital
446  *
447  * Constellation space is divided into pie slices sectors.
448  *
449  * Each slice is associated with the nearest constellation point.
450  *
451  * Works well for PSK but nothing else.
452  *
453  * Assumes that there is a constellation point at 1.x
454  */
456  {
457  public:
459 
460  // public constructor
461  static sptr make(std::vector<gr_complex> constell,
462  std::vector<int> pre_diff_code,
463  unsigned int n_sectors);
464 
466 
467  protected:
468  unsigned int get_sector(const gr_complex *sample);
469 
470  unsigned int calc_sector_value(unsigned int sector);
471 
472  constellation_psk(std::vector<gr_complex> constell,
473  std::vector<int> pre_diff_code,
474  unsigned int n_sectors);
475  };
476 
477 
478  /************************************************************/
479  /* constellation_bpsk */
480  /* */
481  /* Only works for BPSK. */
482  /* */
483  /************************************************************/
484 
485  /*!
486  * \brief Digital constellation for BPSK .
487  * \ingroup digital
488  *
489  * \details
490  * \verbatim
491  0 | 1
492  \endverbatim
493  */
495  {
496  public:
498 
499  // public constructor
500  static sptr make();
501 
503 
504  unsigned int decision_maker(const gr_complex *sample);
505 
506  protected:
508  };
509 
510 
511  /************************************************************/
512  /* constellation_qpsk */
513  /* */
514  /* Only works for QPSK. */
515  /* */
516  /************************************************************/
517 
518  /*!
519  * \brief Digital constellation for QPSK
520  * \ingroup digital
521  *
522  * \details
523  * \verbatim
524  01 | 11
525  -------
526  00 | 10
527  \endverbatim
528  */
530  {
531  public:
533 
534  // public constructor
535  static sptr make();
536 
538 
539  unsigned int decision_maker(const gr_complex *sample);
540 
541  protected:
543  };
544 
545 
546  /************************************************************/
547  /* constellation_dqpsk */
548  /* */
549  /* Works with differential encoding; slower decisions. */
550  /* */
551  /************************************************************/
552 
553  /*!
554  * \brief Digital constellation for DQPSK.
555  * \ingroup digital
556  *
557  * \details
558  * \verbatim
559  01 | 00
560  -------
561  11 | 10
562  \endverbatim
563  */
565  {
566  public:
568 
569  // public constructor
570  static sptr make();
571 
573 
574  unsigned int decision_maker(const gr_complex *sample);
575 
576  protected:
578  };
579 
580 
581  /************************************************************/
582  /* constellation_8psk */
583  /* */
584  /* Only works for 8PSK. */
585  /* */
586  /************************************************************/
587 
588  /*!
589  * \brief Digital constellation for 8PSK.
590  * \ingroup digital
591  *
592  * \details
593  * \verbatim
594  101 | 100
595  001 | 000
596  -----------------
597  011 | 010
598  111 | 110
599  \endverbatim
600  */
602  {
603  public:
605 
606  // public constructor
607  static sptr make();
608 
610 
611  unsigned int decision_maker(const gr_complex *sample);
612 
613  protected:
615  };
616 
617  } /* namespace digital */
618 } /* namespace gr */
619 
620 #endif /* INCLUDED_DIGITAL_CONSTELLATION_H */
int d_lut_precision
Definition: constellation.h:214
boost::shared_ptr< constellation_psk > sptr
Definition: constellation.h:458
boost::shared_ptr< constellation_dqpsk > sptr
Definition: constellation.h:567
float d_scalefactor
The factor by which the user given constellation points were scaled by to achieve an average amplitud...
Definition: constellation.h:210
unsigned int bits_per_symbol()
Definition: constellation.h:110
constellation_pskConstellation space is divided into pie slices sectors.
Definition: constellation.h:455
Digital constellation for DQPSK.
Definition: constellation.h:564
constellation_sptr base()
Definition: constellation.h:120
trellis_metric_type_t
Definition: metric_type.h:29
An abstracted constellation object.
Definition: constellation.h:62
#define DIGITAL_API
Definition: gr-digital/include/gnuradio/digital/api.h:30
std::vector< gr_complex > points()
Returns the set of points in this constellation.
Definition: constellation.h:93
boost::shared_ptr< constellation_bpsk > sptr
Definition: constellation.h:497
Rectangular digital constellation.
Definition: constellation.h:405
Digital constellation for BPSK .
Definition: constellation.h:494
std::vector< std::vector< float > > d_soft_dec_lut
Definition: constellation.h:213
unsigned int dimensionality()
Returns the number of complex numbers in a single symbol.
Definition: constellation.h:108
std::complex< float > gr_complex
Definition: gr_complex.h:27
std::vector< gr_complex > d_constellation
Definition: constellation.h:202
void calc_metric(int O, int D, const std::vector< T > &TABLE, const T *input, float *metric, digital::trellis_metric_type_t type)
Sectorized digital constellation.
Definition: constellation.h:282
std::vector< int > d_pre_diff_code
Definition: constellation.h:203
void set_pre_diff_code(bool a)
Whether to apply an encoding before doing differential encoding. (e.g. gray coding) ...
Definition: constellation.h:102
float d_lut_scale
Definition: constellation.h:215
Digital constellation for QPSK.
Definition: constellation.h:529
boost::shared_ptr< constellation_expl_rect > sptr
Definition: constellation.h:409
unsigned int d_arity
Definition: constellation.h:207
boost::shared_ptr< constellation_8psk > sptr
Definition: constellation.h:604
unsigned int calc_sector_value(unsigned int sector)
Definition: constellation.h:431
float d_re_min
Definition: constellation.h:211
unsigned int arity()
Definition: constellation.h:115
Calculate Euclidian distance for any constellation.
Definition: constellation.h:237
std::vector< int > pre_diff_code()
Returns the encoding to apply before differential encoding.
Definition: constellation.h:104
pmt::pmt_t as_pmt()
Definition: constellation.h:125
PMT_API pmt_t make_any(const boost::any &any)
make an any
bool apply_pre_diff_code()
Whether to apply an encoding before doing differential encoding. (e.g. gray coding) ...
Definition: constellation.h:100
boost::shared_ptr< constellation_rect > sptr
Definition: constellation.h:339
unsigned int n_sectors
Definition: constellation.h:311
boost::intrusive_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:56
boost::shared_ptr< constellation_calcdist > sptr
Definition: constellation.h:241
Rectangular digital constellationOnly implemented for 1-(complex)dimensional constellation.
Definition: constellation.h:335
bool d_apply_pre_diff_code
Definition: constellation.h:204
Digital constellation for 8PSK.
Definition: constellation.h:601
unsigned int d_dimensionality
Definition: constellation.h:206
unsigned int d_rotational_symmetry
Definition: constellation.h:205
unsigned int rotational_symmetry()
Returns the order of rotational symmetry.
Definition: constellation.h:106
boost::shared_ptr< constellation_qpsk > sptr
Definition: constellation.h:532