summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/bitloading/src/bitloading.c
blob: 1e8107d4345b380a1f57fe0ce0b8da39edceb7d2 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/bitloading/src/bitloading.c
 * \brief   Bit loading related functions.
 * \ingroup ce_rx_bl
 */
#include "common/std.h"
#include "ce_rx_bl_fsm_defs.h"
#include "ce/rx/bitloading/inc/ber.h"
#include "ce/rx/bitloading/inc/bitloading.h"

#define LIB_HEAPSORT_USER_TYPE ce_rx_bl_ber_impact_t
#define LIB_HEAPSORT_USER_COMP_LESSER ce_rx_bl_ber_impact_compare
#define LIB_HEAPSORT_USER_SWAP ce_rx_bl_ber_impact_swap
#define LIB_HEAPSORT_USER_PREFIX ce_rx_bl_ber_impact
#include "lib/heapsort.h"

uint ce_rx_bl_pb_false_factor_ = 1;
uint ce_rx_bl_pb_total_factor_ = 4;
uint ce_rx_bl_min_pb_per_frame_ = 2;
uint ce_rx_bl_min_frame_with_high_pb_err_rate_ = 4;
uint ce_rx_bl_ber_lower_bound_ = 70;

blk_t *
ce_rx_bl_get_nsr (ce_rx_bitloading_t *bl)
{
    dbg_assert (bl);

    switch (bl->fsm)
    {
    /* We can only get noise after bit loading initial. */
    case CE_RX_BL_FSM_STATE_TRACKING:
        return bl->noise_nrj;
        break;
    default:
        return NULL;
        break;
    }
}

void
ce_rx_bl_sort_optimization (ce_rx_bl_ber_impact_t *opti_table,
                            uint size)
{
    lib_heapsort (opti_table, size);
}

u64
ce_rx_bl_update_tone_map_under_ber_consign (u64 ber_pt,
                                            tonemask_info_t *tonemask,
                                            phy_fecrate_t fec_rate,
                                            ce_rx_bitloading_t *bl,
                                            tonemap_t *tm,
                                            ce_rx_bl_ber_impact_t *opti_table,
                                            uint *tone_en)
{
    /* Check parameters. */
    dbg_assert (tonemask);
    dbg_assert (tm);
    dbg_assert (opti_table);
    dbg_assert (tone_en);

    u8 modulation;
    u64 ber_weighted_sum = 0;
    u64 ber_upper;
    uint i = 0, carrier_index = 0;
    blk_t *cur_blk = bl->noise_nrj;
    u32 *nsr_data = (u32 *) cur_blk->data;

    tm->bits_per_symbol = 0;
    *tone_en = 0;

    /* Go through the tone map. */
    TONEMAP_ACCESS_BEGIN (tm, tonemask->tonemask)
    {
        /* Index of a tone in a word of tones. */
        uint tm_tone_index_;
        /* The 8 tones of the word. */
        u32 tm_word_tone_ = 0;
        for (tm_tone_index_ = 0;
             tm_tone_index_ < 8;
             tm_tone_index_++)
        {
            /* If tone enable. */
            if (!(tk_cur_ & 0x1))
            {
                /* Compute BER. */
                ce_rx_bl_ber_vs_nsr (ce_rx_bl_ber_poly_coef[fec_rate],
                                     nsr_data[carrier_index % (BLK_SIZE / 4)], ber_pt,
                                     &modulation, &opti_table[i].ber_lower, &ber_upper);
                /* Update optimization table. */
                if (modulation == CE_MOD_COUNT - 1)
                    /* This tone can not be improved, set an impossible BER
                     * difference. */
                    opti_table[i].ber_diff = -1;
                else
                    opti_table[i].ber_diff = (ber_upper - opti_table[i].ber_lower) >> 32;
                opti_table[i].carrier_index = carrier_index;
                /* Update statistics for this tone map. */
                if (modulation)
                    (*tone_en)++;
                ber_weighted_sum += opti_table[i].ber_lower * CE_BIT_PER_MOD[modulation];
                tm->bits_per_symbol += CE_BIT_PER_MOD[modulation];
                i++;
            }
            else
                modulation = 0;
            /* Write tone. */
            tm_word_tone_ |= modulation << (4 * tm_tone_index_);
            /* Next tone mask bit. */
            tk_cur_ >>= 1;
            carrier_index++;
        }
        *tm_ = tm_word_tone_;
        /* Next group of tone (grouped by word). */
        tm_++;
    }
    TONEMAP_ACCESS_MIDDLE;
    /* Get next block of NSR. */
    /* Dirty? You mean? FIXME */
    if (!(carrier_index % (BLK_SIZE / 4)))
    {
        cur_blk = cur_blk->next;
        dbg_assert (cur_blk);
        nsr_data = (u32 *) cur_blk->data;
    }
    TONEMAP_ACCESS_END;

    return ber_weighted_sum;
}

