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
|
unit u_testfhtunit;
interface
uses Math, Sysutils, u_ap, u_ftbase, u_fft, u_fht;
function TestFHT(Silent : Boolean):Boolean;
function testfhtunit_test_silent():Boolean;
function testfhtunit_test():Boolean;
implementation
procedure RefFHTR1D(var A : TReal1DArray; N : Integer);forward;
procedure RefFHTR1DInv(var A : TReal1DArray; N : Integer);forward;
(*************************************************************************
Test
*************************************************************************)
function TestFHT(Silent : Boolean):Boolean;
var
N : Integer;
I : Integer;
R1 : TReal1DArray;
R2 : TReal1DArray;
R3 : TReal1DArray;
MaxN : Integer;
BiDiErr : Double;
RefErr : Double;
ErrTol : Double;
RefErrors : Boolean;
BiDiErrors : Boolean;
WasErrors : Boolean;
begin
MaxN := 128;
ErrTol := 100000*Power(MaxN, AP_Double(3)/2)*MachineEpsilon;
BiDiErrors := False;
RefErrors := False;
WasErrors := False;
//
// Test bi-directional error: norm(x-invFHT(FHT(x)))
//
BiDiErr := 0;
N:=1;
while N<=MaxN do
begin
//
// FHT/invFHT
//
SetLength(R1, N);
SetLength(R2, N);
SetLength(R3, N);
I:=0;
while I<=N-1 do
begin
R1[I] := 2*RandomReal-1;
R2[I] := R1[I];
R3[I] := R1[I];
Inc(I);
end;
FHTR1D(R2, N);
FHTR1DInv(R2, N);
FHTR1DInv(R3, N);
FHTR1D(R3, N);
I:=0;
while I<=N-1 do
begin
BiDiErr := Max(BiDiErr, AbsReal(R1[I]-R2[I]));
BiDiErr := Max(BiDiErr, AbsReal(R1[I]-R3[I]));
Inc(I);
end;
Inc(N);
end;
BiDiErrors := BiDiErrors or AP_FP_Greater(BiDiErr,ErrTol);
//
// Test against reference O(N^2) implementation
//
RefErr := 0;
N:=1;
while N<=MaxN do
begin
//
// FHT
//
SetLength(R1, N);
SetLength(R2, N);
I:=0;
while I<=N-1 do
begin
R1[I] := 2*RandomReal-1;
R2[I] := R1[I];
Inc(I);
end;
FHTR1D(R1, N);
RefFHTR1D(R2, N);
I:=0;
while I<=N-1 do
begin
RefErr := Max(RefErr, AbsReal(R1[I]-R2[I]));
Inc(I);
end;
//
// inverse FHT
//
SetLength(R1, N);
SetLength(R2, N);
I:=0;
while I<=N-1 do
begin
R1[I] := 2*RandomReal-1;
R2[I] := R1[I];
Inc(I);
end;
FHTR1DInv(R1, N);
RefFHTR1DInv(R2, N);
I:=0;
while I<=N-1 do
begin
RefErr := Max(RefErr, AbsReal(R1[I]-R2[I]));
Inc(I);
end;
Inc(N);
end;
RefErrors := RefErrors or AP_FP_Greater(RefErr,ErrTol);
//
// end
//
WasErrors := BiDiErrors or RefErrors;
if not Silent then
begin
Write(Format('TESTING FHT'#13#10'',[]));
Write(Format('FINAL RESULT: ',[]));
if WasErrors then
begin
Write(Format('FAILED'#13#10'',[]));
end
else
begin
Write(Format('OK'#13#10'',[]));
end;
Write(Format('* BI-DIRECTIONAL TEST: ',[]));
if BiDiErrors then
begin
Write(Format('FAILED'#13#10'',[]));
end
else
begin
Write(Format('OK'#13#10'',[]));
end;
Write(Format('* AGAINST REFERENCE FHT: ',[]));
if RefErrors then
begin
Write(Format('FAILED'#13#10'',[]));
end
else
begin
Write(Format('OK'#13#10'',[]));
end;
if WasErrors then
begin
Write(Format('TEST FAILED'#13#10'',[]));
end
else
begin
Write(Format('TEST PASSED'#13#10'',[]));
end;
end;
Result := not WasErrors;
end;
(*************************************************************************
Reference FHT
*************************************************************************)
procedure RefFHTR1D(var A : TReal1DArray; N : Integer);
var
Buf : TReal1DArray;
I : Integer;
J : Integer;
V : Double;
begin
Assert(N>0, 'RefFHTR1D: incorrect N!');
SetLength(Buf, N);
I:=0;
while I<=N-1 do
begin
V := 0;
J:=0;
while J<=N-1 do
begin
V := V+A[J]*(Cos(2*Pi*I*J/N)+Sin(2*Pi*I*J/N));
Inc(J);
end;
Buf[I] := V;
Inc(I);
end;
I:=0;
while I<=N-1 do
begin
A[I] := Buf[I];
Inc(I);
end;
end;
(*************************************************************************
Reference inverse FHT
*************************************************************************)
procedure RefFHTR1DInv(var A : TReal1DArray; N : Integer);
var
I : Integer;
begin
Assert(N>0, 'RefFHTR1DInv: incorrect N!');
RefFHTR1D(A, N);
I:=0;
while I<=N-1 do
begin
A[I] := A[I]/N;
Inc(I);
end;
end;
(*************************************************************************
Silent unit test
*************************************************************************)
function testfhtunit_test_silent():Boolean;
begin
Result := TestFHT(True);
end;
(*************************************************************************
Unit test
*************************************************************************)
function testfhtunit_test():Boolean;
begin
Result := TestFHT(False);
end;
end.
|