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

📄 tcm_8psk.c

📁 The Viterbi algorithm is the same as the binary case with one main difference: The survivor sequence
💻 C
📖 第 1 页 / 共 2 页
字号:
// ------------------------------------------------------------------------
//        File: tcm_8psk.c
//        Date: April 2, 2002.
// Description: TCM decoder, maximum likelihood decoding. 
// ------------------------------------------------------------------------
// This program is complementary material for the book:
//
// R.H. Morelos-Zaragoza, The Art of Error Correcting Coding, Wiley, 2002.
//
// ISBN 0471 49581 6
//
// This and other programs are available at http://the-art-of-ecc.com
//
// You may use this program for academic and personal purposes only. 
// If this program is used to perform simulations whose results are 
// published in a journal or book, please refer to the book above.
//
// The use of this program in a commercial product requires explicitely 
// written permission from the author. The author is not responsible or 
// liable for damage or loss that may be caused by the use of this program. 
//
// Copyright (c) 2002. Robert H. Morelos-Zaragoza. All rights reserved.
// ------------------------------------------------------------------------
 
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <limits.h>

// #include "viterbi_conventional.h"

#define MAX_RANDOM LONG_MAX 

// Use the compiling directive -DSHOW_PROGRESS to see how the decoder
// converges to the decoded sequence by monitoring the survivor memory
#ifdef SHOW_PROGRESS
#define DELTA 1
#endif

int k2=1, n2, m2;
int NUM_STATES, OUT_SYM, NUM_TRANS;
long TRUNC_LENGTH;

double RATE;
double INIT_SNR, FINAL_SNR, SNR_INC;
long NUMSIM;

char name1[40], name2[40];
FILE  *fp;                 /* Pointer for trellis data file */

//  unsigned int g2[n2][k2] =  { /* rate-1/2 memory=6 */
//          0x4f, /* REVERSE ORDER */
//          0x6d }; /* */

unsigned int g2[10][10];
unsigned int memory2, output;            /* Memory and output */
unsigned int data2;                      /* Data */

unsigned long seed;                      /* Seed for random generator */
unsigned int data_symbol[1024];  /* 1-bit data sequence */
unsigned int data_symbol2[1024];  /* 1-bit data sequence */
unsigned long indxx;                     /* Simulation index */

double psk_I[8], psk_Q[8];

int transmitted;                  /* index of transmitted signal */
double transmitted_I;             /* Transmitted signals/branch */
double transmitted_Q;             /* Transmitted signals/branch */
int estimate_data2;

double snr, amp;

double received_I;                       /* Received signals/branch */
double received_Q;                       /* Received signals/branch */

// Data structures used for trellis sections and survivors
struct trel {
  int init;                /* initial state */
  int data;                /* data symbol */
  int final;               /* final state */
  int output;  /* output coded symbols (branch label) */
}; 
struct surv {
  double metric;           /* metric */
  int data[1024];  /* estimated data symbols */
  int data2[1024];  /* estimated data symbols */
  int state[1024];  /* state sequence */
};

// A trellis section is an array of branches, indexed by an initial
//   state and a k_2-bit input data. The values read
//   are the final state and the output symbols 
struct trel trellis[1024][100];

/* A survivor is a sequence of states and estimated data, of length
   equal to TRUNC_LENGTH, together with its corresponding metric.
   A total of NUM_STATES survivors are needed */
struct surv survivor[1024], surv_temp[1024];

/* Function prototypes */
void encoder2(void);           /* Encoder for C_{O2} */
int random_data(void);         /* Random data generator */
void transmit(void);           /* Encoder & BPSK modulator */
void awgn(void);               /* Add AWGN */
void viterbi(void);            /* Viterbi decoder */
double comp_metric(double rec_I, double rec_Q, int ref); /* Metric calc */
double comp_correl(double rec_I, double rec_Q, int ref); /* Metric calc */
void open_files(void);         /* Open files for output */
void close_files(void);        /* Close files */






