summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/bitloading/src/nsr.c
blob: 963aba266395c23752db83c5bdd9924991885fb8 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/bitloading/src/nsr.c
 * \brief   Noise / Signal Ratio.
 * \ingroup ce_rx
 */
#include "common/std.h"

#include "ce/rx/bitloading/inc/nsr.h"
#include "ce/rx/ce_rx_param.h"
#include "ce/rx/bitloading/nsr.h"
#include "ce/rx/bitloading/inc/ber.h"
#include "hal/phy/defs.h"

/**
 * Default value of margin to apply to NSR.
 */
u8 ce_rx_bl_nsr_margin_ = CE_RX_BL_NSR_MARGIN_DEFAULT;

void
ce_rx_bl_nsr_sum_init (ce_rx_bitloading_t *bl, phy_chandata_t *chan_data,
                       uint chan_data_count)
{
    /* Check parameters. */
    dbg_assert (bl);
    dbg_assert (chan_data);
    dbg_assert (chan_data_count != 0);

    /* No block should have been previously allocated. */
    dbg_assert (bl->noise_nrj_blk_count == 0);
    dbg_assert (bl->noise_nrj == INVALID_PTR);

    /* Look for NSR. */
    uint count = 0;
    bool finished = false;
    phy_chandata_t *cur = chan_data;
    /* While not finished. */
    while (!finished)
    {
        dbg_assert (cur);
        /* Noise NRJ block. */
        if (cur->conf.type == PHY_CHANDATA_TYPE_NRJ)
        {
            /* First block. */
            if (cur->conf.address == 0)
                bl->noise_nrj = &cur->blk;
            bl->noise_nrj_blk_count++;
            /* Add a reference to the block. */
            blk_addref_desc (&cur->blk);
        }
        /* Get next channel data block. */
        if (++count < chan_data_count)
            cur = PARENT_OF (phy_chandata_t, blk, cur->blk.next);
        else
            /* No more, it's finished. */
            finished = true;
    }
    {
        /* Sanity check. */
        if (PHY_CARRIER_NB % (BLK_SIZE / CE_RX_BITLOADING_NOISE_NRJ_SIZE))
        {
            dbg_assert (bl->noise_nrj_blk_count ==
                        PHY_CARRIER_NB / (BLK_SIZE /
                                          CE_RX_BITLOADING_NOISE_NRJ_SIZE)
                        + 1);
        }
        else
        {
            dbg_assert (bl->noise_nrj_blk_count ==
                        PHY_CARRIER_NB / (BLK_SIZE /
                                          CE_RX_BITLOADING_NOISE_NRJ_SIZE));
        }
    }
    /* One NSR acquired. */
    bl->mean_count = 1;
}

void
ce_rx_bl_nsr_sum_add (ce_rx_bitloading_t *bl, phy_chandata_t *chan_data,
                      uint chan_data_count)
{
    /* Check parameters. */
    dbg_assert (bl);
    dbg_assert (chan_data);
    dbg_assert (chan_data_count != 0);
    dbg_assert (chan_data_count >= bl->noise_nrj_blk_count);
    dbg_assert_ptr (bl->noise_nrj);

    /* Look for NSR. */
    uint count = 0;
    uint count_blk = 0;
    bool finished = false;
    u32 *cur_sum_nsr, *cur_nsr;
    phy_chandata_t *cur_cd = chan_data;
    blk_t *cur_blk = bl->noise_nrj;
    /* While not finished. */
    while (!finished)
    {
        dbg_assert (cur_cd);
        dbg_assert (cur_blk);
        /* Noise NRJ block. */
        if (cur_cd->conf.type == PHY_CHANDATA_TYPE_NRJ)
        {
            /* Sanity check. */
            dbg_assert (count_blk < bl->noise_nrj_blk_count);
            /* Get mean noise NRJ. */
            cur_sum_nsr = (u32 *) cur_blk->data;
            /* Get noise NRJ. */
            cur_nsr = (u32 *) cur_cd->blk.data;
            uint i;
            for (i = 0; i < cur_cd->conf.size; i++)
            {
                cur_sum_nsr[i] += cur_nsr[i];
            }
            /* Next mean noise NRJ. */
            cur_blk = cur_blk->next;
            count_blk++;
        }
        /* Get next channel data block. */
        if (++count < chan_data_count)
            cur_cd = PARENT_OF (phy_chandata_t, blk, cur_cd->blk.next);
        else
            /* No more, it's finished. */
            finished = true;
    }
    /* Add a new sum. */
    bl->mean_count++;
}

void
ce_rx_bl_nsr_compute_mean (ce_rx_bitloading_t *bl)
{
    /* Check parameter. */
    dbg_assert (bl);
    dbg_assert_ptr (bl->noise_nrj);

    /* Go through each block of NSR sum. */
    blk_t *blk = bl->noise_nrj;
    uint i;
    /* Tone represents the current tone.
     * tone_limit represents the limit of tone for this block. */
    uint tone, tone_limit;
    for (i = 0, tone = 0,
         tone_limit = BLK_SIZE / CE_RX_BITLOADING_NOISE_NRJ_SIZE;
         i < bl->noise_nrj_blk_count;
         i++, tone_limit += BLK_SIZE / CE_RX_BITLOADING_NOISE_NRJ_SIZE)
    {
        /* Sanity check. */
        dbg_assert (blk);
        /* Get current NSR sum. */
        u32 *cur_sum_nsr = (u32 *) blk->data;
        /* If tone limit is over number of tone, we need to force it to the
         * maximum value. */
        if (tone_limit > PHY_CARRIER_NB)
            tone_limit = PHY_CARRIER_NB;
        uint j;
        /* Go through each sum of NSR of this block. */
        for (j = 0; tone < tone_limit; j++, tone++)
        {
            /* Compute mean and rescale (for NSR dynamics). */
            cur_sum_nsr[j] *= CE_RX_BL_NSR_SOUND_RESCALE;
            cur_sum_nsr[j] = ce_rx_bl_nsr_mean (cur_sum_nsr[j],
                                                bl->mean_count)
                * ce_rx_bl_nsr_margin_ / CE_RX_BL_NSR_MARGIN_QUANT_FACTOR;
        }
        /* Next block. */
        blk = blk->next;
    }
}

void
ce_rx_bl_nsr_margin_set (u8 margin_q)
{
    /* Check parameter. */
    dbg_assert (margin_q);
    /* Store margin value. */
    ce_rx_bl_nsr_margin_ = margin_q;
}