Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/correct.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ correct_convolutional *correct_convolutional_create(size_t inv_rate, size_t orde
void correct_convolutional_destroy(correct_convolutional *conv);

/* correct_convolutional_encode_len returns the number of *bits*
* in a msg_len of given size, in *bytes*. In order to convert
* in a msg_len of given size, also in bits. In order to convert
* this returned length to bytes, save the result of the length
* modulo 8. If it's nonzero, then the length in bytes is
* length/8 + 1. If it is zero, then the length is just
* length/8.
*/
size_t correct_convolutional_encode_len(correct_convolutional *conv, size_t msg_len);
size_t correct_convolutional_encode_len(correct_convolutional *conv, size_t msg_num_bits);

/* correct_convolutional_encode uses the given conv instance to
* encode a block of data and write it to encoded. The length of
Expand All @@ -69,7 +69,7 @@ size_t correct_convolutional_encode_len(correct_convolutional *conv, size_t msg_
* this is not an exact multiple of 8, then it occupies an additional
* byte.
*/
size_t correct_convolutional_encode(correct_convolutional *conv, const uint8_t *msg, size_t msg_len,
size_t correct_convolutional_encode(correct_convolutional *conv, const uint8_t *msg, size_t msg_num_bits,
uint8_t *encoded);

/* correct_convolutional_decode uses the given conv instance to
Expand Down
1 change: 1 addition & 0 deletions include/correct/convolutional/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
typedef struct {
uint8_t current_byte;
unsigned int current_byte_len;
unsigned int last_byte_len;
uint8_t *bytes;
size_t byte_index;
size_t len;
Expand Down
4 changes: 3 additions & 1 deletion src/convolutional/bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void bit_writer_reconfigure(bit_writer_t *w, uint8_t *bytes, size_t len) {
w->current_byte = 0;
w->current_byte_len = 0;
w->byte_index = 0;
w->last_byte_len = 0;
}

void bit_writer_destroy(bit_writer_t *w) {
Expand Down Expand Up @@ -159,12 +160,13 @@ void bit_writer_flush_byte(bit_writer_t *w) {
w->current_byte <<= (8 - w->current_byte_len);
w->bytes[w->byte_index] = w->current_byte;
w->byte_index++;
w->last_byte_len = w->current_byte_len;
w->current_byte_len = 0;
}
}

size_t bit_writer_length(bit_writer_t *w) {
return w->byte_index;
return 8 * w->byte_index + w->last_byte_len;
}

uint8_t reverse_byte(uint8_t b) {
Expand Down
12 changes: 6 additions & 6 deletions src/convolutional/encode.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "correct/convolutional/convolutional.h"

size_t correct_convolutional_encode_len(correct_convolutional *conv, size_t msg_len) {
size_t msgbits = 8 * msg_len;
size_t encodedbits = conv->rate * (msgbits + conv->order + 1);
size_t correct_convolutional_encode_len(correct_convolutional *conv, size_t msg_num_bits) {
size_t encodedbits = conv->rate * (msg_num_bits + conv->order + 1);
return encodedbits;
}

Expand All @@ -13,7 +12,7 @@ size_t correct_convolutional_encode_len(correct_convolutional *conv, size_t msg_
// assume that encoded length is long enough?
size_t correct_convolutional_encode(correct_convolutional *conv,
const uint8_t *msg,
size_t msg_len,
size_t msg_num_bits,
uint8_t *encoded) {
// convolutional code convolves filter coefficients, given by
// the polynomial, with some history from our message.
Expand All @@ -25,13 +24,14 @@ size_t correct_convolutional_encode(correct_convolutional *conv,
// e.g. if order is 7, then remove the 8th bit and beyond
unsigned int shiftmask = (1 << conv->order) - 1;

size_t encoded_len_bits = correct_convolutional_encode_len(conv, msg_len);
size_t encoded_len_bits = correct_convolutional_encode_len(conv, msg_num_bits);
size_t encoded_len = (encoded_len_bits % 8) ? (encoded_len_bits / 8 + 1) : (encoded_len_bits / 8);
bit_writer_reconfigure(conv->bit_writer, encoded, encoded_len);
size_t msg_len = (msg_num_bits % 8) ? (msg_num_bits / 8 + 1) : (msg_num_bits / 8);

bit_reader_reconfigure(conv->bit_reader, msg, msg_len);

for (size_t i = 0; i < 8 * msg_len; i++) {
for (size_t i = 0; i < msg_num_bits; i++) {
// shiftregister has oldest bits on left, newest on right
shiftregister <<= 1;
shiftregister |= bit_reader_read(conv->bit_reader, 1);
Expand Down
4 changes: 2 additions & 2 deletions tools/find_conv_libfec_poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void find_poly_coeff(size_t rate, size_t order, uint8_t *msg, size_t msg_len, li

// create a dummy encoder so that we can find how long the resulting encoded value is
correct_convolutional *conv_dummy = correct_convolutional_create(rate, order, poly);
size_t enclen_bits = correct_convolutional_encode_len(conv_dummy, msg_len);
size_t enclen_bits = correct_convolutional_encode_len(conv_dummy, 8 * msg_len);
size_t enclen = (enclen_bits % 8) ? (enclen_bits / 8 + 1) : enclen_bits / 8;
correct_convolutional_destroy(conv_dummy);

Expand All @@ -92,7 +92,7 @@ void find_poly_coeff(size_t rate, size_t order, uint8_t *msg, size_t msg_len, li
poly[search_coeff] = i;
correct_convolutional *conv = correct_convolutional_create(rate, order, poly);

correct_convolutional_encode(conv, (uint8_t*)msg, msg_len, encoded);
correct_convolutional_encode(conv, (uint8_t*)msg, 8 * msg_len, encoded);
byte2bit(encoded, encoded_bits, enclen);

// now erase all the bits we're not searching for
Expand Down
8 changes: 4 additions & 4 deletions util/error-sim-shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ ssize_t conv_shim27_decode(void *conv_v, uint8_t *soft, size_t soft_len, uint8_t
update_viterbi27_blk(conv_v, soft, soft_len / 2 - 2);
size_t n_decoded_bits = (soft_len / 2) - 8;
chainback_viterbi27(conv_v, msg, n_decoded_bits, 0);
return (n_decoded_bits % 8) ? (n_decoded_bits / 8) + 1 : n_decoded_bits / 8;
return n_decoded_bits;
}

ssize_t conv_shim29_decode(void *conv_v, uint8_t *soft, size_t soft_len, uint8_t *msg) {
init_viterbi29(conv_v, 0);
update_viterbi29_blk(conv_v, soft, soft_len / 2 - 2);
size_t n_decoded_bits = (soft_len / 2) - 10;
chainback_viterbi29(conv_v, msg, n_decoded_bits, 0);
return (n_decoded_bits % 8) ? (n_decoded_bits / 8) + 1 : n_decoded_bits / 8;
return n_decoded_bits;
}

ssize_t conv_shim39_decode(void *conv_v, uint8_t *soft, size_t soft_len, uint8_t *msg) {
init_viterbi39(conv_v, 0);
update_viterbi39_blk(conv_v, soft, soft_len / 3 - 2);
size_t n_decoded_bits = (soft_len / 3) - 10;
chainback_viterbi39(conv_v, msg, n_decoded_bits, 0);
return (n_decoded_bits % 8) ? (n_decoded_bits / 8) + 1 : n_decoded_bits / 8;
return n_decoded_bits;
}

ssize_t conv_shim615_decode(void *conv_v, uint8_t *soft, size_t soft_len, uint8_t *msg) {
init_viterbi615(conv_v, 0);
update_viterbi615_blk(conv_v, soft, soft_len / 6 - 2);
size_t n_decoded_bits = (soft_len / 6) - 16;
chainback_viterbi615(conv_v, msg, n_decoded_bits, 0);
return (n_decoded_bits % 8) ? (n_decoded_bits / 8) + 1 : n_decoded_bits / 8;
return n_decoded_bits;
}
8 changes: 4 additions & 4 deletions util/error-sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ conv_testbench *resize_conv_testbench(conv_testbench *scratch, size_t (*enclen_f

scratch->msg_out = realloc(scratch->msg_out, msg_len);

size_t enclen = enclen_f(enc, msg_len);
size_t enclen = enclen_f(enc, 8 * msg_len);
size_t enclen_bytes = (enclen % 8) ? (enclen/8 + 1) : enclen/8;
scratch->enclen = enclen;
scratch->enclen_bytes = enclen_bytes;
Expand All @@ -156,7 +156,7 @@ void free_scratch(conv_testbench *scratch) {

int test_conv_noise(conv_testbench *scratch, uint8_t *msg, size_t n_bytes,
double bpsk_voltage) {
scratch->encode(scratch->encoder, msg, n_bytes, scratch->encoded);
scratch->encode(scratch->encoder, msg, 8 * n_bytes, scratch->encoded);
encode_bpsk(scratch->encoded, scratch->v, scratch->enclen, bpsk_voltage);

memcpy(scratch->corrupted, scratch->v, scratch->enclen * sizeof(double));
Expand All @@ -167,8 +167,8 @@ int test_conv_noise(conv_testbench *scratch, uint8_t *msg, size_t n_bytes,

ssize_t decode_len = scratch->decode(scratch->decoder, scratch->soft, scratch->enclen, scratch->msg_out);

if (decode_len != n_bytes) {
printf("expected to decode %zu bytes, decoded %zu bytes instead\n", n_bytes, decode_len);
if (decode_len != 8 * n_bytes) {
printf("expected to decode %zu bits, decoded %zu bits instead\n", 8 * n_bytes, decode_len);
exit(1);
}

Expand Down