Esempio n. 1
0
void BCH::initialize(int _m, int _length, int _t)
{
	int i;

	m = _m;
	length = _length;
	t = _t;

	p.resize(BCH_MAX_P);
	alpha_to.resize(BCH_MAX_LUT);
	index_of.resize(BCH_MAX_LUT);
	g.resize(BCH_MAX_LUT);

	_elp.resize(BCH_MAX_LUT);
	for(i=0; i<BCH_MAX_LUT; i++) _elp[i].resize(BCH_MAX_LUT);
	_d.resize(BCH_MAX_LUT);
	_l.resize(BCH_MAX_LUT);
	_u_lu.resize(BCH_MAX_LUT);
	_s.resize(BCH_MAX_LUT);
	_root.resize(BCH_MAX_LUT);
	_loc.resize(BCH_MAX_LUT);
	_reg.resize(BCH_MAX_LUT);


	for (i=1; i<m; i++)
		p[i] = 0;
	p[0] = p[m] = 1;
	if (m == 2)			p[1] = 1;
	else if (m == 3)	p[1] = 1;
	else if (m == 4)	p[1] = 1;
	else if (m == 5)	p[2] = 1;
	else if (m == 6)	p[1] = 1;
	else if (m == 7)	p[1] = 1;
	else if (m == 8)	p[4] = p[5] = p[6] = 1;
	else if (m == 9)	p[4] = 1;
	else if (m == 10)	p[3] = 1;
	else if (m == 11)	p[2] = 1;
	else if (m == 12)	p[3] = p[4] = p[7] = 1;
	else if (m == 13)	p[1] = p[3] = p[4] = 1;
	else if (m == 14)	p[1] = p[11] = p[12] = 1;
	else if (m == 15)	p[1] = 1;
	else if (m == 16)	p[2] = p[3] = p[5] = 1;
	else if (m == 17)	p[3] = 1;
	else if (m == 18)	p[7] = 1;
	else if (m == 19)	p[1] = p[5] = p[6] = 1;
	else if (m == 20)	p[3] = 1;

    n = 1;
	for (i = 0; i <= m; i++) 
	{
        n *= 2;
    }
	n = n / 2 - 1;

	generate_gf();          /* Construct the Galois Field GF(2**m) */
	gen_poly(t);            /* Compute the generator polynomial of BCH code */
}
 main()
{
	int             i;

	generate_gf();			/* generate the Galois Field GF(2**m) 算伽罗伐域各个元素 */
	gen_poly();				/* Compute the generator polynomial of BCH code 计算生成多项式 */




	for (i = 0; i < REDUNDANCYSIZE+1; i++) {
		printf("%d, ", g[i]);
		if (i && ((i % 64) == 0))
			printf("\n");
	}
	printf("\n");
	getchar();
	system("pause");
}
main()
{
    int             i;

    read_p();               /* Read m */
    generate_gf();          /* Construct the Galois Field GF(2**m) */
    gen_poly();             /* Compute the generator polynomial of BCH code */

    /* Randomly generate DATA */
    seed = 131073;
    srandom(seed);
    for (i = 0; i < k; i++)
        data[i] = ( random() & 65536 ) >> 16;

    encode_bch();           /* encode data */

    /*
     * recd[] are the coefficients of c(x) = x**(length-k)*data(x) + b(x)
     */
    for (i = 0; i < length - k; i++)
        recd[i] = bb[i];
    for (i = 0; i < k; i++)
        recd[i + length - k] = data[i];
    printf("Code polynomial:\nc(x) = ");
    for (i = 0; i < length; i++) {
        printf("%1d", recd[i]);
        if (i && ((i % 50) == 0))
            printf("\n");
    }
    printf("\n");

    printf("Enter the number of errors:\n");
    scanf("%d", &numerr);	/* CHANNEL errors */
    printf("Enter error locations (integers between");
    printf(" 0 and %d): ", length-1);
    /*
     * recd[] are the coefficients of r(x) = c(x) + e(x)
     */
    for (i = 0; i < numerr; i++)
        scanf("%d", &errpos[i]);
    if (numerr)
        for (i = 0; i < numerr; i++)
            recd[errpos[i]] ^= 1;
    printf("r(x) = ");
    for (i = 0; i < length; i++) {
        printf("%1d", recd[i]);
        if (i && ((i % 50) == 0))
            printf("\n");
    }
    printf("\n");

    decode_bch();             /* DECODE received codeword recv[] */

    /*
     * print out original and decoded data
     */
    printf("Results:\n");
    printf("original data  = ");
    for (i = 0; i < k; i++) {
        printf("%1d", data[i]);
        if (i && ((i % 50) == 0))
            printf("\n");
    }
    printf("\nrecovered data = ");
    for (i = length - k; i < length; i++) {
        printf("%1d", recd[i]);
        if ((i-length+k) && (((i-length+k) % 50) == 0))
            printf("\n");
    }
    printf("\n");

    /*
     * DECODING ERRORS? we compare only the data portion
     */
    for (i = length - k; i < length; i++)
        if (data[i - length + k] != recd[i])
            decerror++;
    if (decerror)
        printf("There were %d decoding errors in message positions\n", decerror);
    else
        printf("Succesful decoding\n");
}