comparison libgsmfrp/prng.c @ 108:3b64f255689a

libgsmfrp: factor out PRNG into its own module, in preparation for using it as part of speech muting too
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 28 Nov 2022 04:00:18 +0000
parents libgsmfrp/comfort_noise.c@286d5f097eb4
children
comparison
equal deleted inserted replaced
107:41f1ae68d253 108:3b64f255689a
1 /*
2 * We use a pseudorandom sequence generator function in two places:
3 * when generating comfort noise (GSM 06.12 section 6.1), and when
4 * randomizing grid position parameters as part of speech muting
5 * during error handling (GSM 06.11 section 6).
6 *
7 * Our PRNG is a copy of the pseudonoise() function from ETSI EFR code,
8 * where it is used for similar purposes.
9 */
10
11 #include <stdint.h>
12 #include "gsm_fr_preproc.h"
13 #include "internal.h"
14
15 uint16_t gsmfr_preproc_prng(struct gsmfr_preproc_state *st, uint16_t no_bits)
16 {
17 uint16_t noise_bits, Sn, i;
18
19 noise_bits = 0;
20 for (i = 0; i < no_bits; i++)
21 {
22 /* State n == 31 */
23 if ((st->cn_random_lfsr & 0x00000001L) != 0)
24 {
25 Sn = 1;
26 }
27 else
28 {
29 Sn = 0;
30 }
31
32 /* State n == 3 */
33 if ((st->cn_random_lfsr & 0x10000000L) != 0)
34 {
35 Sn = Sn ^ 1;
36 }
37 else
38 {
39 Sn = Sn ^ 0;
40 }
41
42 noise_bits = noise_bits << 1;
43 noise_bits = noise_bits | st->cn_random_lfsr & 1;
44
45 st->cn_random_lfsr >>= 1;
46 if (Sn & 1)
47 {
48 st->cn_random_lfsr |= 0x40000000L;
49 }
50 }
51
52 return noise_bits;
53 }