summaryrefslogtreecommitdiff
path: root/gnuradio-core/src/lib/general/gr_constellation.h
blob: 4083e5b5b5094a4c79af92bc1360b70ca1681acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* -*- c++ -*- */
/*
 * Copyright 2010 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifndef INCLUDED_GR_CONSTELLATION_H
#define	INCLUDED_GR_CONSTELLATION_H

#include <vector>
#include <math.h>
#include <gr_complex.h>
#include <boost/enable_shared_from_this.hpp>
#include <gr_metric_type.h>

/************************************************************/
/* gr_constellation                                         */
/*                                                          */
/* Decision maker uses nearest-point method.                */
/************************************************************/

class gr_constellation;
typedef boost::shared_ptr<gr_constellation> gr_constellation_sptr;

// public constructor
gr_constellation_sptr 
gr_make_constellation (std::vector<gr_complex> constellation, std::vector<unsigned int> pre_diff_code,
		       unsigned int rotational_symmetry);

class gr_constellation : public boost::enable_shared_from_this<gr_constellation>
{
 public:

  gr_constellation (std::vector<gr_complex> constellation, std::vector<unsigned int> pre_diff_code,
		    unsigned int rotational_symmetry);
  gr_constellation ();

  //! Returns the set of points in this constellation.
  std::vector<gr_complex> points() { return d_constellation;}
  //! Whether to apply an encoding before doing differential encoding. (e.g. gray coding)
  bool apply_pre_diff_code() { return d_apply_pre_diff_code;}
  //! Returns the encoding to apply before differential encoding.
  std::vector<unsigned int> pre_diff_code() { return d_pre_diff_code;}
  //! Returns the order of rotational symmetry.
  unsigned int rotational_symmetry() { return d_rotational_symmetry;}

  //! Returns the constellation point that matches best.
  //! Also calculates the phase error.
  virtual unsigned int decision_maker (gr_complex sample);
  
  //! Calculates metrics for all points in the constellation.
  //! For use with the viterbi algorithm.
  void calc_metric(gr_complex sample, float *metric, trellis_metric_type_t type);
  void calc_euclidean_metric(gr_complex sample, float *metric);
  void calc_hard_symbol_metric(gr_complex sample, float *metric);
  
  unsigned int bits_per_symbol () {
    return floor(log(d_constellation.size())/log(2));
  }
  
  unsigned int arity () {
    return d_constellation.size();
  }

  gr_constellation_sptr base() {
    return shared_from_this();
  }  

 protected:

  std::vector<gr_complex> d_constellation;
  std::vector<unsigned int> d_pre_diff_code; 
  bool d_apply_pre_diff_code;
  unsigned int d_rotational_symmetry;
  
 private:
  friend gr_constellation_sptr
  gr_make_constellation (std::vector<gr_complex> constellation);
};

/************************************************************/
/* gr_constellation_sector                                  */
/*                                                          */
/* An abstract class.                                       */
/* Constellation space is divided into sectors.             */
/* Each sector is associated with the nearest constellation */
/* point.                                                   */
/************************************************************/

class gr_constellation_sector : public gr_constellation
{
 public:

  gr_constellation_sector (std::vector<gr_complex> constellation,
			   std::vector<unsigned int> pre_diff_code,
			   unsigned int rotational_symmetry,
			   unsigned int n_sectors);

  unsigned int decision_maker (gr_complex sample);

 protected:

  virtual unsigned int get_sector (gr_complex sample) = 0;
  
  virtual unsigned int calc_sector_value (unsigned int sector) = 0;

  void find_sector_values ();

  unsigned int n_sectors;

 private:

  std::vector<unsigned int> sector_values;

};

/************************************************************/
/* gr_constellation_rect                                    */
/*                                                          */
/* Constellation space is divided into rectangular sectors. */
/* Each sector is associated with the nearest constellation */
/* point.                                                   */
/* Works well for square QAM.                               */
/* Works for any generic constellation provided sectors are */
/* not too large.                                           */
/************************************************************/

