컴공생의 다이어리

[c] 2-layer channel coding Encoder(인코더)와 Decoder(디코더) 본문

Development/C & C++

[c] 2-layer channel coding Encoder(인코더)와 Decoder(디코더)

컴공 K 2021. 6. 11. 00:01

2-layer channel coding Encoder(인코더) Code

 - Encoder의 입력은 16bit, 출력은 42bit의 codeword

// 2-layer Channel Coding Encoder
#include<stdio.h>
#include<string.h>

int main() {
	char input[17] = { 0 };
	int input_shift[24] = { 0, };
	int g[9] = { 1,0,0,0,0,0,1,1,1 };
	char temp[25] = { 0 };
	printf("--------- 2-layer Channel Coding Encoder ---------\n");
	printf("Input: ");
	scanf("%s",input);

	///////////////////// crc-8 encoder /////////////////////
	for (int i = 0; input[i]; i++) {
		input_shift[i] = input[i] - '0';
	}

	for (int i = 0; i < 16; i++) {
		if (input_shift[i] == 1) {
			for (int j = 0; j < 9; j++) {
				input_shift[i + j] = g[j] ^ input_shift[i + j];
			}
		}
	}
	sprintf(temp, "%s%d%d%d%d%d%d%d%d", input, input_shift[16], input_shift[17], input_shift[18], input_shift[19],
		input_shift[20], input_shift[21], input_shift[22], input_shift[23]);
	/////////////////////////////////////////////////////////
	

	
	///////////////// (7,4) hamming encoder /////////////////
	int G_T[7][4] = {
			{1,0,0,0},	{0,1,0,0},	{0,0,1,0},
			{0,0,0,1},	{1,1,1,0},	{0,1,1,1},	{1,0,1,1}
	};
	char before_result[6][8] = { 0 };
	char result[43] = { 0 };
	for (int k = 0; k < 6; k++) {
		char in[5] = {0};
		strncat(in, temp + k * 4, 4);
		int s[4] = { 0, };
		int t[7] = { 0, };
		char result[8] = { 0 };
		for (int i = 0; in[i]; i++)
			s[i] = in[i] - '0';
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 4; j++) {
				t[i] += (G_T[i][j] * s[j]);
			}
		}
		for (int i = 0; i < 7; i++)
			t[i] %= 2;
		sprintf(before_result[k], "%d%d%d%d%d%d%d", t[0], t[1], t[2], t[3], t[4], t[5], t[6]);
	}
	/////////////////////////////////////////////////////////

	sprintf(result, "%s%s%s%s%s%s", before_result[0], before_result[1], before_result[2], before_result[3], before_result[4], before_result[5]);
	printf("Output: %s\n", result);
	return 0;
}

 

 

2-layer channel coding Decoder(디코더) Code

 - Decoder의 입력은 42bit 의 codeword, 출력은 Error가 없거나 수정되었을 때는 16bit를 출력하고 수정되지 않는 Error가 있을 때는 "ERROR!"로 출력

// 2-layer Channel Coding Decoder
#include<stdio.h>
#include<string.h>

int main() {
	char input[43] = { 0 };

	printf("--------- 2-layer Channel Coding Decoder ---------\n");
	printf("Input: ");
	scanf("%s",input);
	
	///////////////// (7,4) hamming decoder /////////////////
	int H[3][7] = {
		{1,1,1,0,1,0,0},
		{0,1,1,1,0,1,0},
		{1,0,1,1,0,0,1}
	};
	int syndrome[8] = { 0,7,6,4,5,1,2,3 };
	char temp_ham[6][5] = { 0 };
	for (int k = 0; k < 6; k++) {
		char in[8] = { 0 };
		strncat(in, input + k * 7, 7);
		int r[7] = { 0, };
		int z[3] = { 0, };
		for (int i = 0; in[i]; i++) {
			r[i] = in[i] - '0';
		}
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 7; j++)
				z[i] += (H[i][j] * r[j]);
		}
		for (int i = 0; i < 3; i++)
			z[i] %= 2;
		int temp = z[0] * 4 + z[1] * 2 + z[2];
		if (temp != 0) {
			if (r[syndrome[temp] - 1] == 1)
				r[syndrome[temp] - 1] = 0;
			else
				r[syndrome[temp] - 1] = 1;
		}
		sprintf(temp_ham[k], "%d%d%d%d", r[0], r[1], r[2], r[3]);
	}
	char crc_de_input[25] = { 0 };
	sprintf(crc_de_input, "%s%s%s%s%s%s", temp_ham[0], temp_ham[1], temp_ham[2], temp_ham[3], temp_ham[4], temp_ham[5]);
	/////////////////////////////////////////////////////////


	///////////////////// crc-8 decoder /////////////////////
	int input_int[24] = { 0, };
	int g[9] = { 1,0,0,0,0,0,1,1,1 };
	char str_temp[9] = { 0 };
	for (int i = 0; crc_de_input[i]; i++) {
		input_int[i] = crc_de_input[i] - '0';
	}

	for (int i = 0; i < 16; i++) {
		if (input_int[i] == 1) {
			if (input_int[i] == 1) {
				for (int j = 0; j < 9; j++) {
					input_int[i + j] = g[j] ^ input_int[i + j];
				}
			}
		}
	}
	sprintf(str_temp, "%d%d%d%d%d%d%d%d", input_int[16], input_int[17], input_int[18], input_int[19],
		input_int[20], input_int[21], input_int[22], input_int[23]);
	int res = strcmp(str_temp, "00000000\0");
	/////////////////////////////////////////////////////////


	if (res == 0) {
		char p[17] = { 0 };
		strncat(p, crc_de_input, 16);
		printf("Output: %s\n", p);
	}
	else {
		printf("Output: ERROR!\n");
	}
	return 0;
}
728x90
Comments