簡體   English   中英

漢明碼校驗奇偶校驗

[英]Hamming code check parity

我不確定我是否為我編寫的check Parity bit函數正確計算了奇偶校驗位。 codeWord的長度為11個字符,具有4個奇偶校驗位和7個數據位。 實現看起來不錯嗎?

void parityCheck(char* codeWord) {
int parity[4] = {0}, i = 0, diffParity[4] = {0}, twoPower = 0, bitSum = 0;

// Stores # of 1's for each parity bit in array.
parity[0] = (codeWord[2] - 48) + (codeWord[4] - 48) + (codeWord[6] - 48) + (codeWord[8] - 48) + (codeWord[10] - 48);
parity[1] = (codeWord[2] - 48) + (codeWord[5] - 48) + (codeWord[6] - 48) + (codeWord[9] - 48) + (codeWord[10] - 48);
parity[2] = (codeWord[4] - 48) + (codeWord[5] - 48) + (codeWord[6] - 48);
parity[3] = (codeWord[8] - 48) + (codeWord[9] - 48) + (codeWord[10] - 48);

// Determines if sum of bits is even or odd, then tests for difference from actual parity bit.
for (i = 0; i < 4; i++) {
    twoPower = (int)pow((double)2, i);

    if (parity[i] % 2 == 0)
            parity[i] = 0;
        else
            parity[i] = 1;

        if ((codeWord[twoPower-1] - 48) != parity[i])
            diffParity[i] = 1;
}

// Calculates the location of the error bit.
for (i = 0; i < 4; i++) {
    twoPower = (int)pow((double)2, i);
    bitSum += diffParity[i]*twoPower;
}



// Inverts bit at location of error.
if (bitSum <= 11 && bitSum > 0) {
    if ((codeWord[bitSum-1] - 48)) 
        codeWord[bitSum-1] = '0';
    else
        codeWord[bitSum-1] = '1';
}

實現看起來不錯嗎?

這在很大程度上取決於您對“良好”的衡量。 我可以確認它確實可以完成工作,因此至少是正確的。 您的代碼非常冗長,因此很難檢查其正確性。 我將執行以下操作:

int parity_check(int codeWord) {
  int parity = 0, codeWordBit, bitPos;
  for (bitPos = 1; bitPos <= 11; ++bitPos) {
    codeWordBit = ((codeWord >> (bitPos - 1)) & 1);
    parity ^= bitPos*codeWordBit;
  }
  if (parity != 0) {
    if (parity > 11)
      return -1; // multi-bit error!
    codeWord ^= 1 << (parity - 1);
  }
  return codeWord;
}

我將整個代碼字視為一個整數,而不是數字字符序列,這樣效率更高得多。

查閱Wikipedia的表 ,我看到該表的構成了序列1…11的二進制表示。每個代碼字位正好影響該列中提到的那些奇偶校驗位,因此我將代碼字位(為零)或一個),將其乘以該列的位模式以獲得該模式或零,然后將其與當前奇偶校驗位模式進行異或。 這樣的結果是零代碼字位不會改變任何東西,而非零代碼字位會翻轉所有關聯的奇偶校驗位。

由於位模式是從1開始的,而使用右移技巧的位位置是從0開始的,因此必須小心。 因此,我必須減去一個,然后向右移該數量,然后提取最低有效數字,以獲得codeWordBit

使用我的實現作為參考,我能夠(通過完整的枚舉)驗證您的代碼是否工作相同。

您的代碼通過了我構想的測試用例后,在AFAIK上運行良好。 進行了一些簡化,但OP功能未更改。 進行了一些經典的簡化,以便於查看。

void parityCheck(char* cW) {
  int parity[4] = { 0 }, i = 0, diffParity[4] = { 0 }, twoPower = 0, bitSum = 0;

  // Stores # of 1's for each parity bit in array.
  parity[0] = (cW[2] - '0') + (cW[4] - '0') + (cW[6] - '0') + (cW[8] - '0') + (cW[10] - '0');
  parity[1] = (cW[2] - '0') + (cW[5] - '0') + (cW[6] - '0') + (cW[9] - '0') + (cW[10] - '0');
  parity[2] = (cW[4] - '0') + (cW[5] - '0') + (cW[6] - '0');
  parity[3] = (cW[8] - '0') + (cW[9] - '0') + (cW[10] - '0');

  // Determines if sum of bits is even or odd, then tests for difference from actual parity bit.
  for (i = 0; i < 4; i++) {
    //twoPower = (int) pow((double) 2, i);
    twoPower = 1 << i;
    //if (parity[i] % 2 == 0) parity[i] = 0; else parity[i] = 1;
    parity[i] &= 1;  // Make 0 even, 1 odd.
    if ((cW[twoPower - 1]-'0') != parity[i])
      diffParity[i] = 1;
  }

  // Calculates the location of the error bit.
  for (i = 0; i < 4; i++) {
    // twoPower = (int) pow((double) 2, i);
    twoPower = 1 << i;
    bitSum += diffParity[i] * twoPower;
  }

  // Inverts bit at location of error.
  if (bitSum <= 11 && bitSum > 0) {
    if ((cW[bitSum - 1]-'0')) 
      cW[bitSum - 1] = '0';
    else
      cW[bitSum - 1] = '1';
  }
}