class gr_constellation_rect;
typedef boost::shared_ptr<gr_constellation_rect> gr_constellation_rect_sptr;

// public constructor
gr_constellation_rect_sptr 
gr_make_constellation_rect (std::vector<gr_complex> constellation, std::vector<unsigned int> pre_diff_code,
			    unsigned int rotational_symmetry,
			    unsigned int real_sectors, unsigned int imag_sectors,
			    float width_real_sectors, float width_imag_sectors);

class gr_constellation_rect : public gr_constellation_sector
{
 public:

  gr_constellation_rect (std::vector<gr_complex> constellation, std::vector<unsigned int> pre_diff_code,
			 unsigned int rotational_symmetry,
			 unsigned int real_sectors, unsigned int imag_sectors,
			 float width_real_sectors, float width_imag_sectors);

 protected:

  unsigned int get_sector (gr_complex sample);
  
  unsigned int calc_sector_value (unsigned int sector);

 private:

  unsigned int n_real_sectors;
  unsigned int n_imag_sectors;
  float d_width_real_sectors;
  float d_width_imag_sectors;

  friend gr_constellation_rect_sptr
  gr_make_constellation_rect (std::vector<gr_complex> constellation, std::vector<unsigned int> pre_diff_code,
			      unsigned int rotational_symmetry,
			      unsigned int real_sectors, unsigned int imag_sectors,
			      float width_real_sectors, float width_imag_sectors);
  
};

/************************************************************/
/* gr_constellation_psk                                     */
/*                                                          */
/* Constellation space is divided into pie slices sectors.  */
/* Each slice is associated with the nearest constellation  */
/* point.                                                   */
/* Works well for PSK but nothing else.                     */
/* Assumes that there is a constellation point at 1.        */
/************************************************************/

class gr_constellation_psk;
typedef boost::shared_ptr<gr_constellation_psk> gr_constellation_psk_sptr;

// public constructor
gr_constellation_psk_sptr 
gr_make_constellation_psk (std::vector<gr_complex> constellation, std::vector<unsigned int> pre_diff_code,
			   unsigned int n_sectors);

class gr_constellation_psk : public gr_constellation_sector
{
 public:

  gr_constellation_psk (std::vector<gr_complex> constellation, std::vector<unsigned int> pre_diff_code,
			unsigned int n_sectors);

 protected:

  unsigned int get_sector (gr_complex sample);
  
  unsigned int calc_sector_value (unsigned int sector);

 private:

  friend gr_constellation_psk_sptr
  gr_make_constellation_psk (std::vector<gr_complex> constellation, std::vector<unsigned int> pre_diff_code,
			     unsigned int n_sectors);
  
};

/************************************************************/
/* gr_constellation_bpsk                                    */
/*                                                          */
/* Only works for BPSK.                                     */
/*                                                          */
/************************************************************/

class gr_constellation_bpsk;
typedef boost::shared_ptr<gr_constellation_bpsk> gr_constellation_bpsk_sptr;

// public constructor
gr_constellation_bpsk_sptr 
gr_make_constellation_bpsk ();

class gr_constellation_bpsk : public gr_constellation
{
 public:

  gr_constellation_bpsk ();
  unsigned int decision_maker (gr_complex sample);

  friend gr_constellation_bpsk_sptr
  gr_make_constellation_bpsk ();
  
};

/************************************************************/
/* gr_constellation_qpsk                                    */
/*                                                          */
/* Only works for QPSK.                                     */
/*                                                          */
/************************************************************/

class gr_constellation_qpsk;
typedef boost::shared_ptr<gr_constellation_qpsk> gr_constellation_qpsk_sptr;

// public constructor
gr_constellation_qpsk_sptr 
gr_make_constellation_qpsk ();

class gr_constellation_qpsk : public gr_constellation
{
 public:

  gr_constellation_qpsk ();
  unsigned int decision_maker (gr_complex sample);

  friend gr_constellation_qpsk_sptr
  gr_make_constellation_qpsk ();
  
};

#endif