commfunc.cpp
Go to the documentation of this file.
#include <itpp/comm/commfunc.h>
#include <itpp/base/converters.h>
#include <itpp/base/specmat.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/binary.h>
#include <itpp/base/sort.h>
namespace itpp
{
bmat graycode(int m)
{
if (m == 1) {
smat temp = "0;1";
return to_bmat(temp);
}
else {
bvec temp(1 << (m - 1));
bmat bb = graycode(m - 1);
bmat out(1 << m, m);
out.zeros();
out.set_col(0, concat(zeros_b(1 << (m - 1)), ones_b(1 << (m - 1))));
for (int i = 0; i < m - 1; i++) {
temp = bb.get_col(i);
out.set_col(i + 1, concat(temp, reverse(temp)));
}
return out;
}
}
int hamming_distance(const bvec &a, const bvec &b)
{
int i, n = 0;
it_assert_debug(a.size() == b.size(), "hamming_distance()");
for (i = 0; i < a.size(); i++)
if (a(i) != b(i))
n++;
return n;
}
int weight(const bvec &a)
{
int i, n = 0;
for (i = 0; i < a.size(); i++)
if (a(i) == bin(1))
n++;
return n;
}
vec waterfilling(const vec &alpha, double P) // added by EGL April 2007
{
int n = length(alpha);
it_assert(n > 0, "waterfilling(): alpha vector cannot have zero length");
it_assert(P > 0, "waterfilling(): Power constraint must be positive");
ivec ind = sort_index(alpha); // indices in increasing order
it_assert(alpha(ind(0)) > 0, "waterfilling(): Gains must be positive");
// find lambda
double lambda = 0.0;
for (int m = 0; m < n; m++) {
// try m,...,n-1 nonzero allocation
double t = 0;
for (int j = m; j < n; j++) {
t += 1.0 / alpha(ind(j));
}
t = (t + P) / (n - m);
lambda = 1.0 / t;
if (lambda < alpha(ind(m)))
break;
}
vec result(n);
for (int j = 0; j < n; j++) {
result(j) = ((lambda < alpha(j)) ? (1.0 / lambda - 1.0 / alpha(j)) : 0.0);
}
return result;
}
} // namespace itpp
matfunc.h
Various functions on vectors and matrices - header file.
itpp::ones_b
ITPP_EXPORT bvec ones_b(int size)
A Binary vector of ones.
itpp::reverse
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition: matfunc.h:777
binary.h
Binary class definition.
itpp::zeros_b
ITPP_EXPORT bvec zeros_b(int size)
A Binary vector of zeros.
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
itpp::length
int length(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:51
sort.h
Sorting functions.
itpp::weight
int weight(const bvec &a)
Calculate the Hamming weight of a.
Definition: commfunc.cpp:71
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
converters.h
Definitions of converters between different vector and matrix types.
commfunc.h
Definitions of some specific functions useful in communications.
itpp::waterfilling
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::graycode
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
specmat.h
Definitions of special vectors and matrices.
itpp::bin
Binary arithmetic (boolean) class.
Definition: binary.h:56
itpp::to_bmat
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
Definition: converters.h:168
itpp::hamming_distance
int hamming_distance(const bvec &a, const bvec &b)
Calculate the Hamming distance between a and b.
Definition: commfunc.cpp:59
bmat
Mat< bin > bmat
bin matrix
Definition: mat.h:508
itpp::concat
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 Mar 20 2014 12:26:42 for IT++ by Doxygen 1.8.6