summaryrefslogtreecommitdiffstats
path: root/src/prbs.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-06-18 18:16:02 +0300
committerHarald Welte <laforge@gnumonks.org>2017-07-10 23:42:02 +0200
commit1389e86d116509884b0e5ee3421fe7683afcab9b (patch)
tree52ef42f5f49ac3ae52e687cbc7207426fd11e075 /src/prbs.c
parent548e3712009f68f801be806884d848b47c30dced (diff)
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
Diffstat (limited to 'src/prbs.c')
-rw-r--r--src/prbs.c74
1 files changed, 74 insertions, 0 deletions
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 <laforge@gnumonks.org> */
+
+#include <stdint.h>
+#include <string.h>
+#include <osmocom/core/bits.h>
+#include <osmocom/core/prbs.h>
+
+/*! \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;
+}