我的任务是在2d数组中用一些校验和来编码一些声音字节,即:
b1 b2 check 1 b3 b4 check 2 ch3 ch4
其中check 1和check 2是其行的校验和(例如,检查1 == b1 ^ b2),check 3和4是其列的校验和。
当数组如上所示时,我得到了这个结果:
1) Checking encoder... o.k. -Input block size : 4 byte(s) -Output block size: 9 byte(s). -Overhead : +125.00% 2) Checking decoder... o.k. 3) Checking performance of codec: Trying all combinations of: - 1 byte(s) errors: 9 / 9 recovered o.k. - 2 byte(s) errors: 18 / 36 recovered failed - 3 byte(s) errors: 15 / 84 recovered failed
当它大4 * 4时:
1) Checking encoder... o.k. -Input block size : 16 byte(s) -Output block size: 25 byte(s). -Overhead : +56.25% 2) Checking decoder... o.k. 3) Checking performance of codec: Trying all combinations of: - 1 byte(s) errors: 10 / 25 recovered failed - 2 byte(s) errors: 21 / 300 recovered failed - 3 byte(s) errors: 27 / 2300 recovered failed
我这样做了:
ENCODER:
const int width = 2;
const int height = 2;
const int candwidth = 3;
const int candheight = 3;
guint8 checksum;
int h;
int w;
guint8 parity [candwidth][candheight];
while (bufin->size >= (width*height)){
for ( h =0; h< height; h++)
for( w = 0; w < width; w++)
{
guint8 databyte = bufin->data[0]; //Pick up a byte from input buffer
parity [w][h] = databyte;
buffer_pop (bufin, 1); //Remove it from the input buffer
}
for ( h =0; h< height; h++)
{
for( w = 0; w < width-1; w++)
{
checksum = parity[w][h]^parity[w+1][h]; // width check
}
parity[candwidth-1][h] = checksum;
}
for ( w =0; w< width; w++)
{
for( h = 0; h < height-1; h++)
{
checksum = parity[w][h]^parity[w][h+1];// height check
}
parity[w][candheight-1] = checksum;
}
for (h =0; h< candheight; h++)
for(w = 0; w < candwidth; w++)
buffer_push_byte (bufout,parity [w][h]); //Send it all
}
}
然后我发送数据并解码: 解码器: 我创建一个错误数组,并尝试检查宽度和高度的校验和 如果2列上有错误,我会发现除了错误和错误正确的字节之外的每个字节的xor
void fox_decode(Buffer* bufin, Buffer* bufout, FoxDecData* algorithm_data){
const int width = 2;
const int height = 3;
const int candwidth = 3;
const int candheight = 3;
guint8 thischecksum;
int h;
int w;
int e;
int h2;
int w2;
// error array to check for errors
int error [width][height];
for ( h =0; h< height; h++)
{
for( w = 0; w < width; w++)
{
error[w][h]=0;
}
}
guint8 parity [candwidth][candheight];
// Example:
while (bufin->size >= (candwidth*candheight)){
for ( h =0; h< candheight; h++)
for( w = 0; w < candwidth; w++)
{
guint8 databyte = bufin->data[0]; //Pick up a byte from input buffer
parity [w][h] = databyte;
buffer_pop (bufin, 1); //Remove it from the input buffer
}
for ( h =0; h< height; h++)
{
for( w = 0; w < width-1; w++)
{
thischecksum = parity[w][h]^parity[w+1][h]; // width check
}
if (parity[candwidth-1][h]!= thischecksum)
{
//printf("%i,%i\n",parity[candwidth-1][h], thischecksum);
for( e = 0; e < width; e++)
error[e][h]++;
}
}
for ( w =0; w< width; w++)
{
for( h = 0; h < height-1; h++)
{
thischecksum = parity[w][h]^parity[w][h+1]; //h check
}
if (parity[w][candheight-1]!= thischecksum)
{
// printf("%i,%i\n",parity[w][candheight-1], thischecksum);
for( e = 0; e < height; e++)
error[w][e]++;
}
}
for ( h =0; h< height; h++)
for( w = 0; w < width; w++)
if (error[w][h]>=2)
{
thischecksum=parity[candwidth-1][h];
for( w2 = 0; w2 < width; w2++)
if(w!=w2){
thischecksum = parity[w2][h]^thischecksum; // width check
}
parity[w][h]=thischecksum;
}
for (h =0; h< height; h++)
for(w = 0; w < width; w++)
buffer_push_byte (bufout,parity [w][h]); //Send it all
}
不幸的是,如果我出错了,是不是有人有任何想法? 我不希望别人为我这样做,我想学习,并希望得到一些指示:
答案 0 :(得分:3)
只检查其中一个校验和计算:
for ( h =0; h< height; h++)
{
for( w = 0; w < width-1; w++)
{
checksum = parity[w][h]^parity[w+1][h]; // width check
}
parity[candwidth-1][h] = checksum;
}
您在每个循环(checksum = ...
)上覆盖校验和,而不是将新值添加到旧循环中。其他校验和遭遇同样的问题。解决此问题后,还要确保在开始每次求和之前初始化checksum
,因此前一行或列的校验和不会被转移。还要确保更新内部循环中的索引边界(如果需要,取决于您的实现),否则您将省略总和中的最后一行或列。
至于可以纠正多少错误,此特定校验和方案可以检测但不能纠正在不同行和列中发生的多个错误。也就是说,只有当这些错误全部出现在同一行或同一列中时,此方案才能纠正多个错误。