void TestP(const char * Test) {
  char buf[100];
  strcpy(buf, Test);
  parityCheck(buf);
  printf("'%s' '%s'\n", Test, buf);
}


int main(void) {
  TestP("00000000000");
  TestP("10011100101");
  TestP("10100111001");
}

如果OP發布了測試模式,這將很有用。

這是我的實現。 有用。 公眾可以免費免費使用它。

我在“單錯誤糾正,雙錯誤檢測”中使用了縮寫“ secded”。 如果需要,可以將其重新連接為“三重錯誤檢測器”。 確實,其中的一小部分已被拆分,其余的是漢明7,4-但是我將這些方法命名為我所做的工作。

這里的“字符串”不是以NUL結尾的,而是計算在內。 這段代碼摘自用C語言編寫的Python模塊。這就是您看到的字符串類型的來源。

這里的關鍵是要意識到只有16個漢明7,4碼。 我用一些Python代碼計算了secded_of_nibble(),不幸的是我不再擁有了。

static const unsigned char secded_of_nibble[] = 
{ 0x0, 0xd2, 0x55, 0x87, 0x99, 0x4b, 0xcc, 0x1e, 0xe1, 0x33, 0xb4, 0x66, 0x78, 0
xaa, 0x2d, 0xff };

int fec_secded_encode_cch_bits(const char * strIn, const int cchIn, char * strOu
t, const int cchOut)
{
    assert( cchIn * 2 == cchOut);
    if( cchIn * 2 != cchOut)
        return 0;

    if (!strIn || !strOut)
        return 0;

    int i;
    for (i = 0; i < cchIn; i ++)
    {
        char in_byte = strIn[i];
        char hi_byte = secded_of_nibble[(in_byte >> 4) & 0xf];
        char lo_byte = secded_of_nibble[in_byte & 0xf];

        strOut[i * 2] = hi_byte;
        strOut[i * 2 + 1] = lo_byte;
    }

    return 1;
}

char bv_H[] = {0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x8};

char val_nibble(char ch)
{
    return ((ch & 0x20) >> 2) | ((ch & 0xE) >> 1);
}

char correct_nibble(char ch)
{
    char nibble = 0;
    int i = 0;
    for (i = 0; i < 8; i++)
    if (ch & (1 << (7-i)))
        nibble ^= bv_H[i];

    return nibble;
}

void apply_correct(char nib_correct, char * pbyte, int * pcSec, int *pcDed)
{
    if (0 == nib_correct)
        return;

    if (nib_correct & 0x8)
    {
        (*pcSec) ++;

        int bit = (8 - (nib_correct & 0x7)) & 0x7;
        /*  fprintf(stderr, "bit %d, %02X\n", bit, 1 << bit);*/
        (*pbyte) ^= (1 << bit);
    }
    else
    {
        (*pcDed) ++;
    }
}

int fec_secded_decode_cch_bits
(
    const char * strIn, 
    const int cchIn, 
    char * strOut, 
    const int cchOut,
    int * pcSec,
    int * pcDed
)
{
    assert( cchIn == cchOut *2);
    if( cchIn != cchOut * 2)
        return 0;

    if (!strIn || !strOut)
        return 0;

    int i;
    for (i = 0; i < cchOut; i ++)
    {
        char hi_byte = strIn[i * 2];
        char lo_byte = strIn[i * 2 + 1];


        char hi_correct = correct_nibble(hi_byte);
        char lo_correct = correct_nibble(lo_byte);

        if (hi_correct || lo_correct)
        {
            apply_correct(hi_correct, &hi_byte, pcSec, pcDed);
            apply_correct(lo_correct, &lo_byte, pcSec, pcDed);
/*          fprintf(stderr, "Corrections %x %x.\n", hi_correct, lo_correct);*/
        }

        char hi_nibble = val_nibble(hi_byte);
        char lo_nibble = val_nibble(lo_byte);

        strOut[i] = (hi_nibble << 4) | lo_nibble;
    }

    return 1;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM