我使用C ++中非常基本的CRC子例程创建了CCSDS BCH(64,56)编码器。我打算在GNU Radio应用程序中使用它。 BCH(64,56)码块的格式如下所示。 。 可以将一组代码块组合起来,以形成一个数据单元,称为数据链路传输单元(CLTU),如下所示。
据我了解,BCH和CRC都使用相同的计算形式在数据末尾附加了“余数/奇偶校验”,如this线程中所述。
我要做的就是修改标准的CRC C ++ crcFast() subroutine 。该子例程通过迭代由给定多项式(crcInit())预初始化的数组(表)来计算CRC。下面的代码显示了两个子例程crcInit()和crcFast()。
typedef unsigned char uint8_t;
typedef unsigned short crc;
#define WIDTH (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))
crc crcTable[256];
#define POLYNOMIAL 0xD8 /* 11011 followed by 0's */
void
crcInit(void)
{
crc remainder;
/*
* Compute the remainder of each possible dividend.
*/
for (int dividend = 0; dividend < 256; ++dividend)
{
/*
* Start with the dividend followed by zeros.
*/
remainder = dividend << (WIDTH - 8);
/*
* Perform modulo-2 division, a bit at a time.
*/
for (uint8_t bit = 8; bit > 0; --bit)
{
/*
* Try to divide the current data bit.
*/
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
/*
* Store the result into the table.
*/
crcTable[dividend] = remainder;
}
} /* crcInit() */
crc
crcFast(uint8_t const message[], int nBytes)
{
uint8_t data;
crc remainder = 0;
/*
* Divide the message by the polynomial, a byte at a time.
*/
for (int byte = 0; byte < nBytes; ++byte)
{
data = message[byte] ^ (remainder >> (WIDTH - 8));
remainder = crcTable[data] ^ (remainder << 8);
}
/*
* The final remainder is the CRC.
*/
return (remainder);
} /* crcFast() */
修改后的代码如下所示。表生成函数crcInit()不变。对crcFast算法进行了稍微修改,以将格式所指定的奇偶校验字节(补码和填充位)的更改并入其中。 CRC类型已从short更改为unsigned char(1个字节)。排除汉明码中的BCH(64,56),其生成多项式为g(x)= x ^ 7 + x ^ 6 + x ^ 2 +1,在我看来相当于0xC5。
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <iostream>
#include "debug.h"
typedef unsigned char uint8_t;
typedef unsigned char crc;
#define WIDTH (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))
crc crcTable[256];
#define POLYNOMIAL 0xC5 // x^7 + x^6 + x^2 + 1
#define INITIAL_REMAINDER 0x00
#define BCH_INFORMATION_BLOCK 7
void
crcInit(void)
{
crc remainder;
/*
* Compute the remainder of each possible dividend.
*/
for (int dividend = 0; dividend < 256; ++dividend)
{
/*
* Start with the dividend followed by zeros.
*/
remainder = dividend << (WIDTH - 8);
/*
* Perform modulo-2 division, a bit at a time.
*/
for (uint8_t bit = 8; bit > 0; --bit)
{
/*
* Try to divide the current data bit.
*/
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
/*
* Store the result into the table.
*/
crcTable[dividend] = remainder;
//std::cout << "Remainder from table : " << int (remainder&0xffff) << std::endl;
}
} /* crcInit() */
void
crcEncoder(std::vector<unsigned char> &message, const crc initial_remainder)
{
uint8_t data;
crc remainder = initial_remainder;
/*
* Divide the message by the polynomial, a byte at a time.
*/
for (int byte = 0; byte < message.size(); ++byte)
{
data = message.at(byte) ^ (remainder >> (WIDTH - 8));
remainder = crcTable[data] ^ (remainder << 8);
}
//Flip the remainder and move by 1 bit
remainder ^= 0xFF;
remainder <<= 1;
//Set filler bit to 0 (anding with 1111 1110)
remainder &= 0xFE;
/*
* The final remainder is the CRC.
*/
message.push_back(remainder);
//return message;
}
void bchEncoder(std::vector<unsigned char> &message)
{
std::vector<unsigned char> information; // 7 bytes
std::vector<unsigned char> codewords; // Encoded message
//Ensure integral information symbols
while(!(message.size() % BCH_INFORMATION_BLOCK) == 0)
{
message.push_back(0x55);
}
for(int i = 0; i < message.size(); i += BCH_INFORMATION_BLOCK)
{
//Copy 7 information bytes
std::copy(message.begin() + i, message.begin() + i + BCH_INFORMATION_BLOCK,
std::back_inserter(information));
//BCH encoding
crcEncoder(information,INITIAL_REMAINDER);
//Copy encoded information bits
codewords.insert(codewords.end(), information.begin(), information.end());
//Clear information bytes
information.clear();
}
message = codewords;
}
int main()
{
crcInit();
//hexdump(crcTable,256);
unsigned char message[] = {0xaa, 0xbb, 0xcd, 0xdd, 0xee, 0xff, 0x11,0x00};
//unsigned char tail[] = {0xC5,0xC5,0xC5,0xC5,0xC5,0xC5,0xC5,0x79};
std::vector<unsigned char> info(message, message + sizeof(message)/sizeof(unsigned char));
bchEncoder(info);
hexdump(info.data(),info.size());
//Vector hex dump
return 0;
}
我某种程度上觉得我的方法太幼稚了。我想知道它是否准确。
此致
答案 0 :(得分:0)
如果BCH码的距离为3,仅用于纠正单个位错误或检测(但不纠正)所有两个位错误,则BCH多项式将与字段多项式相同。如果需要更高级别的校正或检测,则BCH多项式会变得复杂。 Wiki文章对此进行了解释:
https://en.wikipedia.org/wiki/BCH_code
由于消息长度(包括填充位)为64位,因此使用的是7位字段(最多127位有效),但是所示的表生成是针对多项式0x1C5的。要解决此问题,请将POLYNOMIAL更改为0x8A,即((0xC5 << 1)&0xFE))。这将导致7位奇偶校验最终以字节的高7位结尾。
编码循环的内部应该是:
Application.Wait Now + TimeValue("00:00:05")
SendMessage h, WM_CLOSE, 0, 0