📄 turbosystem.cpp
字号:
#include "math.h"
#include "stdio.h"
#define L_TOTAL 64
#define NSTATE 4
#include "iostream.h"
#include "iomanip.h"
#include <stdlib.h>
#include<time.h>
#define randomize() srand((unsigned)time(NULL))
#define M 2
#define DELTA 30
typedef unsigned char BYTE ;
typedef unsigned int UINT ;
typedef int INT;
static const char EnNextOut[2][NSTATE] =
{ 0,0,1,1,
1,1,0,0
};
static const char NextOut[2][NSTATE] =
{ -1,-1,1,1,
1,1,-1,-1
};
static const BYTE NextState[2][NSTATE] =
{ 0,2,3,1,
2,0,1,3
};
static const char TailBit[NSTATE] =
{ 0,1,1,0
};
static const char LastOut[2][NSTATE] =
{ -1,1,-1,1,
1,-1,1,-1
};
static const BYTE LastState[2][NSTATE] =
{ 0,3,1,2,
1,2,0,3
};
void RSC_Encode(char *mesg, char *parity, unsigned int size, bool force);
void sova(double *msg, double *parity, double *L_a,double *L_all, bool index);
void systemdecode(double *input,double *output,int n);
void systemencode(char *input,char *stream,int n);
void RSC_Encode(char *mesg, char *parity, unsigned int size, bool force)
{
BYTE state,uk;
unsigned x;
state=0;
for (x=0;x<size;x++)
{
if (x>=size-2 && force)
{
mesg[x] = TailBit[state];
}
uk = mesg[x] ? 1 : 0;
parity[x] = EnNextOut[uk][state];
state = NextState[uk][state];
}
}
void sova(double *msg, double *parity, double *L_a,double *L_all, bool index)
{
double path_metric[L_TOTAL+1][NSTATE];
double m_diff[L_TOTAL+1][NSTATE];
char prev_bit[L_TOTAL+1][NSTATE],estbit[L_TOTAL],bit;
double sum0,sum1;
double llr;
double INFINITY = 1E+10;
UINT mlstate[L_TOTAL+1],nstate,s,tempstate;
INT k,i,j;
nstate=NSTATE;
for (k=0;k<=L_TOTAL;k++)
for (s=0;s<s;s++)
path_metric[k][s]=-INFINITY;
path_metric[0][0] = 0;
for (k=0;k<L_TOTAL;k++)
for (s=0;s<nstate;s++)
{
sum0=-msg[k]+parity[k]*LastOut[0][s]-L_a[k]/2+path_metric[k][LastState[0][s]];//?????????
sum1=msg[k]+parity[k]*LastOut[1][s]+L_a[k]/2+path_metric[k][LastState[1][s]];//????????
if (sum0>sum1){
path_metric[k+1][s]=sum0;
m_diff[k+1][s]=sum0-sum1;
prev_bit[k+1][s]=0;
}else{
path_metric[k+1][s]=sum1;
m_diff[k+1][s]=sum1-sum0;
prev_bit[k+1][s]=1;
}
}
if (index)
mlstate[L_TOTAL]=0;
else{
mlstate[L_TOTAL]=0;
for (s=1;s<nstate;s++)
if (path_metric[L_TOTAL][s]>path_metric[L_TOTAL][mlstate[L_TOTAL]])
mlstate[L_TOTAL]=s;
}
for (k=L_TOTAL-1;k>=0;k--)
{
estbit[k]=prev_bit[k+1][mlstate[k+1]];
mlstate[k]=LastState[estbit[k]][mlstate[k+1]];
}
for (k=0;k<L_TOTAL;k++)
{
llr=INFINITY;
for (i=0;i<=DELTA;i++)
{
if (k+i<L_TOTAL)
{
bit=1-estbit[k+i];//????????????????????????
tempstate=LastState[bit][mlstate[k+i+1]];
for (j=i-1;j>=0;j--)
{
bit=prev_bit[k+j+1][tempstate];
tempstate=LastState[bit][tempstate];
}
if (bit != estbit[k])
if (llr>m_diff[k+i+1][mlstate[k+i+1]])
llr=m_diff[k+i+1][mlstate[k+i+1]];
}
}
L_all[k] = (2*estbit[k]-1) * llr;
}
}
void systemencode(char *input,char *stream,int n)
{ INT i;
char imsg[L_TOTAL],msg[L_TOTAL];
char chkBuffer[2][L_TOTAL];
bool puncture=true;
unsigned m_Inter_table[64];
UINT k1,k2;
for(i=0;i<n;i++)
{msg[i]=*input;
input++;
}
for (k1=0;k1<8;k1++)
for (k2=0;k2<8;k2++)
m_Inter_table[k1*8+k2]=k2*8+k1;
RSC_Encode(msg,chkBuffer[0],L_TOTAL,true);
for (i=0;i<L_TOTAL;i++)
imsg[i]=msg[m_Inter_table[i]];
RSC_Encode(imsg,chkBuffer[1],L_TOTAL,false);
for (i=0;i<L_TOTAL;i++)
{
*stream=msg[i];
stream++;
*stream=chkBuffer[i%2][i];
stream++;
}
}
void systemdecode(double *input,double *output,int n)
{double msghat[L_TOTAL];
INT niter=6;
double msg[L_TOTAL],imsg[L_TOTAL];
double parity[2][L_TOTAL];
double L_all[L_TOTAL];
double L_e[L_TOTAL],L_a[L_TOTAL];
INT i,iter;
UINT k1,k2;
double stream[200];
unsigned m_Inter_table[64];
for(i=0;i<2*n;i++)
{stream[i]=*input;
input++;
}
for (k1=0;k1<8;k1++)
for (k2=0;k2<8;k2++)
m_Inter_table[k1*8+k2]=k2*8+k1;
for (i=0;i<L_TOTAL;i++)
{
parity[0][i]=0;
parity[1][i]=0;
}
for (i=0;i<L_TOTAL;i++)
{
msg[i]=stream[2*i];
parity[i%2][i]=stream[i*2+1];
}
for (i=0;i<L_TOTAL;i++)
imsg[i]=msg[m_Inter_table[i]];
for (i=0;i<L_TOTAL;i++)
L_e[i]=0;
for (iter=0;iter<niter;iter++)
{
for (i=0;i<L_TOTAL;i++)
L_a[m_Inter_table[i]]=L_e[i];
sova(msg,parity[0],L_a,L_all,true);
for (i=0;i<L_TOTAL;i++)
L_e[i]=L_all[i]-2*msg[i]-L_a[i];
for (i=0;i<L_TOTAL;i++)
L_a[i]=L_e[m_Inter_table[i]];
sova(imsg,parity[1],L_a,L_all,false);
for (i=0;i<L_TOTAL;i++)
L_e[i]=L_all[i]-2*imsg[i]-L_a[i];
}
for (i=0;i<L_TOTAL;i++)
if(L_all[i]>0)
msghat[m_Inter_table[i]]=1;
else
msghat[m_Inter_table[i]]=0;
for (i=0;i<L_TOTAL;i++)
{*output=msghat[i];
output++;
}
}
void main()
{ int i,n=64,m,j=0,k;
char encodeinput[100],wrong[10]={0,0,0,0,0,0,0,0,0,0};
char encodeoutput[200],output[100];
double decodeinput[200],decodeoutput[100];
randomize();
for(i=0; i<n; i++)
encodeinput[i]=rand()%2;
systemencode(encodeinput,encodeoutput,n);
printf("the input of encoder is :\n");
for(i=0;i<62; i++)
printf("%2d",encodeinput[i]);
printf("\n");
printf("the output of encoder is :\n");
for(i=0;i<128;i++)
{printf("%2d",encodeoutput[i]);
if(i%20==19)
printf("\n");
}
printf("\n");
printf("please input the number of the wrong bit\n");
scanf("%d",&m);
printf("please input the positions of the wrong bit (0-127)\n");
for(i=0;i<m;i++)
{ scanf("%d",&wrong[m]);
if(encodeoutput[wrong[m]]==0)
encodeoutput[wrong[m]]=1;
else
encodeoutput[wrong[m]]=0;
}
printf("the input of decoder is :\n");
for(i=0;i<128;i++)
{printf("%2d",encodeoutput[i]);
if(i%20==19)
printf("\n");
}
printf("\n");
for(i=0;i<2*n;i++)
decodeinput[i]=encodeoutput[i]*2-1;
systemdecode(decodeinput,decodeoutput,n);
for(i=0;i<n;i++)
output[i]=decodeoutput[i];
printf("the output of decoder is :\n");
for(i=0;i<62;i++)
printf("%2d",output[i]);
printf("\n");
for(i=0;i<62;i++)
{ if(encodeinput[i]!=decodeoutput[i])
j++;
}
printf("the number of incorrect bit is:%d\n",j);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -