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

📄 uvlc.cpp

📁 DPCM编码的联合信源信道译码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	for(j=0;j<256;j++)
		putc(sys_UVLC.decoded[j],ft);
		

	

 	free(sys_UVLC.remain);
	
}


/***********************************************************************/
int  SearchTable(int *in_block, int search_size)
{
	
	// search entire table, to find a symbol which codeword is equal to first
	//search_size elements of in_block. found is equal to found symbol or to 
	//-1 if such the symbol is not found 
	
	int local_found=0;
    int cur_symbol=-1;
//	int cur_symbol=0;
	int tempc;
	int found;
	while ((cur_symbol<sys_UVLC.alphabet_size-1)&(local_found==0)) 
	{
		cur_symbol++;
		if (sys_UVLC.code[cur_symbol][0]==search_size)
		{
			
			local_found=1;
			for ( tempc=0; tempc<search_size; tempc++)
				if (in_block[tempc]!=sys_UVLC.code[cur_symbol][tempc+1]) local_found=0;
		}
	}
	
	if (local_found==1) found=cur_symbol;
	else             found=-1;
	return found;   
}


void UpdateBit()
{
	int i,j,k=0;
	int bpl;
    
/*	for(j=0;j<sys_spt.message_bits;j++)
	{
		sys_spt.source_bits_in[k]=sys_spt.source_bits[j];
		k++;
	}
*/

	for(bpl=0,k=0,i=0;i<256;i++)
	{
		for(j=0;j<sys_UVLC.code[sys_UVLC.decoded[i]][0];j++)
		{
			sys_UVLC.decoded_out[k++]=sys_UVLC.code[sys_UVLC.decoded[i]][j+1];
		}
		bpl+=sys_UVLC.code[sys_UVLC.decoded[i]][0];
	}
	
	for(i=0;i<bpl;i++)
	{
		sys_spt.source_bits_in[i]=sys_UVLC.decoded_out[i];
	}
//	for(i=bpl;i<sys_spt.message_bits;i++)
	{
//		sys_spt.source_bits_in[i]=0;
	} 

//	LdpcEncoder2();

/*	for(i=0;i<sys_spt.col_num;i++)
	{
		if(sys_spt.message_locations[i]==1)
		{
			if(sys_spt.source_bits_in[i]!=sys_spt.source_bits_out[i])
				for(j=0;j<sys_spt.bit_nodes.size[i];j++)
				{   
//					printf("%4.2f ",sys_spt.bit_nodes.llr[i][j]);
//					if(sys_spt.bit_nodes.llr[i][j]>0) sys_spt.bit_nodes.llr[i][j]-=6.0;
//					else sys_spt.bit_nodes.llr[i][j]+=6.0;
					sys_spt.bit_nodes.llr[i][j]=-sys_spt.bit_nodes.llr[i][j];
				}
//				printf("\n");
		}
	}
*/
/*  	for(i=0;i<sys_spt.col_num;i++)
	{
      	if(sys_spt.message_locations[i]==1)
		{
			if(sys_spt.source_bits_in[i]!=sys_spt.source_bits_out[i])
			{
			  if(sys_spt.llr_init[i]>0)
				  sys_spt.llr_init[i]-=0.1; 
			  else
                  sys_spt.llr_init[i]+=0.1;
			}
		}
		 
	}*/
	//bit convert 
/*	for (i=0;i<sys_spt.col_num;i++)
	{
		if(sys_spt.bit_nodes.llr_in[i]!=sys_spt.bit_nodes.llr_out[i])
		{
		
//				if(sys_spt.llr_init[i]>0) sys_spt.llr_init[i]-=sys_spt.llr_init[i]*THRESHOLD;
//				else sys_spt.llr_init[i]-=sys_spt.llr_init[i]*THRESHOLD;
				
		}
		else
		{
                sys_spt.llr_init[i]+=sys_spt.llr_init[i]*(THRESHOLD);
		}
	}

*/

/*	for (i=0;i<sys_spt.col_num;i++)
	{
		if(sys_spt.bit_nodes.llr_in[i]!=sys_spt.bit_nodes.llr_out[i])
		{
			for (j=0;j<sys_spt.bit_nodes.size[i];j++)
			{
				if(sys_spt.bit_nodes.llr[i][j]>0) sys_spt.bit_nodes.llr[i][j]-=0.1;
				else sys_spt.bit_nodes.llr[i][j]+=0.1;
//				sys_spt.bit_nodes.llr[i][j] = -sys_spt.bit_nodes.llr[i][j];
			}		
		}
	}
*/
}