main(int argc, char *argv[])
{

  register int i, j, k;
  int init, data, final, output;
  register int error;
  unsigned long error_count, error_coded, error_uncoded;
  FILE  *fp_ber;             /* Pointer for overall BER data file */
  double RATE;

  // Command line processing
  if (argc != 8)
    {
      printf("Usage %s file_input file_output truncation snr_init snr_final snr_
inc num_sim\n", argv[0]);
      exit(0);
    }

  sscanf(argv[1],"%s", name1);
  sscanf(argv[2],"%s", name2);
  sscanf(argv[3],"%ld", &TRUNC_LENGTH);
  sscanf(argv[4],"%lf", &INIT_SNR);
  sscanf(argv[5],"%lf", &FINAL_SNR);
  sscanf(argv[6],"%lf", &SNR_INC);
  sscanf(argv[7],"%ld", &NUMSIM);

  printf("\nSimulation of TCM decoding with 8-PSK modulation over an AWGN channel\n");
  printf("%ld simulations per Eb/No (dB) point\n", NUMSIM);

  fp_ber = fopen(name2,"w");

  /* Open file with trellis data */
  if (!(fp = fopen(name1,"r")))
    {
    printf("Error opening file!\n");
    exit(1);
    }


  fscanf(fp, "%d %d", &n2, &m2);

  RATE = (2.0 / (double) n2)*3.0;  // 2 bits per symbol if encoder is rate 1/2

  fscanf(fp, "%d %d %d", &NUM_STATES, &OUT_SYM, &NUM_TRANS);
  for (j=0; j<n2; j++)
    fscanf(fp, "%x", &g2[j][0]);

  printf("\n%d-state rate-1/%d binary convolutional encoder\n",
          NUM_STATES, n2);
  printf("with generator polynomials ");
  for (j=0; j<n2; j++) printf("%x ", g2[j][0]); printf("\n");
  printf("\nDecoding depth = %ld\n\n", TRUNC_LENGTH);


  /* =================== READ TRELLIS STRUCTURE ==================== */

  for (j=0; j<NUM_STATES; j++) /* For each state in the section */
	for (k=0; k<NUM_TRANS; k++) /* and for each outgoing branch */
	  {
	    /* Read initial state, input data and final state */
	    fscanf(fp,"%d %d %d", &trellis[j][k].init, &trellis[j][k].data,
		   &trellis[j][k].final);
	    /* Read the output symbols of the branch */
		fscanf(fp,"%d", &data);
		trellis[j][k].output = data;
	  } /* end for states */
    /* end for branches */
      
  fclose(fp);
      
#ifdef PRI
  for (j=0; j<NUM_STATES; j++)   /* For each state in the section */
	for (k=0; k<NUM_TRANS; k++)  /* and for each outgoing branch */
	  {
	    printf("%3d %3d %3d  ", trellis[j][k].init,
		   trellis[j][k].data, trellis[j][k].final);
	    printf("%d", trellis[j][k].output);
	    printf("\n");
	  } /* end for states */
      /* end for branches */
#endif

  snr = INIT_SNR;

  /* Bits-to-8PSK signal mapping as used in Japanese Satellite ISDB

                                |
                        [3] 011 #
                                |
                   [2] 010 $    |    * 001 [1]
                                |
                [4] 100         |         000 [0]
                   -----+---------------+-----
                                |
                                |
                   [5] 101 *    |    $ 110 [6]
                                |
                                # 111 [7]
                                |

   */

  /* Coordinates of the 8-PSK symbols */

  psk_I[0] = 1.0;              psk_Q[0] = 0.0;
  psk_I[1] = cos(M_PI/4.0);    psk_Q[1] = sin(M_PI/4.0);
  psk_I[3] = 0.0;              psk_Q[3] = 1.0;
  psk_I[2] = -cos(M_PI/4.0);   psk_Q[2] = sin(M_PI/4.0);
  psk_I[4] = -1.0;             psk_Q[4] = 0.0;
  psk_I[5] = -cos(M_PI/4.0);   psk_Q[5] = -sin(M_PI/4.0);
  psk_I[7] = 0.0;              psk_Q[7] = -1.0;
  psk_I[6] = cos(M_PI/4.0);    psk_Q[6] = -sin(M_PI/4.0);

/***** Try this:
  psk_I[0] = cos(M_PI/8.0);        psk_Q[0] = sin(M_PI/8.0);
  psk_I[1] = cos(3.0*M_PI/8.0);    psk_Q[1] = sin(3.0*M_PI/8.0);
  psk_I[3] = -cos(3.0*M_PI/8.0);   psk_Q[3] = sin(3.0*M_PI/8.0);
  psk_I[2] = -cos(M_PI/8.0);       psk_Q[2] = sin(M_PI/8.0);
  psk_I[4] = -cos(M_PI/8.0);       psk_Q[4] = -sin(M_PI/8.0);
  psk_I[5] = -cos(3.0*M_PI/8.0);   psk_Q[5] = -sin(3.0*M_PI/8.0);
  psk_I[7] = cos(3.0*M_PI/8.0);    psk_Q[7] = -sin(3.0*M_PI/8.0);
  psk_I[6] = cos(M_PI/8.0);        psk_Q[6] = -sin(M_PI/8.0);
******/

  /* ======================== SNR LOOP ============================= */

  while ( snr < (FINAL_SNR+0.001) )

    {

#ifdef PRINT
  printf("Viterbi decoding of a rate-1/n convolutional code\n");
#endif

      /* Random seed from current time */
      time(&seed);
      srandom(seed);

      amp = sqrt(2.0*RATE*pow(10.0,(snr/10.0)));

      /* Initialize transmitted data sequence */
      
      for (i=0; i<TRUNC_LENGTH; i++)
        {
          data_symbol[i]=0;
          data_symbol2[i]=0;
        }
      
      /* Initialize survivor sequences and metrics */
      
      for (i=0; i<NUM_STATES; i++)
	{
	  survivor[i].metric = 0.0;             /* Metric = 0 */

	  for (j=0; j<TRUNC_LENGTH; j++)
	    {
	      survivor[i].data[j] = 0;        /* Estimated data = 0 */
	      survivor[i].data2[j] = 0;       /* Estimated (uncoded) data = 0 */
	      survivor[i].state[j] = 0;       /* Estimated state = 0 */
	    }
	}
      
      /* Index used in simulation loop */

      indxx = 0;
      
      /* Initialize encoder memories */

      memory2 = 0;

      /* Error counters */

      error_count = error_coded = error_uncoded = 0;


      /* ===================== SIMULATION LOOP ========================= */
      
      while (indxx < NUMSIM) 

        { 
	
          /* GENERATE random two-bit symbols */
	  
      i = random_data();
	  data_symbol[indxx % TRUNC_LENGTH] = (i & 1); /* */
	  data_symbol2[indxx % TRUNC_LENGTH] = ( (i>>1) & 1 ); /* */
	  
#ifdef PRINT
	  printf("Transmitted data sequence:\n");
	  printf("%2d %x   ",(indxx % TRUNC_LENGTH),
		 data_symbol[indxx % TRUNC_LENGTH]);
	  for (i=0; i<TRUNC_LENGTH; i++)
	    printf("%x", data_symbol[i]);
	  printf("\n");
#endif
	  
	  /* ENCODE AND MODULATE (BPSK) data bit */
	  
	  transmit();
	  
#ifdef PRI
	  printf("Transmitted = ");
	  printf("%d ", transmitted);
	  printf("\n");
#endif
	  
	
	  /* ADD ADDITIVE WHITE GAUSSIAN NOISE */
	  
	  awgn(); /* */
	  

	  /* VITERBI DECODE */
	  
	  viterbi();
	  
	  indxx += 1;           /* Increase simulation index */
	  
	  /* COMPUTE ERRORS */

⌨️ 快捷键说明

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