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

📄 coarq1.cpp

📁 主要是针对现在的合作通信中
💻 CPP
字号:

//说明:  利用SRtoDselective.cpp->S和R写作向D传数据。并且SD的穿孔模式可以选择.在它的基础上,来寻找循环条件
//任务:重传时选择S or R
//问题:es_n0为负数不行

// pingtai.cpp : Defines the entry point for the console application.

//
//#include "stdafx.h"
//#include"head_punc.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include "stdafx.h"

#pragma comment(lib,"crc1.lib")
#pragma comment(lib,"dongtai_punc1.lib") 
#pragma comment(lib,"punctured.lib")
#pragma comment(lib,"xiugaihou8.lib")


extern "C" __declspec(dllexport) void crcencoder(int *str,int n,int *str2);
extern "C" __declspec(dllexport) int crcdecoder(int *str1,int n,int *str5);
extern "C"  __declspec(dllexport) void conv_encoder(int kk,int *g[2],int mesg_length,int *input_mesg,int *output_mesg);
extern "C"  __declspec(dllexport) int *deci2bin(int d,int size,int* b);
extern "C"  __declspec(dllexport) int bin2deci(int* b,int size);
extern "C"  __declspec(dllexport) int nxt_stat(int K,int current_state,int input,int* memory_contents);
extern "C"  __declspec(dllexport) float soft_metric(float data,int guess);
extern "C"  __declspec(dllexport) void cov_decode(int K,int *g[2],int channel_length,float* channel_output_vector,int* decoder_output_matrix);
extern "C"  __declspec(dllexport) float gauss_noise(float sigma);
extern "C"  __declspec(dllexport) void add_noise(int* ch_data,float* re_ch_data,float es_n0,int channel_len);
extern "C"  __declspec(dllexport) void gen_poly(int kk,int *g[2]);
extern "C"  __declspec(dllexport) int bit_err(int* data,int* re_data,int data_len);
extern "C"  __declspec(dllexport) int rnd();
extern "C"  __declspec(dllexport) void load();
extern "C"  __declspec(dllexport) void punctured_table(int MM);
extern "C"  __declspec(dllexport) void punctured(int p,int length,int m,int punc[2][5],int *input,int *rcpc_output);
extern "C"  __declspec(dllexport) void depunc(int p,int length,int m,int punc[2][5],float *input, float *output);

    
	int re_num;   //retransmitsion number
	int done;     //retransmit or not
	int temp=0;   //error number for crcdecoder
    int punc_or_not=0;
	int crc_err_or_not_hybrid;
    int crc_err_or_not_sd,crc_err_or_not_sr,crc_err_or_not_rd;
	int RENUM=5;
    time_t Tbegin,Tend;
	time_t t;
int selective(int temp)
{
    if (temp==0)//crc译码错误
	{
		re_num++;
		if(re_num>RENUM)
			return 0;  //传输出错,但已达到最大传输次数
	    else
		{
			done=0;  //穿孔标志 0--穿孔,1--不穿孔
	        punc_or_not++;
			if(punc_or_not==5)
			    done=1;    //不穿孔,以1/2码率传输
			return 1;    //传输出错,但未达到最大重传次数
		}    
	}
    else if(temp==1)
	{
		done=0;
		re_num=0;
		punc_or_not=1;
		return 0;   //传输正确
	}

}

