【问题标题】:BCH -CRC help in CC 中的 BCH -CRC 帮助
【发布时间】:2014-12-20 05:46:17
【问题描述】:

我试图让this 在 C 中工作 用 C 计算 CRC 似乎很容易,但我的 CRC/BCH 例程没有生成与第一个代码字的链接中相同的答案,即使我尝试手动计算甚至与链接中给出的内容不匹配。

我的手动计算是这样的

           11101101001    101010011010000010100
                          111011010010000000000
                          010001001000000010100
                           11101101001
                           01100100001000010100
                            11101101001
                            0010010101100010100
                              11101101001
                              01111000101010100
                               11101101001
                               0001100001110100
                                  11101101001
                                  0010111010000
                                    11101101001
                                    01010111001 ---- remainder (should match with BCH(31,21) value in the link)

我不确定我错在哪里。这是我的代码

int calc_bch_and_parity( int cw_e ) 
{ 
    int bit=0; 
    int local_cw = 0;  
    int parity = 0; 

    local_cw = cw_e;  
    for( bit=1; bit<=21; bit++, cw_e <<= 1 ) 
        if (cw_e & 0x80000000) 
            cw_e ^= 0xED200000; 

    local_cw |= (cw_e >> 21); 
    cw_e =local_cw; 
    for( bit=1; bit<=32; bit++, cw_e <<= 1 ) 
        if (cw_e & 0x80000000) 
            parity++; 

   return (parity%2) ? local_cw+1 : local_cw; 
} 

******************************编辑的代码以获得更多的过程可见性******** ******

 int bit=0;
  int local_cw = 0;
  int parity = 0;
 // int try;
  int  answer;
 // int genpoly = 0x1DA400;
  local_cw=cw;  /* bch */

  for(bit=1; bit<=21; bit++, cw <<= 1)
    {
    if (cw & 0x80000000) 
    {   
    cw ^= 0xED200000;
    printf ("mod2 remainder is %x\n", cw);

    }

    }
    printf ("original data is %x\n", local_cw);
  local_cw |= (cw >> 21);
    printf ("21 bit right shifted remainder is %x\n",cw>>21);

  cw =local_cw;  /* parity */
  for(bit=1; bit<=32; bit++, cw<<=1)
    if (cw & 0x80000000) parity++;
{

   if(parity%2) 
    {
     printf("codeword is %x \n",local_cw+1 );
    answer = local_cw+1;
    }
   else
    {
     printf("codeword is %x \n",local_cw );
     answer =local_cw;
    }   
}

    return answer;

预期的提醒是 1001111100

BCH 的另一个例子。

我尝试过的另一件事是http://www.codeproject.com/Articles/13189/POCSAG-Encoder,对不起,我必须依赖外部链接,因为我还不能发布图片,在图片的这个链接中,你会看到有一条预编码的消息可见以十六进制表示,实际消息是单词“Salam,我设法使用此预编码消息成功地将消息传输到寻呼机(将其作为十六进制整数数组馈送到发送器),但即使是这条消息,即单词” Salam”在编码消息中不可见(至少是第一个或最后一个或任何 20 个连续位)。我将整个十六进制消息转换为二进制,并将单词“Salam”转换为二进制并试图找到第一个或最后 20 位编码消息中的单词“Salam”。现在对于 CRC(基本上是 BCH)来说,这很奇怪,CRC 不假设编码/扭曲实际消息,它应该只附加余数和奇偶校验(在 BCH 的情况下) ) . 所以实际的消息应该是可见的,前 20 位或后 20 位,我什至尝试改变字节顺序但没有快乐。这里是他详细介绍了我在这个新示例中所做的工作

萨拉姆:01010011 01100001 01101100 01100001 01101101

gx: 10010110111

Salam_reversed_by_byte: 11001010 10000110 00110110 10000110 10110110

Salam_rev : 10110110 10000110 00110110 10000110 11001010

1234567 : 00110001001100100011001100110100001101010011011000110111

1234567_reverse :11101100011011001010110000101100110011000100110010001100

