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
binary.h
Binary class definition.
itpp::Array
General array class.
Definition array.h:105
itpp::Array::size
int size() const
Returns the number of data elements in the array object.
Definition array.h:155
itpp::bin
Binary arithmetic (boolean) class.
Definition binary.h:57
commfunc.h
Definitions of some specific functions useful in communications.
converters.h
Definitions of converters between different vector and matrix types.
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
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
itpp::waterfilling
vec waterfilling(const vec &alpha, double P)
Compute the water-filling solution.
Definition commfunc.cpp:82
itpp::weight
int weight(const bvec &a)
Calculate the Hamming weight of a.
Definition commfunc.cpp:71
itpp::graycode
bmat graycode(int m)
Generate Gray code of blocklength m.
Definition commfunc.cpp:39
itpp::hamming_distance
int hamming_distance(const bvec &a, const bvec &b)
Calculate the Hamming distance between a and b.
Definition commfunc.cpp:59
itpp::reverse
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition matfunc.h:777
itpp::ones_b
ITPP_EXPORT bvec ones_b(int size)
A Binary vector of ones.
itpp::zeros_b
ITPP_EXPORT bvec zeros_b(int size)
A Binary vector of zeros.
bmat
Mat< bin > bmat
bin matrix
Definition mat.h:508
matfunc.h
Various functions on vectors and matrices - header file.
itpp
itpp namespace
Definition itmex.h:37
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
itpp::to_bmat
bmat to_bmat(const Mat< T > &m)
Converts a Mat<T> to bmat.
Definition converters.h:168
sort.h
Sorting functions.
specmat.h
Definitions of special vectors and matrices.

Generated on Mon Jun 10 2024 11:49:20 for IT++ by Doxygen 1.9.8