File: modulator.h

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (1173 lines) | stat: -rw-r--r-- 45,242 bytes parent folder | download
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
/*!
 * \file
 * \brief One- and two-dimensional modulators - header file
 * \author Tony Ottosson and Adam Piatyszek
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#ifndef MODULATOR_H
#define MODULATOR_H

#include <itpp/base/mat.h>
#include <itpp/base/math/elem_math.h>
#include <itpp/base/math/log_exp.h>
#include <itpp/base/converters.h>
#include <itpp/base/math/min_max.h>


namespace itpp {

  /*!
   * \ingroup modulators
   * \brief Soft demodulation methods
   */
  enum Soft_Method {
    LOGMAP,			//!< Log-MAP full calculation
    APPROX			//!< Approximate faster method
  };

  /*!
    \ingroup modulators
    \brief General modulator for 1D or 2D signal constellations.

    The Modulator class is designed for modeling any kind of 1D (real) or 2D
    (complex) signal constellations. Therefore it is used as a base class for
    such modulations like PAM, PSK, QAM, etc.

    The constellation of the modulator is described with two vectors. The
    first one contains the real or complex values representing the
    constellation points, whereas the other one includes the corresponding
    bit to symbol mapping (in the decimal from).

    Beside hard demapping, this class can also perform soft demodulation. To
    use it properly the received symbols should be equal to: \f[r_k = c_k
    s_k + n_k,\f] where \f$c_k\f$ is the real or complex channel gain,
    \f$s_k\f$ is the transmitted constellation symbol, and \f$n_k\f$ is the
    AWGN of the channel (with variance \f$N_0\f$).

    It is also assumed that the channel estimates are perfect when
    calculating the soft bits.
  */
  template <typename T>
  class Modulator {
  public:
    //! Default constructor
    Modulator();
    //! Constructor
    Modulator(const Vec<T>& symbols, const ivec& bits2symbols);
    //! Destructor
    virtual ~Modulator() {}

    //! Set the constellation to use in the modulator
    virtual void set(const Vec<T>& symbols, const ivec& bits2symbols);

    //! Returns number of bits per symbol
    virtual int bits_per_symbol() const { return k; }
    //! Get the symbol values used in the modulator
    virtual Vec<T> get_symbols() const { return symbols; }
    /*!
     * \brief Get the bitmap, which maps input bits into symbols
     *
     * The mapping is done as follows. An input bit sequence in decimal
     * notation is used for indexing the \c bits2symbols table. The indexing
     * result denotes the symbol to be used from the \c symbols table, e.g.:
     *
     * \code
     * PSK mod(8); // assume 8-PSK modulator
     * cvec sym =  mod.get_symbols();
     * ivec bits2sym = mod.get_bits2symbols();
     * bvec in_bits = "100" // input bits
     * int d = bin2dec(in_bits); // decimal representation of in_bits = 4
     * // mapping of d into PSK symbol using bits2sym and sym tables
     * std::complex<double> out_symbol = sym(bits2sym(d));
     * \endcode
     */
    virtual ivec get_bits2symbols() const { return bits2symbols; }

    //! Modulation of symbols
    virtual void modulate(const ivec& symbolnumbers, Vec<T>& output) const;
    //! Modulation of symbols
    virtual Vec<T> modulate(const ivec& symbolnumbers) const;

    //! Demodulation of symbols
    virtual void demodulate(const Vec<T>& signal, ivec& output) const;
    //! Demodulation of symbols
    virtual ivec demodulate(const Vec<T>& signal) const;

    //! Modulation of bits
    virtual void modulate_bits(const bvec& bits, Vec<T>& output) const;
    //! Modulation of bits
    virtual Vec<T> modulate_bits(const bvec& bits) const;

    //! Hard demodulation of bits
    virtual void demodulate_bits(const Vec<T>& signal, bvec& bits) const;
    //! Hard demodulation of bits
    virtual bvec demodulate_bits(const Vec<T>& signal) const;

    /*!
      \brief Soft demodulator for AWGN channels

      This function calculates the log-likelihood ratio (LLR) of the
      received signal from AWGN channels. Depending on the soft demodulation
      method chosen, either full log-MAP calculation is performed (default
      method), according to the following equation: \f[\log \left(
      \frac{P(b_i=0|r)}{P(b_i=1|r)} \right) = \log \left( \frac{\sum_{s_i
      \in S_0} \exp \left( -\frac{|r_k - s_i|^2}{N_0} \right)} {\sum_{s_i
      \in S_1} \exp \left( -\frac{|r_k - s_i|^2}{N_0} \right)} \right) \f]
      or approximate, but faster calculation is performed.

      The approximate method finds for each bit the closest constellation
      points that have zero and one in the corresponding position. Let
      \f$d_0 = |r_k - s_0|\f$ denote the distance to the closest zero point
      and \f$d_1 = |r_k - s_1|\f$ denote the distance to the closest one
      point for the corresponding bit respectively. The approximate
      algorithm then computes \f[\frac{d_1^2 - d_0^2}{N_0}\f]

      This function can be used on channels where the channel gain
      \f$c_k = 1\f$.

      When this function is to be used together with MAP-based turbo
      decoding algorithms then the channel reliability factor \f$L_c\f$ of
      the turbo decoder shall be set to 1. The output from this function can
      also be used by a Viterbi decoder using an AWGN based metric
      calculation function.

      \param rx_symbols The received noisy constellation symbols
      \param N0 The spectral density of the AWGN noise
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the
      N-dimensional modulator (\c Modulator_ND) instead, which is
      based on the QLLR (quantized) arithmetic and therefore is
      faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const Vec<T>& rx_symbols, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for AWGN channels
    virtual vec demodulate_soft_bits(const Vec<T>& rx_symbols, double N0,
				     Soft_Method method = LOGMAP) const;

    /*!
      \brief Deprecated soft demodulator for AWGN channels. Please use
      demodulate_soft_bits() with method = APPROX instead.
    */
    virtual void demodulate_soft_bits_approx(const Vec<T>& rx_symbols,
					     double N0, vec& soft_bits) const;
    /*!
      \brief Deprecated soft demodulator for AWGN channels. Please use
      demodulate_soft_bits() with method = APPROX instead.
    */
    virtual vec demodulate_soft_bits_approx(const Vec<T>& rx_symbols,
					    double N0) const;

    /*!
      \brief Soft demodulator for fading channels

      This function calculates the log-likelihood ratio (LLR) of the
      received signal from fading channels. Depending on the soft
      demodulation method chosen, either full log-MAP calculation is
      performed (default method), according to the following equation:
      \f[\log \left( \frac{P(b_i=0|r)}{P(b_i=1|r)} \right) = \log \left(
      \frac{\sum_{s_i \in S_0} \exp \left( -\frac{|r_k - c_k s_i|^2}{N_0}
      \right)} {\sum_{s_i \in S_1} \exp \left( -\frac{|r_k - c_k
      s_i|^2}{N_0} \right)} \right) \f] or approximate, but faster
      calculation is performed.

      The approximate method finds for each bit the closest constellation
      points that have zero and one in the corresponding position. Let
      \f$d_0 = |r_k - c_k s_0|\f$ denote the distance to the closest zero
      point and \f$d_1 = |r_k - c_k s_1|\f$ denote the distance to the
      closest one point for the corresponding bit respectively. The
      approximate algorithm then computes \f[\frac{d_1^2 - d_0^2}{N_0}\f]

      When this function is to be used together with MAP-based turbo
      decoding algorithms then the channel reliability factor \f$L_c\f$ of
      the turbo decoder shall be set to 1. The output from this function can
      also be used by a Viterbi decoder using an AWGN based metric
      calculation function.

      \param rx_symbols The received noisy constellation symbols \f$r_k\f$
      \param channel The channel values \f$c_k\f$
      \param N0 The spectral density of the AWGN noise
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the
      N-dimensional modulator (Modulator_ND) instead, which is based
      on the QLLR (quantized) arithmetic and therefore is
      faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const Vec<T>& rx_symbols,
				      const Vec<T>& channel,
				      double N0, vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for fading channels
    virtual vec demodulate_soft_bits(const Vec<T>& rx_symbols,
				     const Vec<T>& channel,
				     double N0,
				     Soft_Method method = LOGMAP) const;

    /*!
      \brief Deprecated soft demodulator for AWGN channels. Please use
      demodulate_soft_bits() with method = APPROX instead.
    */
    virtual void demodulate_soft_bits_approx(const Vec<T>& rx_symbols,
					     const Vec<T>& channel,
					     double N0, vec& soft_bits) const;
    /*!
      \brief Deprecated soft demodulator for AWGN channels. Please use
      demodulate_soft_bits() with method = APPROX instead.
    */
    virtual vec demodulate_soft_bits_approx(const Vec<T>& rx_symbols,
					    const Vec<T>& channel,
					    double N0) const;

  protected:
    //! Setup indicator
    bool setup_done;
    //! Number of bits per modulation symbol
    int k;
    //! Number of modulation symbols
    int M;
    //! Bit to symbol mapping table (size: M x k)
    bmat bitmap;
    //! Bit to symbol mapping in decimal form (size: M)
    ivec bits2symbols;
    //! Corresponding modulation symbols (size: M)
    Vec<T> symbols;
    /*! \brief Matrix where row k contains the constellation points with '0'
      in bit position k */
    imat S0;
    /*! \brief Matrix where row k contains the constellation points with '1'
      in bit position k */
    imat S1;

    //! This function calculates the soft bit mapping matrices S0 and S1
    void calculate_softbit_matrices(const ivec& bits2symbols);
  };


  // ----------------------------------------------------------------------
  // Type definitions of Modulator_1D and Modulator_2D
  // ----------------------------------------------------------------------

  /*!
   * \relates Modulator
   * \brief Definition of 1D Modulator (with real symbols)
   */
  typedef Modulator<double> Modulator_1D;

  /*!
   * \relates Modulator
   * \brief Definition of 2D Modulator (with complex symbols)
   */
  typedef Modulator<std::complex<double> > Modulator_2D;


  // ----------------------------------------------------------------------
  // Implementation of templated Modulator members
  // ----------------------------------------------------------------------

  template<typename T>
  Modulator<T>::Modulator() :
    setup_done(false), k(0), M(0), bitmap(""), bits2symbols(""), symbols(""),
    S0(""), S1("") {}

  template<typename T>
  Modulator<T>::Modulator(const Vec<T> &symbols, const ivec &bits2symbols)
  {
    set(symbols, bits2symbols);
  }

  template<typename T>
  void Modulator<T>::set(const Vec<T> &in_symbols, const ivec &in_bits2symbols)
  {
    it_assert(in_symbols.size() == in_bits2symbols.size(),
	      "Modulator<T>::set(): Number of symbols and bits2symbols does not match");
    it_assert(is_even(in_symbols.size()) && (in_symbols.size() > 0),
	      "Modulator<T>::set(): Number of symbols needs to be even and non-zero");
    it_assert((max(in_bits2symbols) == in_bits2symbols.size() - 1)
	      && (min(in_bits2symbols) == 0), "Modulator<T>::set(): Improper bits2symbol vector");
    symbols = in_symbols;
    bits2symbols = in_bits2symbols;
    M = bits2symbols.size();
    k = levels2bits(M);
    bitmap.set_size(M, k);
    for (int m = 0; m < M; m++) {
      bitmap.set_row(bits2symbols(m), dec2bin(k, m));
    }
    calculate_softbit_matrices(bits2symbols);
    setup_done = true;
  }


  template<typename T>
  void Modulator<T>::modulate(const ivec &symbolnumbers, Vec<T>& output) const
  {
    it_assert_debug(setup_done, "Modulator<T>::modulate(): Modulator not ready.");
    output.set_size(symbolnumbers.length());
    for (int i = 0; i < symbolnumbers.length(); i++)
      output(i) = symbols(symbolnumbers(i));
  }

  template<typename T>
  Vec<T> Modulator<T>::modulate(const ivec &symbolnumbers) const
  {
    Vec<T> output(symbolnumbers.length());
    modulate(symbolnumbers, output);
    return output;
  }


  template<typename T>
  void Modulator<T>::demodulate(const Vec<T> &signal, ivec& output) const
  {
    it_assert_debug(setup_done, "Modulator<T>::demodulate(): Modulator not ready.");
    double dist, mindist;
    int closest;
    output.set_size(signal.size());

    for (int i = 0; i < signal.size(); i++) {
      mindist = std::abs(symbols(0) - signal(i));
      closest = 0;
      for (int j = 1; j < M; j++) {
	dist = std::abs(symbols(j) - signal(i));
	if (dist < mindist) {
	  mindist = dist;
	  closest = j;
	}
      }
      output(i) = closest;
    }
  }

  template<typename T>
  ivec Modulator<T>::demodulate(const Vec<T>& signal) const
  {
    ivec output(signal.length());
    demodulate(signal, output);
    return output;
  }


  template<typename T>
  void Modulator<T>::modulate_bits(const bvec &bits, Vec<T> &output) const
  {
    it_assert_debug(setup_done, "Modulator<T>::modulate_bits(): Modulator not ready.");
    // Check if some bits have to be cut and print warning message in such
    // case.
    if (bits.length() % k) {
      it_warning("Modulator<T>::modulate_bits(): The number of input bits is not a multiple of k (number of bits per symbol). Remainder bits are not modulated.");
    }
    int no_symbols = bits.length() / k;
    output.set_size(no_symbols);
    for (int i = 0; i < no_symbols; i++) {
      output(i) = symbols(bits2symbols(bin2dec(bits.mid(i*k, k))));
    }
  }

  template<typename T>
  Vec<T> Modulator<T>::modulate_bits(const bvec &bits) const
  {
    Vec<T> output;
    modulate_bits(bits, output);
    return output;
  }

  template<typename T>
  void Modulator<T>::demodulate_bits(const Vec<T> &signal, bvec &bits) const
  {
    it_assert_debug(setup_done, "Modulator<T>::demodulate_bist(): Modulator not ready.");
    double dist, mindist;
    int closest;
    bits.set_size(k*signal.size());

    for (int i = 0; i < signal.size(); i++) {
      mindist = std::abs(symbols(0) - signal(i));
      closest = 0;
      for (int j = 1; j < M; j++) {
	dist = std::abs(symbols(j) - signal(i));
	if (dist < mindist) {
	  mindist = dist;
	  closest = j;
	}
      }
      bits.replace_mid(i*k, bitmap.get_row(closest));
    }
  }

  template<typename T>
  bvec Modulator<T>::demodulate_bits(const Vec<T> &signal) const
  {
    bvec bits;
    demodulate_bits(signal, bits);
    return bits;
  }


  template<typename T>
  void Modulator<T>::demodulate_soft_bits(const Vec<T> &rx_symbols, double N0,
					  vec &soft_bits,
					  Soft_Method method) const
  {
    it_assert_debug(setup_done, "Modulator<T>::demodulate_soft_bits(): Modulator not ready.");
    double P0, P1, d0min, d1min, temp;
    vec metric(M);

    soft_bits.set_size(k * rx_symbols.size());

    if (method == LOGMAP) {
      for (int l = 0; l < rx_symbols.size(); l++) {
	for (int j = 0; j < M; j++) {
	  metric(j) = std::exp(-sqr(rx_symbols(l) - symbols(j)) / N0);
	}
	for (int i = 0; i < k; i++) {
	  P0 = P1 = 0;
	  for (int j = 0; j < (M >> 1); j++) {
	    P0 += metric(S0(i, j));
	    P1 += metric(S1(i, j));
	  }
	  soft_bits(l*k+i) = trunc_log(P0) - trunc_log(P1);
	}
      }
    }
    else { // method == APPROX
      for (int l = 0; l < rx_symbols.size(); l++) {
	for (int j = 0; j < M; j++) {
	  metric(j) = sqr(rx_symbols(l) - symbols(j));
	}
	for (int i = 0; i < k; i++) {
	  d0min = d1min = std::numeric_limits<double>::max();
	  for (int j = 0; j < (M >> 1); j++) {
	    temp = metric(S0(i, j));
	    if (temp < d0min) { d0min = temp; }
	    temp = metric(S1(i, j));
	    if (temp < d1min) { d1min = temp; }
	  }
	  soft_bits(l*k+i) = (-d0min + d1min) / N0;
	}
      }
    }
  }

  template<typename T>
  vec Modulator<T>::demodulate_soft_bits(const Vec<T> &rx_symbols,
					 double N0,
					 Soft_Method method) const
  {
    vec output;
    demodulate_soft_bits(rx_symbols, N0, output, method);
    return output;
  }

  template<typename T>
  void Modulator<T>::demodulate_soft_bits_approx(const Vec<T> &rx_symbols,
						 double N0,
						 vec &soft_bits) const
  {
    it_warning("Modulator<T>::demodulate_soft_bits_approx(): This function is deprecated. Please use demodulate_soft_bits() with method=APPROX instead.");
    demodulate_soft_bits(rx_symbols, N0, soft_bits, APPROX);
  }

  template<typename T>
  vec Modulator<T>::demodulate_soft_bits_approx(const Vec<T> &rx_symbols,
						double N0) const
  {
    it_warning("Modulator<T>::demodulate_soft_bits_approx(): This function is deprecated. Please use demodulate_soft_bits() with method=APPROX instead.");
    vec output;
    demodulate_soft_bits(rx_symbols, N0, output, APPROX);
    return output;
  }


  template<typename T>
  void Modulator<T>::demodulate_soft_bits(const Vec<T> &rx_symbols,
					  const Vec<T> &channel, double N0,
					  vec &soft_bits,
					  Soft_Method method) const
  {
    it_assert_debug(setup_done, "Modulator_2D::demodulate_soft_bits(): Modulator not ready.");
    double P0, P1, d0min, d1min, temp;
    vec metric(M);

    soft_bits.set_size(k * rx_symbols.size());

    if (method == LOGMAP) {
      for (int l = 0; l < rx_symbols.size(); l++) {
	for (int j = 0; j < M; j++) {
	  metric(j) = std::exp(-sqr(rx_symbols(l) - channel(l) * symbols(j))
			       / N0);
	}
	for (int i = 0; i < k; i++) {
	  P0 = P1 = 0;
	  for (int j = 0; j < (M >> 1); j++) {
	    P0 += metric(S0(i, j));
	    P1 += metric(S1(i, j));
	  }
	  soft_bits(l*k+i) = trunc_log(P0) - trunc_log(P1);
	}
      }
    }
    else { // method == APPROX
      for (int l = 0; l < rx_symbols.size(); l++) {
	for (int j = 0; j < M; j++) {
	  metric(j) = sqr(rx_symbols(l) - channel(l) * symbols(j));
	}
	for (int i = 0; i < k; i++) {
	  d0min = d1min = std::numeric_limits<double>::max();
	  for (int j = 0; j < (M >> 1); j++) {
	    temp = metric(S0(i, j));
	    if (temp < d0min) { d0min = temp; }
	    temp = metric(S1(i, j));
	    if (temp < d1min) { d1min = temp; }
	  }
	  soft_bits(l*k+i) = (-d0min + d1min) / N0;
	}
      }
    }
  }

  template<typename T>
  vec Modulator<T>::demodulate_soft_bits(const Vec<T> &rx_symbols,
					 const Vec<T> &channel,
					 double N0,
					 Soft_Method method) const
  {
    vec output;
    demodulate_soft_bits(rx_symbols, channel, N0, output, method);
    return output;
  }

  template<typename T>
  void Modulator<T>::demodulate_soft_bits_approx(const Vec<T> &rx_symbols,
						 const Vec<T> &channel,
						 double N0,
						 vec &soft_bits) const
  {
    it_warning("Modulator<T>::demodulate_soft_bits_approx(): This function is deprecated. Please use demodulate_soft_bits() with method=APPROX instead.");
    demodulate_soft_bits(rx_symbols, channel, N0, soft_bits, APPROX);
  }

  template<typename T>
  vec Modulator<T>::demodulate_soft_bits_approx(const Vec<T> &rx_symbols,
						const Vec<T> &channel,
						double N0) const
  {
    it_warning("Modulator<T>::demodulate_soft_bits_approx(): This function is deprecated. Please use demodulate_soft_bits() with method=APPROX instead.");
    vec output;
    demodulate_soft_bits(rx_symbols, channel, N0, output, APPROX);
    return output;
  }


  template<typename T>
  void Modulator<T>::calculate_softbit_matrices(const ivec& in_bits2symbols)
  {
    int count0, count1;

    // Allocate storage space for the result matrices:
    S0.set_size(k, M >> 1, false);
    S1.set_size(k, M >> 1, false);

    for (int i = 0; i < k; i++) {
      count0 = 0;
      count1 = 0;
      for (int j = 0; j < M; j++) {
	if (bitmap(j, i) == bin(0)) {
	  S0(i, count0++) = j;
	}
	else {
	  S1(i, count1++) = j;
	}
      }
    }
  }



  // ----------------------------------------------------------------------
  // QAM : Modulator_2D
  // ----------------------------------------------------------------------

  /*!
    \ingroup modulators
    \brief M-ary QAM modulator with square lattice.

    The size of the QAM constellation is \f$M = 2^k\f$, where \f$k = 1, 2,
    \ldots \f$. Symbol values in each dimension are: \f$\{-(\sqrt{M}-1),
    \ldots, -3, -1, 1, 3, \ldots, (\sqrt{M}-1)\}\f$. The bitmap is Gray
    encoded. Symbols are normalized so that the average energy is 1. That
    is, normalized with \f$\sqrt{2(M-1)/3}\f$.

    Beside hard demapping, this class can also perform soft demodulation,
    calculating the log-MAP estimate of the individual bits. To use it
    properly the received symbols should be equal to: \f[r_k = c_k s_k +
    n_k,\f] where \f$c_k\f$ is the real or complex channel gain, \f$s_k\f$
    is the transmitted constellation symbol, and \f$n_k\f$ is the AWGN of
    the channel (with variance \f$N_0\f$).

    It is also assumed that the channel estimates are perfect when
    calculating the soft bits.
  */
  class QAM : public Modulator<std::complex<double> > {
  public:
    //! Default Constructor
    QAM() {}
    //! Class Constructor
    QAM(int M) { set_M(M); }
    //! Destructor
    virtual ~QAM() { }
    //! Change the size of the signal constellation
    void set_M(int M);

    //! Hard demodulation of bits
    void demodulate_bits(const cvec& signal, bvec& bits) const;
    //! Hard demodulation of bits
    bvec demodulate_bits(const cvec& signal) const;

  protected:
    //! The square-root of M
    int L;
    //! Scaling factor of square QAM constellation (sqrt((M-1)*2/3))
    double scaling_factor;
  };


  // ----------------------------------------------------------------------
  // PSK : Modulator<std::complex<double> >
  // ----------------------------------------------------------------------

  /*!
    \ingroup modulators
    \brief M-ary PSK modulator.

    This class implements the M-ary PSK modulator with \f$M = 2^k\f$
    constellation points, where \f$k = 1, 2, \ldots \f$. The symbol
    numbering is counter clockwise starting from the real axis, i.e. symbol
    \f$(1, 0)\f$. The bitmap is Gray encoded. The symbol energy is
    normalized to 1.

    Beside hard demapping, this class can also perform soft demodulation,
    calculating the log-MAP estimate of the individual bits. To use it
    properly the received symbols should be equal to: \f[r_k = c_k s_k +
    n_k,\f] where \f$c_k\f$ is the real or complex channel gain, \f$s_k\f$
    is the transmitted constellation symbol, and \f$n_k\f$ is the AWGN of
    the channel (with variance \f$N_0\f$).

    It is also assumed that the channel estimates are perfect when
    calculating the soft bits.
  */
  class PSK : public Modulator<std::complex<double> > {
  public:
    //! Default Constructor
    PSK() {}
    //! Class constructor
    PSK(int M) { set_M(M); }
    //! Destructor
    virtual ~PSK() { }
    //! Change the size of the signal constellation
    void set_M(int M);

    //! Hard demodulation of bits
    void demodulate_bits(const cvec& signal, bvec& bits) const;
    //! Hard demodulation of bits
    bvec demodulate_bits(const cvec& signal) const;
  };


  // ----------------------------------------------------------------------
  // QPSK : PSK : Modulator<std::complex<double> >
  // ----------------------------------------------------------------------

  /*!
    \ingroup modulators
    \brief QPSK modulator.

    This is a special version of the PSK modulator with \f$M = 4\f$
    constellation points. Symbol numbering is counter clockwise starting
    from the real axis. Bits are Gray coded onto symbols. Symbol energy is
    normalized to 1.

    Beside hard demapping, this class can also perform soft demodulation,
    calculating the log-MAP estimate of the individual bits. To use it
    properly the received symbols should be equal to: \f[r_k = c_k s_k +
    n_k,\f] where \f$c_k\f$ is the real or complex channel gain, \f$s_k\f$
    is the transmitted constellation symbol, and \f$n_k\f$ is the AWGN of
    the channel (with variance \f$N_0\f$).

    It is also assumed that the channel estimates are perfect when
    calculating the soft bits.
  */
  class QPSK : public PSK {
  public:
    //! Class Constructor
    QPSK(): PSK(4) {}
    //! Destructor
    virtual ~QPSK() {}

    /*!
      \brief Soft demodulator for AWGN channel

      This function calculates the log-MAP estimate assuming equally likely
      bits transmitted: \f[\log \left( \frac{P(b=0|r)}{P(b=1|r)} \right) =
      \frac{2 \sqrt{2}}{N_0} \Im\{r_k \exp \left(j \frac{\Pi}{4} \right)
      \}\f] and \f[\log \left( \frac{P(b=0|r)}{P(b=1|r)} \right) = \frac{2
      \sqrt{2}}{N_0} \Re\{r_k \exp \left(j \frac{\Pi}{4} \right) \}\f]
      depending on the bit positon in the QPSK symbol.

      \param rx_symbols The received noisy constellation symbols, \f$r\f$
      \param N0 The spectral density of the AWGN noise, \f$n\f$
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the
      N-dimensional modulator (Modulator_ND) instead, which is based
      on the QLLR (quantized) arithmetic and therefore is
      faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const cvec& rx_symbols, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for AWGN channel
    vec demodulate_soft_bits(const cvec& rx_symbols, double N0,
			     Soft_Method method = LOGMAP) const;


    /*!
      \brief Soft demodulator for a known channel in AWGN

      This function calculates the log-MAP estimate assuming equally likely
      bits transmitted: \f[\log \left( \frac{P(b=0|r)}{P(b=1|r)} \right) =
      \frac{2 \sqrt{2}}{N_0} \Im\{r_k c_k \exp \left(j \frac{\Pi}{4} \right)
      \}\f] and \f[\log \left( \frac{P(b=0|r)}{P(b=1|r)} \right) = \frac{2
      \sqrt{2}}{N_0} \Re\{r_k c_k \exp \left(j \frac{\Pi}{4} \right) \}\f]
      depending on the bit positon in the QPSK symbol.

      \param rx_symbols The received noisy constellation symbols, \f$r\f$
      \param channel The channel coefficients, \f$c\f$
      \param N0 The spectral density of the AWGN noise, \f$n\f$
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the
      N-dimensional modulator (Modulator_ND) instead, which is based
      on the QLLR (quantized) arithmetic and therefore is
      faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const cvec& rx_symbols,
				      const cvec& channel, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for a known channel in AWGN
    vec demodulate_soft_bits(const cvec& rx_symbols, const cvec& channel,
			     double N0, Soft_Method method = LOGMAP) const;
  };


  // ----------------------------------------------------------------------
  // BPSK_c : PSK : Modulator<std::complex<double> >
  // ----------------------------------------------------------------------

  /*!
    \ingroup modulators
    \brief BPSK modulator with complex symbols.

    This is a special version of the PSK modulator with \f$M = 2\f$
    constellation points. The following bit to symbol mapping is used:
    - \f$0 \rightarrow 1+0i\f$
    - \f$1 \rightarrow -1+0i\f$.

    Beside hard demapping, this class can also perform soft demodulation,
    calculating the log-MAP estimate of the individual bits. To use it
    properly the received symbols should be equal to: \f[r_k = c_k s_k +
    n_k,\f] where \f$c_k\f$ is the real or complex channel gain, \f$s_k\f$
    is the transmitted constellation symbol, and \f$n_k\f$ is the AWGN of
    the channel (with variance \f$N_0\f$).

    It is also assumed that the channel estimates are perfect when
    calculating the soft bits.

    \note Although constellation points of the BPSK modulator can be
    represented in the real domain only, this class uses complex signals to
    be compatible with other PSK and QAM based modulators.

    \sa BPSK
  */
  class BPSK_c : public PSK {
  public:
    //! Constructor
    BPSK_c(): PSK(2) {}
    //! Destructor
    virtual ~BPSK_c() {}

    //! Modulate bits into BPSK symbols in complex domain
    void modulate_bits(const bvec& bits, cvec& output) const;
    //! Modulate bits into BPSK symbols  in complex domain
    cvec modulate_bits(const bvec& bits) const;
    //! Demodulate noisy BPSK symbols in complex domain into bits
    void demodulate_bits(const cvec& signal, bvec& output) const;
    //! Demodulate noisy BPSK symbols in complex domain into bits
    bvec demodulate_bits(const cvec& signal) const;

    /*!
      \brief Soft demodulator for AWGN channel

      This function calculates the log-MAP estimate assuming equally likely
      bits transmitted: \f[\log \left( \frac{P(b=0|r)}{P(b=1|r)} \right) =
      \frac{4 \Re\{r\}} {N_0}\f]

      \param rx_symbols The received noisy constellation symbols, \f$r\f$
      (complex but symbols in real part)
      \param N0 The spectral density of the AWGN noise, \f$n\f$
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the
      N-dimensional modulator (Modulator_ND) instead, which is based
      on the QLLR (quantized) arithmetic and therefore is
      faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const cvec& rx_symbols, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for AWGN channel
    vec demodulate_soft_bits(const cvec& rx_symbols, double N0,
			     Soft_Method method = LOGMAP) const;

    /*!
      \brief Soft demodulator for a known channel in AWGN

      This function calculates the log-MAP estimate assuming equally likely
      bits transmitted: \f[\log \left( \frac{P(b=0|r)}{P(b=1|r)} \right) =
      \frac{4 \Re\{r c^{*}\}}{N_0}\f]

      \param rx_symbols The received noisy constellation symbols, \f$r\f$
      (complex but symbols in real part)
      \param channel The channel coefficients, \f$c\f$ (complex)
      \param N0 The spectral density of the AWGN noise, \f$n\f$
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the
      N-dimensional modulator (Modulator_ND) instead, which is based
      on the QLLR (quantized) arithmetic and therefore is
      faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const cvec& rx_symbols,
				      const cvec& channel, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for a known channel in AWGN
    vec demodulate_soft_bits(const cvec& rx_symbols, const cvec& channel,
			     double N0, Soft_Method method = LOGMAP) const;
  };



  // ----------------------------------------------------------------------
  // BPSK : Modulator<double>
  // ----------------------------------------------------------------------

  /*!
    \ingroup modulators
    \brief BPSK modulator with real symbols.

    This is a special version of the PSK modulator with \f$M = 2\f$
    constellation points. The following bit to symbol mapping is used:
    - \f$0 \rightarrow 1\f$
    - \f$1 \rightarrow -1\f$.

    Beside hard demapping, this class can also perform soft demodulation,
    calculating the log-MAP estimate of the individual bits. To use it
    properly the received symbols should be equal to: \f[r_k = c_k s_k +
    n_k,\f] where \f$c_k\f$ is the real or complex channel gain, \f$s_k\f$
    is the transmitted constellation symbol, and \f$n_k\f$ is the AWGN of
    the channel (with variance \f$N_0\f$).

    It is also assumed that the channel estimates are perfect when
    calculating the soft bits.

    \note This class uses real values for representing symbols. There is
    a similar class named BPSK_c, which uses complex values for symbols and
    therefore is compatible with other PSK and QAM based modulators.
  */
  class BPSK : public Modulator<double> {
  public:
    //! Constructor
    BPSK(): Modulator<double>("1.0 -1.0", "0 1") {}
    //! Destructor
    virtual ~BPSK() {}

    //! Modulate bits into BPSK symbols in complex domain
    void modulate_bits(const bvec& bits, vec& output) const;
    //! Modulate bits into BPSK symbols  in complex domain
    vec modulate_bits(const bvec& bits) const;
    //! Demodulate noisy BPSK symbols in complex domain into bits
    void demodulate_bits(const vec& signal, bvec& output) const;
    //! Demodulate noisy BPSK symbols in complex domain into bits
    bvec demodulate_bits(const vec& signal) const;

    /*!
      \brief Soft demodulator for AWGN channel

      This function calculates the log-MAP estimate assuming equally likely
      bits transmitted: \f[\log \left( \frac{P(b=0|r)}{P(b=1|r)} \right) =
      \frac{4 r}{N_0}\f]

      \param rx_symbols The received noisy constellation symbols, \f$r\f$
      \param N0 The spectral density of the AWGN noise, \f$n\f$
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the N-dimensional
      modulator (Modulator_ND) instead, which is based on the QLLR
      (quantized) arithmetic and therefore is faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const vec& rx_symbols, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for AWGN channel
    vec demodulate_soft_bits(const vec& rx_symbols, double N0,
			     Soft_Method method = LOGMAP) const;

    /*!
      \brief Soft demodulator for a known channel in AWGN

      This function calculates the log-MAP estimate assuming equally likely
      bits transmitted: \f[\log \left( \frac{P(b=0|r)}{P(b=1|r)} \right) =
      \frac{4 \Re\{r c^{*}\}}{N_0}\f]

      \param rx_symbols The received noisy constellation symbols, \f$r\f$
      (complex but symbols in real part)
      \param channel The channel coefficients, \f$c\f$ (complex)
      \param N0 The spectral density of the AWGN noise, \f$n\f$
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the N-dimensional
      modulator (Modulator_ND) instead, which is based on the QLLR
      (quantized) arithmetic and therefore is faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const vec& rx_symbols,
				      const vec& channel, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for a known channel in AWGN
    vec demodulate_soft_bits(const vec& rx_symbols, const vec& channel,
			     double N0, Soft_Method method = LOGMAP) const;
  };


  // ----------------------------------------------------------------------
  // PAM_c : Modulator<std::complex<double> >
  // ----------------------------------------------------------------------

  /*!
    \ingroup modulators
    \brief M-ary PAM modulator with complex symbols.

    This class implements an M-ary PAM modulator with the following signal
    values: \f$\{-(M-1), \ldots, -3, -1, 1, 3, \ldots, (M-1)\}\f$. Symbol
    numbering is from right to left in the increasing order. The Gray
    encoding of bits to symbols is used.

    The constellation symbols are normalized so that the average energy is
    equal to 1. That is, normalized with \f$ \sqrt{(M^2-1)/3}\f$.

    \note Although the constellation points can be represented in the real
    domain only, this class uses complex based interface to be compatible
    with other PSK and QAM based modulators.

    \sa PAM
  */
  class PAM_c : public Modulator<std::complex<double> > {
  public:
    //! Default Constructor
    PAM_c() {}
    //! Constructor
    PAM_c(int M) { set_M(M); }
    //! Destructor
    virtual ~PAM_c() {}
    //! Set the size of the signal constellation
    void set_M(int M);

    //! Hard demodulation of PAM symbols in complex domain to bits
    void demodulate_bits(const cvec& signal, bvec& output) const;
    //! Hard demodulation of PAM symbols in complex domain to bits
    bvec demodulate_bits(const cvec& signal) const;

    /*!
      \brief Soft demodulator for AWGN channels.

      This function calculates the log-likelihood ratio (LLR) of the
      received signal from AWGN channels. Depending on the soft demodulation
      method chosen, either full log-MAP calculation is performed (default
      method), according to the following equation: \f[\log \left(
      \frac{P(b_i=0|r)}{P(b_i=1|r)} \right) = \log \left( \frac{\sum_{s_i
      \in S_0} \exp \left( -\frac{|r_k - s_i|^2}{N_0} \right)} {\sum_{s_i
      \in S_1} \exp \left( -\frac{|r_k - s_i|^2}{N_0} \right)} \right) \f]
      or approximate, but faster calculation is performed.

      The approximate method finds for each bit the closest constellation
      points that have zero and one in the corresponding position. Let
      \f$d_0 = |r_k - s_0|\f$ denote the distance to the closest zero point
      and \f$d_1 = |r_k - s_1|\f$ denote the distance to the closest one
      point for the corresponding bit respectively. The approximate
      algorithm then computes \f[\frac{d_1^2 - d_0^2}{N_0}\f]

      This function can be used on channels where the channel gain
      \f$c = 1\f$.

      When this function is to be used together with MAP-based turbo
      decoding algorithms then the channel reliability factor \f$L_c\f$ of
      the turbo decoder shall be set to 1. The output from this function can
      also be used by a Viterbi decoder using an AWGN based metric
      calculation function.

      \param rx_symbols The received noisy constellation symbols \f$r_k\f$
      (complex, but symbols are real)
      \param N0 The spectral density of the AWGN noise
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the N-dimensional
      modulator (Modulator_ND) instead, which is based on the QLLR
      (quantized) arithmetic and therefore is faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const cvec& rx_symbols, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for AWGN channels.
    virtual vec demodulate_soft_bits(const cvec& rx_symbols, double N0,
				     Soft_Method method = LOGMAP) const;

    /*!
      \brief Soft demodulator for known fading channels.

      This function calculates the log-likelihood ratio (LLR) of the
      received signal from fading channels. Depending on the soft
      demodulation method chosen, either full log-MAP calculation is
      performed (default method), according to the following equation:
      \f[\log \left( \frac{P(b_i=0|r)}{P(b_i=1|r)} \right) = \log \left(
      \frac{\sum_{s_i \in S_0} \exp \left( -\frac{|r_k - c_k s_i|^2}{N_0}
      \right)} {\sum_{s_i \in S_1} \exp \left( -\frac{|r_k - c_k
      s_i|^2}{N_0} \right)} \right) \f] or approximate, but faster
      calculation is performed.

      The approximate method finds for each bit the closest constellation
      points that have zero and one in the corresponding position. Let
      \f$d_0 = |r_k - c_k s_0|\f$ denote the distance to the closest zero
      point and \f$d_1 = |r_k - c_k s_1|\f$ denote the distance to the
      closest one point for the corresponding bit respectively. The
      approximate algorithm then computes \f[\frac{d_1^2 - d_0^2}{N_0}\f]

      When this function is to be used together with MAP-based turbo
      decoding algorithms then the channel reliability factor \f$L_c\f$ of
      the turbo decoder shall be set to 1. The output from this function can
      also be used by a Viterbi decoder using an AWGN based metric
      calculation function.

      \param rx_symbols The received noisy constellation symbols \f$r_k\f$
      (complex)
      \param channel The channel values \f$c_k\f$
      \param N0 The spectral density of the AWGN noise
      \param soft_bits The soft bits calculated using the expression above
      \param method The method used for demodulation (LOGMAP or APPROX)

      \note For soft demodulation it is suggested to use the N-dimensional
      modulator (Modulator_ND) instead, which is based on the QLLR
      (quantized) arithmetic and therefore is faster. Please note, however, that mixed use of \c
      Modulator_1D/\c Modulator_2D and \c Modulator_ND is not advised.
    */
    virtual void demodulate_soft_bits(const cvec& rx_symbols,
				      const cvec& channel, double N0,
				      vec& soft_bits,
				      Soft_Method method = LOGMAP) const;
    //! Soft demodulator for known fading channels.
    virtual vec demodulate_soft_bits(const cvec& rx_symbols,
				     const cvec& channel, double N0,
				     Soft_Method method = LOGMAP) const;

  protected:
    //! Scaling factor used to normalize the average energy to 1
    double scaling_factor;
  };


  // ----------------------------------------------------------------------
  // PAM : Modulator<double>
  // ----------------------------------------------------------------------

  /*!
    \ingroup modulators
    \brief M-ary PAM modulator with real symbols.

    This class implements an M-ary PAM modulator with the following signal
    values: \f$\{-(M-1), \ldots, -3, -1, 1, 3, \ldots, (M-1)\}\f$. Symbol
    numbering is from right to left in the increasing order. The Gray
    encoding of bits to symbols is used.

    The constellation symbols are normalized so that the average energy is
    equal to 1. That is, normalized with \f$ \sqrt{(M^2-1)/3}\f$.

    \note This class uses real values for representing symbols. There is
    a similar class named PAM_c, which uses complex values for symbols and
    therefore is compatible with other PSK and QAM based modulators.
  */
  class PAM : public Modulator<double> {
  public:
    //! Default Constructor
    PAM() {}
    //! Constructor
    PAM(int M) { set_M(M); }
    //! Destructor
    virtual ~PAM() {}
    //! Set the size of the signal constellation
    void set_M(int M);

    //! Hard demodulation of PAM symbols in complex domain to bits
    void demodulate_bits(const vec& signal, bvec& output) const;
    //! Hard demodulation of PAM symbols in complex domain to bits
    bvec demodulate_bits(const vec& signal) const;

  protected:
    //! Scaling factor used to normalize the average energy to 1
    double scaling_factor;
  };

} // namespace itpp

#endif // #ifndef MODULATOR_H