Salam 的前 20 位:01010011011000010110 Salam 的前 20_reverse:01101000011011001010

十六进制顺时针:0x4B5A1A25,0xE5866E6E,0x7CD215D8,0xE1DB221B,0x84081630

0x4B5A1A25 : 01001011010110100001101000100101

0xE5866E6E : 11100101100001100110111001101110

0xE1DB221B : 11100001110110110010001000011011

0x84081630 : 10000100000010000001011000110000

【问题讨论】:

  • 如果您发布代码、输入和预期输出,有人可能会提供帮助。
  • 我认为你应该用额外的零填充红利,例如10101001101000001010000000000000。但我不确定有多少个零(我猜是 11 或 12)。您可以添加一堆零,然后继续除法,直到得到正确答案:)
  • Thnaks @user3386109 ,我尝试附加 10 个额外的零以使数字 31 位长但仍然没有乐趣,事实上我认为这不应该是必要的 21 位数据(股息)和 10位余数应创建 31 位 CRC 码字。 @R Sahu,这是我的代码。
  • code int calc_bch_and_parity(int cw_e) { int bit=0; int local_cw = 0;整数奇偶校验 = 0; local_cw=cw_e; /* bch / for(bit=1; bit> 21); cw_e =local_cw; / 奇偶校验 */ for(bit=1; bitcode
  • 嗨@CHRISTOPHERCHRISTIAN 我尝试将代码从您的评论移到问题中,但我不确定我是否做对了。在标签下,有一些“分享”和“编辑”等按钮。如果我写错了,你可以使用编辑按钮来修复代码。

标签: c binary crc


【解决方案1】:

为我工作,我所做的只是将红利增加到 32 位

10101001101000001010000000000000
11101101001
01000100100000001010000000000000
 11101101001
00110010000100001010000000000000
  11101101001
00001001010110001010000000000000
    11101101001
00000111100010101010000000000000
     11101101001
00000000111000111010000000000000
        11101101001
00000000000011101000000000000000
            11101101001
00000000000000000101001000000000
                 11101101001
00000000000000000010010010010000
                  11101101001
00000000000000000001111111011000
                   11101101001
00000000000000000000001001111100

这是“手动”进行计算的代码

#include <stdio.h>
#include <string.h>

char blank[]    = "                                ";
char dividend[] = "10101001101000001010000000000000";
char divisor[]  = "11101101001";

int main( void )
{
    int i, j;

    int N = strlen( dividend );
    int M = strlen( divisor );
    printf( "%d %d\n", N, M );

    for ( i = 0; i < N - M; i++ )
    {
        if ( dividend[i] == '1' )
        {
            printf( "%s\n", dividend );
            printf( "%s%s\n", &blank[N-i], divisor );
            for ( j = 0; j < M; j++ )
            {
                if ( dividend[i+j] == divisor[j] )
                    dividend[i+j] = '0';
                else
                    dividend[i+j] = '1';
            }
        }
    }
    printf( "%s\n", dividend );
}

这也是玩弄比特的东西

#include <stdio.h>
#include <stdint.h>

int main( void )
{
    uint32_t dividend  = 0x153414;
    uint32_t generator = 0x769;
    uint32_t mask;

    dividend  <<= 11;                   // pad the dividend with 11 zeros
    generator <<= 21;                   // left justify the generator
    mask = 1 << 31;                     // start the mask at the MSB
    for ( int i = 0; i < 21; i++ )
    {
        if ( dividend & mask )          // if the dividend has 1 at the current mask position
            dividend ^= generator;      // "subtract" the generator
        generator >>= 1;                
        mask >>= 1;                     // move to the next bit
    }

    printf( "%08x\n", dividend );       // whatever's left is the remainder
}

【讨论】:

  • 感谢@user3386109,我认为最后一位(第 32 位)用于奇偶校验,因此只会在计算的最后一步中添加,但现在我明白了我的错误。非常感谢
猜你喜欢
  • 2017-12-27
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
相关资源
最近更新 更多