void NCEDecoder()
{
	int i,j,ncesym,dn_1,N=4;
	int dpcmsym;
	FILE *ft,*fp;

	ft=fopen(NCE_IMG_FILE,"rb");
//	ft=fopen("dh2.bin","rb");
	fp=fopen(DPCM_IMG_FILE,"wb");
	
	for(i=0;i<256;i++)
	{
		dn_1=0;		
		for(j=0;j<256;j++)
		{
			ncesym=fgetc(ft);
			dpcmsym=ncesym-(N*dn_1);
			if(dpcmsym<0)
				while(dpcmsym<0)	dpcmsym+=4;
			else	dpcmsym=dpcmsym%4;

			putc(dpcmsym,fp);
			dn_1=dpcmsym;
		}
	}
	fclose(ft);
	fclose(fp);
}

void DPCMDecoder()
{
	
	FILE *fi,*fp,*fo;

	double rec[4]={-1.8340,-0.4196,0.4196,1.8340};
	double s,snr,n;
	int i,j,xt,**image,**d; 
	unsigned char xt_char;
	
	image=imatrix(0,255,0,255);  
	d=imatrix(0,255,0,255);  
	n=0.;s=0.; 
	

	fi=NULL;
//	fi=fopen("hat.img","rb");
	fi=fopen(ORI_IMG_FILE,"rb");
	if(fi==NULL)
		printf("error1");

	fp = NULL;
//	fp=fopen("dh1.bin","rb");
	fp=fopen(DPCM_IMG_FILE,"rb");
	if(fp==NULL)
		printf("error2");

	fo=fopen(GEN_IMG_FILE,"wb");
	
	for(i=0;i<256;i++)
		for(j=0;j<256;j++)
		{
			image[i][j]=fgetc(fi)%256;
			d[i][j]=fgetc(fp)%256;
		//	s+=pow(image[i][j],2);
			s+=pow(255,2);
		}
	fclose(fp);
	fclose(fi);

	for(i=0;i<4;i++)	rec[i]=(double)rec[i]*VAR;
	xt=image[0][0]/COEF;
		
	for(i=0;i<256;i++)
	{
		xt=image[i][0]/COEF;
		for(j=0;j<256;j++)
		{
			xt=rec[d[i][j]]+(COEF*xt)+MEAN;	
			if (xt>255) xt=255;
			if (xt<0) xt=0;
			xt_char=unsigned char (xt);
			fputc(xt_char,fo);	
			n+=pow((image[i][j]-xt),2);
		}
	}
		
	fclose(fo);
	snr=10*(log10(s)-log10(n));
	printf("\nsnr=%f\n",snr);
	fprintf(sys_spt.fp_result,"snr=%f\n",snr);

	//  printf("%f\t",96.329598-10*log10(n));
	free_imatrix(image,0,255,0,255);
	free_imatrix(d,0,255,0,255);
}

void Term_UVLC()
{
			int i,j,k;

	free_matrix(sys_UVLC.hist1,0,sys_UVLC.alphabet_size-1,0,sys_UVLC.alphabet_size-1);

	free_imatrix(sys_UVLC.used_bits_old,0,sys_UVLC.alphabet_size-1,0,sys_UVLC.BESTC-1);

	free_imatrix(sys_UVLC.used_bits_new,0,sys_UVLC.alphabet_size-1,0,sys_UVLC.BESTC-1);

	free_dmatrix(sys_UVLC.cost_t,0,sys_UVLC.alphabet_size-1,0,sys_UVLC.BESTC-1);

	free_dmatrix(sys_UVLC.cost_o,0,sys_UVLC.alphabet_size-1,0,sys_UVLC.BESTC-1);

	free_dmatrix(sys_UVLC.cost_n,0,sys_UVLC.alphabet_size-1,0,sys_UVLC.BESTC-1);

	free_imatrix(sys_UVLC.code,0,sys_UVLC.alphabet_size-1,0,sys_UVLC.maxin);

	free_ivector(sys_UVLC.bpl,0,256-1);

	free_ivector(sys_UVLC.word,0,sys_UVLC.maxin-1);

	free_ivector(sys_UVLC.received,0,sys_spt.message_bits-1);

	free_ivector(sys_UVLC.decoded_out,0,sys_spt.message_bits*2-1);

	free_ivector(sys_UVLC.decoded,0,256*2-1);

	free_ivector(sys_UVLC.encoded,0,256*2-1);

	free_ivector(sys_UVLC.dpcmsym,0,256-1);

	for(i=0;i<sys_UVLC.alphabet_size;i++)
		for(j=0;j<256;j++)
			for(k=0;k<sys_UVLC.BESTC;k++) 
			{
				if(sys_UVLC.history[i][j][k]!=NULL)
					free(sys_UVLC.history[i][j][k]);
			}
			

	for(i=0;i<sys_UVLC.alphabet_size;i++)
		for(j=0;j<256;j++)
		{
			if(sys_UVLC.history[i][j]!=NULL)
				free(sys_UVLC.history[i][j]);
		}

	for(i=0;i<sys_UVLC.alphabet_size;i++)
    {
		if(sys_UVLC.history[i]!=NULL)
			free(sys_UVLC.history[i]);
    }


	if(sys_UVLC.history!=NULL)
		free(sys_UVLC.history);

}
  
