hammcode.cpp
Go to the documentation of this file.
#include <itpp/comm/hammcode.h>
#include <itpp/base/math/log_exp.h>
#include <itpp/base/converters.h>
namespace itpp
{
Hamming_Code::Hamming_Code(int m)
{
n = pow2i(m) - 1;
k = pow2i(m) - m - 1;
H.set_size(n - k, n);
G.set_size(k, n);
generate_H(); // generate_H must be run before generate_G
generate_G();
}
void Hamming_Code::generate_H(void)
{
int i, j, NextPos;
char NotUsed;
bvec temp;
ivec indexes(n);
indexes.zeros();
for (i = 1; i <= n - k; i++) { indexes(i - 1) = pow2i(n - k - i); }
NextPos = n - k;
for (i = 1; i <= n; i++) {
NotUsed = 1;
for (j = 0; j < n; j++)
if (i == indexes(j)) { NotUsed = 0; }
if (NotUsed) { indexes(NextPos) = i; NextPos = NextPos + 1; }
}
for (i = 0; i < n; i++) {
temp = dec2bin(n - k, indexes(i)); //<-CHECK THIS OUT!!!!
for (j = 0; j < (n - k); j++) {
H(j, i) = temp(j);
}
}
}
void Hamming_Code::generate_G(void)
{
int i, j;
for (i = 0; i < k; i++) {
for (j = 0; j < n - k; j++)
G(i, j) = H(j, i + n - k);
}
for (i = 0; i < k; i++) {
for (j = n - k; j < n; j++)
G(i, j) = 0;
}
for (i = 0; i < k; i++)
G(i, i + n - k) = 1;
}
void Hamming_Code::encode(const bvec &uncoded_bits, bvec &coded_bits)
{
int length = uncoded_bits.length();
int Itterations = floor_i(static_cast<double>(length) / k);
bmat Gt = G.T();
int i;
coded_bits.set_size(Itterations * n, false);
//Code all codewords
for (i = 0; i < Itterations; i++)
coded_bits.replace_mid(n*i, Gt * uncoded_bits.mid(i*k, k));
}
bvec Hamming_Code::encode(const bvec &uncoded_bits)
{
bvec coded_bits;
encode(uncoded_bits, coded_bits);
return coded_bits;
}
void Hamming_Code::decode(const bvec &coded_bits, bvec &decoded_bits)
{
int length = coded_bits.length();
int Itterations = floor_i(static_cast<double>(length) / n);
ivec Hindexes(n);
bvec temp(n - k);
bvec coded(n), syndrome(n - k);
int isynd, errorpos = 0;
int i, j;
decoded_bits.set_size(Itterations*k, false);
for (i = 0; i < n; i++) {
for (j = 0; j < n - k; j++)
temp(j) = H(j, i);
Hindexes(i) = bin2dec(temp);
}
//Decode all codewords
for (i = 0; i < Itterations; i++) {
coded = coded_bits.mid(i * n, n);
syndrome = H * coded;
isynd = bin2dec(syndrome);
if (isynd != 0) {
for (j = 0; j < n; j++)
if (Hindexes(j) == isynd) { errorpos = j; };
coded(errorpos) += 1;
}
decoded_bits.replace_mid(k*i, coded.right(k));
}
}
bvec Hamming_Code::decode(const bvec &coded_bits)
{
bvec decoded_bits;
decode(coded_bits, decoded_bits);
return decoded_bits;
}
// -------------- Soft-decision decoding is not implemented ----------------
void Hamming_Code::decode(const vec &, bvec &)
{
it_error("Hamming_Code::decode(vec, bvec); soft-decision decoding is not implemented");
}
bvec Hamming_Code::decode(const vec &)
{
it_error("Hamming_Code::decode(vec, bvec); soft-decision decoding is not implemented");
return bvec();
}
} // namespace itpp
itpp::Hamming_Code::Hamming_Code
Hamming_Code(int m)
Constructor for hamming(n,k). n = pow(2,m)-1 and k = pow(2,m)-m-1.
Definition: hammcode.cpp:37
log_exp.h
Logarithmic and exponenential functions - header file.
itpp::length
int length(const Vec< T > &v)
Length of vector.
Definition: matfunc.h:51
converters.h
Definitions of converters between different vector and matrix types.
hammcode.h
Definitions of a Hamming code class.
itpp::pow2i
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
Definition: log_exp.h:53
itpp::floor_i
int floor_i(double x)
The nearest smaller integer.
Definition: converters.h:350
itpp
itpp namespace
Definition: itmex.h:36
itpp::dec2bin
ITPP_EXPORT bvec dec2bin(int length, int index)
Convert a decimal int index to bvec using length bits in the representation.
itpp::Hamming_Code::decode
virtual void decode(const bvec &coded_bits, bvec &decoded_bits)
Hamming decoder. Will truncate some bits if not length = integer * n.
Definition: hammcode.cpp:109
itpp::Hamming_Code::encode
virtual void encode(const bvec &uncoded_bits, bvec &coded_bits)
Hamming encoder. Will truncate some bits if not length = integer * k.
Definition: hammcode.cpp:89
it_error
#define it_error(s)
Abort unconditionally.
Definition: itassert.h:126
itpp::bin2dec
ITPP_EXPORT int bin2dec(const bvec &inbvec, bool msb_first=true)
Convert a bvec to decimal int with the first bit as MSB if msb_first == true.
bmat
Mat< bin > bmat
bin matrix
Definition: mat.h:508

Generated on Thu Aug 20 2015 21:02:50 for IT++ by Doxygen 1.8.9.1