📄 main.c
字号:
#include "stdio.h"
#include "head.h"
#include "math.h"
#include "malloc.h"
#include <stdlib.h>
//#include <stdio.h>
#include <time.h>
float gngausscos(float);
float gngausssin(float);
void main()
{
//------xin dao
int x1,x2,x3,x4,x5,x6,x7,x8;
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};
int temp[16]={0},decoder_resut[80]={0};
int numb[8],numb_shu[8];
//long in_length=100000,out_length=300018;
//-------------------------------------------
int z=0;
//---------------------------------------------------
long j,m,h=0,g,step,l,ll=0,tempt,last_stop,b=0,p,x=0;
long q,a=0,i=0,a1=0,a2=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,branch_metric1=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_temp,*survivor_state,*state_metric,*state_metric_temp;
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*8 * sizeof(int));
(int *)state_metric_temp=(int *) malloc (number_of_states*8 * sizeof(int));
(int *)survivor_state=(int *) malloc (number_of_states*(depth_of_trellis+1)*(L+1) * sizeof(int));
(int *)survivor_state_temp=(int *) malloc (number_of_states*(depth_of_trellis+1)*(L+1) * sizeof(int));
(int *)decodeder_output_matrix=(int *) malloc ((depth_of_trellis-L+1)*8 * 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(a2=0;a2<10000;a2++)
{
//---------------------------------------------------
for (q=0;q<in_length-8;q++)
{
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
/* Display 10 numbers. */
j=rand()%2;
if (j==0)
channel_input[q]=0;
else
channel_input[q]=1;
}
for(q=in_length-8;q<in_length+6;q++)
channel_input[q]=0;
//-----------------------CRC jiao yan
q=0;
z=0;
while(q<in_length)
{
if((z&0x100)==0)
z=(z<<1)|channel_input[q++];
else
z=(z&0xff)^0x1D;
}
if((z&0x100)!=0)
z=(z&0xff)^0x1D;
for(q=0;q<8;q++)
{
channel_input[in_length-8+q]=(z>>(7-q))&0x01;
}
//for(q=0;q<in_length+6;q++)
//printf("%d ",channel_input[q]);
//-----------------------------------------------------------------------CRC
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;
*/
//-----------------------------------------------------------add noise
//-----------------------------------------------------------------store datas
//printf("%d\n",out_put[63*2+1]);
//-----------------------------------------------------------------store datas
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++];
h=0;
//-----------------------------------------------------
//--------------------------------------start decoding of non-tail channel outputs.
for (i=0;i<number_of_states*8;i++)
state_metric[i]=0;
for (i=0;i<number_of_states*8;i++)
state_metric_temp[i]=0;
for (i=0;i<number_of_states*(depth_of_trellis+1)*(L+1);i++)
survivor_state[i]=0;
for (i=0;i<number_of_states*(depth_of_trellis+1)*(L+1);i++)
survivor_state_temp[i]=0;
// printf("%d\n",survivor_state[i]);
//---------------------------------------------------------------1~7dian
//
//
//
//
//
//
//
//
//
//----------------------------------------------------------------- 1~7 dian
for(i=1;i<L;i++)
{
step=pow(2,L-i);
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++;
for(q=0;q<depth_of_trellis+1;q++)
survivor_state[nextstate[j*2+l]*(depth_of_trellis+1)*8+q]
=survivor_state_temp[j*(depth_of_trellis+1)*8+q];
survivor_state[nextstate[j*2+l]*(depth_of_trellis+1)*8+i]=j;
state_metric[nextstate[j*2+l]*(L+1)]=state_metric_temp[j*(L+1)]+branch_metric;
// printf("%d,%d, ",j,nextstate[j*2+l]);
}
// printf("%d,%d, ",j,nextstate[j*2+l]);
for(q=0;q<number_of_states*8;q++)
state_metric_temp[q]=state_metric[q];
for (q=0;q<number_of_states*(depth_of_trellis+1)*(L+1);q++)
survivor_state_temp[q]=survivor_state[q];
}
/*
for (j=0;j<64;j++)
{
for(i=0;i<7;i++)
printf("%d ",survivor_state[i+(depth_of_trellis+1)*8*j]);
printf("\n");
}
*/
//---------------------------------------------------------------1~7
//--------------------------------------------------------------8~10
//
//
//
//
//
//
//
//
//-------------------------------------------------------------8~10 dian
for(i=7;i<=9;i++)
{
for(l=0;l<=1;l++)
for (j=0;j<number_of_states;j=j+2)
{
//--------------------------------------------------
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++;
//--------------------------------------------------
branch_metric1=0;
branch_output_1=out_put[(j+1)*2+l]&0x0001;
// printf("%d\n",out_put[j*2+l]);
branch_output_2=(out_put[(j+1)*2+l]&0x0002)>>1;
branch_output_3=(out_put[(j+1)*2+l]&0x0004)>>2;
//for(ll=0;ll<n;ll++)
if (branch_output_1!=channel_output_matrix[i-1])
branch_metric1++;
if (branch_output_2!=channel_output_matrix[i-1+depth_of_trellis])
branch_metric1++;
if (branch_output_3!=channel_output_matrix[i-1+2*depth_of_trellis])
branch_metric1++;
//-------------------------------------------------
y=pow(2,i-8+1);
for(q=0;q<(depth_of_trellis+1)*(pow(2,i-8+1));q++)
{
survivor_state[nextstate[j*2+l]*(depth_of_trellis+1)*8+(depth_of_trellis+1)*0+q]
=survivor_state_temp[j*(depth_of_trellis+1)*8+(depth_of_trellis+1)*0+q];
survivor_state[nextstate[j*2+l]*(depth_of_trellis+1)*8+(depth_of_trellis+1)*y+q]
=survivor_state_temp[(j+1)*(depth_of_trellis+1)*8+(depth_of_trellis+1)*0+q];
}
for(q=0;q<pow(2,i-8+1);q++)
{
survivor_state[nextstate[j*2+l]*(depth_of_trellis+1)*8+(depth_of_trellis+1)*q+i]
=j;
survivor_state[nextstate[j*2+l]*(depth_of_trellis+1)*8+(depth_of_trellis+1)*(y+q)+i]
=j+1;
}
for(q=0;q<pow(2,i-8+1);q++)
{
state_metric[nextstate[j*2+l]*(L+1)+q]=state_metric_temp[j*(L+1)+q]+branch_metric;
state_metric[nextstate[j*2+l]*(L+1)+q+y]=state_metric_temp[(j+1)*(L+1)+q]+branch_metric1;
}
}
for(q=0;q<number_of_states*8;q++)
state_metric_temp[q]=state_metric[q];
for (q=0;q<number_of_states*(depth_of_trellis+1)*(L+1);q++)
survivor_state_temp[q]=survivor_state[q];
}
// for(i=0;i<10;i++)
//printf("%d ",survivor_state[i+(depth_of_trellis+1)*8*1]);
for(i=10;i<=depth_of_trellis-L+1;i++)
{
for(l=0;l<=1;l++)
for (j=0;j<number_of_states;j=j+2)
{
//--------------------------------------------------
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++;
//--------------------------------------------------
branch_metric1=0;
branch_output_1=out_put[(j+1)*2+l]&0x0001;
// printf("%d\n",out_put[j*2+l]);
branch_output_2=(out_put[(j+1)*2+l]&0x0002)>>1;
branch_output_3=(out_put[(j+1)*2+l]&0x0004)>>2;
//for(ll=0;ll<n;ll++)
if (branch_output_1!=channel_output_matrix[i-1])
branch_metric1++;
if (branch_output_2!=channel_output_matrix[i-1+depth_of_trellis])
branch_metric1++;
if (branch_output_3!=channel_output_matrix[i-1+2*depth_of_trellis])
branch_metric1++;
//-------------------------------------------------------------------------
temp[0]=state_metric_temp[j*(L+1)+0]+branch_metric;
temp[1]=state_metric_temp[j*(L+1)+1]+branch_metric;
temp[2]=state_metric_temp[j*(L+1)+2]+branch_metric;
temp[3]=state_metric_temp[j*(L+1)+3]+branch_metric;
temp[4]=state_metric_temp[j*(L+1)+4]+branch_metric;
temp[5]=state_metric_temp[j*(L+1)+5]+branch_metric;
temp[6]=state_metric_temp[j*(L+1)+6]+branch_metric;
temp[7]=state_metric_temp[j*(L+1)+7]+branch_metric;
temp[8]=state_metric_temp[(j+1)*(L+1)+0]+branch_metric1;
temp[9]=state_metric_temp[(j+1)*(L+1)+1]+branch_metric1;
temp[10]=state_metric_temp[(j+1)*(L+1)+2]+branch_metric1;
temp[11]=state_metric_temp[(j+1)*(L+1)+3]+branch_metric1;
temp[12]=state_metric_temp[(j+1)*(L+1)+4]+branch_metric1;
temp[13]=state_metric_temp[(j+1)*(L+1)+5]+branch_metric1;
temp[14]=state_metric_temp[(j+1)*(L+1)+6]+branch_metric1;
temp[15]=state_metric_temp[(j+1)*(L+1)+7]+branch_metric1;
//-----------------------------------------------------------pai xu
//
//
//
//
//
//
//
//------------------------------------------------------------pai xu
for(y=0;y<8;y++)
{
numb[y]=temp[0]; numb_shu[y]=0;
for(q=1;q<16;q++)
{
if(temp[q]<numb[y])
{
numb[y]=temp[q];
numb_shu[y]=q;
}
}
temp[numb_shu[y]]= 32767;
}
//----------------------------------------------------------matrix
//
//
//
//
//
//
//----------------------------------------------------------matrix
for(y=0;y<8;y++)
{
for(q=0;q<depth_of_trellis+1;q++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -