www.pudn.com > viterbi_decoder.rar > vittest.c


/* Test a rate 1/2 soft decision viterbi decoder */

#include 
#include 
#include 

#define	RATE	0.5

void modnoise(unsigned char *,unsigned int,double,double);

/* Lookup table giving count of 1 bits for integers 0-255 */
unsigned char Bitcnt[] = {
 0, 1, 1, 2, 1, 2, 2, 3,
 1, 2, 2, 3, 2, 3, 3, 4,
 1, 2, 2, 3, 2, 3, 3, 4,
 2, 3, 3, 4, 3, 4, 4, 5,
 1, 2, 2, 3, 2, 3, 3, 4,
 2, 3, 3, 4, 3, 4, 4, 5,
 2, 3, 3, 4, 3, 4, 4, 5,
 3, 4, 4, 5, 4, 5, 5, 6,
 1, 2, 2, 3, 2, 3, 3, 4,
 2, 3, 3, 4, 3, 4, 4, 5,
 2, 3, 3, 4, 3, 4, 4, 5,
 3, 4, 4, 5, 4, 5, 5, 6,
 2, 3, 3, 4, 3, 4, 4, 5,
 3, 4, 4, 5, 4, 5, 5, 6,
 3, 4, 4, 5, 4, 5, 5, 6,
 4, 5, 5, 6, 5, 6, 6, 7,
 1, 2, 2, 3, 2, 3, 3, 4,
 2, 3, 3, 4, 3, 4, 4, 5,
 2, 3, 3, 4, 3, 4, 4, 5,
 3, 4, 4, 5, 4, 5, 5, 6,
 2, 3, 3, 4, 3, 4, 4, 5,
 3, 4, 4, 5, 4, 5, 5, 6,
 3, 4, 4, 5, 4, 5, 5, 6,
 4, 5, 5, 6, 5, 6, 6, 7,
 2, 3, 3, 4, 3, 4, 4, 5,
 3, 4, 4, 5, 4, 5, 5, 6,
 3, 4, 4, 5, 4, 5, 5, 6,
 4, 5, 5, 6, 5, 6, 6, 7,
 3, 4, 4, 5, 4, 5, 5, 6,
 4, 5, 5, 6, 5, 6, 6, 7,
 4, 5, 5, 6, 5, 6, 6, 7,
 5, 6, 6, 7, 6, 7, 7, 8,
};

main(argc,argv)
int argc;
char *argv[];
{
	double ebn0,esn0,noise;
	int mettab[2][256];
	int amp,nbits,i;
	unsigned char *symbols;
	unsigned char *data,*decdata;
	unsigned long metric;
	long t,ntrials;
	long seed;
	extern char *optarg;
	unsigned long biterrs = 0;
	unsigned long framerrs = 0;
	int timetrial;

	amp = 100;
	ebn0 = 5.0;
	nbits = 1152;
	ntrials = 10;
	timetrial = 0;
	time(&seed);
	while((i = getopt(argc,argv,"a:e:n:N:qs:t")) != EOF){
		switch(i){
		case 'a':
			amp = atoi(optarg);	/* Signal amplitude in units */
			break;
		case 'e':
			ebn0 = atof(optarg);	/* Eb/N0 in dB */
			break;
		case 'n':
			nbits = atoi(optarg);	/* Number of data bits */
			break;
		case 'N':
			ntrials = atoi(optarg);
			break;
		case 's':
			seed = atoi(optarg);
			break;
		case 't':	/* Generate noise once, for decoder timing */
			timetrial = 1;
			break;
		case '?':
			usage();
			exit(1);
		}
	}
	srandom(seed);

	esn0 = RATE * pow(10.,ebn0/10);	/* Es/N0 as power ratio */	

	noise = 0.5/esn0;	/* only half the noise for BPSK */
	noise = sqrt(noise);	/* noise/signal Voltage ratio */
	
	data = malloc(nbits/8);
	decdata = malloc(nbits/8);
	symbols = malloc(nbits*2);

	/* Generate metrics analytically, with gaussian pdf */	
	gen_met(mettab,amp,esn0,0.,4);

	/* Generate data */
	memset(data,0,nbits/8);	/* All 0's */
	data[nbits/8 - 1] = 0;	/* Tail of 0's*/


	i = 0;
	if(timetrial){
		encode(symbols,data,nbits/8);
		modnoise(symbols,nbits*2,amp,noise);
		for(t = 1;t <= ntrials;t++)
			viterbi(&metric,decdata,symbols,nbits,mettab);
		printf("bits decoded = %ld\n",nbits*ntrials);
		exit(0);
	}
	for(t = 1;t <= ntrials;t++){
		/* Modulate and add noise */
		encode(symbols,data,nbits/8);
		modnoise(symbols,nbits*2,amp,noise);
		viterbi(&metric,decdata,symbols,nbits,mettab);
		if(memcmp(data,decdata,nbits/8) != 0){
			printf("decoded data:\n");
			for(i=0;i