void
ce_rx_bl_update_tone_map_at_ber_consign (u64 ber_pt,
                                         tonemask_info_t *tonemask,
                                         ce_rx_bitloading_t *bl,
                                         tonemap_t *tm,
                                         ce_rx_bl_ber_impact_t *opti,
                                         u64 *ber_weighted_sum,
                                         uint *tone_en)
{
    /* Check parameters. */
    dbg_assert (ber_pt < CE_RX_BL_BER_DEFAULT_OVER);
    dbg_assert (tonemask);
    dbg_assert (tm);
    dbg_assert (bl);
    dbg_assert (opti);
    dbg_assert (tone_en);
    dbg_assert (ber_weighted_sum);

    uint pos = 0;
    u32 *tone_word;
    s8 mod = -1;

    /* Optimize tone map until we reach the BER consign or we are out of
     * table. */
    while (ber_pt * tm->bits_per_symbol >= *ber_weighted_sum
           && pos < tonemask->carrier_nb)
    {
        /* Get the modulation of the lowest BER impact. */
        mod = tonemap_get_tone (tm, opti[pos].carrier_index, &tone_word);
        /* If the tone we would like to increase is already at its maximum,
         * all the remaining tone that can be increase are already at the
         * maximum too. So basically, we should stop the algorithm here. */
        if (mod == CE_MOD_COUNT - 1)
            return;
        /* Remove from number of bits of the tone map the current value. */
        tm->bits_per_symbol -= CE_BIT_PER_MOD[mod];
        /* Remove from the summed BER of the tone map the current value. */
        *ber_weighted_sum -= opti[pos].ber_lower * CE_BIT_PER_MOD[mod];
        /* Increase modulation of this tone index. */
        tonemap_write_tone_to_word (tone_word, opti[pos].carrier_index,
                                    ++mod);
        /* Update number of bits of the tone map. */
        tm->bits_per_symbol += CE_BIT_PER_MOD[mod];
        /* Update summed BER of the tone map. */
        *ber_weighted_sum += (opti[pos].ber_lower
                              + ((u64) opti[pos].ber_diff << 32))
            * CE_BIT_PER_MOD[mod];
        /* Update tone enabled count if needed. */
        if (mod == 1)
            (*tone_en)++;
        /* Next one. */
        pos++;
    }
    /* Sanity check: we must have done something! */
    dbg_assert (mod != -1);
    /* Remove last tone if we are over target. */
    if (*ber_weighted_sum > ber_pt * tm->bits_per_symbol)
    {
        /* Go back to last position. */
        pos--;
        dbg_assert (pos < tonemask->carrier_nb);
        /* Remove last one. */
        tm->bits_per_symbol -= CE_BIT_PER_MOD[mod];
        *ber_weighted_sum -= (opti[pos].ber_lower
                              + ((u64) opti[pos].ber_diff << 32))
            * CE_BIT_PER_MOD[mod];
        /* Write new value. */
        tonemap_write_tone_to_word (tone_word, opti[pos].carrier_index,
                                    --mod);
        tm->bits_per_symbol += CE_BIT_PER_MOD[mod];
        *ber_weighted_sum += opti[pos].ber_lower * CE_BIT_PER_MOD[mod];
        if (mod == 0)
            (*tone_en)--;
    }
}

