WuYufei的Turbo编译码c语言实现
时间: 2023-12-07 13:04:00 浏览: 242
以下是WuYufei的Turbo编码和译码的C语言实现:
Turbo编码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1024
#define K 512
#define M 4
#define PUNCTURE 3
int main()
{
int i, j, k;
int input[N];
int output[K];
int state1, state2, state3;
int out1, out2, out3;
int temp1, temp2, temp3;
int *ptr;
int puncture[PUNCTURE] = {1, 2, 3};
//输入比特流
printf("Input bit stream:\n");
for(i = 0; i < N; i++)
scanf("%d", &input[i]);
//初始化状态
state1 = 0;
state2 = 0;
state3 = 0;
//Turbo码编码
for(i = 0; i < K; i += 2)
{
//第一个卷积码器
temp1 = (state1 << 1) | input[i];
temp2 = (state2 << 1) | input[i + 1];
out1 = (temp1 ^ temp2) & 1;
state1 = (temp1 >> 1) & 7;
state2 = (temp2 >> 1) & 7;
//第二个卷积码器
temp1 = (state2 << 1) | input[i];
temp2 = (state3 << 1) | input[i + 1];
out2 = (temp1 ^ temp2) & 1;
state2 = (temp1 >> 1) & 7;
state3 = (temp2 >> 1) & 7;
//交织器
ptr = output + i;
*ptr = out1;
*(ptr + 1) = out2;
//打孔
for(j = 0; j < PUNCTURE; j++)
{
if(i + puncture[j] < K)
*(ptr + puncture[j]) = -1;
}
}
//输出编码比特流
printf("Output code bit stream:\n");
for(i = 0; i < K; i++)
{
if(output[i] == -1)
printf("-");
else
printf("%d", output[i]);
}
printf("\n");
return 0;
}
```
Turbo译码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1024
#define K 512
#define M 4
#define PUNCTURE 3
#define SOFT_DECISION 1
int main()
{
int i, j, k;
int input[K];
int output[N];
int state1, state2, state3;
int out1, out2, out3;
int temp1, temp2, temp3;
int *ptr;
int puncture[PUNCTURE] = {1, 2, 3};
//输入编码比特流
printf("Input code bit stream:\n");
for(i = 0; i < K; i++)
{
char c = getchar();
while(c == '\n' || c == '\r' || c == ' ')
c = getchar();
if(c == '-')
input[i] = -1;
else
input[i] = c - '0';
}
//初始化状态
state1 = 0;
state2 = 0;
state3 = 0;
//Turbo码译码
for(i = 0; i < N; i++)
{
//第一个卷积码器
temp1 = (state1 << 1) | input[2 * (i % K)];
temp2 = (state2 << 1) | input[2 * (i % K) + 1];
out1 = (temp1 ^ temp2) & 1;
state1 = (temp1 >> 1) & 7;
state2 = (temp2 >> 1) & 7;
//第二个卷积码器
temp1 = (state2 << 1) | input[2 * (i % K)];
temp2 = (state3 << 1) | input[2 * (i % K) + 1];
out2 = (temp1 ^ temp2) & 1;
state2 = (temp1 >> 1) & 7;
state3 = (temp2 >> 1) & 7;
//交织器
ptr = output + i;
*ptr = out1;
*(ptr + M) = out2;
//打孔
for(j = 0; j < PUNCTURE; j++)
{
if(i % M == puncture[j])
*(ptr - M + puncture[j]) = -1;
}
}
//输出译码比特流
printf("Output decoded bit stream:\n");
for(i = 0; i < N; i++)
{
if(output[i] == -1)
printf("-");
else
printf("%d", output[i]);
}
printf("\n");
return 0;
}
```
其中,Turbo编码和Turbo译码的区别在于输入的数据不同,Turbo编码输入原始比特流,Turbo译码输入编码比特流。同时,在Turbo译码中,还需要对输入的编码比特流进行去打孔和交织处理,以还原出原始比特流。
阅读全文