int main(int argc, char* argv[])
{
	int i;
	int number00=0;
	int number11=0;
	int number10=0;
	int number01=0;
	int numpunc3=0;
	int numpunc2=0;
	int numpunc1=0;
	int numpunc0=0;
	float err_bit_ratio;
	float begin=0.0;
	float end=8.5;
	float step=0.25;
	int n=384;
	int K=3;
	int right_packet_num=0;

	int num_retrans;
	float ave_num;
	float R;

	int biaozhi_sd=2;
	int biaozhi_sr=2;
	int biaozhi_hybrid=2;
	int biaozhi_rd=2;
    int punc0[2][5]={1,1,0,1,1,1,1,1,1,1};
	int punc1[2][5]={1,1,0,0,1,1,1,1,1,1};
	int punc2[2][5]={0,1,0,0,1,1,1,1,1,1};
	int punc3[2][5]={0,0,0,0,1,1,1,1,1,1};
	int channel_len;
	int *data,*crc_output_hybrid,*deconv_output_hybrid,*deconv_output_sd;
	int *decrc_output_sd,*deconv_output_sr,*deconv_output_rd;
	int *conv_output_hybrid,*decrc_output_hybrid;
	int *conv_output_rd,*decrc_output_rd,*decrc_output_sr;
	int *punc_output_sd,*punc_output_rd;
	int *punc_output_sr;
	float *add_noise0_sd,*add_noise0_rd,*add_noise0_sr;
	float *depunc_output_sd,*depunc_output_rd,*depunc_output_sr;
	float  *channel_output_sd;
	float *Receive, *previous_receive;
	int *g[2];
	int mm=0;
	float es_n0_sd,es_n0_rd,es_n0_sr;
	float eb_n0;
   
    int err_num;
	int num_of_packet;
    float per=0.0;
	float through_output=0.0;
    int k=n-16;
	channel_len=(n+16+K-1)*2;
	g[0]=(int *)malloc(sizeof(int)*K);
	g[1]=(int *)malloc(sizeof(int)*K);
	data=(int *)malloc(sizeof(int)*n);
	crc_output_hybrid=(int *)malloc(sizeof(int)*(n+16));
	
	deconv_output_hybrid=(int *)malloc(sizeof(int)*(n+16));
    deconv_output_sd=(int *)malloc(sizeof(int)*(n+16));
	deconv_output_sr=(int *)malloc(sizeof(int)*(n+16));
	deconv_output_rd=(int *)malloc(sizeof(int)*(n+16));
	
	conv_output_hybrid=(int *)malloc(sizeof(int)*channel_len);
	conv_output_rd=(int *)malloc(sizeof(int)*channel_len);

	punc_output_sd=(int *)malloc(sizeof(int)*channel_len);
	punc_output_sr=(int *)malloc(sizeof(int)*channel_len);
	punc_output_rd=(int *)malloc(sizeof(int)*channel_len);


	add_noise0_sd=(float *)malloc(sizeof(float)*channel_len);
	add_noise0_rd=(float *)malloc(sizeof(float)*channel_len);
	add_noise0_sr=(float *)malloc(sizeof(float)*channel_len);

	depunc_output_sd=(float *)malloc(sizeof(float)*channel_len);
	depunc_output_rd=(float *)malloc(sizeof(float)*channel_len);
	depunc_output_sr=(float *)malloc(sizeof(float)*channel_len);

	decrc_output_sd=(int *)malloc(sizeof(int)*n);
	decrc_output_hybrid=(int *)malloc(sizeof(int)*n);
	decrc_output_rd=(int *)malloc(sizeof(int)*n);
	decrc_output_sr=(int *)malloc(sizeof(int)*n);

	Receive=(float *)malloc(sizeof(float)*channel_len);


	previous_receive = (float *)malloc(sizeof(float)*channel_len);


	printf(" 信噪比       错误比特数     传输桢数     误比特率      误帧率     吞吐量\n");
	for(eb_n0=begin;eb_n0<=end;eb_n0+=step)
	 
	{
		num_retrans=0;
		Tbegin=time(NULL);
		//initialization
		num_of_packet=1;
		err_bit_ratio=0.0;
		per=0.0;
		through_output=0.0;
		re_num=0;
		err_num=0;
		right_packet_num=0;
		punc_or_not=1;
		int biaozhi_sd=2;
		int biaozhi_sr=2;
		int biaozhi_hybrid=2;
	
		for(i=0;i<n;i++)
		    data[i]=rnd();
		done=0;
		es_n0_sr=1+(float)(10*log10((double)k/channel_len));              //修改此处的常数,即可改变eb_n0_sr
		es_n0_sd=eb_n0+(float)(10*log10((double)k/channel_len));
		es_n0_rd=es_n0_sd;
		//printf("%.6f",es_n0_sr);
		do
		{
			
		
			num_retrans++;
			if(biaozhi_sd==0||biaozhi_hybrid==0)
			{
			    for(i=0;i<n;i++)
		            data[i]=rnd();
				num_of_packet++;
				done=0;
				re_num=0;
				punc_or_not=1;
			}

			gen_poly(K,g);
			crcencoder(data,n,crc_output_hybrid);
			conv_encoder(K,g,n+16,crc_output_hybrid,conv_output_hybrid);

        
			int num_r_retra=0;
				if(done==0)
			{
				switch(punc_or_not)
				{
				case 1: punctured(5,channel_len,K-1,punc3,conv_output_hybrid,punc_output_sd);
					break;
		        case 2: punctured(5,channel_len,K-1,punc2,conv_output_hybrid,punc_output_sd);
					break;
		        case 3: punctured(5,channel_len,K-1,punc1,conv_output_hybrid,punc_output_sd);
					break;
		        case 4: punctured(5,channel_len,K-1,punc0,conv_output_hybrid,punc_output_sd);
					break;
                }
			}

            punctured(5,channel_len,K-1,punc2,conv_output_hybrid,punc_output_sr);

            if(done==1)
				add_noise(conv_output_hybrid,channel_output_sd,es_n0_sd,channel_len);
			    
			else if (done==0)
				add_noise(punc_output_sd,add_noise0_sd,es_n0_sd,channel_len);
			     
			add_noise(punc_output_sr,add_noise0_sr,es_n0_sr,channel_len);

			if(done==0)
			{
				switch(punc_or_not)
				{
					case 1: depunc(5,channel_len,K-1,punc3,add_noise0_sd,depunc_output_sd);
						break;
					case 2: depunc(5,channel_len,K-1,punc2,add_noise0_sd,depunc_output_sd);
						break;
					case 3: depunc(5,channel_len,K-1,punc1,add_noise0_sd,depunc_output_sd);
						break;
					case 4: depunc(5,channel_len,K-1,punc0,add_noise0_sd,depunc_output_sd);
						break;
				}
			}
			
			depunc(5,channel_len,K-1,punc2,add_noise0_sr,depunc_output_sr);

			if(done==1)
				cov_decode(K,g,channel_len,channel_output_sd,deconv_output_sd);
			else if(done==0)
                cov_decode(K,g,channel_len,depunc_output_sd,deconv_output_sd);
			crc_err_or_not_sd=crcdecoder(deconv_output_sd,n,decrc_output_sd);

			 cov_decode(K,g,channel_len,depunc_output_sr,deconv_output_sr);
			 crc_err_or_not_sr=crcdecoder(deconv_output_sr,n,decrc_output_sr);

			//biaozhi_sd=selective(crc_err_or_not_sd);

		  
            //中继点r和源结点s同时向d重传数据
			if(crc_err_or_not_sr==1&&crc_err_or_not_sd==0)  //crc_err_or_not_sr==1,r译码正确;crc_err_or_not_sd==0 d译码错误 
            { 
			  do
			  {    
					num_r_retra++;           //计算r and s 联合重传次数 
			      /*punctured(5,channel_len,K-1,punc2,conv_output_hybrid,punc_output_sd);  //重传时s-d穿孔
					add_noise(punc_output_sd,add_noise0_sd,es_n0_sd,channel_len);
					depunc(5,channel_len,K-1,punc2,add_noise0_sd,depunc_output_sd);*/
					
					punctured(5,channel_len,K-1,punc2,conv_output_hybrid,punc_output_rd); 
					add_noise(punc_output_rd,add_noise0_rd,es_n0_rd,channel_len);
					depunc(5,channel_len,K-1,punc2,add_noise0_rd,depunc_output_rd);
					
					for (i=0;i<channel_len;i++)                                        //在目的结点d等增益合并
					{ 
						{
							if (num_r_retra > 1)
							{
					            *(previous_receive+i)=(*(depunc_output_rd+i)+*(depunc_output_sd+i))/2;
					            *(Receive + i) = (*(Receive + i) + *(previous_receive+i))/2;
							}
						    else
							    *(Receive + i)=(*(depunc_output_rd+i)+*(depunc_output_sd+i))/2;
						
						}
					}

					 
					cov_decode(K,g,channel_len,Receive,deconv_output_hybrid);
					crc_err_or_not_hybrid=crcdecoder(deconv_output_hybrid,n,decrc_output_hybrid);
					biaozhi_hybrid=selective(crc_err_or_not_hybrid);
					
				}while(biaozhi_hybrid==1);
			temp=bit_err(data,decrc_output_hybrid,n);
			err_num+=temp;	
			if(crc_err_or_not_hybrid==1)
			printf("right  ");
			}

			else if(crc_err_or_not_sr==0&&crc_err_or_not_sd==0)
				{
				do
					{
					punctured(5,channel_len,K-1,punc2,conv_output_hybrid,punc_output_sd);
					add_noise(punc_output_sd,add_noise0_sd,es_n0_sd,channel_len);
					depunc(5,channel_len,K-1,punc2,add_noise0_sd,depunc_output_sd);
					cov_decode(K,g,channel_len,depunc_output_sd,deconv_output_sd);
					crc_err_or_not_sd=crcdecoder(deconv_output_sd,n,decrc_output_sd);
					biaozhi_sd=selective(crc_err_or_not_sd);
					}while(biaozhi_sd==1);
					temp=bit_err(data,decrc_output_sd,n);
					err_num+=temp;	
					//if(crc_err_or_not_sd==1)
						//printf(" Sright  ");

				}
			if(crc_err_or_not_hybrid==1||crc_err_or_not_sd==1)
				right_packet_num++;
	
		}while(err_num<500);
		ave_num=float(num_of_packet)/(num_retrans);
		err_bit_ratio=(float)err_num/(num_of_packet*n);
		Tend=time(NULL);
		R=(float)5/7;
		t=Tend-Tbegin;
		per=1-(float)right_packet_num/num_of_packet;
		through_output=(float)5/(float)7*(1-per)*ave_num;
		printf("\n%.6f   %d        %d    %f     %f    %f\n",eb_n0,err_num,num_of_packet,err_bit_ratio,per,through_output);
      
 }
	    free(g[0]);
		free(g[1]);
		free(data);
		free(crc_output_hybrid);
	

		free(conv_output_hybrid);
		free(conv_output_rd);
		free(conv_output_rd);
		
		free(deconv_output_hybrid);
		free(deconv_output_rd);
		free(deconv_output_sr);
		free(deconv_output_sd);

		free(decrc_output_hybrid);
		free(decrc_output_sd);
		free(decrc_output_rd);
		free(decrc_output_sr);

		free(add_noise0_sd);
		free(add_noise0_rd);
		free(add_noise0_sr);

		free(punc_output_sd);
		free(punc_output_rd);
		free(punc_output_sr);

		free(depunc_output_sd);
		free(depunc_output_rd);
		free(depunc_output_sr);
		return 0;
}

⌨️ 快捷键说明

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