C语言编程实现 输入:长度为4的任意二进制序列。 输出:输入数据经Hamming(7,4)编码器编码之后,通过二元对称信道模拟器(错误概率为0.1)传输后,再经过Hamming(7,4)译码器译码输出得到信宿端的长度为4的二进制序列。
时间: 2024-01-21 17:18:20 浏览: 144
c代码-输出二进制数
以下是C语言实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 7 //编码器编码后的长度
#define K 4 //编码器编码前的长度
#define P 0.1 //错误概率
//生成随机01序列
void generate_random_sequence(int seq[], int len) {
srand((unsigned int) time(NULL));
for (int i = 0; i < len; i++) {
seq[i] = rand() % 2;
}
}
//计算校验位
int calculate_parity_bit(int seq[], int len, int start) {
int sum = 0;
for (int i = start; i < len; i += start * 2) {
for (int j = 0; j < start; j++) {
if (seq[i + j] == 1) {
sum++;
}
}
}
return sum % 2;
}
//Hamming编码器
void hamming_encoder(int input[], int output[]) {
for (int i = 0; i < K; i++) {
output[i + 3] = input[i];
}
output[0] = calculate_parity_bit(output, N, 1);
output[1] = calculate_parity_bit(output, N, 2);
output[3] = calculate_parity_bit(output, N, 4);
output[7] = calculate_parity_bit(output, N, 8);
}
//二元对称信道模拟器
void binary_symmetric_channel(int input[], int output[], int len, float error_prob) {
srand((unsigned int) time(NULL));
for (int i = 0; i < len; i++) {
if ((float) rand() / RAND_MAX < error_prob) { //以一定概率翻转每一位
output[i] = (input[i] + 1) % 2;
} else {
output[i] = input[i];
}
}
}
//计算Hamming距离
int calculate_hamming_distance(int seq1[], int seq2[], int len) {
int count = 0;
for (int i = 0; i < len; i++) {
if (seq1[i] != seq2[i]) {
count++;
}
}
return count;
}
//查找错误位置
int find_error_position(int syndrome[]) {
if (syndrome[0] == 0 && syndrome[1] == 0 && syndrome[2] == 0) {
return 0;
}
if (syndrome[0] == 0 && syndrome[1] == 0 && syndrome[2] == 1) {
return 4;
}
if (syndrome[0] == 0 && syndrome[1] == 1 && syndrome[2] == 0) {
return 2;
}
if (syndrome[0] == 0 && syndrome[1] == 1 && syndrome[2] == 1) {
return 6;
}
if (syndrome[0] == 1 && syndrome[1] == 0 && syndrome[2] == 0) {
return 1;
}
if (syndrome[0] == 1 && syndrome[1] == 0 && syndrome[2] == 1) {
return 5;
}
if (syndrome[0] == 1 && syndrome[1] == 1 && syndrome[2] == 0) {
return 3;
}
if (syndrome[0] == 1 && syndrome[1] == 1 && syndrome[2] == 1) {
return 7;
}
return -1;
}
//Hamming译码器
void hamming_decoder(int input[], int output[]) {
int syndrome[3] = {0}; //计算校验位
syndrome[0] = (input[0] + input[2] + input[4] + input[6]) % 2;
syndrome[1] = (input[1] + input[2] + input[5] + input[6]) % 2;
syndrome[2] = (input[3] + input[4] + input[5] + input[6]) % 2;
int error_position = find_error_position(syndrome); //查找错误位置
if (error_position == -1) { //没有出错
for (int i = 0; i < K; i++) {
output[i] = input[i + 3];
}
} else { //出错
input[error_position + 3] = (input[error_position + 3] + 1) % 2; //纠正错误
for (int i = 0; i < K; i++) {
output[i] = input[i + 3];
}
}
}
int main() {
int input[K] = {0}; //输入数据
int encoded[N] = {0}; //编码后的数据
int received[N] = {0}; //接收到的数据
int decoded[K] = {0}; //译码后的数据
printf("请输入长度为4的任意二进制序列:");
for (int i = 0; i < K; i++) {
scanf("%d", &input[i]);
}
hamming_encoder(input, encoded); //编码
printf("编码后的数据:");
for (int i = 0; i < N; i++) {
printf("%d", encoded[i]);
}
printf("\n");
binary_symmetric_channel(encoded, received, N, P); //模拟信道传输
printf("接收到的数据:");
for (int i = 0; i < N; i++) {
printf("%d", received[i]);
}
printf("\n");
hamming_decoder(received, decoded); //译码
printf("译码后的数据:");
for (int i = 0; i < K; i++) {
printf("%d", decoded[i]);
}
printf("\n");
int dist = calculate_hamming_distance(encoded, received, N); //计算汉明距离
printf("编码前后的汉明距离:%d\n", dist);
return 0;
}
```
运行程序后,输入长度为4的任意二进制序列,程序将自动进行Hamming编码器编码、二元对称信道模拟器传输、Hamming译码器译码,并输出编码前后的汉明距离以及译码后的数据。
阅读全文