-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric_fft.adb
More file actions
305 lines (295 loc) · 10.6 KB
/
Copy pathgeneric_fft.adb
File metadata and controls
305 lines (295 loc) · 10.6 KB
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
-- --
-- package Generic_FFT Copyright (c) Dmitry A. Kazakov --
-- Implementation Luebeck --
-- Winter, 2025 --
-- --
-- Last revision : 14:35 11 Mar 2025 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
package body Generic_FFT is
Pi : constant :=
3.1415_9265_3589_7932_3846_2643_3832_7950_2884_1971_6939_9375_106;
use Elementary_Functions;
subtype Complex is Complex_Types.Complex;
type Bit_Type is mod (Integer'Last + 1) * 2;
procedure FFT
( Vector : in out Complex_Vector;
Invert : Boolean := False
) is
function Log2 return Bit_Type is
Count : Bit_Type := Vector'Length;
Result : Bit_Type := 0;
begin
if Count = 0 then
raise Constraint_Error;
end if;
while (Count and 1) = 0 loop
Result := Result + 1;
Count := Count / 2;
end loop;
Count := Count / 2;
if Count /= 0 then
raise Constraint_Error;
end if;
return Result;
end Log2;
Size : constant Bit_Type := Log2;
S : Real'Base := -2.0;
begin
if Vector'Length = 1 then
return;
end if;
if Invert then
S := 2.0;
end if;
for I in Bit_Type range 1..Vector'Length - 1 loop
declare -- Reverse Size bits of I
Bits : Bit_Type := Bit_Type (I) / 2;
J : Bit_Type := Bit_Type (I) and 1;
begin
for Bit in 2..Size loop
J := J * 2;
J := J or (Bits and 1);
Bits := Bits / 2;
end loop;
if J < I then
declare
X : Complex renames
Vector (Vector'First + Index_Type'Base (I));
Y : Complex renames
Vector (Vector'First + Index_Type'Base (J));
T : constant Complex := X;
begin
X := Y;
Y := T;
end;
end if;
end;
end loop;
declare
Length : Index_Type'Base := 2;
begin
while Length <= Vector'Length loop
declare
Angle : constant Real'Base :=
PI * S / Real'Base (Length);
Exp : constant Complex := (cos (Angle), sin (Angle));
I : Index_Type'Base := 0;
begin
while I < Vector'Length loop
declare
W : Complex := (1.0, 0.0);
L : constant Index_Type'Base := Length / 2;
begin
for J in 0..L - 1 loop
declare
Index : constant Index_Type :=
Vector'First + I + J;
X : Complex renames Vector (Index);
Y : Complex renames Vector (Index + L);
U : constant Complex := X;
V : constant Complex := Y * W;
begin
X := U + V;
Y := U - V;
W := W * Exp;
end;
end loop;
end;
I := I + Length;
end loop;
end;
Length := Length * 2;
end loop;
end;
if Invert then
declare
N : constant Real'Base := Real'Base (Vector'Length);
begin
for I in Vector'Range loop
Vector (I) := Vector (I) / N;
end loop;
end;
end if;
end FFT;
procedure FFT
( Vector : in out Complex_Vector;
Permutation : Permutation_Array;
Factors : Complex_Vector;
Invert : Boolean := False
) is
Size : constant Bit_Type := Vector'Length;
begin
if Size = 0 or 0 /= ((Size - 1) and Size) then
raise Constraint_Error; -- Vector'Length is not a power of two
elsif 2 ** Factors'Length /= Vector'Length then
raise Constraint_Error;
end if;
if Vector'Length = 1 then
return;
end if;
for Pair in Permutation'Range loop
declare
X : Complex renames
Vector (Vector'First + Permutation (Pair).I);
Y : Complex renames
Vector (Vector'First + Permutation (Pair).J);
T : constant Complex := X;
begin
X := Y;
Y := T;
end;
end loop;
declare
Length : Index_Type'Base := 2;
begin
for Angle in Factors'Range loop
declare
I : Index_Type'Base := 0;
begin
while I < Vector'Length loop
declare
W : Complex := (1.0, 0.0);
L : constant Index_Type'Base := Length / 2;
begin
for J in 0..L - 1 loop
declare
Index : constant Index_Type :=
Vector'First + I + J;
X : Complex renames Vector (Index);
Y : Complex renames Vector (Index + L);
U : constant Complex := X;
V : constant Complex := Y * W;
begin
X := U + V;
Y := U - V;
W := W * Factors (Angle);
end;
end loop;
end;
I := I + Length;
end loop;
end;
Length := Length * 2;
end loop;
end;
if Invert then
declare
N : constant Real'Base := Real'Base (Vector'Length);
begin
for I in Vector'Range loop
Vector (I) := Vector (I) / N;
end loop;
end;
end if;
end FFT;
function FFT
( Vector : Complex_Vector;
Invert : Boolean := False
) return Complex_Vector is
Count : Bit_Type := Vector'Length;
Size : Bit_Type := 0;
begin
if Vector'Length = 0 then
raise Constraint_Error;
end if;
while (Count and 1) = 0 loop
Size := Size + 1;
Count := Count / 2;
end loop;
Count := Count / 2;
if Count = 0 then
declare
Result : Complex_Vector := Vector;
begin
FFT (Result, Invert);
return Result;
end;
end if;
Size := Size + 1;
loop
Size := Size + 1;
Count := Count / 2;
exit when Count = 0;
end loop;
declare
Result : Complex_Vector
( Vector'First
.. Vector'First + 2 ** Natural (Size) - 1
);
begin
Result (Vector'Range) := Vector;
for Index in Vector'Last + 1..Result'Last loop
Result (Index) := (0.0, 0.0);
end loop;
FFT (Result, Invert);
return Result;
end;
end FFT;
function FFT_Factors
( N : Positive;
Invert : Boolean := False
) return Complex_Vector is
Result : Complex_Vector
( Index_Type'First
.. Index_Type'First + Index_Type (N) - 1
);
Angle : Real;
begin
if Invert then
Angle := Pi;
else
Angle :=-Pi;
end if;
for I in Result'Range loop
Result (I) := (cos (Angle), sin (Angle));
Angle := Angle / 2.0;
end loop;
return Result;
end FFT_Factors;
function FFT_Permutation (N : Positive) return Permutation_Array is
Result : Permutation_Array (1..2 ** N);
Last : Natural := 0;
begin
if N > 1 then
for I in Bit_Type range 1..2 ** N - 1 loop
declare -- Reverse Size bits of I
Bits : Bit_Type := Bit_Type (I) / 2;
J : Bit_Type := Bit_Type (I) and 1;
begin
for Bit in 2..Bit_Type (N) loop
J := J * 2;
J := J or (Bits and 1);
Bits := Bits / 2;
end loop;
if I < J then
Last := Last + 1;
Result (Last) :=
( I => Index_Type'Base (I),
J => Index_Type'Base (J)
);
end if;
end;
end loop;
end if;
return Result (1..Last);
end FFT_Permutation;
end Generic_FFT;