itfanr
2013-08-05 20:55:17 +08:00
用到了两个主要文件,在main里面调用turbo_decode函数:
//部分main代码:
double ** soft_out ;
soft_out = (double **)malloc(sizeof(double *) * inter_num);
for (int soft_out_i =0; soft_out_i < inter_num ; soft_out_i++)
{
soft_out[soft_out_i] = (double *)malloc(sizeof(double)* frame_size);
}
turbo_decode(Lc ,g,ispunctured ,pattern ,decode_method ,receive ,soft_out) ;
//1.decode.cpp
#include "stdafx.h"
#include "tools.h"
#include <math.h>
#include <string.h>
#include "decode.h"
#include "decode.h"
void turbo_decode(double Lc ,int g[][4] ,int ispunctured ,const int pattern [] ,int decode_method ,const double * receive , double **soft_out){
if (ispunctured)//删余
{
//
}
else
{
/*double e_info [frame_size+3] ;
double die_info [frame_size+3] ;
double so [frame_size+3 ] ;
double so1 [frame_size+3] ;*/
double in1 [2][frame_size+3] ;
double in2 [2][frame_size+3] ;
for (int i=0; i<frame_size+3; i++)
{
in1[0][i] = receive[3 * i] ;
in1[1][i] = receive[3 * i + 1] ;
in2[1][i] = receive[3 * i + 2] ;
}
for(int i=0 ;i< frame_size+3 ; i++){
in2[0][i] = in1[0][pattern[i]-1];//交织 因为交织的数组为(1-1024,故减1)
}
double * e_info = (double *)malloc(sizeof(double) * (frame_size+3) );
double * die_info = (double *)malloc(sizeof(double) * (frame_size+3) );
double * so = (double *)malloc(sizeof(double) * (frame_size+3) );
double so1 [frame_size+3] ;
for (int i=0; i<frame_size+3; i++)
{
e_info[i] = 0 ;
}
for (int inter = 0 ; inter< inter_num; inter++)//开始迭代
{
// printf(" \n\n inter = %d\n\n ", inter) ;
for(int i=0 ;i< frame_size+3 ; i++){ //解交织
die_info [pattern[i]-1] = e_info[i] ;
}
// printf("before decoder 1---------die_info[0] = %lf \n", die_info[0] ) ;
maxLogMap(in1, g, Lc, (const double *)die_info, e_info, so) ; //
//rintf("after decoder 1---------e_info[0] = %lf \n", e_info[0] ) ;
for(int i=0 ;i< frame_size+3 ; i++){
die_info[i] = e_info[pattern[i]-1] ;//交织
}
// printf("before decoder 2---------die_info[0] = %lf \n", die_info[0] ) ;
maxLogMap(in2 , g, Lc, (const double *) die_info, e_info, so) ; //改变了e_info
//rintf("after decoder 2 ---------e_info[0] = %lf \n", e_info[0] ) ;
for(int i=0 ;i< frame_size+3 ; i++){ //解交织
so1 [(pattern[i]-1)] = so[i] ;
}
for (int i1 = 0 ; i1<frame_size ; i1++)//舍去最后的三个数字
{
soft_out[inter][i1] = so1[i1] ;
}
}
free(e_info) ;
e_info = NULL ;
free(die_info) ;
die_info = NULL ;
free(so) ;
so=NULL;
}//else 结束
}
void genTrellis(int g[][4],int lstate [][8] , int nstate [][8],int lpara[][8],int npara[][8]){
int k = 4 ;
int m =k-1 ;
int in = 0 ;
int state_temp [3] ;//input0和input1都用
int state[3] ;//input0和input1都用
int *gp1 = &g[0][1] ;//g(1,2:end)
int *gp2 = &g[1][0] ;//g(2,:)
int para = 0 ;
int state0_temp1 [4] ;
int state0_temp2[3] ;
int state1_temp1[4] ;
int state1_temp2[3] ;
int nstate_index ;
for (int i=0 ;i<8 ;i++ )
{
de2bi(i,state_temp) ;
//input 0
fliplr(state,state_temp,3) ;
in = (matrix_Mul(gp1,state,3)%2)^0 ;
state0_temp1[0] = in ;
for (int j = 1; j<4 ; j++)
{
state0_temp1[j] = state[j-1] ;
}
para = matrix_Mul(gp2,state0_temp1,4)%2 ;
right_shift(in,state,3) ;
fliplr(state0_temp2,state,3) ;
nstate_index = bi2de(state0_temp2, 3) ;
nstate[0][i] = nstate_index +1 ;
lpara[0][nstate_index] = 2 * para -1 ;
lstate[0][nstate_index] = i+1 ;
//input 1
fliplr(state,state_temp,3) ; //重置state
in = (matrix_Mul(gp1, state,3)%2)^1 ;
state1_temp1[0] = in ;
for (int j = 1; j<4 ; j++)
{
state1_temp1[j] = state[j-1] ;
}
para = matrix_Mul(gp2,state1_temp1,4 )%2 ;
right_shift(in,state,3) ;
fliplr(state1_temp2, state, 3) ;
nstate_index = bi2de(state1_temp2, 3);
nstate[1][i] = nstate_index +1 ;
lpara[1][nstate_index] = 2* para - 1 ;
lstate[1][nstate_index] = i+1 ;
}
//以下产生npara
for (int i = 0 ; i<2 ;i++)
{
for (int j=0; j<8; j++)
{
//找到上一个状态
int state_temp ;
state_temp = lstate[i][j] ;
int para_temp ;
para_temp = lpara[i][j] ;
npara[i][state_temp-1] = para_temp ;
}
}
}
2.subdecode.cpp
#include "stdafx.h"
#include "tools.h"
#include <math.h>
#include "decode.h"
double maxVect(const double vect[], int vect_len) ;
void maxLogMap(const double input[][frame_size+3] , int g[][4],double Lc,const double * pri_info, double * e_info, double *so ) {
//产生网格
int lstate [2][8];
int nstate [2][8] ;
int lpara [2][8] ;
int npara [2][8] ;
genTrellis(g, lstate , nstate, lpara, npara) ;
//主要译码部分
const int nstates = 8 ;
//alpha beta gamma 初始化
double alpha[frame_size +3][nstates] , beta[frame_size +3+1][nstates] ;
double gamma0[frame_size +3][nstates], gamma1[frame_size +3][nstates] ;
const int infinity = 1000 ;
alpha[0][0] = 0 ;
for (int st = 1; st<nstates; st++)
{
alpha [0][st] = -infinity ;//初始化第1行
}
//beta
double log_temp = 1.0/8 ;
for (int beta_col = 0; beta_col<nstates; beta_col++ )
{
beta[frame_size +3][beta_col] = log(log_temp) ; //初始化最后一行
}
//------------------------------------------------------------------------------------
for (int i = 0; i<frame_size +3; i++)
{
for (int st = 0; st<nstates; st++)
{
double mul_temp ;
mul_temp = -1 * input[0][i] + lpara[0][st] * input[1][i] ; //输入0
gamma0[i][st] = -1 * pri_info[i]/2 + Lc/2 * mul_temp ;
mul_temp = 1 * input[0][i] + lpara[1][st] * input[1][i] ; //输入1
gamma1[i][st] = pri_info[i]/2 + Lc/2 * mul_temp ;
}
}
//--------------------------------------------------------------------------------------
for (int i = 1; i<frame_size +3; i++)
{
int lstate_temp1 ,lstate_temp2 ;
double data0 ,data1 ;
for (int st = 0; st<nstates; st++ )
{
lstate_temp1 = lstate[0][st] -1;
data0 = gamma0[i-1][st] + alpha[i-1][lstate_temp1] ;
lstate_temp2 = lstate[1][st]-1 ;
data1 = gamma1[i-1][st] + alpha[i-1][lstate_temp2] ;
alpha[i][st] = (data0>data1 ? data0 :data1 );
}
}
//------------------------------------------------------------------------------------------------------------
for (int i = frame_size +3-1; i>0 ; i--)
{
int data0, data1 ;
int nstate_temp0,nstate_temp1 ;
for (int st = 0 ; st<nstates; st++)
{
nstate_temp0 = nstate[0][st] -1;
data0 = gamma0[i][nstate_temp0] + beta[i+1][nstate_temp0] ;
nstate_temp1 = nstate[1][st] -1;
data1 = gamma1[i][nstate_temp1] + beta[i+1][nstate_temp1] ;
beta[i][st] = (data0>data1 ? data0 :data1 );
}
}
//----------------------------------------------------------------------------------------------------------------
double temp0[nstates] = {0} ;
double temp1[nstates] ={0 };
double so_temp [frame_size+3] = {0};
for (int i=0; i<frame_size +3; i++)
{
int lstate_temp1, lstate_temp2 ;
for (int st = 0 ; st<nstates; st++)
{
lstate_temp1 = lstate[0][st] -1 ;
lstate_temp2 = lstate[1][st] -1;
temp0[st] = gamma0[i][st] + alpha[i][lstate_temp1] + beta[i+1][st] ;
temp1[st] = gamma1[i][st] + alpha[i][lstate_temp2] + beta[i+1][st] ;
}
so_temp [i] = maxVect(temp1,nstates) - maxVect(temp0, nstates) ;
}
//----------------------------------------------------------------------------
for (int i =0; i<frame_size+3; i++)
{
so[i] = so_temp[i] ;
}
for (int i = 0; i<frame_size +3; i++)
{
e_info[i] = so[i] - pri_info[i] - Lc * input[0][i] ;
}
}
double maxVect(const double vect[], int vect_len){
double max = vect[0] ;
int i = 1 ;
while (i<vect_len)
{
if(vect[i]>=max ){
max = vect[i] ;
}
i++ ;
}
return max ;
}