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

📄 decode.c

📁 LDPC 编码 解码仿真程序 包括编码解码 矩阵生成
💻 C
字号:
/* DECODE.C - Decode blocks of received data. *//* Copyright (c) 2000, 2001 by Radford M. Neal  * * Permission is granted for anyone to copy, use, or modify this program  * for purposes of research or education, provided this copyright notice  * is retained, and note is made of any changes that have been made.  * * This program is distributed without any warranty, express or implied. * As this program was written for research purposes only, it has not been * tested to the degree that would be advisable in any important application. * All use of this program is entirely at the user's own risk. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include "rand.h"#include "alloc.h"#include "blockio.h"#include "open.h"#include "mod2sparse.h"#include "mod2dense.h"#include "mod2convert.h"#include "channel.h"#include "rcode.h"#include "check.h"#include "dec.h"void usage(void);/* MAIN PROGRAM. */int main( int argc, char **argv){  char *pchk_file, *rfile, *dfile, *pfile;  char **meth;  FILE *rf, *df, *pf;  char *dblk, *pchk;  double *lratio;  double *bitpr;  double *awn_data;		/* Places to store channel data */  int *bsc_data;  unsigned iters;		/* Unsigned because can be huge for enum */  double tot_iter;		/* Double because can be huge for enum */  double chngd, tot_changed;	/* Double because can be fraction if lratio==1*/  int tot_valid;  char junk;  int valid;  int i, j, k;  /* Look at arguments up to the decoding method specification. */  table = 0;  if (argc>1 && strcmp(argv[1],"-t")==0)  { 
	table = 1;    argc -= 1;    argv += 1;  }  if (argc>1 && strcmp(argv[1],"-T")==0)  { 
	table = 2;    argc -= 1;    argv += 1;  }  if (!(pchk_file = argv[1]) || !(rfile = argv[2]) || !(dfile = argv[3]))  { 
	  usage();  }  if (argv[4]==0 || argv[5]==0) usage();  k = channel_parse(argv+4,argc-4);//bp-file bsc 0.1 prprp -100;  if (k<=0)  {
	pfile = argv[4];    k = channel_parse(argv+5,argc-5);    if (k<=0) usage();    meth = argv+5+k;  }  else  { 
	pfile = 0;    meth = argv+4+k;  }  /* Look at the specification of the decoding method, which starts at meth and     continues to the end of the command line (marked by a zero pointer). */  if (!meth[0]) usage();  if (strcmp(meth[0],"prprp")==0)  { 
	dec_method = Prprp;    if (!meth[1] || sscanf(meth[1],"%d%c",&max_iter,&junk)!=1 || meth[2])     { 
		usage();    }  }  else if (strcmp(meth[0],"enum-block")==0)  { 
	dec_method = Enum_block;    if (!(gen_file = meth[1]) || meth[2]) usage();  }  else if (strcmp(meth[0],"enum-bit")==0)  { 
	dec_method = Enum_bit;    if (!(gen_file = meth[1]) || meth[2]) usage();  }  else   { 
	  usage();  }  /* Check that we aren't overusing standard input or output. */  if ((strcmp(pchk_file,"-")==0) + (strcmp(rfile,"-")==0) > 1)  { 
	fprintf(stderr,"Can't read more than one stream from standard input\n");    exit(1);  }  if ((table>0) + (strcmp(dfile,"-")==0) + (pfile!=0 && strcmp(pfile,"-")==0) > 1)  { 
	fprintf(stderr,"Can't send more than one stream to standard output\n");    exit(1);  }  /* Read parity check file. */  read_pchk(pchk_file);

