Use data whitening

This commit is contained in:
Thomas Kolb 2022-02-12 22:06:49 +01:00
parent 87b844a2da
commit eb3fc5f9c7
5 changed files with 45 additions and 0 deletions

View file

@ -23,6 +23,8 @@ set(sources
src/correlator.c src/correlator.c
src/freq_est.h src/freq_est.h
src/freq_est.c src/freq_est.c
src/whitening.h
src/whitening.c
) )
include_directories( include_directories(

View file

@ -12,6 +12,7 @@
#include "transmission.h" #include "transmission.h"
#include "correlator.h" #include "correlator.h"
#include "freq_est.h" #include "freq_est.h"
#include "whitening.h"
typedef enum { typedef enum {
RX_STATE_ACQUISITION, RX_STATE_ACQUISITION,
@ -286,6 +287,9 @@ int main(void)
fec_decode(payload_fec, payload_len_bytes, payload_enc, payload); fec_decode(payload_fec, payload_len_bytes, payload_enc, payload);
// de-whiten the data
whitening_apply_in_place(payload, payload_len_bytes);
int valid = crc_validate_message(PAYLOAD_CRC_SCHEME, payload, payload_len_bytes, payload_crc); int valid = crc_validate_message(PAYLOAD_CRC_SCHEME, payload, payload_len_bytes, payload_crc);
payload[payload_len_bytes] = '\0'; payload[payload_len_bytes] = '\0';

View file

@ -1,6 +1,7 @@
#include <liquid/liquid.h> #include <liquid/liquid.h>
#include <string.h> #include <string.h>
#include "whitening.h"
#include "preamble.h" #include "preamble.h"
#include "config.h" #include "config.h"
@ -95,6 +96,10 @@ result_t packet_mod_encode(packet_mod_ctx_t *ctx)
return ERR_NO_MEM; return ERR_NO_MEM;
} }
// whiten the raw data
whitening_apply_in_place(ctx->pkt_bytes, ctx->length);
// apply the FEC
ERR_CHECK_LIQUID(fec_encode(ctx->fec, ctx->length, ctx->pkt_bytes, enc_msg)); ERR_CHECK_LIQUID(fec_encode(ctx->fec, ctx->length, ctx->pkt_bytes, enc_msg));
free(ctx->pkt_bytes); free(ctx->pkt_bytes);

25
impl/src/whitening.c Normal file
View file

@ -0,0 +1,25 @@
#include <stdio.h>
#include "whitening.h"
void whitening_apply_in_place(uint8_t *data, size_t length)
{
/* 9-bit LFSR according to CCITT data whitening procedure. */
uint16_t lfsr = 0x1FF;
for(size_t i = 0; i < length; i++) {
uint8_t byte = 0;
// advance the LFSR and collect the output in a byte.
for(int k = 0; k < 8; k++) {
byte <<= 1;
byte |= lfsr & 0x001;
uint16_t tmp = lfsr ^ (lfsr >> 5);
lfsr >>= 1;
lfsr |= (tmp & 0x001) << 8;
}
data[i] ^= byte;
}
}

9
impl/src/whitening.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef WHITENING_H
#define WHITENING_H
#include <stdint.h>
#include <stdlib.h>
void whitening_apply_in_place(uint8_t *data, size_t length);
#endif // WHITENING_H