Esempio n. 1
0
File: main.c Progetto: ryangu/BPSK
int main()
{
    int infoBit[N],demodedBit[N];
    double modedSeq[N],receivedSeq[N];
    int count,SNRnum=11;
    double SNR[11] = {0,1,2,3,4,5,6,7,8,9,10};
    double ber[11];
    double totalbit,errorbit,errorlimit=50;
    int i;
    FILE *fp;
    
    for(count=0;count<SNRnum;count++)
    {
        errorbit=0;totalbit=0;
        while(errorbit<errorlimit)
        {
            srand((unsigned)time(NULL));
            for(i=0;i<N;i++)
            {
                infoBit[i] = rand()%2;
                //printf("%2d",infoSequ[i]);
            }
            //printf("\n");
            totalbit += N;
            
            BPSKmodeled(infoBit,modedSeq,N);
            AWGN(modedSeq,receivedSeq,SNR[count],N);
            //awgn(SNR[count],modedSeq,receivedSeq);
            BPSKdemodeled(receivedSeq,demodedBit,N);
            
            for(i=0;i<N;i++)
            {
                if(infoBit[i]!=demodedBit[i])
                    errorbit++;
            }
        }
        ber[count] = errorbit/totalbit;
    }
    
    if((fp=fopen("BERresult.txt","w")) == NULL)
        printf("can't open the file!\n");
    else
        for(i=0;i<SNRnum;i++)
        {
            fprintf(fp,"%f ",ber[i]);
            printf("%f",ber[i]);
            printf("\n");
        }
    fclose(fp);
    system("pause");
    return 0;
}
Esempio n. 2
0
int main(){

    /* Compressing the data using Huffman encoding*/
    encode();

    FRAME_LENGTH = codes.length(); // this would contain the compressed, Huffman coded bits

    /* TURBO ENCODING */
   	int			*trafficflow_source = NULL,traffic_source_length, *trafficflow_decoded = NULL, err_bit_num_traffic[8],i, j, ien,total;
	float		*coded_trafficflow_source = NULL,*trafficflow_for_decode = NULL,EbN0dB = 0,en, sgma,err_bit_rate_traffic[8], max_LLR = 0.0;

	double Eb_N0_dB[8] = {2.8,2.8,2.8,2.8,2.8,2.8,2.8,2.8};

	int codedlength = 2*FRAME_LENGTH*sizeof(float);

	clock_t start, endtime;

	trafficflow_source=(int *)malloc(FRAME_LENGTH*sizeof(int));	//systematic data length
	coded_trafficflow_source=(float *)malloc(2*FRAME_LENGTH*sizeof(float)); //systematic data + parity
	trafficflow_for_decode=(float *)malloc(2*FRAME_LENGTH*sizeof(float));//coded data at the receiving end
	trafficflow_decoded=(int *)malloc(FRAME_LENGTH*sizeof(int));//data after decoding
	traffic_source_length = FRAME_LENGTH; //source data length

    /* Initialize the parameters for Turbo encoding */
    TurboCodingInit(traffic_source_length);

    		/*====   output the simulation parameters to screen======*/
    printf("\n======================== Turbo code simulation :========================\n");
	printf("\n Some parameters are as follows: \n");
	printf("\nlog map decoder\n");
	printf("frame length : %d \n", traffic_source_length);
	printf("generators g = \n");

	for (i=0; i<turbo_g.N_num_row; i++)
	{
	  for (j=0; j<turbo_g.K_num_col; j++)
      {
		printf(" %d  ", *(turbo_g.g_matrix+i*turbo_g.K_num_col+j));
      }
      printf("\n");
    }
    //TO BE REMOVED
    ien=0;
    /* Printing the SNR */
    //for (ien=0; ien<8; ien++)  // for the Simulations
	//{
		total = 0;

		printf("\n /*---------------------------------------------*/\n");
		printf("\n Simulation %d \n", ien + 1);

		EbN0dB = (float)Eb_N0_dB[ien];

		en = (float)pow(10,(EbN0dB)/10);

		sgma = (float)(1.0/sqrt(2*rate_coding*en));

		err_bit_num_traffic[ien] = 0;

		err_bit_rate_traffic[ien] = 0.0;


		printf("\n Eb/N0 = %f \n", EbN0dB);

		if (TURBO_PUNCTURE)
     		printf(" punctured to rate 1/2\n");
		else
		    printf(" unpunctured\n");


		printf(" Number of iterations: %d \n", N_ITERATION);

		if (TURBO_PUNCTURE)
			printf( "\n punctured to rate 1/2\n");
		else
			printf( "\n unpunctured\n");

		/* Turbo Encoding commencing */
		start=clock();/*staring time*/
		cout << "Starting time : " << start <<"\n";

        traffic_source_length = FRAME_LENGTH;

		gen_source(trafficflow_source, traffic_source_length);

        cout << "Printing the generated traffic flow source \n";

        for (i=0; i< traffic_source_length ; i++){
            cout << *(trafficflow_source+i) << "  ";

        }
        cout <<"\n";

        //-----------------------------------------------------------
		/* Turbo Encoding */
		cout << "Commencing Turbo encoding \n";
		TurboCodingTraffic(trafficflow_source, coded_trafficflow_source, &traffic_source_length);

		cout<<"\n Turbo Coded data \n";
		for(i=0;i<codedlength;i++){
            cout << *(coded_trafficflow_source+i) <<"    ";
		}

		cout<<"\n \nTurbo Encoding complete \n";

		//------------------------------------------------------------
		/* Adding AWGN Noise to corrupt the data */

        AWGN(coded_trafficflow_source, trafficflow_for_decode, sgma,traffic_source_length);

        cout<<"\n Coded data after adding Noise    :  Coded data length  :  "<< codedlength << "  \n";
        for(i=0;i<codedlength;i++){
            cout << *(trafficflow_for_decode+i) <<"    ";
		}

		//-----------------------------------------------------------
        cout<<"\n \n Commencing Turbo Decode \n";
        cout <<"@@@@"<< traffic_source_length<<"\n";

        /* Turbo decoding */
        TurboDecodingTraffic(trafficflow_for_decode, trafficflow_decoded, &traffic_source_length, EbN0dB);
        //TurboDecodingTraffic(float *trafficflow_for_decode, int *trafficflow_decoded,int *trafficflow_length, float EbN0dB)


        cout<<"\n Turbo Decoding complete \n";
        //int resultSize = result.size();
        for(int p=0;p<final_result.size();p++){
            cout<<final_result[p].code << " : "<<final_result[p].c <<"\n";
        }



        /*
		for (i=0; i< traffic_source_length ; i++)
		{
			if (*(trafficflow_source+i) != *(trafficflow_decoded+i))
			{
				err_bit_num_traffic[ien] = err_bit_num_traffic[ien]+1;
				printf("LLR[%d] =  %f\n\n",*(reverse_index_randomintlvr + i), *(LLR_all_turbo + *(reverse_index_randomintlvr + i) ));
				if(fabs(*(LLR_all_turbo + *(reverse_index_randomintlvr + i) )) > max_LLR)
						max_LLR = fabs(*(LLR_all_turbo + *(reverse_index_randomintlvr + i)));
			}
			else
			{
				printf("LLR[%d] =  %f\n\n",*(reverse_index_randomintlvr + i), *(LLR_all_turbo + *(reverse_index_randomintlvr + i) ));

			}
		}


		for (i=0; i< traffic_source_length ; i++)
		{
			if (*(trafficflow_source+i) == *(trafficflow_decoded+i)&&fabs(*(LLR_all_turbo +*(reverse_index_randomintlvr + i))) < max_LLR)
			{
				total ++;
			}
		}

		float wrong = (float) total/(float) traffic_source_length;
		err_bit_rate_traffic[ien] = (float)err_bit_num_traffic[ien]/(traffic_source_length) ;
		printf("\n The Error bit probability is %f  \n",err_bit_rate_traffic[ien]);
		printf("\n The Error bit probability is %f \n",err_bit_rate_traffic[ien]);
		printf("maxmum LLR of error bit is %f and SNR is %f \n\n", max_LLR,EbN0dB);
		printf("Among correct bits, there are %f % of them whose LLR less than max_LLR\n", wrong);
		printf("===========================================================================\n\n");
		printf("===========================================================================\n\n");
        */

        decode();

        endtime=clock();
        double totalTime = double(endtime - start)/CLOCKS_PER_SEC;
	    cout<< "%Total time taken : "<<totalTime;
        cout<<"\n";

	    printf("The corresponding LLR of error bits for simulation %d :\n\n", ien + 1);
        free(LLR_all_turbo);
	//}
   return 0;
}