forked from VE3NEA/MorseRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RndFunc.pas
142 lines (114 loc) · 3.04 KB
/
RndFunc.pas
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
//------------------------------------------------------------------------------
//This Source Code Form is subject to the terms of the Mozilla Public
//License, v. 2.0. If a copy of the MPL was not distributed with this
//file, You can obtain one at http://mozilla.org/MPL/2.0/.
//------------------------------------------------------------------------------
unit RndFunc;
interface
uses
SysUtils, Math, SndTypes, Ini;
function RndNormal: Single;
function RndGaussLim(AMean, ALim: Single): Single;
function RndRayleigh(AMean: Single): Single;
function BlocksToSeconds(Blocks: Single): Single;
function SecondsToBlocks(Sec: Single): integer;
function RndUniform: Single;
function RndUShaped: Single;
function RndPoisson(AMean: Single): integer;
implementation
function RndNormal: Single;
begin
repeat
try
Result := Sqrt(-2 * Ln(Random)) * Cos(TWO_PI * Random);
Exit;
except
end;
until
False;
end;
function RndGaussLim(AMean, ALim: Single): Single;
begin
Result := AMean + RndNormal * 0.5 * ALim;
Result := Max(AMean-ALim, Min(AMean+ALim, Result));
end;
function RndRayleigh(AMean: Single): Single;
begin
Result := AMean * Sqrt(-Ln(Random) - Ln(Random));
end;
function SecondsToBlocks(Sec: Single): integer;
begin
Result := Round(DEFAULTRATE / Ini.BufSize * Sec);
end;
function BlocksToSeconds(Blocks: Single): Single;
begin
Result := Blocks * Ini.BufSize / DEFAULTRATE;
end;
function RndUniform: Single;
begin
Result := 2*Random - 1;
end;
function RndUShaped: Single;
begin
Result := Sin(Pi*(Random-0.5));
end;
//http://www.library.cornell.edu/nr/bookcpdf/c7-3.pdf
function RndPoisson(AMean: Single): integer;
var
g, t: Single;
begin
g := Exp(-AMean);
t := 1;
for Result:=0 to 30 do
begin
t := t * Random;
if t <= g then Break;
end;
end;
{
//http://www.esbconsult.com.au/
function RndPoisson(AMean: Single): integer;
var
p,q,p0,u: Extended;
l,m,j,k: integer;
pp: array [1..35] of Extended;
begin
// C A S E B. mu < 10
// START NEW TABLE AND CALCULATE P0 IF NECESSARY
m := Max(1, Trunc (AMean));
l := 0;
p := Exp(-AMean);
q := p;
p0 := p;
// STEP U. UNIFORM SAMPLE FOR INVERSION METHOD
repeat
u := Random;
Result := 0;
if (u <= p0) then Exit;
// STEP T. TABLE COMPARISON UNTIL THE END PP(L) OF THE
// PP-TABLE OF CUMULATIVE POISSON PROBABILITIES
// (0.458=PP(9) FOR MU=10)
if l <> 0 then
begin
j := 1;
if (u > 0.458) then j := Min(l, m);
for k := j to l do
if (u <= pp [k]) then
begin Result := K; Exit; end;
if l = 35 then Continue;
end;
// STEP C. CREATION OF NEW POISSON PROBABILITIES P
// AND THEIR CUMULATIVES Q=PP(K)
l := l + 1; // 150
for k := l to 35 do
begin
p := p * AMean / k;
q := q + p;
pp [k] := q;
if (u <= q) then begin Result := K; Exit; end;
end;
l := 35;
until False;
end;
}
end.