컴공생의 다이어리
[c] 2-layer channel coding Encoder(인코더)와 Decoder(디코더) 본문
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
반응형
'Development > C & C++' 카테고리의 다른 글
[c] (7,4)Hamming Code Encoder(인코더)와 Decoder(디코더) (0) | 2021.06.10 |
---|---|
[c] CRC(cyclic redundancy check, 순환 중복 검사) Encoder(인코더)와 Decoder(디코더) (0) | 2021.06.08 |
[c/c++] Ubuntu Makefile 만들기 (0) | 2021.06.01 |
[c++] 원본 행렬(original matrix)과 전치 행렬(transposed matrix) (0) | 2021.03.28 |
[c/c++] 연산자 우선순위(Operator Priority) (0) | 2021.03.18 |
Comments