|
16 | 16 | * See the License for the specific language governing permissions and |
17 | 17 | * limitations under the License. |
18 | 18 | */ |
19 | | -#include <stdio.h> |
| 19 | +#include "fec.h" |
20 | 20 | #include <stdint.h> |
| 21 | +#include <stdio.h> |
| 22 | +#include <stdlib.h> |
21 | 23 | #include <string.h> |
22 | 24 | #include <time.h> |
23 | | -#include <stdlib.h> |
24 | | -#include "fec.h" |
25 | 25 |
|
26 | 26 | #define BINARY 0 |
27 | 27 |
|
28 | | - |
29 | | -const char *byte_to_binary(uint8_t x) |
| 28 | +const char* byte_to_binary(uint8_t x) |
30 | 29 | { |
31 | 30 | static char b[9]; |
32 | 31 | b[0] = '\0'; |
33 | 32 |
|
34 | 33 | uint8_t z; |
35 | | - for (z = 128; z > 0; z >>= 1) |
| 34 | + for(z = 128; z > 0; z >>= 1) |
36 | 35 | { |
37 | 36 | strcat(b, ((x & z) == z) ? "1" : "0"); |
38 | 37 | } |
39 | 38 |
|
40 | 39 | return b; |
41 | 40 | } |
42 | 41 |
|
43 | | -unsigned char Partab[] = { 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0 |
44 | | -,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1 |
45 | | -,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0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static uint8_t fec_lut[16] = { 0, 3, 1, 2, 3, 0, 2, 1, 3, 0, 2, 1, 0, 3, 1, 2 }; |
51 | 53 |
|
52 | 54 | void test_interleaver() |
53 | 55 | { |
54 | | - uint8_t fecbuffer[4] = {0xFF,0x00,0xAB,0x00}; |
55 | | - uint8_t buffer[4]= {0,0,0,0}; |
56 | | - uint8_t buffer2[4]= {0,0,0,0}; |
57 | | - uint8_t *output = buffer; |
58 | | - uint8_t *input = buffer; |
59 | | - |
60 | | - *output++ = ((fecbuffer[0] & 0x03)) |\ |
61 | | - ((fecbuffer[1] & 0x03) << 2) |\ |
62 | | - ((fecbuffer[2] & 0x03) << 4) |\ |
63 | | - ((fecbuffer[3] & 0x03) << 6); |
64 | | - *output++ = (((fecbuffer[0] >> 2) & 0x03)) |\ |
65 | | - (((fecbuffer[1] >> 2) & 0x03) << 2) |\ |
66 | | - (((fecbuffer[2] >> 2) & 0x03) << 4) |\ |
67 | | - (((fecbuffer[3] >> 2) & 0x03) << 6); |
68 | | - *output++ = (((fecbuffer[0] >> 4) & 0x03)) |\ |
69 | | - (((fecbuffer[1] >> 4) & 0x03) << 2) |\ |
70 | | - (((fecbuffer[2] >> 4) & 0x03) << 4) |\ |
71 | | - (((fecbuffer[3] >> 4) & 0x03) << 6); |
72 | | - *output++ = (((fecbuffer[0] >> 6) & 0x03)) |\ |
73 | | - (((fecbuffer[1] >> 6) & 0x03) << 2) |\ |
74 | | - (((fecbuffer[2] >> 6) & 0x03) << 4) |\ |
75 | | - (((fecbuffer[3] >> 6) & 0x03) << 6); |
76 | | - |
77 | | - printf("Input : "); print_array((uint8_t*) fecbuffer, 4, BINARY); printf("\n"); |
78 | | - printf("Interl: "); print_array((uint8_t*) buffer, 4, BINARY); printf("\n"); |
79 | | - |
80 | | - fecbuffer[0] = ((input[0] & 0x03)) |\ |
81 | | - ((input[1] & 0x03) << 2) |\ |
82 | | - ((input[2] & 0x03) << 4) |\ |
83 | | - ((input[3] & 0x03) << 6); |
84 | | - fecbuffer[1] = (((input[0] >> 2) & 0x03)) |\ |
85 | | - (((input[1] >> 2) & 0x03) << 2) |\ |
86 | | - (((input[2] >> 2) & 0x03) << 4) |\ |
87 | | - (((input[3] >> 2) & 0x03) << 6); |
88 | | - fecbuffer[2] = (((input[0] >> 4) & 0x03)) |\ |
89 | | - (((input[1] >> 4) & 0x03) << 2) |\ |
90 | | - (((input[2] >> 4) & 0x03) << 4) |\ |
91 | | - (((input[3] >> 4) & 0x03) << 6); |
92 | | - fecbuffer[3] = (((input[0] >> 6) & 0x03)) |\ |
93 | | - (((input[1] >> 6) & 0x03) << 2) |\ |
94 | | - (((input[2] >> 6) & 0x03) << 4) |\ |
95 | | - (((input[3] >> 6) & 0x03) << 6); |
96 | | - |
97 | | - |
98 | | - printf("Deint : "); print_array((uint8_t*) fecbuffer, 4, BINARY); printf("\n"); |
99 | | - |
100 | | - |
| 56 | + uint8_t fecbuffer[4] = { 0xFF, 0x00, 0xAB, 0x00 }; |
| 57 | + uint8_t buffer[4] = { 0, 0, 0, 0 }; |
| 58 | + uint8_t buffer2[4] = { 0, 0, 0, 0 }; |
| 59 | + uint8_t* output = buffer; |
| 60 | + uint8_t* input = buffer; |
| 61 | + |
| 62 | + *output++ = ((fecbuffer[0] & 0x03)) | ((fecbuffer[1] & 0x03) << 2) | ((fecbuffer[2] & 0x03) << 4) | ((fecbuffer[3] & 0x03) << 6); |
| 63 | + *output++ = (((fecbuffer[0] >> 2) & 0x03)) | (((fecbuffer[1] >> 2) & 0x03) << 2) | (((fecbuffer[2] >> 2) & 0x03) << 4) |
| 64 | + | (((fecbuffer[3] >> 2) & 0x03) << 6); |
| 65 | + *output++ = (((fecbuffer[0] >> 4) & 0x03)) | (((fecbuffer[1] >> 4) & 0x03) << 2) | (((fecbuffer[2] >> 4) & 0x03) << 4) |
| 66 | + | (((fecbuffer[3] >> 4) & 0x03) << 6); |
| 67 | + *output++ = (((fecbuffer[0] >> 6) & 0x03)) | (((fecbuffer[1] >> 6) & 0x03) << 2) | (((fecbuffer[2] >> 6) & 0x03) << 4) |
| 68 | + | (((fecbuffer[3] >> 6) & 0x03) << 6); |
| 69 | + |
| 70 | + printf("Input : "); |
| 71 | + print_array((uint8_t*)fecbuffer, 4, BINARY); |
| 72 | + printf("\n"); |
| 73 | + printf("Interl: "); |
| 74 | + print_array((uint8_t*)buffer, 4, BINARY); |
| 75 | + printf("\n"); |
| 76 | + |
| 77 | + fecbuffer[0] = ((input[0] & 0x03)) | ((input[1] & 0x03) << 2) | ((input[2] & 0x03) << 4) | ((input[3] & 0x03) << 6); |
| 78 | + fecbuffer[1] = (((input[0] >> 2) & 0x03)) | (((input[1] >> 2) & 0x03) << 2) | (((input[2] >> 2) & 0x03) << 4) | (((input[3] >> 2) & 0x03) << 6); |
| 79 | + fecbuffer[2] = (((input[0] >> 4) & 0x03)) | (((input[1] >> 4) & 0x03) << 2) | (((input[2] >> 4) & 0x03) << 4) | (((input[3] >> 4) & 0x03) << 6); |
| 80 | + fecbuffer[3] = (((input[0] >> 6) & 0x03)) | (((input[1] >> 6) & 0x03) << 2) | (((input[2] >> 6) & 0x03) << 4) | (((input[3] >> 6) & 0x03) << 6); |
| 81 | + |
| 82 | + printf("Deint : "); |
| 83 | + print_array((uint8_t*)fecbuffer, 4, BINARY); |
| 84 | + printf("\n"); |
101 | 85 | } |
102 | 86 |
|
103 | | -int main(int argc, char *argv[]) |
| 87 | +int main(int argc, char* argv[]) |
104 | 88 | { |
105 | | - //test_interleaver(); |
106 | | - uint8_t input[] = {0x19,0x00,0x80,0x00,0x0B,0x57,0x00,0x00,0x07,0x8C,0x63,0x20,0x00,0x0B,0x57,0x00,0x00,0x07,0x8F,0x52,0x41,0x07,0x00,0x00,0xD0,0x81}; |
107 | | - uint8_t input_length = sizeof(input); |
108 | | - uint8_t encoded[255]; |
109 | | - uint8_t decoded[255]; |
110 | | - uint8_t errors[255]; |
111 | | - |
112 | | - memset(errors, 0, 255); |
113 | | - |
114 | | - |
115 | | - printf("Input: %d ", input_length); |
116 | | - print_array(input, input_length, BINARY); |
117 | | - printf("\n"); |
118 | | - |
119 | | - memcpy(encoded, input, input_length); |
120 | | - |
121 | | - uint16_t lenght_encoded = fec_encode(encoded, input_length); |
122 | | - |
123 | | - printf("Encoded: %d ", lenght_encoded); |
124 | | - print_array(encoded, lenght_encoded, BINARY); |
125 | | - printf("\n"); |
126 | | - |
127 | | - memcpy(decoded, encoded, lenght_encoded); |
128 | | - |
129 | | - |
130 | | - uint8_t length_decoded = fec_decode_packet(decoded, lenght_encoded, 255); |
131 | | - |
132 | | - printf("Decoded: %d ", length_decoded); |
133 | | - print_array(decoded, length_decoded, BINARY); |
134 | | - printf("\n"); |
135 | | - |
136 | | - int nr_errors = 1; |
137 | | - int notrecovered = 0; |
138 | | - srand(time(NULL)); |
139 | | - |
140 | | -// while (notrecovered < 1) |
141 | | -// { |
142 | | -// int r = rand() % (lenght_encoded * 8); |
143 | | -// //printf("Rand: %d\n", r); |
144 | | -// |
145 | | -// encoded[r/8] ^= 1 << r % 8; |
146 | | -// errors[r/8] ^= 1 << r % 8; |
147 | | -// printf("Errors %d - BER %d%%\n", nr_errors, (nr_errors * 100) / (lenght_encoded * 8)); |
148 | | -// printf("Error: "); |
149 | | -// print_array(errors, lenght_encoded); |
150 | | -// printf("\n"); |
151 | | -// printf("Enc. error: "); |
152 | | -// print_array(encoded, lenght_encoded); |
153 | | -// printf("\n"); |
154 | | -// |
155 | | -// length_decoded = fec_decode_packet(encoded, lenght_encoded, decoded, 255); |
156 | | -// |
157 | | -// printf("Decoded: "); |
158 | | -// print_array(decoded, length_decoded); |
159 | | -// printf("\n"); |
160 | | -// |
161 | | -// if (memcmp(input, decoded, input_length) == 0) |
162 | | -// { |
163 | | -// printf("ok\n"); |
164 | | -// } |
165 | | -// else |
166 | | -// { |
167 | | -// printf("error\n"); |
168 | | -// notrecovered++; |
169 | | -// } |
170 | | -// |
171 | | -// nr_errors++; |
172 | | -// } |
| 89 | + // test_interleaver(); |
| 90 | + uint8_t input[] = { 0x19, 0x00, 0x80, 0x00, 0x0B, 0x57, 0x00, 0x00, 0x07, 0x8C, 0x63, 0x20, 0x00, |
| 91 | + 0x0B, 0x57, 0x00, 0x00, 0x07, 0x8F, 0x52, 0x41, 0x07, 0x00, 0x00, 0xD0, 0x81 }; |
| 92 | + uint8_t input_length = sizeof(input); |
| 93 | + uint8_t encoded[255]; |
| 94 | + uint8_t decoded[255]; |
| 95 | + uint8_t errors[255]; |
| 96 | + |
| 97 | + memset(errors, 0, 255); |
| 98 | + |
| 99 | + printf("Input: %d ", input_length); |
| 100 | + print_array(input, input_length, BINARY); |
| 101 | + printf("\n"); |
| 102 | + |
| 103 | + memcpy(encoded, input, input_length); |
| 104 | + |
| 105 | + uint16_t lenght_encoded = fec_encode(encoded, input_length); |
| 106 | + |
| 107 | + printf("Encoded: %d ", lenght_encoded); |
| 108 | + print_array(encoded, lenght_encoded, BINARY); |
| 109 | + printf("\n"); |
| 110 | + |
| 111 | + memcpy(decoded, encoded, lenght_encoded); |
| 112 | + |
| 113 | + uint8_t length_decoded = fec_decode_packet(decoded, lenght_encoded, 255); |
| 114 | + |
| 115 | + printf("Decoded: %d ", length_decoded); |
| 116 | + print_array(decoded, length_decoded, BINARY); |
| 117 | + printf("\n"); |
| 118 | + |
| 119 | + int nr_errors = 1; |
| 120 | + int notrecovered = 0; |
| 121 | + srand(time(NULL)); |
| 122 | + |
| 123 | + if(memcmp(input, decoded, input_length) == 0) |
| 124 | + { |
| 125 | + printf("Input was decoded successfully\n"); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + printf("error\n"); |
| 130 | + // notrecovered++; |
| 131 | + } |
173 | 132 |
|
| 133 | + // while (notrecovered < 1) |
| 134 | + // { |
| 135 | + // int r = rand() % (lenght_encoded * 8); |
| 136 | + // //printf("Rand: %d\n", r); |
| 137 | + // |
| 138 | + // encoded[r/8] ^= 1 << r % 8; |
| 139 | + // errors[r/8] ^= 1 << r % 8; |
| 140 | + // printf("Errors %d - BER %d%%\n", nr_errors, (nr_errors * 100) / (lenght_encoded * 8)); |
| 141 | + // printf("Error: "); |
| 142 | + // print_array(errors, lenght_encoded); |
| 143 | + // printf("\n"); |
| 144 | + // printf("Enc. error: "); |
| 145 | + // print_array(encoded, lenght_encoded); |
| 146 | + // printf("\n"); |
| 147 | + // |
| 148 | + // length_decoded = fec_decode_packet(encoded, lenght_encoded, decoded, 255); |
| 149 | + // |
| 150 | + // printf("Decoded: "); |
| 151 | + // print_array(decoded, length_decoded); |
| 152 | + // printf("\n"); |
| 153 | + // |
| 154 | + // if (memcmp(input, decoded, input_length) == 0) |
| 155 | + // { |
| 156 | + // printf("ok\n"); |
| 157 | + // } |
| 158 | + // else |
| 159 | + // { |
| 160 | + // printf("error\n"); |
| 161 | + // notrecovered++; |
| 162 | + // } |
| 163 | + // |
| 164 | + // nr_errors++; |
| 165 | + // } |
174 | 166 |
|
175 | 167 | return 0; |
176 | 168 | } |
0 commit comments