From 1389e86d116509884b0e5ee3421fe7683afcab9b Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 18 Jun 2017 18:16:02 +0300 Subject: Add pseudo-random bit sequence generator to libosmcoore These PRBS sequences are specified in ITU-T O.150. They are typically used as test data to be transmitted for BER (bit error rate) testing. Change-Id: I227b6a6e86a251460ecb816afa9a7439d5fb94d1 --- src/prbs.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/prbs.c (limited to 'src/prbs.c') diff --git a/src/prbs.c b/src/prbs.c new file mode 100644 index 00000000..be52fd4c --- /dev/null +++ b/src/prbs.c @@ -0,0 +1,74 @@ +/* Osmocom implementation of pseudo-random bit sequence generation */ +/* (C) 2017 by Harald Welte */ + +#include +#include +#include +#include + +/*! \brief PRBS-7 according ITU-T O.150 */ +const struct osmo_prbs osmo_prbs7 = { + /* x^7 + x^6 + 1 */ + .name = "PRBS-7", + .len = 7, + .coeff = (1<<6) | (1<<5), +}; + +/*! \brief PRBS-9 according ITU-T O.150 */ +const struct osmo_prbs osmo_prbs9 = { + /* x^9 + x^5 + 1 */ + .name = "PRBS-9", + .len = 9, + .coeff = (1<<8) | (1<<4), +}; + +/*! \brief PRBS-11 according ITU-T O.150 */ +const struct osmo_prbs osmo_prbs11 = { + /* x^11 + x^9 + 1 */ + .name = "PRBS-11", + .len = 11, + .coeff = (1<<10) | (1<<8), +}; + +/*! \brief PRBS-15 according ITU-T O.150 */ +const struct osmo_prbs osmo_prbs15 = { + /* x^15 + x^14+ 1 */ + .name = "PRBS-15", + .len = 15, + .coeff = (1<<14) | (1<<13), +}; + +/*! \brief Initialize the given caller-allocated PRBS state */ +void osmo_prbs_state_init(struct osmo_prbs_state *st, const struct osmo_prbs *prbs) +{ + memset(st, 0, sizeof(*st)); + st->prbs = prbs; + st->state = 1; +} + +static void osmo_prbs_process_bit(struct osmo_prbs_state *state, ubit_t bit) +{ + state->state >>= 1; + if (bit) + state->state ^= state->prbs->coeff; +} + +/*! \brief Get the next bit out of given PRBS instance */ +ubit_t osmo_prbs_get_ubit(struct osmo_prbs_state *state) +{ + ubit_t result = state->state & 0x1; + osmo_prbs_process_bit(state, result); + + return result; +} + +/*! \brief Fill buffer of unpacked bits with next bits out of given PRBS instance */ +int osmo_prbs_get_ubits(ubit_t *out, unsigned int out_len, struct osmo_prbs_state *state) +{ + unsigned int i; + + for (i = 0; i < out_len; i++) + out[i] = osmo_prbs_get_ubit(state); + + return i; +} -- cgit v1.2.3