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
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::Array::left
Array< T > left(int n) const
Get n left elements of the array.
Definition array.h:357
itpp::Array::right
Array< T > right(int n) const
Get n right elements of the array.
Definition array.h:367
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::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::CRC_Code::encode
void encode(const bvec &in_bits, bvec &out) const
Calculate and add parity to the in_bits.
Definition crc.cpp:130
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::parity
void parity(const bvec &in_bits, bvec &out) const
Calulate the parity bits.
Definition crc.cpp:87
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
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::poly
void poly(const vec &r, vec &p)
Create a polynomial of the given roots.
Definition poly.cpp:40
itpp::reverse
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition matfunc.h:777
itpp::zeros_b
ITPP_EXPORT bvec zeros_b(int size)
A Binary vector of zeros.
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
specmat.h
Definitions of special vectors and matrices.

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