1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 
12 /*
13  * This file contains implementations of the randomization functions
14  * WebRtcSpl_IncreaseSeed()
15  * WebRtcSpl_RandU()
16  * WebRtcSpl_RandN()
17  * WebRtcSpl_RandUArray()
18  *
19  * The description header can be found in signal_processing_library.h
20  *
21  */
22 
23 #include "signal_processing_library.h"
24 
25 static const WebRtc_Word16 kRandNTable[] = {
26     9178,    -7260,       40,    10189,     4894,    -3531,   -13779,    14764,
27    -4008,    -8884,    -8990,     1008,     7368,     5184,     3251,    -5817,
28    -9786,     5963,     1770,     8066,    -7135,    10772,    -2298,     1361,
29     6484,     2241,    -8633,      792,      199,    -3344,     6553,   -10079,
30   -15040,       95,    11608,   -12469,    14161,    -4176,     2476,     6403,
31    13685,   -16005,     6646,     2239,    10916,    -3004,     -602,    -3141,
32     2142,    14144,    -5829,     5305,     8209,     4713,     2697,    -5112,
33    16092,    -1210,    -2891,    -6631,    -5360,   -11878,    -6781,    -2739,
34    -6392,      536,    10923,    10872,     5059,    -4748,    -7770,     5477,
35       38,    -1025,    -2892,     1638,     6304,    14375,   -11028,     1553,
36    -1565,    10762,     -393,     4040,     5257,    12310,     6554,    -4799,
37     4899,    -6354,     1603,    -1048,    -2220,     8247,     -186,    -8944,
38   -12004,     2332,     4801,    -4933,     6371,      131,     8614,    -5927,
39    -8287,   -22760,     4033,   -15162,     3385,     3246,     3153,    -5250,
40     3766,      784,     6494,      -62,     3531,    -1582,    15572,      662,
41    -3952,     -330,    -3196,      669,     7236,    -2678,    -6569,    23319,
42    -8645,     -741,    14830,   -15976,     4903,      315,   -11342,    10311,
43     1858,    -7777,     2145,     5436,     5677,     -113,   -10033,      826,
44    -1353,    17210,     7768,      986,    -1471,     8291,    -4982,     8207,
45   -14911,    -6255,    -2449,   -11881,    -7059,   -11703,    -4338,     8025,
46     7538,    -2823,   -12490,     9470,    -1613,    -2529,   -10092,    -7807,
47     9480,     6970,   -12844,     5123,     3532,     4816,     4803,    -8455,
48    -5045,    14032,    -4378,    -1643,     5756,   -11041,    -2732,   -16618,
49    -6430,   -18375,    -3320,     6098,     5131,    -4269,    -8840,     2482,
50    -7048,     1547,   -21890,    -6505,    -7414,     -424,   -11722,     7955,
51     1653,   -17299,     1823,      473,    -9232,     3337,     1111,      873,
52     4018,    -8982,     9889,     3531,   -11763,    -3799,     7373,    -4539,
53     3231,     7054,    -8537,     7616,     6244,    16635,      447,    -2915,
54    13967,      705,    -2669,    -1520,    -1771,   -16188,     5956,     5117,
55     6371,    -9936,    -1448,     2480,     5128,     7550,    -8130,     5236,
56     8213,    -6443,     7707,    -1950,   -13811,     7218,     7031,    -3883,
57       67,     5731,    -2874,    13480,    -3743,     9298,    -3280,     3552,
58    -4425,      -18,    -3785,    -9988,    -5357,     5477,   -11794,     2117,
59     1416,    -9935,     3376,      802,    -5079,    -8243,    12652,       66,
60     3653,    -2368,     6781,   -21895,    -7227,     2487,     7839,     -385,
61     6646,    -7016,    -4658,     5531,    -1705,      834,      129,     3694,
62    -1343,     2238,   -22640,    -6417,   -11139,    11301,    -2945,    -3494,
63    -5626,      185,    -3615,    -2041,    -7972,    -3106,      -60,   -23497,
64    -1566,    17064,     3519,     2518,      304,    -6805,   -10269,     2105,
65     1936,     -426,     -736,    -8122,    -1467,     4238,    -6939,   -13309,
66      360,     7402,    -7970,    12576,     3287,    12194,    -6289,   -16006,
67     9171,     4042,    -9193,     9123,    -2512,     6388,    -4734,    -8739,
68     1028,    -5406,    -1696,     5889,     -666,    -4736,     4971,     3565,
69     9362,    -6292,     3876,    -3652,   -19666,     7523,    -4061,      391,
70   -11773,     7502,    -3763,     4929,    -9478,    13278,     2805,     4496,
71     7814,    16419,    12455,   -14773,     2127,    -2746,     3763,     4847,
72     3698,     6978,     4751,    -6957,    -3581,      -45,     6252,     1513,
73    -4797,    -7925,    11270,    16188,    -2359,    -5269,     9376,   -10777,
74     7262,    20031,    -6515,    -2208,    -5353,     8085,    -1341,    -1303,
75     7333,     5576,     3625,     5763,    -7931,     9833,    -3371,   -10305,
76     6534,   -13539,    -9971,      997,     8464,    -4064,    -1495,     1857,
77    13624,     5458,     9490,   -11086,    -4524,    12022,     -550,     -198,
78      408,    -8455,    -7068,    10289,     9712,    -3366,     9028,    -7621,
79    -5243,     2362,     6909,     4672,    -4933,    -1799,     4709,    -4563,
80      -62,     -566,     1624,    -7010,    14730,   -17791,    -3697,    -2344,
81    -1741,     7099,    -9509,    -6855,    -1989,     3495,    -2289,     2031,
82    12784,      891,    14189,    -3963,    -5683,      421,   -12575,     1724,
83   -12682,    -5970,    -8169,     3143,    -1824,    -5488,    -5130,     8536,
84    12799,      794,     5738,     3459,   -11689,     -258,    -3738,    -3775,
85    -8742,     2333,     8312,    -9383,    10331,    13119,     8398,    10644,
86   -19433,    -6446,   -16277,   -11793,    16284,     9345,    15222,    15834,
87     2009,    -7349,      130,   -14547,      338,    -5998,     3337,    21492,
88     2406,     7703,     -951,    11196,     -564,     3406,     2217,     4806,
89     2374,    -5797,    11839,     8940,   -11874,    18213,     2855,    10492
90 };
91 
WebRtcSpl_IncreaseSeed(WebRtc_UWord32 * seed)92 WebRtc_UWord32 WebRtcSpl_IncreaseSeed(WebRtc_UWord32 *seed)
93 {
94     seed[0] = (seed[0] * ((WebRtc_Word32)69069) + 1) & (WEBRTC_SPL_MAX_SEED_USED - 1);
95     return seed[0];
96 }
97 
WebRtcSpl_RandU(WebRtc_UWord32 * seed)98 WebRtc_Word16 WebRtcSpl_RandU(WebRtc_UWord32 *seed)
99 {
100     return (WebRtc_Word16)(WebRtcSpl_IncreaseSeed(seed) >> 16);
101 }
102 
WebRtcSpl_RandN(WebRtc_UWord32 * seed)103 WebRtc_Word16 WebRtcSpl_RandN(WebRtc_UWord32 *seed)
104 {
105     return kRandNTable[WebRtcSpl_IncreaseSeed(seed) >> 23];
106 }
107 
108 // Creates an array of uniformly distributed variables
WebRtcSpl_RandUArray(WebRtc_Word16 * vector,WebRtc_Word16 vector_length,WebRtc_UWord32 * seed)109 WebRtc_Word16 WebRtcSpl_RandUArray(WebRtc_Word16* vector,
110                                    WebRtc_Word16 vector_length,
111                                    WebRtc_UWord32* seed)
112 {
113     int i;
114     for (i = 0; i < vector_length; i++)
115     {
116         vector[i] = WebRtcSpl_RandU(seed);
117     }
118     return vector_length;
119 }
120