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

📄 turbo.h

📁 采用随机输入作为码源
💻 H
字号:
#include <math.h>
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>

#define randomize() srand((unsigned)time(NULL))     
#define M     2            
#define DELTA  30
#define L_TOTAL 64     
#define NSTATE  4

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];//msg是原始输入,imsg是通过交织器之后
        char chkBuffer[2][L_TOTAL];	//chkBuffer[0],[1]分别用来存放msg和imsg通过RSC编码器后的结果
		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;//8*8行列交织器
          
	
        RSC_Encode(msg,chkBuffer[0],L_TOTAL,true); //Rsc编码器1
        for (i=0;i<L_TOTAL;i++)
                imsg[i]=msg[m_Inter_table[i]];
        
        RSC_Encode(imsg,chkBuffer[1],L_TOTAL,false);//Rsc编码器2
		for (i=0;i<L_TOTAL;i++)//删余和复接,结果为stream
		{
		  *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++;
	   }
}

⌨️ 快捷键说明

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