?? rs_encode.cpp
字號:
#include "stdio.h"
#include "stdlib.h"
#define TT 32
#define N 255
#define K 223
int T[8][8]={
1,0,0,0,1,1,0,1,
1,1,1,0,1,1,1,1,
1,1,1,0,1,1,0,0,
1,0,0,0,0,1,1,0,
1,1,1,1,1,0,1,0,
1,0,0,1,1,0,0,1,
1,0,1,0,1,1,1,1,
0,1,1,1,1,0,1,1
};
int TI[8][8]={
1,1,0,0,0,1,0,1,
0,1,0,0,0,0,1,0,
0,0,1,0,1,1,1,0,
1,1,1,1,1,1,0,1,
1,1,1,1,0,0,0,0,
0,1,1,1,1,0,0,1,
1,0,1,0,1,1,0,0,
1,1,0,0,1,1,0,0
};
int u[8],z[8];
unsigned char x,B[TT],G[TT+1],H[N],F[N+1],VV;
FILE *finp, *fout;
int symbol_no, word_no;
void exit();
FILE *fwtest;
void main()
{
int i,j;
int bit;
int k,t;
finp=fopen("encinp","rb");
fout=fopen("encout","w");
fwtest=fopen("11.txt","w");
/*** H[ ] and F[ ] compute the power and log in GF(256) ********/
H[0]=1;
for(i=0;i<8;i++) H[i+1]=2*H[i];
for(i=8;i<N;i++) H[i]=H[i-1]^H[i-6]^H[i-7]^H[i-8];
for(j=1;j<N+1;j++)
for(i=0;i<N;i++) if(H[i]==j) F[j]=i;
/*
for(i=0;i<N;i++) printf("\n%4d %4d",i,H[i]);
for(i=0;i<=N;i++) printf("\n%4d %4d",i,F[i]);
*/
/////
// for(i=0;i<N;i++) fprintf(fwtest,"\n%4d %4d",i,H[i]);
for(i=0;i<=N;i++) fprintf(fwtest,"\n%4d %4d",i,F[i]);
////
symbol_no=0;
word_no=0;
VV=0;
F[0]=0;
/*** g[ ] are the coefficients of the generating polynomial ****/
G[0]=H[0];
G[1]=H[249];
G[2]=H[59];
G[3]=H[66];
G[4]=H[4];
G[5]=H[43];
G[6]=H[126];
G[7]=H[251];
G[8]=H[97];
G[9]=H[30];
G[10]=H[3];
G[11]=H[213];
G[12]=H[50];
G[13]=H[66];
G[14]=H[170];
G[15]=H[5];
G[16]=H[24];
for(i=0;i<TT/2;i++) G[TT-i]=G[i];
while(1>0)
{
if(symbol_no==0)
{
for(i=0;i<TT;i++)
B[i]=0;
word_no++;
if(word_no==2)
exit(1);
}
/******************************************************************************************/
printf("\n%4d ",symbol_no+1);
if(symbol_no<K) /* information bits */
{
x=getc(finp); /* encoder gets one symbol from input data */
// printf(" %2x ",x);
// for(bit=0;bit<8;bit++)
// printf("%1d",(x>>(7-bit))&1);
/***** transformation T^-1 from dual basis (Berlekamp) to standard basis (conventional) */
/* Input symbols x are in dual basis, x=z_0*l_0,...,z_7*l_7 */
for(k=0;k<8;k++)
z[7-k]=(x>>k)&1;
for(t=0;t<8;t++)
{
u[7-t]=0;
for(k=0;k<8;k++)
u[7-t] ^= z[k]*TI[k][t];
}
x=0;
for(t=0;t<8;t++)
x ^= u[t]<<t;
/* Output symbols x are in standard basis,x=u_7*alpha^7,...,u_0*alpha^0 */
/**** end of transformation ***************************************************************/
printf(" %2x ",x);
for(bit=0;bit<8;bit++)
printf("%1d",(x>>(7-bit))&1);
/* beginning of conventional (not Berlekamp) RS encoder.
Input symbols x are in standard basis, x=u_7*alpha^7,...,u_0*alpha^0 */
VV=x^B[TT-1];
}
else /** parity bits **/
{
VV=0;
x=B[TT-1];
}
for(i=TT-1;i>0;i--)
B[i]=B[i-1]^(VV!=0)*(H[(F[G[i]]+F[VV])%N]);
B[0]=(VV!=0)*(H[(F[G[0]]+F[VV])%N]);
/* end of conventional (not Berlekamp) RS encoder.
Output symbols x are in standard basis, x=u_7*alpha^7,...,u_0*alpha^0 */
if(symbol_no>=K)
printf(" ");
printf(" %2x ",x);
for(bit=0;bit<8;bit++)
printf("%1d",(x>>(7-bit))&1);
/***** transformation T from standard basis (conventional) to dual basis (Berlekamp) ****/
/* Input symbols x are in standard basis, x=u_7*alpha^7,...,u_0*alpha^0 */
for(t=0;t<8;t++)
u[t]=(x>>t)&1;
for(k=0;k<8;k++)
{
z[k]=0;
for(t=0;t<8;t++)
z[k] ^= u[7-t]*T[t][k];
}
x=0;
for(k=0;k<8;k++)
x ^= z[7-k]<<k;
/* Output symbols x are in dual basis, x=z_0*l_0,...,z_7*l_7 */
/**** end of transformation *************************************************************/
printf(" %2x ",x);
for(bit=0;bit<8;bit++)
printf("%1d",(x>>(7-bit))&1);
putc(x,fout);
symbol_no=(symbol_no+1)%N;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -