www.pudn.com > Golay-new.rar > withECC.cpp


 
#include  
#include  
#include  
#define znew (z=36969*(z&65535)+(z>>16)) 
#define wnew (w=18000*(w&65535)+(w>>16)) 
#define MWC ((znew<<16)+wnew ) 
#define SHR3 (jsr^=(jsr<<17), jsr^=(jsr>>13), jsr^=(jsr<<5)) 
#define CONG (jcong=69069*jcong+1234567) 
#define KISS ((MWC^CONG)+SHR3) 
#define UNI (KISS*2.328306e-10) 
typedef unsigned long UL; 
/* Global static variables: */ 
static UL z=362436069, w=521288629, jsr=123456789, jcong=380116160; 
 
//Generate Gaussian distributed random variates 
double G(double U,double S2) 
{ 
	double U1,U2,V1,V2,W,Y,X1,X2; 
	do{ 
		U1=UNI;	U2=UNI; 
		V1=2*U1-1; V2=2*U2-1; 
		W=V1*V1+V2*V2; 
	}while(W>1); 
	Y=sqrt((-2*log(W)/W)); 
	X1=V1*Y; X2=V2*Y; 
	X1=X1*sqrt(S2)+U;X2=X2*sqrt(S2)+U; 
	return X1; 
} 
 
/* Function to compute the Hamming weight of a 12-bit integer */ 
int weight(int vector) 
 { 
   int i, aux; 
   aux = 0; 
   for (i=0; i<12; i++) 
     if ( (vector>>i)&1 ) 
       aux++; 
   return(aux); 
 } 
 
 