tonemap_t *
ce_rx_bl_compute_tone_map_iterative (const u64 bpt_initial[PHY_FEC_RATE_NB],
                                     uint iteration_max,
                                     tonemask_info_t *tonemask,
                                     ce_rx_bitloading_t *bl)
{
    /* Check parameters. */
    dbg_assert (bpt_initial);
    dbg_assert (iteration_max);
    dbg_assert (bl);

    u64 ber_prev[PHY_FEC_RATE_NB], ber_reached[PHY_FEC_RATE_NB], ber_cur;
    uint it_count;
    /* Dynamic table size is not possible (because of compiler bug on stack).
     * For the moment being we use a define (even if table is too big). */
    dbg_assert (tonemask && tonemask->carrier_nb <= PHY_CARRIER_NB);
    ce_rx_bl_ber_impact_t opti[PHY_CARRIER_NB];
    u32 sort[PHY_FEC_RATE_NB];
    tonemap_t *tm[PHY_FEC_RATE_NB];
    uint tone_en;

    phy_fecrate_t fec_rate;
    /* For each FEC rate. */
    for (fec_rate = PHY_FEC_RATE_1_2; fec_rate < PHY_FEC_RATE_NB; fec_rate++)
    {
        /* Initialize. */
        it_count = 0;
        /* Allocate a tone map for this FEC rate. */
        tm[fec_rate] = tonemap_alloc ();
        /* Get initial BER consign from initial bits per tone. */
        ber_cur = ce_rx_bl_ber_pt_bpt (fec_rate, tonemask->carrier_nb,
                                       bpt_initial[fec_rate]);
        /* Apply BER target margin. */
        if (ber_cur > ce_rx_bl_ber_margin_[fec_rate])
            ber_cur = ber_cur - ce_rx_bl_ber_margin_[fec_rate];
        else
            ber_cur = 0;
        do
        {
            /* Make the tone map reach the BER consign, no optimization. */
            u64 ber_weighted_sum =
                ce_rx_bl_update_tone_map_under_ber_consign (ber_cur, tonemask,
                                                            fec_rate, bl,
                                                            tm[fec_rate],
                                                            opti, &tone_en);

            /* Sort optimization table. */
            ce_rx_bl_sort_optimization (opti, tonemask->carrier_nb);

            /* Optimize tone map to reach BER consign. */
            ce_rx_bl_update_tone_map_at_ber_consign
                (ber_cur, tonemask, bl, tm[fec_rate], opti, &ber_weighted_sum,
                 &tone_en);
            /* Store previous BER consign. */
            ber_prev[fec_rate] = ber_cur;
            /* Store BER target reached. */
            ber_reached[fec_rate] = ber_weighted_sum
                / tm[fec_rate]->bits_per_symbol;
            /* Get new BER consign. */
            ber_cur = ce_rx_bl_ber_pt_bpt
                (fec_rate, tonemask->carrier_nb,
                 (tm[fec_rate]->bits_per_symbol *
                  CE_RX_BL_BPT_QUANT_FACTOR) / tone_en);
            /* Apply BER target margin. */
            if (ber_cur > ce_rx_bl_ber_margin_[fec_rate])
                ber_cur = ber_cur - ce_rx_bl_ber_margin_[fec_rate];
            else
                ber_cur = 0;
        }
        /* Repeat until no modification of the BER consign or until maximum
         * number of iterations is reached. */
        while (++it_count < iteration_max && ber_prev[fec_rate] != ber_cur);
        /* Store generated tone map. */
        sort[fec_rate] = tm[fec_rate]->bits_per_symbol;
    }

    /* Choose best tone map depending on FEC rate. */
    /* 1/2 * 42 -> 21.
     * 16/21 * 42 -> 32. */
    phy_fecrate_t good, bad;
    if ((sort[PHY_FEC_RATE_1_2] * 21)
        > sort[PHY_FEC_RATE_16_21] * 32)
    {
        good = PHY_FEC_RATE_1_2;
        bad = PHY_FEC_RATE_16_21;
    }
    else
    {
        good = PHY_FEC_RATE_16_21;
        bad = PHY_FEC_RATE_1_2;
    }
    /* Remove worst tone map. */
    tonemap_free (tm[bad]);
    /* Configure generated tone map. */
    tm[good]->fecrate = good;
    /* Update pre-computed parameters. */
    tonemap_update (tm[good], TONEMAP_P_PBERROR_DEFAULT_UF32);
    /* Store BER consign of this tone map. */
    tm[good]->ber_target = ber_prev[good];
    /* Store BER reached by this tone map. */
    tm[good]->ber_target_reached = ber_reached[good];
    /* Configure tone map for usage. */
    tm[good]->cpf = true;
    /* Return best tone map. */
    return tm[good];
}

