IT++
4.3.1
Toggle main menu visibility
itpp
comm
commfunc.cpp
Go to the documentation of this file.
1
28
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
binary.h
Binary class definition.
itpp::Vec::sort_index
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
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 by
1.17.0