crc.cpp
Go to the documentation of this file.
#include <itpp/comm/crc.h>
#include <itpp/base/specmat.h>
#include <itpp/base/matfunc.h>
namespace itpp
{
void CRC_Code::set_generator(const bvec &poly)
{
//it_assert(poly(0) == 1 && poly(poly.size()-1) == 1, "CRC_Code::set_polynomial: not a valid polynomial");
it_assert(poly(0) == 1, "CRC_Code::set_polynomial: not a valid polynomial");
polynomial = poly;
no_parity = polynomial.size() - 1;
}
std::string crccode[18][2] = {
{"CRC-4", "1 1 1 1 1"},
{"CRC-7", "1 1 0 1 0 0 0 1"},
{"CRC-8", "1 1 1 0 1 0 1 0 1"},
{"CRC-12", "1 1 0 0 0 0 0 0 0 1 1 1 1"},
{"CRC-24", "1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1"},
{"CRC-32", "1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 0 0 1 1 1 0 0 0 1 0"},
{"CCITT-4", "1 0 0 1 1"},
{"CCITT-5", "1 1 0 1 0 1"},
{"CCITT-6", "1 0 0 0 0 1 1"},
{"CCITT-16", "1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1"},
{"CCITT-32", "1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0 1 1 0 1 1 1"},
{"WCDMA-8", "1 1 0 0 1 1 0 1 1"},
{"WCDMA-12", "1 1 0 0 0 0 0 0 0 1 1 1 1"},
{"WCDMA-16", "1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1"},
{"WCDMA-24", "1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1"},
{"ATM-8", "1 0 0 0 0 0 1 1 1"},
{"ANSI-16", "1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1"},
{"SDLC-16", "1 1 0 1 0 0 0 0 0 1 0 0 1 0 1 1 1"},
};
void CRC_Code::set_code(const std::string &code)
{
bvec poly;
for (int i = 0; i < 18;i++) {
if (crccode[i][0] == code)
poly = bvec(crccode[i][1]);
}
if ((code == "WCDMA-8") || (code == "WCDMA-12") || (code == "WCDMA-16") || (code == "WCDMA-24")) {
reverse_parity = true;
}
it_assert(poly.size() > 0, "This CRC code doesn't exist in the tables");
set_generator(poly);
}
// Not optimized for speed!
void CRC_Code::parity(const bvec &in_bits, bvec &out) const
{
bvec temp = concat(in_bits, zeros_b(no_parity));
for (int i = 0; i < temp.size() - polynomial.size() + 1; i++) {
if (temp(i) == 1) {
temp.set_subvector(i, temp(i, i + no_parity) + polynomial);
}
}
out = temp(temp.size() - no_parity, temp.size() - 1);
if (reverse_parity) {
out = reverse(out);
}
}
// Not optimized for speed
bool CRC_Code::check_parity(const bvec &coded_bits) const
{
int n = coded_bits.size();
bvec temp;
if (reverse_parity) {
temp = concat(coded_bits.left(n - no_parity), reverse(coded_bits.right(no_parity)));
}
else {
temp = coded_bits;
}
for (int i = 0; i < temp.size() - polynomial.size() + 1; i++) {
if (temp(i) == 1) {
temp.set_subvector(i, temp(i, i + no_parity) + polynomial);
}
}
if (temp(temp.size() - no_parity, temp.size() - 1) == zeros_b(no_parity))
return true;
else
return false;
}
void CRC_Code::encode(const bvec &in_bits, bvec &out) const
{
bvec p;
parity(in_bits, p);
out = concat(in_bits, p);
}
bvec CRC_Code::encode(const bvec &in_bits) const
{
bvec temp;
encode(in_bits, temp);
return temp;
}
bool CRC_Code::decode(const bvec &coded_bits, bvec &out) const
{
out = coded_bits(0, coded_bits.size() - no_parity - 1);
if (check_parity(coded_bits)) {
return true;
}
else
return false;
}
bool CRC_Code::decode(bvec &coded_bits) const
{
//coded_bits = coded_bits(0, coded_bits.size()-no_parity-1); <-- OLD CODE
if (check_parity(coded_bits)) {
return true;
}
else
return false;
}
} // namespace itpp
matfunc.h
Various functions on vectors and matrices - header file.
itpp::reverse
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition: matfunc.h:777
itpp::CRC_Code::set_code
void set_code(const std::string &code)
Set CRC code to one of the standardpolynomials using the string value.
Definition: crc.cpp:70
itpp::zeros_b
ITPP_EXPORT bvec zeros_b(int size)
A Binary vector of zeros.
itpp::CRC_Code::parity
void parity(const bvec &in_bits, bvec &out) const
Calulate the parity bits.
Definition: crc.cpp:87
crc.h
Definition of a CRC code class.
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
itpp::CRC_Code::check_parity
bool check_parity(const bvec &coded_bits) const
Return true if parity checks OK otherwise flase.
Definition: crc.cpp:106
itpp::CRC_Code::encode
void encode(const bvec &in_bits, bvec &out) const
Calculate and add parity to the in_bits.
Definition: crc.cpp:130
specmat.h
Definitions of special vectors and matrices.
itpp::CRC_Code::decode
bool decode(const bvec &coded_bits, bvec &out) const
Return true if parity checks OK otherwise flase. Also returns the message part in out...
Definition: crc.cpp:144
itpp::poly
void poly(const vec &r, vec &p)
Create a polynomial of the given rootsCreate a polynomial p with roots r.
Definition: poly.cpp:40
itpp::CRC_Code::set_generator
void set_generator(const bvec &poly)
Set an arbitary polynomial in bvec form. Start with highest order terms.
Definition: crc.cpp: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

Generated on Thu Mar 20 2014 12:26:42 for IT++ by Doxygen 1.8.6