IT++ Logo
commfunc.cpp
Go to the documentation of this file.
1 
29 #include <itpp/comm/commfunc.h>
30 #include <itpp/base/converters.h>
31 #include <itpp/base/specmat.h>
32 #include <itpp/base/matfunc.h>
33 #include <itpp/base/binary.h>
34 #include <itpp/base/sort.h>
35 
36 namespace itpp
37 {
38 
39 bmat graycode(int m)
40 {
41  if (m == 1) {
42  smat temp = "0;1";
43  return to_bmat(temp);
44  }
45  else {
46  bvec temp(1 << (m - 1));
47  bmat bb = graycode(m - 1);
48  bmat out(1 << m, m);
49  out.zeros();
50  out.set_col(0, concat(zeros_b(1 << (m - 1)), ones_b(1 << (m - 1))));
51  for (int i = 0; i < m - 1; i++) {
52  temp = bb.get_col(i);
53  out.set_col(i + 1, concat(temp, reverse(temp)));
54  }
55  return out;
56  }
57 }
58 
59 int hamming_distance(const bvec &a, const bvec &b)
60 {
61  int i, n = 0;
62 
63  it_assert_debug(a.size() == b.size(), "hamming_distance()");
64  for (i = 0; i < a.size(); i++)
65  if (a(i) != b(i))
66  n++;
67 
68  return n;
69 }
70 
71 int weight(const bvec &a)
72 {
73  int i, n = 0;
74 
75  for (i = 0; i < a.size(); i++)
76  if (a(i) == bin(1))
77  n++;
78 
79  return n;
80 }
81 
82 vec waterfilling(const vec &alpha, double P) // added by EGL April 2007
83 {
84  int n = length(alpha);
85  it_assert(n > 0, "waterfilling(): alpha vector cannot have zero length");
86  it_assert(P > 0, "waterfilling(): Power constraint must be positive");
87 
88  ivec ind = sort_index(alpha); // indices in increasing order
89  it_assert(alpha(ind(0)) > 0, "waterfilling(): Gains must be positive");
90 
91  // find lambda
92  double lambda = 0.0;
93  for (int m = 0; m < n; m++) {
94  // try m,...,n-1 nonzero allocation
95  double t = 0;
96  for (int j = m; j < n; j++) {
97  t += 1.0 / alpha(ind(j));
98  }
99  t = (t + P) / (n - m);
100  lambda = 1.0 / t;
101  if (lambda < alpha(ind(m)))
102  break;
103  }
104 
105  vec result(n);
106  for (int j = 0; j < n; j++) {
107  result(j) = ((lambda < alpha(j)) ? (1.0 / lambda - 1.0 / alpha(j)) : 0.0);
108  }
109 
110  return result;
111 }
112 
113 } // namespace itpp
Various functions on vectors and matrices - header file.
ITPP_EXPORT bvec ones_b(int size)
A Binary vector of ones.
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition: matfunc.h:777
Binary class definition.
ITPP_EXPORT bvec zeros_b(int size)
A Binary vector of zeros.
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
int length(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:51
Sorting functions.
int weight(const bvec &a)
Calculate the Hamming weight of a.
Definition: commfunc.cpp:71
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
Definitions of converters between different vector and matrix types.
Definitions of some specific functions useful in communications.
vec waterfilling(const vec &alpha, double P)
Compute the water-filling solutionThis function computes the solution of the water-filling problem s...
Definition: commfunc.cpp:82
itpp namespace
Definition: itmex.h:36
bmat graycode(int m)
Generate Gray code of blocklength m.The codes are contained as binary codewords {0,1} in the rows of the returned matrix. See also the gray() function in math/scalfunc.h.
Definition: commfunc.cpp:39
Definitions of special vectors and matrices.
Binary arithmetic (boolean) class.
Definition: binary.h:56
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
Definition: converters.h:168
int hamming_distance(const bvec &a, const bvec &b)
Calculate the Hamming distance between a and b.
Definition: commfunc.cpp:59
Mat< bin > bmat
bin matrix
Definition: mat.h:508
const Array< T > concat(const Array< T > &a, const T &e)
Append element e to the end of the Array a.
Definition: array.h:486

Generated on Thu Jun 21 2018 16:06:18 for IT++ by Doxygen 1.8.13