IT++ Logo
commfunc.cpp
Go to the documentation of this file.
1
29#include <itpp/comm/commfunc.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
36namespace itpp
37{
38
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
59int 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
71int 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
82vec 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
Binary class definition.
ivec sort_index(const Vec< T > &data, SORTING_METHOD method=INTROSORT)
Return an index vector corresponding to a sorted vector data in increasing order.
Definition sort.h:203
Binary arithmetic (boolean) class.
Definition binary.h:57
Definitions of some specific functions useful in communications.
Definitions of converters between different vector and matrix types.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
#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
vec waterfilling(const vec &alpha, double P)
Compute the water-filling solution.
Definition commfunc.cpp:82
int weight(const bvec &a)
Calculate the Hamming weight of a.
Definition commfunc.cpp:71
bmat graycode(int m)
Generate Gray code of blocklength m.
Definition commfunc.cpp:39
int hamming_distance(const bvec &a, const bvec &b)
Calculate the Hamming distance between a and b.
Definition commfunc.cpp:59
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition matfunc.h:777
ITPP_EXPORT bvec ones_b(int size)
A Binary vector of ones.
ITPP_EXPORT bvec zeros_b(int size)
A Binary vector of zeros.
Mat< bin > bmat
bin matrix
Definition mat.h:508
Various functions on vectors and matrices - header file.
itpp namespace
Definition itmex.h:37
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
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
Definition converters.h:168
Sorting functions.
Definitions of special vectors and matrices.
SourceForge Logo

Generated on Tue Dec 10 2024 04:49:37 for IT++ by Doxygen 1.12.0