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
SourceForge Logo

Generated on Sat May 25 2013 16:32:20 for IT++ by Doxygen 1.8.2