ce_rx_bl_tone_map_update_actions_t
ce_rx_bl_tone_map_update_action (u64 ber_target,
                                 u64 means[CE_RX_BL_BER_SLIDING_MEAN_NB])
{
    /* Default action. */
    ce_rx_bl_tone_map_update_actions_t action = CE_RX_BL_TONE_MAP_UPDATE_NONE;

    /* Fast mean above target ? */
    if (means[CE_RX_BL_BER_SLIDING_MEAN_FAST] > ber_target)
        action = CE_RX_BL_TONE_MAP_UPDATE_MINUS;
    else
    {
        /* Computing middle zone where no action is performed. */
        u64 middle_limit = ber_target
            - (u64) (ber_target / CE_RX_BL_TONE_MAP_UPDATE_MIDDLE_PERCENTAGE);
        /* Are we located inside this middle zone ? */
        if (means[CE_RX_BL_BER_SLIDING_MEAN_FAST] < middle_limit
            && means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] < middle_limit)
            action = CE_RX_BL_TONE_MAP_UPDATE_PLUS;
    }
    return action;
}

u16
ce_rx_bl_tone_map_update_count (ce_rx_bl_tone_map_update_actions_t action,
                                u64 ber_target,
                                u64 means[CE_RX_BL_BER_SLIDING_MEAN_NB])
{
    dbg_assert (action < CE_RX_BL_TONE_MAP_UPDATE_NB);
    dbg_assert (ber_target != 0);

    u64 nb = 0;

    if (action == CE_RX_BL_TONE_MAP_UPDATE_MINUS)
    {
        /* Number of carriers we may shift in the optimisation table. */
        nb = ROUND_DIV (CE_RX_BL_TONE_MAP_UPDATE_MINUS_COEFF *
                        MAX (means[CE_RX_BL_BER_SLIDING_MEAN_FAST],
                             means[CE_RX_BL_BER_SLIDING_MEAN_SLOW]),
                        ber_target)
            - CE_RX_BL_TONE_MAP_UPDATE_MINUS_COEFF + 1;
    }
    else if (action == CE_RX_BL_TONE_MAP_UPDATE_PLUS)
    {
        /* Ber target -X% where X is
         * CE_RX_BL_TONE_MAP_UPDATE_MIDDLE_PERCENTAGE. */
        u64 middle_limit = ber_target
            - (u64) (ber_target / CE_RX_BL_TONE_MAP_UPDATE_MIDDLE_PERCENTAGE);

        /* Number of carriers we may shift in the optimisation table. */
        nb = -1 * ROUND_DIV (CE_RX_BL_TONE_MAP_UPDATE_PLUS_COEFF *
                             MAX (means[CE_RX_BL_BER_SLIDING_MEAN_FAST],
                                  means[CE_RX_BL_BER_SLIDING_MEAN_SLOW]),
                             middle_limit)
            + CE_RX_BL_TONE_MAP_UPDATE_PLUS_COEFF + 1;
    }
    else
        return 0;

    /* Return value must be <= PHY_CARRIER_NB (max shift value). */
    return (nb > PHY_CARRIER_NB ? PHY_CARRIER_NB : nb);
}