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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
unit u_testconvunit;
interface
uses Math, Sysutils, u_ap, u_ftbase, u_fft, u_conv;
function TestConv(Silent : Boolean):Boolean;
function testconvunit_test_silent():Boolean;
function testconvunit_test():Boolean;
implementation
procedure RefConvC1D(const A : TComplex1DArray;
M : Integer;
const B : TComplex1DArray;
N : Integer;
var R : TComplex1DArray);forward;
procedure RefConvC1DCircular(const A : TComplex1DArray;
M : Integer;
const B : TComplex1DArray;
N : Integer;
var R : TComplex1DArray);forward;
procedure RefConvR1D(const A : TReal1DArray;
M : Integer;
const B : TReal1DArray;
N : Integer;
var R : TReal1DArray);forward;
procedure RefConvR1DCircular(const A : TReal1DArray;
M : Integer;
const B : TReal1DArray;
N : Integer;
var R : TReal1DArray);forward;
(*************************************************************************
Test
*************************************************************************)
function TestConv(Silent : Boolean):Boolean;
var
M : Integer;
N : Integer;
I : Integer;
RKind : Integer;
CircKind : Integer;
RA : TReal1DArray;
RB : TReal1DArray;
RR1 : TReal1DArray;
RR2 : TReal1DArray;
CA : TComplex1DArray;
CB : TComplex1DArray;
CR1 : TComplex1DArray;
CR2 : TComplex1DArray;
MaxN : Integer;
RefErr : Double;
RefRErr : Double;
InvErr : Double;
InvRErr : Double;
ErrTol : Double;
RefErrors : Boolean;
RefRErrors : Boolean;
InvErrors : Boolean;
InvRErrors : Boolean;
WasErrors : Boolean;
begin
MaxN := 32;
ErrTol := 100000*Power(MaxN, AP_Double(3)/2)*MachineEpsilon;
RefErrors := False;
RefRErrors := False;
InvErrors := False;
InvRErrors := False;
WasErrors := False;
//
// Test against reference O(N^2) implementation.
//
// Automatic ConvC1D() and different algorithms of ConvC1DX() are tested.
//
RefErr := 0;
RefRErr := 0;
M:=1;
while M<=MaxN do
begin
N:=1;
while N<=MaxN do
begin
CircKind:=0;
while CircKind<=1 do
begin
RKind:=-3;
while RKind<=1 do
begin
//
// skip impossible combinations of parameters:
// * circular convolution, M<N, RKind<>-3 - internal subroutine does not support M<N.
//
if (CircKind<>0) and (M<N) and (RKind<>-3) then
begin
Inc(RKind);
Continue;
end;
//
// Complex convolution
//
SetLength(CA, M);
I:=0;
while I<=M-1 do
begin
CA[I].X := 2*RandomReal-1;
CA[I].Y := 2*RandomReal-1;
Inc(I);
end;
SetLength(CB, N);
I:=0;
while I<=N-1 do
begin
CB[I].X := 2*RandomReal-1;
CB[I].Y := 2*RandomReal-1;
Inc(I);
end;
SetLength(CR1, 1);
if RKind=-3 then
begin
//
// test wrapper subroutine:
// * circular/non-circular
//
if CircKind=0 then
begin
ConvC1D(CA, M, CB, N, CR1);
end
else
begin
ConvC1DCircular(CA, M, CB, N, CR1);
end;
end
else
begin
//
// test internal subroutine
//
if M>=N then
begin
//
// test internal subroutine:
// * circular/non-circular mode
//
ConvC1DX(CA, M, CB, N, CircKind<>0, RKind, 0, CR1);
end
else
begin
//
// test internal subroutine - circular mode only
//
Assert(CircKind=0, 'Convolution test: internal error!');
ConvC1DX(CB, N, CA, M, False, RKind, 0, CR1);
end;
end;
if CircKind=0 then
begin
RefConvC1D(CA, M, CB, N, CR2);
end
else
begin
RefConvC1DCircular(CA, M, CB, N, CR2);
end;
if CircKind=0 then
begin
I:=0;
while I<=M+N-2 do
begin
RefErr := Max(RefErr, AbsComplex(C_Sub(CR1[I],CR2[I])));
Inc(I);
end;
end
else
begin
I:=0;
while I<=M-1 do
begin
RefErr := Max(RefErr, AbsComplex(C_Sub(CR1[I],CR2[I])));
Inc(I);
end;
end;
//
// Real convolution
//
SetLength(RA, M);
I:=0;
while I<=M-1 do
begin
RA[I] := 2*RandomReal-1;
Inc(I);
end;
SetLength(RB, N);
I:=0;
while I<=N-1 do
begin
RB[I] := 2*RandomReal-1;
Inc(I);
end;
SetLength(RR1, 1);
if RKind=-3 then
begin
//
// test wrapper subroutine:
// * circular/non-circular
//
if CircKind=0 then
begin
ConvR1D(RA, M, RB, N, RR1);
end
else
begin
ConvR1DCircular(RA, M, RB, N, RR1);
end;
end
else
begin
if M>=N then
begin
//
// test internal subroutine:
// * circular/non-circular mode
//
ConvR1DX(RA, M, RB, N, CircKind<>0, RKind, 0, RR1);
end
else
begin
//
// test internal subroutine - non-circular mode only
//
ConvR1DX(RB, N, RA, M, CircKind<>0, RKind, 0, RR1);
end;
end;
if CircKind=0 then
begin
RefConvR1D(RA, M, RB, N, RR2);
end
else
begin
RefConvR1DCircular(RA, M, RB, N, RR2);
end;
if CircKind=0 then
begin
I:=0;
while I<=M+N-2 do
begin
RefRErr := Max(RefRErr, AbsReal(RR1[I]-RR2[I]));
Inc(I);
end;
end
else
begin
I:=0;
while I<=M-1 do
begin
RefRErr := Max(RefRErr, AbsReal(RR1[I]-RR2[I]));
Inc(I);
end;
end;
Inc(RKind);
end;
Inc(CircKind);
end;
Inc(N);
end;
Inc(M);
end;
RefErrors := RefErrors or AP_FP_Greater(RefErr,ErrTol);
RefRErrors := RefRErrors or AP_FP_Greater(RefRErr,ErrTol);
//
// Test inverse convolution
//
InvErr := 0;
InvRErr := 0;
M:=1;
while M<=MaxN do
begin
N:=1;
while N<=MaxN do
begin
//
// Complex circilar and non-circular
//
SetLength(CA, M);
I:=0;
while I<=M-1 do
begin
CA[I].X := 2*RandomReal-1;
CA[I].Y := 2*RandomReal-1;
Inc(I);
end;
SetLength(CB, N);
I:=0;
while I<=N-1 do
begin
CB[I].X := 2*RandomReal-1;
CB[I].Y := 2*RandomReal-1;
Inc(I);
end;
SetLength(CR1, 1);
SetLength(CR2, 1);
ConvC1D(CA, M, CB, N, CR2);
ConvC1DInv(CR2, M+N-1, CB, N, CR1);
I:=0;
while I<=M-1 do
begin
InvErr := Max(InvErr, AbsComplex(C_Sub(CR1[I],CA[I])));
Inc(I);
end;
SetLength(CR1, 1);
SetLength(CR2, 1);
ConvC1DCircular(CA, M, CB, N, CR2);
ConvC1DCircularInv(CR2, M, CB, N, CR1);
I:=0;
while I<=M-1 do
begin
InvErr := Max(InvErr, AbsComplex(C_Sub(CR1[I],CA[I])));
Inc(I);
end;
//
// Real circilar and non-circular
//
SetLength(RA, M);
I:=0;
while I<=M-1 do
begin
RA[I] := 2*RandomReal-1;
Inc(I);
end;
SetLength(RB, N);
I:=0;
while I<=N-1 do
begin
RB[I] := 2*RandomReal-1;
Inc(I);
end;
SetLength(RR1, 1);
SetLength(RR2, 1);
ConvR1D(RA, M, RB, N, RR2);
ConvR1DInv(RR2, M+N-1, RB, N, RR1);
I:=0;
while I<=M-1 do
begin
InvRErr := Max(InvRErr, AbsReal(RR1[I]-RA[I]));
Inc(I);
end;
SetLength(RR1, 1);
SetLength(RR2, 1);
ConvR1DCircular(RA, M, RB, N, RR2);
ConvR1DCircularInv(RR2, M, RB, N, RR1);
I:=0;
while I<=M-1 do
begin
InvRErr := Max(InvRErr, AbsReal(RR1[I]-RA[I]));
Inc(I);
end;
Inc(N);
end;
Inc(M);
end;
InvErrors := InvErrors or AP_FP_Greater(InvErr,ErrTol);
InvRErrors := InvRErrors or AP_FP_Greater(InvRErr,ErrTol);
//
// end
//
WasErrors := RefErrors or RefRErrors or InvErrors or InvRErrors;
if not Silent then
begin
Write(Format('TESTING CONVOLUTION'#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('* AGAINST REFERENCE COMPLEX CONV: ',[]));
if RefErrors then
begin
Write(Format('FAILED'#13#10'',[]));
end
else
begin
Write(Format('OK'#13#10'',[]));
end;
Write(Format('* AGAINST REFERENCE REAL CONV: ',[]));
if RefRErrors then
begin
Write(Format('FAILED'#13#10'',[]));
end
else
begin
Write(Format('OK'#13#10'',[]));
end;
Write(Format('* COMPLEX INVERSE: ',[]));
if InvErrors then
begin
Write(Format('FAILED'#13#10'',[]));
end
else
begin
Write(Format('OK'#13#10'',[]));
end;
Write(Format('* REAL INVERSE: ',[]));
if InvRErrors 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 implementation
*************************************************************************)
procedure RefConvC1D(const A : TComplex1DArray;
M : Integer;
const B : TComplex1DArray;
N : Integer;
var R : TComplex1DArray);
var
I : Integer;
V : Complex;
i_ : Integer;
i1_ : Integer;
begin
SetLength(R, M+N-1);
I:=0;
while I<=M+N-2 do
begin
R[I] := C_Complex(0);
Inc(I);
end;
I:=0;
while I<=M-1 do
begin
V := A[I];
i1_ := (0) - (I);
for i_ := I to I+N-1 do
begin
R[i_] := C_Add(R[i_], C_Mul(V, B[i_+i1_]));
end;
Inc(I);
end;
end;
(*************************************************************************
Reference implementation
*************************************************************************)
procedure RefConvC1DCircular(const A : TComplex1DArray;
M : Integer;
const B : TComplex1DArray;
N : Integer;
var R : TComplex1DArray);
var
I1 : Integer;
I2 : Integer;
J2 : Integer;
Buf : TComplex1DArray;
i_ : Integer;
i1_ : Integer;
begin
RefConvC1D(A, M, B, N, Buf);
SetLength(R, M);
for i_ := 0 to M-1 do
begin
R[i_] := Buf[i_];
end;
I1 := M;
while I1<=M+N-2 do
begin
I2 := Min(I1+M-1, M+N-2);
J2 := I2-I1;
i1_ := (I1) - (0);
for i_ := 0 to J2 do
begin
R[i_] := C_Add(R[i_], Buf[i_+i1_]);
end;
I1 := I1+M;
end;
end;
(*************************************************************************
Reference FFT
*************************************************************************)
procedure RefConvR1D(const A : TReal1DArray;
M : Integer;
const B : TReal1DArray;
N : Integer;
var R : TReal1DArray);
var
I : Integer;
V : Double;
begin
SetLength(R, M+N-1);
I:=0;
while I<=M+N-2 do
begin
R[I] := 0;
Inc(I);
end;
I:=0;
while I<=M-1 do
begin
V := A[I];
APVAdd(@R[0], I, I+N-1, @B[0], 0, N-1, V);
Inc(I);
end;
end;
(*************************************************************************
Reference implementation
*************************************************************************)
procedure RefConvR1DCircular(const A : TReal1DArray;
M : Integer;
const B : TReal1DArray;
N : Integer;
var R : TReal1DArray);
var
I1 : Integer;
I2 : Integer;
J2 : Integer;
Buf : TReal1DArray;
begin
RefConvR1D(A, M, B, N, Buf);
SetLength(R, M);
APVMove(@R[0], 0, M-1, @Buf[0], 0, M-1);
I1 := M;
while I1<=M+N-2 do
begin
I2 := Min(I1+M-1, M+N-2);
J2 := I2-I1;
APVAdd(@R[0], 0, J2, @Buf[0], I1, I2);
I1 := I1+M;
end;
end;
(*************************************************************************
Silent unit test
*************************************************************************)
function testconvunit_test_silent():Boolean;
begin
Result := TestConv(True);
end;
(*************************************************************************
Unit test
*************************************************************************)
function testconvunit_test():Boolean;
begin
Result := TestConv(False);
end;
end.
|