error-correction - 单字节纠错

标签 error-correction hamming-code reed-solomon

一条 200 字节的消息有一个随机字节损坏。

修复损坏字节的最有效方法是什么?

A Hamming(255,247)代码有 8 个字节的开销,但实现起来很简单。

Reed-Solomon error correction有 2 个字节的开销,但实现起来很复杂。

有没有我忽略的更简单的方法?

最佳答案

我找到了一篇论文,介绍了一种非常适合这种情况的方法——两个字节的开销,易于实现。这是代码:

// Single-byte error correction for messages <255 bytes long
//  using two check bytes. Based on "CA-based byte error-correcting code"
//  by Chowdhury et al.
//
// rmmh 2013

uint8_t lfsr(uint8_t x) {
    return (x >> 1) ^ (-(x&1) & 0x8E);
}

void eccComputeChecks(uint8_t *data, int data_len, uint8_t *out_c0, uint8_t *out_c1) {
    uint8_t c0 = 0; // parity: m_0 ^ m_1 ^ ... ^ m_n-1
    uint8_t c1 = 0; // lfsr: f^n-1(m_0) ^ f^n(m_1) ^ ... ^ f^0(m_n-1)
    for (int i = 0; i < data_len; ++i) {
        c0 ^= data[i];
        c1 = lfsr(c1 ^ data[i]);
    }
    *out_c0 = c0;
    *out_c1 = c1;
}

void eccEncode(uint8_t *data, int data_len, uint8_t check[2]) {;
    eccComputeChecks(data, data_len, &check[0], &check[1]);
}

bool eccDecode(uint8_t *data, int data_len, uint8_t check[2]) {
    uint8_t s0, s1;
    eccComputeChecks(data, data_len, &s0, &s1);
    s0 ^= check[0];
    s1 ^= check[1];
    if (s0 && s1) {
        int error_index = data_len - 255;
        while (s1 != s0) {  // find i st s1 = lfsr^i(s0) 
            s1 = lfsr(s1);
            error_index++;
        }
        if (error_index < 0 || error_index >= data_len) {
            // multi-byte error?
            return false;
        }
        data[error_index] ^= s0;
    } else if (s0 || s1) {
        // parity error
    }
    return true;
}

关于error-correction - 单字节纠错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15996240/

相关文章:

c++ - 丢包纠错码 (UDP)

algorithm - 错误检测和纠错算法

java - 用Java编码代码

algorithm - 海明级数的产生

Python 代码在 print 语句期间/之后停止输出,但代码的同一部分在作为其自己的程序隔离时可以工作。这是怎么回事?

math - 伽罗瓦域中的加法和乘法

c++ - 使用 reed solomon 完全恢复数据

c++ - 重复流的纠错

c++ - OMNeT++ cPacket 作为 std::bitset 应用 Reed-Solomon 编码