📄 encclass.cpp
字号:
// encoder.cpp: implementation of the encoder class.
//
//////////////////////////////////////////////////////////////////////
#include "encClass.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
encoder::encoder()
{
}
encoder::~encoder()
{
}
/*================================================================*
Function: bitnsc
bit operation for the nsc(non systemetic conv) encoder
Input
x - input bit
y - output bits(n0 bits)
in_state - the state of register at input time
out_state - the state of register at output time
gen - the generate of rsc, in the form of [1][k0]-array,
for example: g=[1 1 1; 1 0 1]==> gen={7,5}
n0 - number of rows of 'g', ex. g[2][3], n0=2
k0 - number of cols of 'g' ex. k0=3
Output
y, out_state
Return
Non
*=================================================================*/
void encoder::bitnsc(int x, int *y, int *in_state, int *out_state,
int *gen, int n0, int k0)
{
int i,j;
int *g = new int[n0*k0];
NULLDetection(g);
int *gtemp;
int lreg=k0-1;
int temp;
//Convert gen to bit array form
for (i=0; i<n0; i++)
{
gtemp = dec2bin(gen[i],k0);
for (j=0; j<k0; j++)
{
g[i*k0+j] = gtemp[j];
}
}
for (i=0; i<n0; i++)
{
y[i] = g[i*k0+0]*x;
for (j=1; j<k0; j++)
{
temp = g[i*k0+j]*in_state[j-1];
y[i] ^= temp;
}
}
out_state[0] = x;
for (i=1; i<lreg; i++)
{
out_state[i] = in_state[i-1];
}
//encode ending, free space
delete []g;
}
/*================================================================*
Function: nsc
bit operation for the rsc encoder
Input
data - input bit sequence
code - output bit sequence bits, it's length should be according to
index; ex. index=1, length of code should be n0*(lreg+length)
index=0, ........................ n0*length
in_state - the state of register at input time
out_state - the state of register at output time
length - length of data
gen - the generate of rsc, in the form of [1][k0]-array,
for example: g=[1 1 1; 1 0 1]==> gen={7,5}
n0 - number of rows of 'g', ex. g[2][3], n0=2
k0 - number of cols of 'g' ex. k0=3
index - '1', the register'final state return to all-zero
'0', don't return to all-zero state
Output
code, out_state
Return
Non
*=================================================================*/
void encoder::nsc(int *data, int *code, int *in_state, int *out_state,
int length, int *gen, int n0, int k0, int index)
{
int i,j;
int lreg = k0-1;
int *temp_in = new int[lreg];
NULLDetection(temp_in);
int *temp_out = new int[lreg];
NULLDetection(temp_out);
int *y = new int[n0];
NULLDetection(y);
for (i=0; i<lreg; i++) //将输入状态赋值给临时状态量
{
temp_in[i] = in_state[i];
}
//编码开始
for (i=0; i<length; i++)
{
bitnsc(data[i], y, temp_in, temp_out, gen, n0, k0);
for (j=0; j<lreg; j++)
{
temp_in[j] = temp_out[j];
}
for (j=0; j<n0; j++) //将输出编码存放到code数组
{
code[i*n0+j] = y[j];
}
}
if (index == 1) //添加尾比特
{
for (i=0; i<lreg; i++)
{
bitnsc(0, y, temp_in, temp_out, gen, n0, k0);
for (j=0; j<lreg; j++)
{
temp_in[j] = temp_out[j];
}
for (j=0; j<n0; j++) //将输出编码存放到code数组
{
code[(length+i)*n0+j] = y[j];
}
}
}
for (i=0; i<lreg; i++) //将临时状态量赋值给输出状态
{
out_state[i] = temp_out[i] ;
}
//encode ending, free space
delete []temp_in;
delete []temp_out;
delete []y;
}
/*==================================================================*
Function: bitrsc
RSC Encoder in bit form
Type:(n,1,m) Conventional Enc
Input:
x - input bit, signal form is 1/0
y - output bit
in_state - the state of register at input time
out_state - the state of register at output time
gen - the generate of rsc, in the form of [1][k0]-array,
for example: g=[1 1 1; 1 0 1]==> gen={7,5}
g=[gb,gf];gb--back generator, gf--forward generator
n0 - number of rows of 'g', ex. g[2][3], n0=2
k0 - number of cols of 'g' ex. k0=3
Onput:
y----output bit, the forward path output, 1/0
out_state----out state of registers, form:[0 1]
Return:
y
*==================================================================*/
int encoder::bitrsc(int x, int *in_state, int *out_state,
int *gen, int n0, int k0)
{
int i,j;
int lreg=k0-1;
int y,yb,yf;
int *gtemp;
int *g = new int[n0*k0];
NULLDetection(g);
//Convert gen to bit array form
for (i=0; i<n0; i++)
{
gtemp = dec2bin(gen[i],k0);
for (j=0; j<k0; j++)
{
g[i*k0+j] = gtemp[j];
}
}
//后向比特RSC编码yb
yb = g[0]*x;
for (j=1; j<k0; j++)
{
yb ^= g[j]*in_state[j-1];
}
//前向比特RSC编码yf
yf = g[k0+0]*yb;
for (j=1; j<k0; j++)
{
yf ^= g[k0+j]*in_state[j-1];
}
//前向编码比特yf作为输出比特
y = yf;
//Update register
out_state[0] = yb;
for (i=1; i<lreg; i++)
{
out_state[i] = in_state[i-1];
}
//bit encode ending, free space
delete []g;
//return value
return y;
}
/*==================================================================*
Function: rsc
RSC Encoder
Type:(n,1,m) Conventional Enc
Input:
data - input bits, signal form is 1/0
code - output bits
in_state - the state of register at input time
out_state - the state of register at output time
length - the length of "data"
gen - the generate of rsc, in the form of [1][k0]-array,
for example: g=[1 1 1; 1 0 1]==> gen={7,5}
g=[gb,gf];gb--back generator, gf--forward generator
n0 - number of rows of 'g', ex. g[2][3], n0=2
k0 - number of cols of 'g' ex. k0=3
index - index = 1, the final state of reg is return to all-zero
index = 0, just keep the length of output equal to the
length of input, and return the end state of reg
syscode - syscode = 0, just output the forward output;
syscode = 1, output the info bits and output in systemetic form;
Onput:
code----output bits
out_state----out state of registers, form:[0 1]
tail - added tail bits, if index = 1, return tail bits
if index = 0, tail bits not used
Return:
tail
*==================================================================*/
int *encoder::rsc(int *data, int *code, int *in_state, int *out_state,
int length, int *gen, int n0, int k0, int index, int syscode)
{
int i,j;
int lreg = k0-1, Lin;
int yb,yf;
int *tail=new int[lreg];
NULLDetection(tail);
int *temp_in = new int[lreg];
NULLDetection(temp_in);
int *temp_out = new int[lreg];
NULLDetection(temp_out);
int *g = new int[n0*k0];
NULLDetection(g);
int *gtemp;
//计算输入实际输入信息比特长度
if (index == 1)
{
Lin = length +lreg;
}
else if(index ==0)
{
Lin = length;
}
int *y = new int[Lin];
NULLDetection(y);
//set tail[] to all-zero
for (i=0; i<lreg; i++)
{
tail[i] = 0;
}
//Convert gen to bit array form
for (i=0; i<n0; i++)
{
gtemp = dec2bin(gen[i],k0);
for (j=0; j<k0; j++)
{
g[i*k0+j] = gtemp[j];
}
}
//将输入状态赋值给临时状态量
for (i=0; i<lreg; i++)
{
temp_in[i] = in_state[i];
}
//进行信息比特部分的RSC编码
for (i=0; i<length; i++)
{
y[i]=bitrsc(data[i],temp_in,temp_out,gen,n0,k0);
//cout<<"info bit encode out y"<<i<<" = "<<y[i]<<endl;
// Update bitrsc in_state(viz. temp_in)
for (j=0; j<lreg; j++)
{
temp_in[j] = temp_out[j];
}
}
//将临时状态量赋值给输出状态
for (i=0; i<lreg; i++)
{
out_state[i] = temp_out[i] ;
}
//判断是否添加尾比特
if (index == 1) //添加尾比特
{
//cout<<"tail begin"<<endl;
yb = 0;
for (i=0; i<lreg; i++)
{
//Add input tail bits tail[lreg]
//NOTE: 目前主要针对n0=2的情况,所以此处只考虑g[0~k0-1],g[k0~2*k0-1]
tail[i] = g[0]*yb;
for (j=1; j<k0; j++)
{
tail[i] ^= g[j]*out_state[j-1];
}
//Count output tail bits
yf = g[k0+0]*yb;
for (j=1; j<k0; j++)
{
yf ^= g[k0+j]*out_state[j-1];
}
//Evaluate the code[length:length+lreg-1]
y[length+i] = yf;
//Update the out_state
for (j=lreg-1; j>0; j--)
{
out_state[j] = out_state[j-1];
}
out_state[0] = yb;
}
}
//判断是否采用系统码形式输出
if (syscode == 1) //系统码
{
for (i=0; i<Lin; i++)
{
if (i>=length)
{
code[i*n0] = tail[i-length];
}
else
{
code[i*n0] = data[i];
}
code[i*n0+1] = y[i];
}
}
else if(syscode == 0)
{
for (i=0; i<Lin; i++)
{
code[i] = y[i];
}
}
//encode ending, free space
delete []temp_in;
delete []temp_out;
delete []g;
delete []y;
return tail;
}
/*==============================================================*
Function: turbo
Note : the length of code should equal to
(puncture+2)*(length(data)+lreg)
the length of interleaver is length(data)+lreg
Input:
data - input bits, signal form is 1/0
code - output bits
in_s1 - the state of RSC1 register at input time
in_s2 - the state of RSC2 register at input time
length - the length of "data"
gen - the generate of rsc, in the form of [1][k0]-array,
for example: g=[1 1 1; 1 0 1]==> gen={7,5}
g=[gb,gf];gb--back generator, gf--forward generator
n0 - number of rows of 'g', ex. g[2][3], n0=2
k0 - number of cols of 'g' ex. k0=3
puncture - '0'=puncture,rate = 1/2
'1'=un-puncture, rate = 1/3
interleaver - interleaver array
Output:
code
Return:
Non
================================================================*/
void encoder::turbo(int *data, int *code, int *in_s1, int *in_s2,
int length, int *gen, int n0, int k0,
int puncture, int *interleaver)
{
int i,j;
int lreg=k0-1;
int L = length+lreg;
int *tail1,*tail2;
int *out_s = new int[lreg];
NULLDetection(out_s);
int *rsc1_in = new int[L]; // Input of RSC1
NULLDetection(rsc1_in);
int *rsc2_in = new int[L]; // Input of RSC2
NULLDetection(rsc2_in);
int *xp1 = new int[L]; // Output of RSC1
NULLDetection(xp1);
int *xp2 = new int[L]; // Output of RSC2
NULLDetection(xp2);
// RSC-1
// Added tail bits, register should return to all-zero state
// rsc(..) return valid tail bits
// the input para. "length" is the length of data viz. length
// index = 1;
// syscode = 0;
tail1 = rsc(data, xp1, in_s1, out_s, length, gen, n0, k0, 1, 0);
// Generate rsc1_in according to the output tail bits
for (i=0; i<L; i++)
{
if (i >= length)
{
rsc1_in[i] = tail1[i-length];
}
else
{
rsc1_in[i] = data[i];
}
}
// Interleaver
for (i=0; i<L; i++)
{
j = interleaver[i];
rsc2_in[i] = rsc1_in[j];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -