GNU Radio 3.7.2 C++ API
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  * Constellation which calculates the distance to each point in the
233  * constellation for decision making. Inefficient for large
234  * constellations.
235  */
237  : public constellation
238  {
239  public:
241 
242  /*!
243  * Make a general constellation object that calculates the Euclidean distance for hard decisions.
244  *
245  * \param constell List of constellation points (order of list matches pre_diff_code)
246  * \param pre_diff_code List of alphabet symbols (before applying any differential
247  * coding) (order of list matches constell)
248  * \param rotational_symmetry Number of rotations around unit circle that have the same representation.
249  * \param dimensionality Number of dimensions to the constellation.
250  */
251  static sptr make(std::vector<gr_complex> constell,
252  std::vector<int> pre_diff_code,
253  unsigned int rotational_symmetry,
254  unsigned int dimensionality);
255 
256  unsigned int decision_maker(const gr_complex *sample);
257  // void calc_metric(gr_complex *sample, float *metric, trellis_metric_type_t type);
258  // void calc_euclidean_metric(gr_complex *sample, float *metric);
259  // void calc_hard_symbol_metric(gr_complex *sample, float *metric);
260 
261  protected:
262  constellation_calcdist(std::vector<gr_complex> constell,
263  std::vector<int> pre_diff_code,
264  unsigned int rotational_symmetry,
265  unsigned int dimensionality);
266  };
267 
268 
269  /************************************************************/
270  /*! constellation_sector */
271  /************************************************************/
272 
273  /*!
274  * \brief Sectorized digital constellation
275  * \ingroup digital
276  *
277  * Constellation space is divided into sectors. Each sector is
278  * associated with the nearest constellation point.
279  *
280  */
282  {
283  public:
284 
285  constellation_sector(std::vector<gr_complex> constell,
286  std::vector<int> pre_diff_code,
287  unsigned int rotational_symmetry,
288  unsigned int dimensionality,
289  unsigned int n_sectors);
290 
292 
293  unsigned int decision_maker(const gr_complex *sample);
294 
295  protected:
296  virtual unsigned int get_sector(const gr_complex *sample) = 0;
297  virtual unsigned int calc_sector_value(unsigned int sector) = 0;
298  void find_sector_values();
299 
300  unsigned int n_sectors;
301 
302  private:
303  std::vector<int> sector_values;
304  };
305 
306  /************************************************************/
307  /* constellation_rect */
308  /************************************************************/
309 
310  /*!
311  * \brief Rectangular digital constellation
312  * \ingroup digital
313  *
314  * Only implemented for 1-(complex)dimensional constellation.
315  *
316  * Constellation space is divided into rectangular sectors. Each
317  * sector is associated with the nearest constellation point.
318  *
319  * Works well for square QAM.
320  *
321  * Works for any generic constellation provided sectors are not
322  * too large.
323  */
324 
325 
327  : public constellation_sector
328  {
329  public:
331 
332  /*!
333  * Make a rectangular constellation object.
334  *
335  * \param constell List of constellation points (order of list matches pre_diff_code)
336  * \param pre_diff_code List of alphabet symbols (before applying any differential
337  * coding) (order of list matches constell)
338  * \param rotational_symmetry Number of rotations around unit circle that have the same representation.
339  * \param real_sectors Number of sectors the real axis is split in to.
340  * \param imag_sectors Number of sectors the imag axis is split in to.
341  * \param width_real_sectors width of each real sector to calculate decision boundaries.
342  * \param width_imag_sectors width of each imag sector to calculate decision boundaries.
343  */
344  static constellation_rect::sptr make(std::vector<gr_complex> constell,
345  std::vector<int> pre_diff_code,
346  unsigned int rotational_symmetry,
347  unsigned int real_sectors,
348  unsigned int imag_sectors,
349  float width_real_sectors,
350  float width_imag_sectors);
352 
353  protected:
354 
355  constellation_rect(std::vector<gr_complex> constell,
356  std::vector<int> pre_diff_code,
357  unsigned int rotational_symmetry,
358  unsigned int real_sectors,
359  unsigned int imag_sectors,
360  float width_real_sectors,
361  float width_imag_sectors);
362 
363  unsigned int get_sector(const gr_complex *sample);
364  gr_complex calc_sector_center(unsigned int sector);
365  unsigned int calc_sector_value(unsigned int sector);
366 
367  private:
368  unsigned int n_real_sectors;
369  unsigned int n_imag_sectors;
370  float d_width_real_sectors;
371  float d_width_imag_sectors;
372  };
373 
374 
375  /************************************************************/
376  /* constellation_expl_rect */
377  /************************************************************/
378 
379  /*!
380  * \brief Rectangular digital constellation
381  * \ingroup digital
382  *
383  * Only implemented for 1-(complex)dimensional constellation.
384  *
385  * Constellation space is divided into rectangular sectors. Each
386  * sector is associated with the nearest constellation point.
387  *
388  * This class is different from constellation_rect in that the
389  * mapping from sector to constellation point is explicitly passed
390  * into the constructor as sector_values. Usually we do not need
391  * this, since we want each sector to be automatically mapped to
392  * the closest constellation point, however sometimes it's nice to
393  * have the flexibility.
394  */
396  : public constellation_rect
397  {
398  public:
400 
401  static sptr make(std::vector<gr_complex> constellation,
402  std::vector<int> pre_diff_code,
403  unsigned int rotational_symmetry,
404  unsigned int real_sectors,
405  unsigned int imag_sectors,
406  float width_real_sectors,
407  float width_imag_sectors,
408  std::vector<unsigned int> sector_values);
410 
411  protected:
412  constellation_expl_rect(std::vector<gr_complex> constellation,
413  std::vector<int> pre_diff_code,
414  unsigned int rotational_symmetry,
415  unsigned int real_sectors,
416  unsigned int imag_sectors,
417  float width_real_sectors,
418  float width_imag_sectors,
419  std::vector<unsigned int> sector_values);
420 
421  unsigned int calc_sector_value (unsigned int sector) {
422  return d_sector_values[sector];
423  }
424 
425  private:
426  std::vector<unsigned int> d_sector_values;
427  };
428 
429  /************************************************************/
430  /* constellation_psk */
431  /************************************************************/
432 
433  /*!
434  * \brief constellation_psk
435  * \ingroup digital
436  *
437  * Constellation space is divided into pie slices sectors.
438  *
439  * Each slice is associated with the nearest constellation point.
440  *
441  * Works well for PSK but nothing else.
442  *
443  * Assumes that there is a constellation point at 1.x
444  */
446  {
447  public:
449 
450  // public constructor
451  static sptr make(std::vector<gr_complex> constell,
452  std::vector<int> pre_diff_code,
453  unsigned int n_sectors);
454 
456 
457  protected:
458  unsigned int get_sector(const gr_complex *sample);
459 
460  unsigned int calc_sector_value(unsigned int sector);
461 
462  constellation_psk(std::vector<gr_complex> constell,
463  std::vector<int> pre_diff_code,
464  unsigned int n_sectors);
465  };
466 
467 
468  /************************************************************/
469  /* constellation_bpsk */
470  /* */
471  /* Only works for BPSK. */
472  /* */
473  /************************************************************/
474 
475  /*!
476  * \brief Digital constellation for BPSK
477  * \ingroup digital
478  */
480  {
481  public:
483 
484  // public constructor
485  static sptr make();
486 
488 
489  unsigned int decision_maker(const gr_complex *sample);
490 
491  protected:
493  };
494 
495 
496  /************************************************************/
497  /* constellation_qpsk */
498  /* */
499  /* Only works for QPSK. */
500  /* */
501  /************************************************************/
502 
503  /*!
504  * \brief Digital constellation for QPSK
505  * \ingroup digital
506  *
507  * 01 | 11
508  * -------
509  * 00 | 10
510  */
512  {
513  public:
515 
516  // public constructor
517  static sptr make();
518 
520 
521  unsigned int decision_maker(const gr_complex *sample);
522 
523  protected:
525  };
526 
527 
528  /************************************************************/
529  /* constellation_dqpsk */
530  /* */
531  /* Works with differential encoding; slower decisions. */
532  /* */
533  /************************************************************/
534 
535  /*!
536  * \brief Digital constellation for DQPSK
537  * \ingroup digital
538  *
539  * 01 | 00
540  * -------
541  * 11 | 10
542  */
544  {
545  public:
547 
548  // public constructor
549  static sptr make();
550 
552 
553  unsigned int decision_maker(const gr_complex *sample);
554 
555  protected:
557  };
558 
559 
560  /************************************************************/
561  /* constellation_8psk */
562  /* */
563  /* Only works for 8PSK. */
564  /* */
565  /************************************************************/
566 
567  /*!
568  * \brief Digital constellation for 8PSK
569  * \ingroup digital
570  *
571  * 101 | 100
572  * 001 | 000
573  * -----------------
574  * 011 | 010
575  * 111 | 110
576  */
578  {
579  public:
581 
582  // public constructor
583  static sptr make();
584 
586 
587  unsigned int decision_maker(const gr_complex *sample);
588 
589  protected:
591  };
592 
593  } /* namespace digital */
594 } /* namespace gr */
595 
596 #endif /* INCLUDED_DIGITAL_CONSTELLATION_H */
int d_lut_precision
Definition: constellation.h:214
boost::shared_ptr< constellation_psk > sptr
Definition: constellation.h:448
boost::shared_ptr< constellation_dqpsk > sptr
Definition: constellation.h:546
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:445
Digital constellation for DQPSK01 | 00 11 | 10.
Definition: constellation.h:543
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:482
Rectangular digital constellationOnly implemented for 1-(complex)dimensional constellation.
Definition: constellation.h:395
Digital constellation for BPSK.
Definition: constellation.h:479
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 constellationConstellation space is divided into sectors. Each sector is associate...
Definition: constellation.h:281
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 QPSK01 | 11 00 | 10.
Definition: constellation.h:511
boost::shared_ptr< constellation_expl_rect > sptr
Definition: constellation.h:399
unsigned int d_arity
Definition: constellation.h:207
boost::shared_ptr< constellation_8psk > sptr
Definition: constellation.h:580
unsigned int calc_sector_value(unsigned int sector)
Definition: constellation.h:421
float d_re_min
Definition: constellation.h:211
unsigned int arity()
Definition: constellation.h:115
Calculate Euclidian distance for any constellationConstellation which calculates the distance to each...
Definition: constellation.h:236
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:330
unsigned int n_sectors
Definition: constellation.h:300
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:240
Rectangular digital constellationOnly implemented for 1-(complex)dimensional constellation.
Definition: constellation.h:326
bool d_apply_pre_diff_code
Definition: constellation.h:204
Digital constellation for 8PSK101 | 100 001 | 000 011 | 010 111 | 110.
Definition: constellation.h:577
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:514