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

📄 main.c

📁 这是lva算法
💻 C
字号:
#include "stdio.h"
#include "head.h"
#include "math.h"
#include "malloc.h"
#include "stdlib.h"
#include "time.h"



float gngausscos(float);
float gngausssin(float);
void main()
{
//------xin dao
FILE *fp;
float snrdb=-1,sgma=0,snr=0;
float s00[2]={1,0},s01[2]={0,1},s11[2]={-1,0},s10[2]={0,-1},add[2]={0,0},r[2]={0,0},c[4]={0};
float numerror=0;
double errorate[9]={0};
//long in_length=100000,out_length=300018;

//---------------------------------------------------
long j,m,h=0,g,step,l,ll=0,tempt,last_stop,b=0,p,x=0;
long q,a=0,i,a1=0;
//int nextstate[number_of_states][2];
//int channel_input_zero[3000]={0};
int channel_input[in_length+6]={0};
int channel_output[out_length]={0};
int channel_qpskoutput[out_length]={0};
//int a[][];
int next_state=0,memory_contents=0,branch_output=0,branch_metric=0;
//int nextstate[number_of_states*2]={0},input[number_of_states*number_of_states]={0},output[number_of_states*2]={0};
//----------------------------------------------------------------
//int state_metric[number_of_states*2]={0};
int flag[number_of_states]={0};
//----------------------------------------------------------------
int state_temp=0,statetemp=0,branch_output_1,branch_output_2,branch_output_3;
long depth_of_trellis,y=0;
int *channel_output_matrix,*input,*out_put,*survivor_state,*state_metric;
int *nextstate;
int *state_sequence,*decodeder_output_matrix;
//------------------------------------------------------------------kai pi
 depth_of_trellis=out_length/n;
 (int *)channel_output_matrix=(int *) malloc (out_length * sizeof(int));
(int *)nextstate=(int *) malloc (number_of_states*2 * sizeof(int));
(int *)input=(int *) malloc (number_of_states*number_of_states * sizeof(int));
(int *)out_put=(int *) malloc (number_of_states*2 * sizeof(int));
(int *)state_metric=(int *) malloc (number_of_states*2 * sizeof(int));
(int *)survivor_state=(int *) malloc (number_of_states*(depth_of_trellis+1) * sizeof(int));
(int *)decodeder_output_matrix=(int *) malloc ((depth_of_trellis-L+1) * sizeof(int));
(int *) state_sequence=(int *) malloc ((depth_of_trellis+1) * sizeof(int));





//--------------------------------------------------------------------
 for (j=0;j<number_of_states;j++)
 for (m=0;m<2;m++)
	{
  state_temp=j;
 // printf("%d\n",state_temp);

  statetemp=state_temp>>1;
//printf("%d\n",state_temp);

next_state=statetemp|m<<5;

//printf("%d\n",next_state);




branch_output_1=m^(state_temp&0x0010)>>4^(state_temp&0x0008)>>3^(state_temp&0x0002)>>1^(state_temp&0x0001);
branch_output_2=m^(state_temp&0x0001)^(state_temp&0x0008)>>3^(state_temp&0x0010)>>4^(state_temp&0x0020)>>5;
branch_output_3=m^(state_temp&0x0001)^(state_temp&0x0004)>>2^(state_temp&0x0010)>>4^(state_temp&0x0020)>>5;



branch_output=branch_output_1|branch_output_2<<1|branch_output_3<<2;



//-----------------------------------------------------------------
input[j*number_of_states+next_state]=m;
nextstate[j*2+m]=next_state;
out_put[j*2+m]=branch_output;

 }

//------------------------------------------------------------------code mode

//----------------------------------------------------------add noise
/*
function [n1,n2]=gngauss(m,sgma)
% [n1 n2]=gngauss(m,sgma)
% [n1 n2]=gngauss(sgma)
% [n1 n2]=gngauss
% GNGAUSS generates two independent gaussian variables with 
% mean m & S.D sgma
% if one of the input argement missing it takes mean as zero
% if neither mean nor variance is given, it generates two 
%standard gaussian random variables

if nargin==0
m=0;
sgma=1;
elseif nargin==1
sgma=m; 
m=0;
end
u=rand; % uniform random variable in (0,1)
z=sgma*(sqrt(2*log(1/(1-u)))); % rayleigh distributed random variable
u=rand; % another uniform distributed variable in (0,1)
n1=m+z*cos(2*pi*u); 
n2=m+z*sin(2*pi*u);  
*/
srand((unsigned)time (NULL));


for (p=0;p<9;p++)
{

	snrdb=snrdb+1;
	snr=pow(10,snrdb/10);
	sgma=(sqrt((3*E)/snr))/2;




for(y=0;y<10000;y++)

{

for (i=0;i<number_of_states*2;i++)

 state_metric[i]=0;

//---------------------------------------------------
	for (q=0;q<in_length;q++)
{
	j=rand()%2;
//	printf("%d ",j);
	if (j==0)
		channel_input[q]=0;
	else 
		channel_input[q]=1;
}
for(q=in_length;q<in_length+6;q++)
 channel_input[q]=0;






//-----------------------------------------------------------------------
 state_temp=0;
for(q=0;q<in_length+6;q++)


{

m=channel_input[q];
branch_output_1=m^(state_temp&0x0002)>>1^(state_temp&0x0004)>>2^(state_temp&0x0010)>>4^(state_temp&0x0020)>>5;
branch_output_2=m^(state_temp&0x0001)^(state_temp&0x0002)>>1^(state_temp&0x0004)>>2^(state_temp&0x0020)>>5;
branch_output_3=m^(state_temp&0x0001)^(state_temp&0x0002)>>1^(state_temp&0x0008)>>3^(state_temp&0x0020)>>5;

statetemp=(state_temp<<1)&0x003f;
state_temp=statetemp|channel_input[q];

channel_output[a1]=branch_output_1;
channel_output[a1+1]=branch_output_2;
channel_output[a1+2]=branch_output_3;
a1=a1+3;

}
a1=0;


//-----------------------------------------------------










	for(q=0;q<out_length;q=q+2)
	{
		
			add[0]=gngausscos(sgma);
			add[1]=gngausscos(sgma);
if((channel_output[q]==0)&&(channel_output[q+1]==0))
		for(x=0;x<2;x++)
			r[x]=add[x]+s00[x];

		else if((channel_output[q]==0)&&(channel_output[q+1]==1))
				for(x=0;x<2;x++)
					r[x]=add[x]+s01[x];
			else if((channel_output[q]==1)&&(channel_output[q+1]==0))
				for(x=0;x<2;x++)
					r[x]=add[x]+s10[x];
				else 
					for(x=0;x<2;x++)
					r[x]=s11[x]+add[x];



				c[0]=r[0]*s00[0]+r[1]*s00[1];
				c[1]=r[0]*s01[0]+r[1]*s01[1];
				c[2]=r[0]*s10[0]+r[1]*s10[1];
				c[3]=r[0]*s11[0]+r[1]*s11[1];

//-----------------------------------------------------------pai xu
if((c[0]>c[1])&&(c[0]>c[2])&&(c[0]>c[3]))
{
channel_qpskoutput[q]=0;
channel_qpskoutput[q+1]=0;
}
else if((c[1]>c[0])&&(c[1]>c[2])&&(c[1]>c[3]))
{
channel_qpskoutput[q]=0;
channel_qpskoutput[q+1]=1;
}
else if((c[2]>c[0])&&(c[2]>c[1])&&(c[2]>c[3]))
{
channel_qpskoutput[q]=1;
channel_qpskoutput[q+1]=0;
}
else
{
channel_qpskoutput[q]=1;
channel_qpskoutput[q+1]=1;
}


	}
	
	
/*
 for (i=0;i<30018;i++){
	 if(channel_output[i]!=channel_qpskoutput[i])
		 numerror++;
 }

 errorate=numerror/30018;
 printf("%f ",errorate);
 numerror=0;



*/

//for(i=0;i<9018;i++)
//printf("%d%d ",channel_output[i],channel_qpskoutput[i]);






















//-----------------------------------------------------------add noise


//-----------------------------------------------------------------store datas


 
//printf("%d\n",out_put[63*2+1]);
 
//-----------------------------------------------------------------store datas

  h=0;

 for (j=0;j<depth_of_trellis;j++)
	 for (i=0;i<n;i++)
	 	channel_output_matrix[i*depth_of_trellis+j]=channel_qpskoutput[h++];

	
//-----------------------------------------------------


 


//--------------------------------------start decoding of non-tail channel outputs.


 for (i=0;i<number_of_states*(depth_of_trellis+1);i++)
 
 survivor_state[i]=0;
	//	 printf("%d\n",survivor_state[i]);
 


for(i=1;i<=(depth_of_trellis-L+1);i++)
{
  for(g=0;g<number_of_states;g++)
	  flag[g]=0;
  if (i<=L)
	  step=pow(2,L-i);
   
  else
	  step=1;
  //printf("%d\n",step);
  for (j=0;j<number_of_states;j=j+step)
	  for(l=0;l<=1;l++)
	  {
       branch_metric=0;
	   branch_output_1=out_put[j*2+l]&0x0001;

	  // printf("%d\n",out_put[j*2+l]);

	   branch_output_2=(out_put[j*2+l]&0x0002)>>1;
	   branch_output_3=(out_put[j*2+l]&0x0004)>>2;


 //for(ll=0;ll<n;ll++)
    if (branch_output_1!=channel_output_matrix[i-1])
		branch_metric++;
		    if (branch_output_2!=channel_output_matrix[i-1+depth_of_trellis])
		branch_metric++;
		    if (branch_output_3!=channel_output_matrix[i-1+2*depth_of_trellis])
		branch_metric++;
//--------------------------------------------------------------------
if(state_metric[nextstate[j*2+l]*2+1]>(state_metric[j*2]+branch_metric)||flag[nextstate[j*2+l]]==0)
{
state_metric[nextstate[j*2+l]*2+1]=state_metric[j*2]+branch_metric;
survivor_state[nextstate[j*2+l]*(depth_of_trellis+1)+i]=j;


//printf("%d\n",survivor_state[nextstate[j,l],i]);


flag[nextstate[j*2+l]]=1;
}
}
//---------------------------------------------	    
//	  for(a=0;a<64*2;a++)
//  printf("%d ",state_metric[a]);

//-------------------------------------------
  for (a=0;a<number_of_states;a++)
  {
 tempt=state_metric[a*2];
 state_metric[a*2]=state_metric[a*2+1];
 state_metric[a*2+1]=tempt;
  }
}
//-----------------------------------------start decoding of non-tail channel outputs.

//--------------------------------------start decoding of the tail channel outputs.

for(i=depth_of_trellis-L+2;i<=depth_of_trellis;i++)
{
    for(g=0;g<number_of_states;g++)
	  flag[g]=0;
   last_stop=number_of_states/pow(2,i-depth_of_trellis+L-2);
  // printf("%d\n",last_stop);
  for (j=0;j<last_stop;j++)
	  //for(l=0;l<=1;l++)
	  {
       branch_metric=0;
	   branch_output_1=out_put[j*2+0]&0x0001;

	  // printf("%d\n",out_put[j*2+l]);

	   branch_output_2=(out_put[j*2+0]&0x0002)>>1;
	   branch_output_3=(out_put[j*2+0]&0x0004)>>2;
 //for(ll=0;ll<n;ll++)
    if (branch_output_1!=channel_output_matrix[i-1])
		branch_metric++;
		    if (branch_output_2!=channel_output_matrix[i-1+depth_of_trellis])
		branch_metric++;
		    if (branch_output_3!=channel_output_matrix[i-1+2*depth_of_trellis])
		branch_metric++;
//--------------------------------------------------------------------
if(state_metric[nextstate[j*2]*2+1]>(state_metric[j*2]+branch_metric)||flag[nextstate[j*2]]==0)
{
state_metric[nextstate[j*2]*2+1]=state_metric[j*2]+branch_metric;

survivor_state[nextstate[j*2]*(depth_of_trellis+1)+i]=j;


//printf("%d\n",depth_of_trellis);


flag[nextstate[j*2]]=1;
}
}


  for (a=0;a<number_of_states;a++)
  {
 tempt=state_metric[a*2];
 state_metric[a*2]=state_metric[a*2+1];
 state_metric[a*2+1]=tempt;
  }
}


//-------------------------------------------------

//for (a=0;a<64;a++)
//if(input[a]==0||input[a]==1)
//{
//	i++;
//printf("%d ",survivor_state[a*(depth_of_trellis+1)+5]);
//}
/**/
//---------------------------------------







//-----------------------------------generate the decoder output from the optimal path



for (i=0;i<=depth_of_trellis;i++)
  state_sequence[i]=0;
state_sequence[depth_of_trellis-1]=survivor_state[depth_of_trellis];

for(i=1;i<=depth_of_trellis;i++)
{
state_sequence[depth_of_trellis-i]=survivor_state[state_sequence[depth_of_trellis-i+1]*(depth_of_trellis+1)+depth_of_trellis-i+1];
//printf("%d\n",state_sequence[depth_of_trellis-i]);
}
 

for (i=0;i<=depth_of_trellis-L;i++)
   decodeder_output_matrix[i]=0;
 for (i=0;i<=depth_of_trellis-L;i++)
 {
    decodeder_output_matrix[i]=input[state_sequence[i]*number_of_states+state_sequence[i+1]];
//	printf("%d%d ",channel_input[i],decodeder_output_matrix[i]);
 }
//-------------------------------------------------over decodeder_output_matrix
 /**/
 
 for (i=0;i<in_length;i++){
	 if(channel_input[i]!=decodeder_output_matrix[i])
		 numerror++;
 }

 }
 errorate[p]=numerror/800000;
 printf("%.9f ",errorate[p]);
 numerror=0;

}

   if((fp=fopen("xxx.bin","wb"))==NULL)
 printf("cannot open infile\n");
   for(p=0;p<9;p++)
 fwrite(&errorate[p],sizeof(double),1,fp);
 fclose(fp);

  free (decodeder_output_matrix);
  free (state_sequence);
  free (survivor_state);
  free (channel_output_matrix);
  free (nextstate);
  free (input);
  free (out_put);
  free (state_metric);
 
 


 //----------------------------------- free store




}




⌨️ 快捷键说明

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