void main() 
 { 
   /* Array x contains the twelve rows (columns) of the identity matrix */ 
   int x[12] = { 0x800, 0x400, 0x200, 0x100, 0x080, 0x040, 
		 0x020, 0x010, 0x008, 0x004, 0x002, 0x001 }; 
 
   /* Array y contains the twelve rows (columns) of the parity-check matrix */ 
   int y[12] = { 0x7ff, 0xee2, 0xdc5, 0xb8b, 0xf16, 0xe2d, 
		 0xc5b, 0x8b7, 0x96e, 0xadc, 0xdb8, 0xb71 };    /*????????????*/ 
 
   int c[2];          /* Codeword composed of 12-bit info and 12-bit parity */ 
   int r[2];          /* Received vector in two halfs of 12 bits each */ 
   int e[2];          /* Estimated error vector */ 
 
   int s;             /* syndrome */ 
   int q;             /* modified syndrome */ 
   int c_hat[2];      /* Estimated codeword */ 
 
   int i,j; 
   int aux, found; 
   int weight(int vector);  
 
   double rate=12.0/24.0; 
 
   int ebno,loop; 
   int errbit; //errbit:with ECC 
   double ber,attn,sigma;//ber:with ECC 
 
   int code[24],decode[24]; 
   double mocode[24];//mocode:with ECC 
 
   //long seed; 
 
   cout<<"Caculating BER over AWGN channel (with ECC Golay24)"<>j & 0x01); 
       c[1] = (c[1] << 1) ^ aux; 
     } 
   //printf("c =%3x%3x\n", c[0], c[1]); 
 
   /* --------------------- BPSK modulation ------------------------- */ 
   //e[0] = KISS&0x030; 
   //e[1] = KISS&0x101; 
   //printf("e =%3x%3x, w(e) = %d\n", e[0], e[1], weight(e[0])+weight(e[1])); 
    
   for (i=0;i<24;i++) 
   { 
	   code[i]=0; 
	   if(i<=11) 
	   { 
		   code[i]=(c[0]>>i)&0x001; 
	   } 
	   else 
	   { 
		   code[i]=(c[1]>>(i-12)&0x001); 
	   } 
	    
	   mocode[i]=2*code[i]-1; 
   } 
 
   /*--------------------- AWGN channel --------------------*/ 
   for(i=0;i<24;i++) 
   { 
	   mocode[i]=mocode[i]+G(0,1)*sigma; 
   } 
 
 
   /* --------------------- BPSK demodulation ------------------------- */ 
   for(i=0;i<24;i++) 
   { 
	   if(mocode[i]>=0) 
	   { 
		   decode[i]=1; 
	   } 
	   else 
	   { 
		   decode[i]=0; 
	   } 
   } 
 
 
 
   /* ---------------------- Received word --------------------------- */ 
   //r[0] = c[0]^e[0]; 
   //r[1] = c[1]^e[1]; 
 
   r[0]=r[1]=0;    
 
   for(i=0;i<=11;i++) 
   { 
	   r[0]+=pow(2,i)*decode[i]; 
	   r[1]+=pow(2,i)*decode[i+12]; 
   } 
 
    
   /******* STEP 1: Compute the syndrome of the received vector ********/ 
   s = 0; 
   for (j=0; j<12; j++) 
     { 
       aux = 0; 
       for (i=0; i<12; i++) 
	 aux = aux ^ ( (x[j]&r[0])>>i &0x01 ); /* identity part */    /*????????????*/ 
       for (i=0; i<12; i++) 
	 aux = aux ^ ( (y[j]&r[1])>>i &0x01 ); /* parity part */ 
       s = (s<<1)^aux; 
     } 
   //printf("r =%3x%3x, s = %x, w(s) = %d\n", r[0], r[1], s, weight(s)); 
 
   /******* STEP 2 */ 
   //printf("Step 2\n"); 
   if (weight(s)<=3) 
     { 
       e[0] = s; 
       e[1] = 0; 
     } 
   else 
     { 
       /******* STEP 3 */ 
       //printf("Step 3\n"); 
       i = 0; 
       found = 0; 
       do { 
	 if (weight(s^y[i]) <=2)    /*????????????*/ 
	   { 
	     e[0] = s^y[i]; 
	     e[1] = x[i]; 
	     found = 1; 
	   } 
	 i++; 
       } while ( (i<12) && (!found) ); 
 
       if (( i==12 ) && (!found)) 
	 { 
	   /******* STEP 4 */ 
	   //printf("Step 4\n"); 
	   q = 0; 
	   for (j=0; j<12; j++) 
	     { 
	       aux = 0; 
	       for (i=0; i<12; i++) 
		 aux = aux ^ ( (y[j]&s)>>i & 0x01 );  
	       q = (q<<1) ^ aux; 
	     } 
 
	   /******* STEP 5 */ 
	   //printf("Step 5\n"); 
	   if (weight(q) <=3) 
	     { 
	       e[0] = 0; 
	       e[1] = q; 
	     } 
	   else 
	     { 
	       /******* STEP 6 */ 
	       //printf("Step 6\n"); 
	       i = 0; 
	       found = 0; 
	       do { 
		 if (weight(q^y[i]) <=2) 
		   { 
		     e[0] = x[i]; 
		     e[1] = q^y[i]; 
		     found = 1; 
		   } 
		 i++; 
	       } while ( (i<12) && (!found) ); 
	        
	       if ((i==12) && (!found)) 
		 { 
		   /******* STEP 7 */ 
		   //printf("uncorrectable error pattern\n"); 
		   /* You can raise a flag here, or output the vector as is */ 
		   //exit(1); 
		 } 
	     } 
	 } 
     } 
 
   /* ------------------- Correct received word --------------------- */ 
   c_hat[0] = r[0]^e[0]; 
   c_hat[1] = r[1]^e[1]; 
   //printf("Estimated codeword =%x%x\n", c_hat[0], c_hat[1]); 
    
   errbit+=weight(c[0]^c_hat[0]); 
   loop++; 
 
}//while end 
 
 
/*--------------------Caculate ber and output------------------------------*/ 
 
ber=errbit/(12.0*loop); 
 
cout<<"ebno="<