⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stbc_um.c

📁 此文件用于仿真MIMO-STBC 系统性能程序
💻 C
字号:

#include "STBC_includ.h"

t_CONTEXT *s_p_context;
int flag ; /*since UM M=1, the step for viterbi algorthims will become 1, just one step after the beginning*/

int initialize_random_variable()
{
   int i, j;
   FILE *fp;
   
   s_p_context = BUFFER_ALLOC(sizeof(t_CONTEXT), 1, t_CONTEXT);
   if(s_p_context == FAILURE )
   {
      printf(("error! context Alloc failed \n"));
      return FAILURE;
   }
   
   switch (N_BIT)
   {
		case 6:
			s_p_context->psk_1 = 8;  /*8psk signal*/
			s_p_context->psk_2 = 8;
	   break;

		default:
			printf("\n wrong output bit\n");
   }

   if ((fp = fopen("../result/init.dat","rb")) == NULL)
   {
      printf("can't open the initialization data file\n");
      exit (0);
   }
   
   for (i = 0; i < K_BIT; i++)
      for (j = 0; j < N_BIT; j++)
         fscanf(fp,"%d",&s_p_context->gen_matrix_1[i][j]);

   for (i = 0; i < K_BIT; i++)
      for (j = 0; j < N_BIT; j++)
         fscanf(fp,"%d",&s_p_context->gen_matrix_2[i][j]);

   for (i = 0; i < N_BIT; i++)
      for (j = 0; j < N_SIG; j++)
         fscanf(fp,"%d",&s_p_context->map_matrix[i][j]);
      
   fclose(fp);

   return SUCCESS;
}

void  main()
{
   int i,j,k, frame_count, flag_frame; 
   int eb_ovr_n0_db;
   long int bit_error_count[20], frame_error_count[20];
   double es_ovr_n0, frame_error_rate[20], bit_error_rate[20];
   int binary_sequence[K_BIT*NUM_SIGS_PER_FRAME], estimate_binary_sequence[K_BIT*NUM_SIGS_PER_FRAME];
   /*STBC block has 2 symbol period and each antenna transmit one signal*/
   t_COMPLEX output_signals[2*NUM_SIGS_PER_FRAME][NUM_TX];
   t_COMPLEX receive_signals[2*NUM_SIGS_PER_FRAME];
   t_COMPLEX path_gain[NUM_TX];
   FILE *fp;

   initialize_random_variable();

   /*generate the trellis*/
   generate_trellis();

   k = 0; /* pointer to the number for saving the data*/
  
   //for (eb_ovr_n0_db = 18; eb_ovr_n0_db <= HIESN0; eb_ovr_n0_db += ESN0STEP) 
   for (eb_ovr_n0_db = LOESN0; eb_ovr_n0_db <= HIESN0; eb_ovr_n0_db += ESN0STEP) 
   {
      /*Es/N0 is calculated from receiver side, which equals to average symbol 
         engergy per symbol time/average noise energy per symbol time R= sqrt(Es/Nt)HX+N, 
         where the energy of HX is 1 and N's variance is 1, Es/Nt is the 
         average SNR per transmit antenna
      */
       es_ovr_n0 = pow (10, 0.1 * eb_ovr_n0_db) * B_4 /(NUM_TX * 1.0);
   //  es_ovr_n0 = pow (10, 0.1 * eb_ovr_n0_db) * B_8 /(NUM_TX * 1.0);
    //  es_ovr_n0 = pow (10, 0.1 * es_ovr_n0_db) * 1/ (2*1.0);
      frame_error_count[k] = 0;
      bit_error_count[k] = 0;

      for (frame_count = 0; frame_count < NUM_FRAME; frame_count++)
      {
         flag = 0; /* skip the memory part*/
         flag_frame = 0;
         /* initialize the statistics register*/
         for (i = 0; i < NUM_STATES_DEC; i++) 
         {
             /* initial accum_error_metric[x][0] = zero */        
	         s_p_context->accum_err_metric[i][0] = 0;       
	         /* by setting accum_error_metric[x][1] to MAXINT, we don't need a flag */        
	         s_p_context->accum_err_metric[i][1] = MAXINT;   

            for (j = 0; j < NUM_SIGS_PER_FRAME; j++) 
		         s_p_context->state_history[i][j] = 0;
         }


         /* previous block decides the current state*/
         memset(s_p_context->previous_block, 0, sizeof(int)*K_BIT);

         /* since channel is quasi_static, therefore generate path gain per frame 
            for 4 transmit antennas, the path gain is simulated as a Gaussian random 
            variable with zero mean and unit variance
         */
         for (i = 0; i < NUM_TX; i++)
            path_gain[i] = Cgaussian(&random_seed);

         /* initialize the memory*/
         memset(binary_sequence, 0, sizeof(int) * K_BIT * NUM_SIGS_PER_FRAME);
         memset(estimate_binary_sequence, 0, sizeof(int) * K_BIT * NUM_SIGS_PER_FRAME);
         memset(receive_signals, 0, sizeof(t_COMPLEX) * 2 * NUM_SIGS_PER_FRAME);
         memset(output_signals, 0, sizeof(t_COMPLEX) * NUM_TX * NUM_SIGS_PER_FRAME * 2);
   
         generate_binary_info_sequence(binary_sequence); 
		   
         /* transmitter part */
         unit_memory_STBC_code(binary_sequence, output_signals);
   
   
         /* pass through the channle*/
         get_receive_signal_sequence(es_ovr_n0, path_gain, output_signals, receive_signals);

         /* decode the received signals by viterbi algorithms*/
         viterbi_decode(es_ovr_n0, path_gain, receive_signals, estimate_binary_sequence);
      
         /*calculate the frame error count and bit error count*/
         for (i = 0; i < K_BIT * (NUM_SIGS_PER_FRAME - 1); i++)
         {
            if(binary_sequence[i] != estimate_binary_sequence[i])
            {
               bit_error_count[k]++;
               if (flag_frame == 0)
               {
                  frame_error_count[k]++;
                  flag_frame = 1;
                 //printf("SNR is %d, the frame count is %d, the bit is %10ld\n", eb_ovr_n0_db,frame_count, i);
               }
            }
         }
      }
      
      frame_error_rate[k] = frame_error_count[k] / (NUM_FRAME * 1.0);
      bit_error_rate[k] = bit_error_count[k] / (TOTAL_SYMBOL_NUM  * K_BIT * 1.0);
      k++;
   }
      

   if ((fp = fopen("../result/BER.dat","wb")) == NULL)
   {
      printf("can't open the initialization data file\n");
      exit (0);
   }
   
   fprintf(fp, "frame error count and frame error rate\n\n");

   for (j = 0; j < k; j++)
      fprintf(fp, " %8d  ", frame_error_count[j]);

   fprintf(fp, "\n");

   for (j = 0; j < k; j++)
      fprintf(fp, "%10.7f  ", frame_error_rate[j]);

   
   fprintf(fp, "\n\nbit error rate\n\n");

   for (j = 0; j < k; j++)
      fprintf(fp, " %8d  ", bit_error_count[j]);

   fprintf(fp, "\n");
   
   for (j = 0; j < k; j++)
      fprintf(fp, "%10.7f  ", bit_error_rate[j]);

   fclose(fp);

   BUFFER_FREE(s_p_context);
   printf("the program end\n");
 //  getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -