summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/bitloading/src/pber.c
blob: 85c0987b48f6604e0b60a302259593014660708f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Cesar project {{{
 *
 * Copyright (C) 2011 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/bitloading/src/pber.c
 * \brief   PB Error Rate functions
 * \ingroup ce_rx
 */
#include "common/std.h"

#include "mac/common/defs.h"
#include "ce/rx/bitloading/pber.h"
#include "ce/rx/bitloading/inc/pber.h"

/**
 * Maximum value of the PB error rate quantified.
 * This is a possible value.
 */
#define CE_RX_BL_PBER_MAX (1 << 16)

/**
 * PBER quantification factor.
 */
#define CE_RX_BL_PBER_QUANT (1 << 16)

ce_rx_bl_pber_conf_t ce_rx_bl_pber_conf
= {
    .mean_coef = 6912,
    .mean_init = 655,
};

void
ce_rx_bl_pber_reset (ce_rx_bl_pber_t *ctx)
{
    dbg_assert (ctx);

    /* Reset to an invalid value. */
    ctx->mean_q16 = ce_rx_bl_pber_conf.mean_init;
}

void
ce_rx_bl_pber_update (ce_rx_bl_pber_t *ctx, uint pb_total, uint pb_crc_false)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (pb_total);

    /* Quantify PBER mean of current frame by PB. */
    uint pb_mean_q16 = pb_crc_false * CE_RX_BL_PBER_QUANT;

    /* If set to impossible value, this is the first frame. */
    if (ctx->mean_q16 > CE_RX_BL_PBER_MAX)
        ctx->mean_q16 = pb_mean_q16;
    else
    {
        dbg_assert (pb_crc_false <= pb_total);
        dbg_assert (pb_total < CE_RX_BL_PBER_MAX_PB_PER_FRAME);
        dbg_assert (pb_total < ce_rx_bl_pber_conf.mean_coef);
        ctx->mean_q16
            = (ctx->mean_q16 * (ce_rx_bl_pber_conf.mean_coef - pb_total)
            + pb_mean_q16)
            / ce_rx_bl_pber_conf.mean_coef;
    }
}