float *vector(int nl,int nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
	float *v;

	v=(float *)malloc((unsigned) (nh-nl+1)*sizeof(float))-nl;
//	if (!v) nrerror("allocation failure in vector()");
	return v;
}

int *ivector(int nl,int nh)
/* allocate an int vector with subscript range v[nl..nh] */
{
	int *v;

	v=(int *)malloc((unsigned) (nh-nl+1)*sizeof(int))-nl;
	return v;
}

double *dvector(int nl,int nh)
/* allocate a double vector with subscript range v[nl..nh] */
{
	double *v;

	v=(double *)malloc((unsigned) (nh-nl+1)*sizeof(double))-nl;
//	if (!v) nrerror("allocation failure in dvector()");
	return v;
}

float **matrix(int nrl,int nrh,int ncl,int nch)
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
{
	int i;
	float **m;

	/* allocate pointers to rows */
	m=(float **) malloc((unsigned) (nrh-nrl+1)*sizeof(float*))-nrl;
//	if (!m) nrerror("allocation failure 1 in matrix()");

	/* allocate rows and set pointers to them */
	for(i=nrl;i<=nrh;i++) {
		m[i]=(float *) malloc((unsigned) (nch-ncl+1)*sizeof(float))-ncl;
//		if (!m[i]) nrerror("allocation failure 2 in matrix()");
	}
	/* return pointer to array of pointers to rows */
	return m;
}

double **dmatrix(int nrl,int nrh,int ncl,int nch)
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
{
	int i;
	double **m;

	/* allocate pointers to rows */
	m=(double **) malloc((unsigned) (nrh-nrl+1)*sizeof(double*))-nrl;

	/* allocate rows and set pointers to them */
	for(i=nrl;i<=nrh;i++) {
		m[i]=(double *) malloc((unsigned) (nch-ncl+1)*sizeof(double))-ncl;
	}
	/* return pointer to array of pointers to rows */
	return m;
}

int **imatrix(int nrl,int nrh,int ncl,int nch)
/* allocate an int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
	int i,**m;

	/* allocate pointers to rows */
	m=(int **)malloc((unsigned) (nrh-nrl+1)*sizeof(int*))-nrl;

	/* allocate rows and set pointers to them */
	for(i=nrl;i<=nrh;i++) {
		m[i]=(int *)malloc((unsigned) (nch-ncl+1)*sizeof(int))-ncl;
	}
	/* return pointer to array of pointers to rows */
	return m;
}

void free_vector(float *v,int nl,int nh)
/* free a float vector allocated with vector() */
{
	free((char*) (v+nl));
}

void free_ivector(int *v,int nl,int nh)
/* free an int vector allocated with ivector() */
{
	free((char*) (v+nl));
}

void free_dvector(double *v,int nl,int nh)
/* free a double vector allocated with dvector() */
{
	free((char*) (v+nl));
}

void free_matrix(float **m,int nrl,int nrh,int ncl,int nch)
/* free a float matrix allocated by matrix() */
{
	int i;

	for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
	free((char*) (m+nrl));
}

void free_dmatrix(double **m,int nrl,int nrh,int ncl,int nch)
/* free a double matrix allocated by dmatrix() */
{
	int i;

	for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
	free((char*) (m+nrl));
}

void free_imatrix(int **m,int nrl,int nrh,int ncl,int nch)
/* free an int matrix allocated by imatrix() */
{
	int i;

	for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
	free((char*) (m+nrl));
}

int twotothe(int num)
 {
 int result=1;
 int i;

 for (i=0; i<num;i++)
 result*=2;

 return result;
 }

⌨️ 快捷键说明

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