//  struct mod2entry *e;
 // ee = mod2sparse_first_in_col(H,3);  if (N<=M)  { 
	fprintf(stderr, "Number of bits (%d) should be greater than number of checks (%d)\n",N,M);    exit(1);  }  /* Open file of received data. */  rf = open_file_std(rfile,"r");  if (rf==NULL)  { 
	fprintf(stderr,"Can't open file of received data: %s\n",rfile);    exit(1);  }  /* Create file for decoded data. */  df = open_file_std(dfile,"w");  if (df==NULL)  { 
	fprintf(stderr,"Can't create file for decoded data: %s\n",dfile);    exit(1);  }  /* Create file for bit probabilities, if specified. */  if (pfile)  { 
	pf = open_file_std(pfile,"w");    if (pf==NULL)    { 
	  fprintf(stderr,"Can't create file for bit probabilities: %s\n",pfile);      exit(1);    }  }  /* Allocate space for data from channel. */  switch (channel)  { 
    case BSC:    { 
	  bsc_data = chk_alloc (N, sizeof *bsc_data);      break;    }    case AWGN: case AWLN:    { 
	  awn_data = chk_alloc (N, sizeof *awn_data);      break;    }    default:    { 
	  abort();    }  }  /* Allocate other space. */  dblk   = chk_alloc (N, sizeof *dblk);  lratio = chk_alloc (N, sizeof *lratio);  pchk   = chk_alloc (M, sizeof *pchk);  bitpr  = chk_alloc (N, sizeof *bitpr);  /* Print header for summary table. */  if (table==1)  { 
	  printf("  block iterations valid  changed\n");  }  /* Do the setup for the decoding method. */  switch (dec_method)  { 
    case Prprp:     { 
	  prprp_decode_setup();      break;    }  }  /* Read received blocks, decode, and write decoded blocks. */  tot_iter = 0;  tot_valid = 0;  tot_changed = 0;  for (block_no = 0; ; block_no++)  { 		/* Read block from received file, exit if endo-f-file encountered. */
	    //下面的for循环,读一个块出来,从1到N,长度为N;		for (i = 0; i<N; i++)		{ 
			  int c;			  switch (channel)			  { 
					case AWGN: case AWLN:					{
					  c = fscanf(rf,"%lf",&awn_data[i]); 					  break;					}			  }			  if (c==EOF) 				  			  { 
				int iyy=8888;
				if (i>0)				{ 
					fprintf(stderr, "Warning: Short block (%d long) at end of received file ignored\n",i);				}				goto done;			  }			  if (c<1 || channel==BSC && bsc_data[i]!=0 && bsc_data[i]!=1)			  { 
				fprintf(stderr,"File of received data is garbled\n");				exit(1);			  }		}//for (i = 0; i<N; i++);		/* Find likelihood ratio for each bit. */
		//下面这个switch解决了lratio的数值;一个block,从1到N;		switch (channel)		{ 
			  case AWGN:			  { 
				for (i = 0; i<N; i++)				{ 
					lratio[i] = exp(2*awn_data[i]/(std_dev*std_dev));				}				break;			  }		}		/* Try to decode using the specified method. */		switch (dec_method)		{ 
			  case Prprp:			  { 
				iters = prprp_decode (H, lratio, dblk, pchk, bitpr);//lratio=1/9 when 0;or lratio=9when 1;				//上面的函数后3个参数好像都是输出参量啊,这3个参数在这儿给的都是地址,即指针形式,没有内容;
				break;			  }		}		/* See if it worked, and how many bits were changed. */		valid = check(H,dblk,pchk)==0;		chngd = changed(lratio,dblk,N);
	//	printf("%s",dblk);
	//	printf("%s",pchk);
	//	printf("%s",bitpr);		tot_iter += iters;
		tot_valid += valid;		tot_changed += chngd;		/* Print summary table entry. */		if (table==1)		{ 
		  printf ("%7d %10f  %d  %8.1f\n", block_no, (double)iters, valid, (double)chngd);///和上面的table==1互相对应;			/* iters is printed as a double to avoid problems if it's >= 2^31 */		  fflush(stdout);		}		/* Write decoded block. */		blockio_write(df,dblk,N);		/* Write bit probabilities, if asked to. */		if (pfile)		{ 
		  for (j = 0; j<N; j++)		  { 
			  fprintf(pf," %.5f",bitpr[j]);		  }		  fprintf(pf,"\n");		}  }////for (block_no = 0; ; block_no++)	  /* Finish up. */   done: 	  fprintf(stderr, "Decoded %d blocks, %d valid.  Average %.1f iterations, %.0f%% bit changes\n", block_no, tot_valid, (double)tot_iter/block_no, 100.0*(double)tot_changed/(N*block_no));	  if (ferror(df) || fclose(df)!=0)	  { 
		fprintf(stderr,"Error writing decoded blocks to %s\n",dfile);		exit(1);	  }	  if (pfile)	  { 
		if (ferror(pf) || fclose(pf)!=0)		{ 
		  fprintf(stderr,"Error writing bit probabilities to %s\n",dfile);		  exit(1);		}	  }   exit(0);}//main( int argc, char **argv)/* PRINT USAGE MESSAGE AND EXIT. *///}void usage(void){ 
  fprintf(stderr,"Usage:\n");  fprintf(stderr,"  decode [ -t ] pchk-file received-file decoded-file [ bp-file ] channel method\n");  channel_usage();  fprintf(stderr,"Method:  enum-block gen-file | enum-bit gen-file | prprp [-]max-iterations\n");  exit(1);}

⌨️ 快捷键说明

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