📄 harq.cpp
字号:
#include "parameter_sets.h"
const int PuncPos[15] = {17, 19, 21, 23, 25, 27, 29, 31, 18, 24, 22, 28, 30, 20, 26};
int * HarqF(struct BasicParaS * ctrl, int * input, double *chanelout, double Eb_No_dB)
{
int i,j,k,h,k1,h1,k2,h2,n1,n2;
int * decodeOut1,* decodeOut2;
struct Complex * moduOut1,* moduOut2, * chanOut1,* chanOut2;
double * demodOut1,* demodOut2;
double ber,ber1;
int * temp1, * temp2, * temp6, * temp;
double * temp3 ,* temp5,* temp7;
int numIter; //计算迭代次数
numIter = ctrl->numIter;
temp1 = new int[ctrl->numOutBits];
//将编码出的母码赋值给temp1
for(i=0; i<ctrl->numOutBits; i++)
{
*(temp1 + i)=ctrl->motherCode1[i];
}
if (ctrl->mode == 1)
{
printf("The temp1 bits are ..\n");
k = ctrl->numOutBits/36;
h = ctrl->numOutBits%36;
for (i=0; i<k; i++)
{
for (j=0; j<36; j++)
{
printf("%2d", *(temp1+i*36+j));
}
printf("\n");
}
for (i=0; i<h; i++)
{
printf("%2d", *(temp1+k*36+i));
}
if (h)
{
printf("\n");
}
}
k1 = (ctrl->numChk-ctrl->codeM)/ctrl->zfactor; //k=576时该值为10
h1 = (ctrl->numChk-ctrl->codeM)%ctrl->zfactor; ////k=576时该值为24
if (k1 > 14)
exit(EXIT_FAILURE);
ctrl->r = 1.5; //标记出2/3码率打掉的校验比特
ctrl->codeN = (int)(ctrl->r*ctrl->codeK); //重新计算N
ctrl->codeM = ctrl->codeN - ctrl->codeK; //重新计算M
k2 = (ctrl->numChk-ctrl->codeM)/ctrl->zfactor; //K=576时该值为8
h2 = (ctrl->numChk-ctrl->codeM)%ctrl->zfactor; //K=576时该值为0
if (k2 > 14)
exit(EXIT_FAILURE);
n1=h1 +(k1-k2)*ctrl->zfactor- h2;
temp2 = new int[n1]; //h1 +(k1-k2)*ctrl->zfactor- h2为两个码率相差的比特数
i=0;
for(h=h2; h<ctrl->zfactor; h++)
{
*(temp2 + i)=*(temp1 +PuncPos[k2]*ctrl->zfactor + h);
i++;
}
for(k=k2+1; k<k1; k++)
{
for(j=0; j<ctrl->zfactor; j++)
{
*(temp2 + i)=* (temp1 +PuncPos[k]*ctrl->zfactor+j);
i++;
}
}
for(j=0; j<h1;j++)
{
*(temp2+i)=* (temp1 +PuncPos[k1]*ctrl->zfactor+j);
i++;
}
if (ctrl->mode == 1)
{
printf("The temp2 bits are ..\n");
k = (n1)/36;
h = (n1)%36;
for (i=0; i<k; i++)
{
for (j=0; j<36; j++)
{
printf("%2d", *(temp2+i*36+j));
}
printf("\n");
}
for (i=0; i<h; i++)
{
printf("%2d", *(temp2+k*36+i));
}
if (h)
{
printf("\n");
}
}
//temp2进行BPSK调制,结果存放在moduOut1
if ((moduOut1 = ModulatingF(ctrl, temp2,n1)) == NULL)
exit(EXIT_FAILURE);
delete [] temp2;
//通过信道,结果存放在chanOut1
if ((chanOut1 = ChanAwgnF(ctrl, moduOut1, Eb_No_dB)) == NULL)
exit(EXIT_FAILURE);
//解调
if ((demodOut1 = DemodulatingF(ctrl, chanOut1, Eb_No_dB)) == NULL)
exit(EXIT_FAILURE);
delete [] chanOut1;
//把demoduOut1插入到chanelout中,存放在temp3中
temp3=new double[ctrl->codeN];
for(i=0; i<17*ctrl->zfactor; i++)
{
*(temp3 + i) = * (chanelout+i);
}
for(i=0; i<ctrl->zfactor; i++)
{
*(temp3+17*ctrl->zfactor + i) = * (demodOut1 + i);
*(temp3+18*ctrl->zfactor + i) = * (chanelout + 17*ctrl->zfactor + i);
}
for(i=0; i<h1; i++)
{
*(temp3 + 19*ctrl->zfactor +i) = * (demodOut1 + 2*ctrl->zfactor +i);
}
for( i=0; i<ctrl->zfactor-h1; i++)
{
*(temp3 + 19*ctrl->zfactor +h1 +i) = * (chanelout + 18*ctrl->zfactor +i);
}
for(i=0; i<ctrl->zfactor; i++)
{
*(temp3 + 20*ctrl->zfactor +i ) = *(demodOut1 + ctrl->zfactor +i);
}
for(i=0; i<ctrl->codeN-21*ctrl->zfactor; i++)
{
*(temp3+21*ctrl->zfactor +i )= * (chanelout + 18*ctrl->zfactor+ ctrl->zfactor-h1 + i);
}
if (ctrl->mode == 1)
{
printf("The temp3 bits are ..\n");
k = ctrl->codeN/24;
h = ctrl->codeN%24;
for (i=0; i<k; i++)
{
for (j=0; j<24; j++)
{
printf("%2d", *(temp3+i*24+j));
}
printf("\n");
}
for (i=0; i<h; i++)
{
printf("%2d", *(temp3+k*24+i));
}
if (h)
{
printf("\n");
}
}
temp5= new double[ctrl->codeN];
for(i=0; i<ctrl->codeN;i++)
{
*(temp5+i)=*(temp3+i);
}
if ((decodeOut1 = DecodingF(ctrl, temp3)) == NULL)
exit(EXIT_FAILURE);
numIter += ctrl->numIter;
delete [] demodOut1;
delete [] temp3;
delete [] temp1;
if ((ber = CountBerF(ctrl, input, decodeOut1)) < 0)
exit(EXIT_FAILURE);
printf("the BER1 is %E\n", ber);
ctrl->ber1 = ber;
if(ber==0)
{
ctrl->numIter = numIter;
return decodeOut1;
}
else
{
delete [] decodeOut1;
//第二次重传
temp6 = new int[k2*ctrl->zfactor]; //k2*ctrl->zfactor为两个码率相差的比特数
n2 = k2*ctrl->zfactor;
temp = new int[ctrl->numOutBits];
for(i=0; i<ctrl->numOutBits; i++)
{
*(temp + i)=ctrl->motherCode1[i];
}
i=0;
for(k=0; k<k2; k++)
{
for(j=0; j<ctrl->zfactor; j++)
{
*(temp6 + i)=* (temp +PuncPos[k]*ctrl->zfactor+j);
i++;
}
}
if (ctrl->mode == 1)
{
printf("\nThe temp6 bits are ..\n");
k = k2*ctrl->zfactor/24;
h = k2*ctrl->zfactor%24;
for (i=0; i<k; i++)
{
for (j=0; j<24; j++)
{
printf("%2d", *(temp6+i*24+j));
}
printf("\n");
}
for (i=0; i<h; i++)
{
printf("%2d", *(temp6+k*24+i));
}
if (h)
{
printf("\n");
}
}
//temp6进行BPSK调制,结果存放在moduOut2
if ((moduOut2 = ModulatingF(ctrl, temp6,k2*ctrl->zfactor)) == NULL)
exit(EXIT_FAILURE);
delete [] temp6;
//通过信道
if ((chanOut2 = ChanAwgnF(ctrl, moduOut2, Eb_No_dB)) == NULL)
exit(EXIT_FAILURE);
//解调
if ((demodOut2 = DemodulatingF(ctrl, chanOut2, Eb_No_dB)) == NULL)
exit(EXIT_FAILURE);
delete [] chanOut2;
ctrl->r = 2; //标记出1/2码率打掉的校验比特
ctrl->codeN = (int)(ctrl->r*ctrl->codeK); //重新计算N
ctrl->codeM = ctrl->codeN - ctrl->codeK; //重新计算M
//将demodOut2插入到temp5中
temp7=new double[ctrl->codeK*2];
for(i=0; i<17*ctrl->zfactor; i++)
{
*(temp7 + i) = * (temp5+i);
}
for(i=0; i<7; i++)
{
for(j=0; j<ctrl->zfactor; j++)
{
*(temp7 + (17+2*i)*ctrl->zfactor + j) = * (demodOut2+i*ctrl->zfactor +j);
*(temp7 + (18+2*i)*ctrl->zfactor + j) = * (temp5+(17+i)*ctrl->zfactor+j);
}
}
for(i=0; i<ctrl->zfactor;i++)
{
*(temp7 + 31*ctrl->zfactor+i)= * (demodOut2 + 7*ctrl->zfactor+i);
}
if (ctrl->mode == 1)
{
printf("The temp7 bits are ..\n");
k = ctrl->numOutBits/24;
h = ctrl->numOutBits%24;
for (i=0; i<k; i++)
{
for (j=0; j<24; j++)
{
printf("%2d", *(temp7+i*24+j));
}
printf("\n");
}
for (i=0; i<h; i++)
{
printf("%2d", *(temp7+k*24+i));
}
if (h)
{
printf("\n");
}
}
if ((decodeOut2 = DecodingF(ctrl, temp7)) == NULL)
exit(EXIT_FAILURE);
delete [] demodOut2;
delete [] temp7;
delete [] temp5;
delete [] temp;
if ((ber1 = CountBerF(ctrl, input, decodeOut2)) < 0)
exit(EXIT_FAILURE);
printf("the BER2 is %E\n", ber1);
numIter += ctrl->numIter;
ctrl->numIter=numIter;
return decodeOut2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -