-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
188 lines (149 loc) · 5.99 KB
/
main.c
File metadata and controls
188 lines (149 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Import configuration
#include "config.h"
//Import all modules and libs
#include "../lib_data/data.h"
#include "../lib_utilities/utilities.h"
#include "src/code_matrix.h"
#include "src/matrix.h"
int main()
{
struct Matrix_config* conf = cmatrix_generate_config();
// Display the generator and control matrix
printf("Control matrix: \n");
matrix_show(conf->CONTROL_MATRIX);
printf("Generator matrix: \n");
matrix_show(conf->GENERATOR_MATRIX);
// Generation of the word to encode
struct Matrix* dte = matrix_generate(K, 1);
matrix_set(dte, 3, 1, 1);
matrix_set(dte, 1, 1, 1);
// Display
printf("Data to encode : %d elements\n", dte->data->data_number);
matrix_show_word(dte);
// Encoding
struct Matrix* d = cmatrix_encode(dte, conf);
printf("Data encoded : %d elements\n", d->data->data_number);
matrix_show_word(d);
// Add an error
matrix_set(d, 2, 1, 1);
matrix_set(d, 5, 1, 1);
printf("Data modified : %d elements\n", d->data->data_number);
matrix_show_word(d);
// Correction
struct Matrix* r = cmatrix_syndrome(d, conf);
printf("\n\nCorrection\n-----------\n\n");
printf("Syndrome of the modified code : %d \n", matrix_word_to_int(r));
printf("Corrected matrix :\n", matrix_word_to_int(r));
matrix_show_word(cmatrix_correction(d, conf));
/*
if(!matrix_is_null(r))
{
uint8_t b = cmatrix_check_syndrome(r, conf);
printf("The bit %d is corrupted\n", b + 1);
}
else
printf("No bit corrupted\n");
printf("Corrected matrix :\n", matrix_word_to_int(r));
matrix_show_word(cmatrix_decode(d, conf));
matrix_free(dte);
matrix_free(d);
matrix_free(r);
cmatrix_free_config(conf);*/
/*
uint8_t synd_array[2048][3] = {0};
struct Matrix* syndrome_test_matrix = matrix_generate(conf->CONTROL_MATRIX->cols, 1);
struct Matrix* syndrome_result;
uint16_t syndrome;
uint16_t compteur = 0;
// Correction de seulement une erreur (valable pour tout les codes)
for(uint8_t i = 0; i < conf->CONTROL_MATRIX->cols; i++)
for(uint8_t j = 0; j <= i; j++)
for(uint8_t k = 0; k <= j; k++)
{
// Sets the "N - i" bit
matrix_set(syndrome_test_matrix, conf->CONTROL_MATRIX->cols - i, 1, 1);
if(j != i)
matrix_set(syndrome_test_matrix, conf->CONTROL_MATRIX->cols - j, 1, 1);
if(k != j)
matrix_set(syndrome_test_matrix, conf->CONTROL_MATRIX->cols - k, 1, 1);
// Computation of the syndrome
syndrome_result = cmatrix_syndrome(syndrome_test_matrix, conf);
syndrome = matrix_word_to_int(syndrome_result);
/*
printf("------------------\n\n(%d, %d, %d) : %d\n", i, j, k, syndrome);
matrix_show_word(syndrome_test_matrix);
// Adds the syndrome
synd_array[syndrome][0] = conf->CONTROL_MATRIX->cols - i;
synd_array[syndrome][1] = (j == i) ? 0 : conf->CONTROL_MATRIX->cols - j;
synd_array[syndrome][2] = (k == j) ? 0 : conf->CONTROL_MATRIX->cols - k;
// Resets the bit
// Sets the "N - i" bit
matrix_set(syndrome_test_matrix, conf->CONTROL_MATRIX->cols - i, 1, 0);
if(j != i)
matrix_set(syndrome_test_matrix, conf->CONTROL_MATRIX->cols - j, 1, 0);
if(k != j)
matrix_set(syndrome_test_matrix, conf->CONTROL_MATRIX->cols - k, 1, 0);
matrix_free(syndrome_result);
compteur++;
}
printf("\n\nTotal : %d; taille : %d", compteur, sizeof(synd_array));
matrix_free(syndrome_test_matrix);
/*
printf("\n\n\n");
printf("uint8_t SYNDROME_ARRAY[%d][3] PROGMEM = {", 1 << conf->CONTROL_MATRIX->rows);
for(uint16_t k = 0; k < 1 << conf->CONTROL_MATRIX->rows; k++)
if(k == (1 << conf->CONTROL_MATRIX->rows) - 1)
printf("{%d, %d, %d}", synd_array[k][0], synd_array[k][1], synd_array[k][2]);
else
printf("{%d, %d, %d}, ", synd_array[k][0], synd_array[k][1], synd_array[k][2]);
printf("};\n\n\n");
*/
/*
#if USED_CODE == CODE_HAMMING
printf("\nTest of the (%d, %d, 3) Hamming code\n--------------------------------\n\n", N, K);
#elif USED_CODE == CODE_REPETITION
printf("\nTest of the (%d, %d) repetition code\n--------------------------------\n\n", N, K);
#endif
// Display the generator and control matrix
printf("Control matrix: \n");
matrix_show(conf->CONTROL_MATRIX);
printf("Generator matrix: \n");
matrix_show(conf->GENERATOR_MATRIX);
printf("Syndromes array: \n");
for(uint8_t i = 0; i < (1 << conf->CONTROL_MATRIX->rows); i++)
printf("|%d : %d|\n", i, conf->SYNDROMES_ARRAY[i]);
printf("\n");
// Generation of the word to encode
struct Matrix* dte = matrix_generate(K, 1);
matrix_set(dte, 3, 1, 1);
matrix_set(dte, 1, 1, 1);
// Display
printf("Data to encode : %d elements\n", dte->data->data_number);
matrix_show_word(dte);
// Encoding
struct Matrix* d = cmatrix_encode(dte, conf);
printf("Data encoded : %d elements\n", d->data->data_number);
matrix_show_word(d);
// Add an error
data_set(4, 0, d->data);
printf("Data modified : %d elements\n", d->data->data_number);
matrix_show_word(d);
// Correction
struct Matrix* r = cmatrix_syndrome(d, conf);
printf("\n\nCorrection\n-----------\n\n");
printf("Syndrome of the modified code : %d \n", matrix_word_to_int(r));
if(!matrix_is_null(r))
{
uint8_t b = cmatrix_check_syndrome(r, conf);
printf("The bit %d is corrupted\n", b + 1);
}
else
printf("No bit corrupted\n");
printf("Corrected matrix :\n", matrix_word_to_int(r));
matrix_show_word(cmatrix_decode(d, conf));
matrix_free(dte);
matrix_free(d);
matrix_free(r);
cmatrix_free_config(conf);
return 0;*/
}