IT++ Logo
crc.cpp
Go to the documentation of this file.
1 
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
SourceForge Logo

Generated on Sat May 25 2013 16:32:20 for IT++ by Doxygen 1.8.2