2 Free FFT and convolution (C)
4 Copyright (c) 2020 Project Nayuki. (MIT License)
5 https://www.nayuki.io/page/free-small-fft-in-multiple-languages
7 Permission is hereby granted, free of charge, to any person obtaining a copy of
8 this software and associated documentation files (the "Software"), to deal in
9 the Software without restriction, including without limitation the rights to
10 use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 - The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 - The Software is provided "as is", without warranty of any kind, express or
16 implied, including but not limited to the warranties of merchantability,
17 fitness for a particular purpose and noninfringement. In no event shall the
18 authors or copyright holders be liable for any claim, damages or other
19 liability, whether in an action of contract, tort or otherwise,
20 arising from, out of or in connection with the Software or the use
21 or other dealings in the Software.
28 #include "fft-real-pair.h"
31 // Private function prototypes
32 static size_t reverse_bits(size_t val
, int width
);
33 static void *memdup(const void *src
, size_t n
);
36 bool Fft_transform(float real
[], float imag
[], size_t n
) {
39 else if ((n
& (n
- 1)) == 0) // Is power of 2
40 return Fft_transformRadix2(real
, imag
, n
);
44 bool Fft_inverseTransform(float real
[], float imag
[], size_t n
) {
45 return Fft_transform(imag
, real
, n
);
49 /* looking to replace this with assembler, so limit it to n <= 8 */
50 void Fft_transformRadixL8(float real
[], float imag
[],
51 float cos_table
[], float sin_table
[],
54 size_t size
= (n
/ 2) * sizeof(float);
56 // Cooley-Tukey decimation-in-time radix-2 FFT
57 for (size_t size
= 2; size
<= n
; size
*= 2) {
58 size_t halfsize
= size
/ 2;
59 size_t tablestep
= n
/ size
;
60 for (size_t i
= 0; i
< n
; i
+= size
) {
61 for (size_t j
= i
, k
= 0; j
< i
+ halfsize
; j
++, k
+= tablestep
) {
62 size_t l
= j
+ halfsize
;
63 float tpre
= real
[l
] * cos_table
[k
] + imag
[l
] * sin_table
[k
];
64 float tpim
= -real
[l
] * sin_table
[k
] + imag
[l
] * cos_table
[k
];
65 real
[l
] = real
[j
] - tpre
;
66 imag
[l
] = imag
[j
] - tpim
;
71 if (size
== n
) // Prevent overflow in 'size *= 2'
76 bool Fft_transformRadix2(float real
[], float imag
[], size_t n
) {
79 int levels
= 0; // Compute levels = floor(log2(n))
80 for (size_t temp
= n
; temp
> 1U; temp
>>= 1)
82 if ((size_t)1U << levels
!= n
)
83 return false; // n is not a power of 2
85 // Trigonometric tables
86 if (SIZE_MAX
/ sizeof(float) < n
/ 2)
88 size_t size
= (n
/ 2) * sizeof(float);
89 float *cos_table
= malloc(size
);
90 float *sin_table
= malloc(size
);
91 if (cos_table
== NULL
|| sin_table
== NULL
)
93 for (size_t i
= 0; i
< n
/ 2; i
++) {
94 cos_table
[i
] = cos(2 * M_PI
* i
/ n
);
95 sin_table
[i
] = sin(2 * M_PI
* i
/ n
);
98 // Bit-reversed addressing permutation
99 for (size_t i
= 0; i
< n
; i
++) {
100 size_t j
= reverse_bits(i
, levels
);
102 float temp
= real
[i
];
112 Fft_transformRadixL8(real
, imag
, cos_table
, sin_table
, n
);
114 // Cooley-Tukey decimation-in-time radix-2 FFT
115 for (size_t size
= 2; size
<= n
; size
*= 2) {
116 size_t halfsize
= size
/ 2;
117 size_t tablestep
= n
/ size
;
118 for (size_t i
= 0; i
< n
; i
+= size
) {
119 for (size_t j
= i
, k
= 0; j
< i
+ halfsize
; j
++, k
+= tablestep
) {
120 size_t l
= j
+ halfsize
;
121 float tpre
= real
[l
] * cos_table
[k
] + imag
[l
] * sin_table
[k
];
122 float tpim
= -real
[l
] * sin_table
[k
] + imag
[l
] * cos_table
[k
];
123 real
[l
] = real
[j
] - tpre
;
124 imag
[l
] = imag
[j
] - tpim
;
129 if (size
== n
) // Prevent overflow in 'size *= 2'
143 static size_t reverse_bits(size_t val
, int width
) {
145 for (int i
= 0; i
< width
; i
++, val
>>= 1)
146 result
= (result
<< 1) | (val
& 1U);
151 static void *memdup(const void *src
, size_t n
) {
152 void *dest
= malloc(n
);
153 if (n
> 0 && dest
!= NULL
)
154 memcpy(dest
, src
, n
);