IT++
4.3.1
Toggle main menu visibility
itpp
comm
crc.cpp
Go to the documentation of this file.
1
28
29
#include <
itpp/comm/crc.h
>
30
#include <
itpp/base/specmat.h
>
31
#include <
itpp/base/matfunc.h
>
32
33
34
namespace
itpp
35
{
36
37
void
CRC_Code::set_generator
(
const
bvec &
poly
)
38
{
39
//it_assert(poly(0) == 1 && poly(poly.size()-1) == 1, "CRC_Code::set_polynomial: not a valid polynomial");
40
it_assert
(
poly
(0) == 1,
"CRC_Code::set_polynomial: not a valid polynomial"
);
41
polynomial =
poly
;
42
no_parity = polynomial.size() - 1;
43
}
44
46
47
std::string crccode[18][2] = {
48
{
"CRC-4"
,
"1 1 1 1 1"
},
49
{
"CRC-7"
,
"1 1 0 1 0 0 0 1"
},
50
{
"CRC-8"
,
"1 1 1 0 1 0 1 0 1"
},
51
{
"CRC-12"
,
"1 1 0 0 0 0 0 0 0 1 1 1 1"
},
52
{
"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"
},
53
{
"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"
},
54
{
"CCITT-4"
,
"1 0 0 1 1"
},
55
{
"CCITT-5"
,
"1 1 0 1 0 1"
},
56
{
"CCITT-6"
,
"1 0 0 0 0 1 1"
},
57
{
"CCITT-16"
,
"1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1"
},
58
{
"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"
},
59
{
"WCDMA-8"
,
"1 1 0 0 1 1 0 1 1"
},
60
{
"WCDMA-12"
,
"1 1 0 0 0 0 0 0 0 1 1 1 1"
},
61
{
"WCDMA-16"
,
"1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1"
},
62
{
"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"
},
63
{
"ATM-8"
,
"1 0 0 0 0 0 1 1 1"
},
64
{
"ANSI-16"
,
"1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1"
},
65
{
"SDLC-16"
,
"1 1 0 1 0 0 0 0 0 1 0 0 1 0 1 1 1"
},
66
};
67
69
70
void
CRC_Code::set_code
(
const
std::string &code)
71
{
72
bvec
poly
;
73
for
(
int
i = 0; i < 18;i++) {
74
if
(crccode[i][0] == code)
75
poly
= bvec(crccode[i][1]);
76
}
77
78
if
((code ==
"WCDMA-8"
) || (code ==
"WCDMA-12"
) || (code ==
"WCDMA-16"
) || (code ==
"WCDMA-24"
)) {
79
reverse_parity =
true
;
80
}
81
82
it_assert
(
poly
.size() > 0,
"This CRC code doesn't exist in the tables"
);
83
set_generator
(
poly
);
84
}
85
86
// Not optimized for speed!
87
void
CRC_Code::parity
(
const
bvec &in_bits, bvec &out)
const
88
{
89
bvec temp =
concat
(in_bits,
zeros_b
(no_parity));
90
91
for
(
int
i = 0; i < temp.size() - polynomial.size() + 1; i++) {
92
if
(temp(i) == 1) {
93
temp.set_subvector(i, temp(i, i + no_parity) + polynomial);
94
}
95
}
96
97
out = temp(temp.size() - no_parity, temp.size() - 1);
98
99
if
(reverse_parity) {
100
out =
reverse
(out);
101
}
102
103
}
104
105
// Not optimized for speed
106
bool
CRC_Code::check_parity
(
const
bvec &coded_bits)
const
107
{
108
int
n = coded_bits.size();
109
bvec temp;
110
111
if
(reverse_parity) {
112
temp =
concat
(coded_bits.left(n - no_parity),
reverse
(coded_bits.right(no_parity)));
113
}
114
else
{
115
temp = coded_bits;
116
}
117
118
for
(
int
i = 0; i < temp.size() - polynomial.size() + 1; i++) {
119
if
(temp(i) == 1) {
120
temp.set_subvector(i, temp(i, i + no_parity) + polynomial);
121
}
122
}
123
124
if
(temp(temp.size() - no_parity, temp.size() - 1) ==
zeros_b
(no_parity))
125
return
true
;
126
else
127
return
false
;
128
}
129
130
void
CRC_Code::encode
(
const
bvec &in_bits, bvec &out)
const
131
{
132
bvec p;
133
parity
(in_bits, p);
134
out =
concat
(in_bits, p);
135
}
136
137
bvec
CRC_Code::encode
(
const
bvec &in_bits)
const
138
{
139
bvec temp;
140
encode
(in_bits, temp);
141
return
temp;
142
}
143
144
bool
CRC_Code::decode
(
const
bvec &coded_bits, bvec &out)
const
145
{
146
out = coded_bits(0, coded_bits.size() - no_parity - 1);
147
if
(
check_parity
(coded_bits)) {
148
return
true
;
149
}
150
else
151
return
false
;
152
}
153
154
bool
CRC_Code::decode
(bvec &coded_bits)
const
155
{
156
//coded_bits = coded_bits(0, coded_bits.size()-no_parity-1); <-- OLD CODE
157
if
(
check_parity
(coded_bits)) {
158
return
true
;
159
}
160
else
161
return
false
;
162
}
163
164
}
// namespace itpp
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 by
1.17.0