-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.h
215 lines (195 loc) · 8.97 KB
/
fft.h
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
/**
* API of Fast Fourier Transformation (FFT) in 5 flavours:
* 1.) FFT by definition algorithm (actually DFT)
* 2.) Cooley–Tukey FFT algorithm (recursive)
* 3.) Cooley–Tukey FFT algorithm (iterative)
* 4.) Bluestein FFT algorithm
* 5.) Chirp-Z FFT algorithm (!!!Does currently NOT work!!!)
*
* @version 1.0
* @date 2023-07-10
* @file fft.h
* @author Richard Saeuberlich ([email protected])
*/
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <complex.h>
/**
* Holds actual array (with data) & size of this array
*/
struct FFTResult {
float complex* data;
size_t size;
};
/**
* Bit significance:
* - MSB (Most significant bit): Most left one
* - LSB (Least significant bit): Most right one
*/
enum BitSignificance {
MSB, LSB
};
/**
* Enumeration of all implemented FFT algorithms
*/
enum FFTAlgorithm {
BY_DEFINITION,
COOLEY_TUKEY_RECURSIVE,
COOLEY_TUKEY_ITERATIVE,
BLUESTEIN,
CHIRP_Z
};
static const bool CHECK_ARGUMENTS = true;
static const enum BitSignificance SIGNIFICANCE = MSB;
/**
* This is not really a FFT but the implement of the
* Discrete Fourier Transformation (DFT) by definition.
*
* @param dataIN Samples/Data array of size 'size'
* @param dataOUT Result array of size 'size'
* @param size Number of samples/data points
* @see https://en.wikipedia.org/wiki/Fast_Fourier_transform#Definition
*/
void fftByDefinition(const float complex* dataIN, float complex* dataOUT, size_t size);
/**
* This is not really a FFT but the implement of the
* Discrete Fourier Transformation (DFT) by definition.
*
* @param dataIN Samples/Data array of size 'size'
* @param dataOUT Result array of size 'size'
* @param size Number of samples/data points
* @see https://en.wikipedia.org/wiki/Fast_Fourier_transform#Definition
*/
void inverseFFTByDefinition(const float complex* dataIN, float complex* dataOUT, size_t size);
/**
* This is the classical FFT implementation after the Cooley–Tukey algorithm.
*
* @param dataIN Samples/Data array of size 'size' (must be of length of power of 2)
* @param dataOUT Result array of size 'size' (must be of length of power of 2)
* @param size Number of samples/data points (must be power of 2)
* @see https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm
*/
void fftCooleyTukeyRecursive(const float complex* dataIN, float complex* dataOUT, size_t size);
/**
* This is the inverse FFT of the 'fftCooleyTukeyRecursive'.
*
* @param dataIN Samples/Data array of size 'size' (must be of length of power of 2)
* @param dataOUT Result array of size 'size' (must be of length of power of 2)
* @param size Number of samples/data points (must be power of 2)
* @see https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm
*/
void inverseFFTCooleyTukeyRecursive(const float complex* dataIN, float complex* dataOUT, size_t size);
/**
* This Cooley-Tukey FFT implementation works NOT recursive, but with a reordering
* of the resulting array. In this specific case the radix-2 decimation-in-time (DIT) is used.
*
* @param dataIN Samples/Data array of size 'size' (must be of length of power of 2)
* @param dataOUT Result array of size 'size' (must be of length of power of 2)
* @param size Number of samples/data points (must be power of 2)
* @see https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm
*/
void fftCooleyTukeyIterative(const float complex* dataIN, float complex* dataOUT, size_t size);
/**
* This is the inverse FFT of the 'fftCooleyTukeyIterative'.
*
* @param dataIN Samples/Data array of size 'size' (must be of length of power of 2)
* @param dataOUT Result array of size 'size' (must be of length of power of 2)
* @param size Number of samples/data points (must be power of 2)
* @see https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm
*/
void inverseFFTCooleyTukeyIterative(const float complex* dataIN, float complex* dataOUT, size_t size);
/**
* The Bluestein FFT algorithm, needs some zero initialized arrays.
* This is a quick/efficient way to fill a given array with zeros.
*
* @param data Array that will hold only zeros afterwards of size 'size'
* @param size Size of given array
*/
void resetArray(float complex* data, size_t size);
/**
* Main advantage of Bluestein algorithm is, you don't need an array of size of power 2
*
* @param dataIN Samples/Data array of size 'size'
* @param dataOUT Result array of size 'size'
* @param twiddleFactors Array of size 'size', array doesn't need to be initialized
* @param a Array of size 'powerOfTwoSize', all entries must be initialized with 0
* @param b Array of size 'powerOfTwoSize', all entries must be initialized with 0
* @param c Array of size 'powerOfTwoSize', array doesn't need to be initialized with zeros
* @param d Array of size 'powerOfTwoSize', array doesn't need to be initialized with zeros
* @param powerOfTwoSize Next power-of-2 such that following equation is true: 'powerOfTwoSize' >= 'size' * 2 + 1
* @param size Number of samples/data points
* @see https://en.wikipedia.org/wiki/Chirp_Z-transform#Bluestein's_algorithm
*/
void fftBluestein(const float complex* dataIN, float complex* dataOUT, float complex* twiddleFactors, float complex* a, float complex* b, float complex* c, float complex* d, size_t powerOfTwoSize, size_t size);
/**
* This is the inverse FFT of the 'fftBluestein'.
*
* @param dataIN Samples/Data array of size 'size'
* @param dataOUT Result array of size 'size'
* @param twiddleFactors Array of size 'size', array doesn't need to be initialized for twiddle calculation
* @param a Array of size 'powerOfTwoSize', all entries must be initialized with 0
* @param b Array of size 'powerOfTwoSize', all entries must be initialized with 0
* @param c Array of size 'powerOfTwoSize', array doesn't need to be initialized with zeros
* @param d Array of size 'powerOfTwoSize', array doesn't need to be initialized with zeros
* @param powerOfTwoSize Next power-of-2 such that following equation is true: 'powerOfTwoSize' >= 'size' * 2 + 1
* @param size Number of samples/data points
* @see https://en.wikipedia.org/wiki/Chirp_Z-transform#Bluestein's_algorithm
*/
void inverseFFTBluestein(const float complex* dataIN, float complex* dataOUT, float complex* twiddleFactors, float complex* a, float complex* b, float complex* c, float complex* d, size_t powerOfTwoSize, size_t size);
/**
* Main advantage of Chirp-Z algorithm is, you don't need an array of size of power 2
*
* @param dataIN Samples/Data array of size 'size'
* @param dataOUT Result
* @param chirps Empty array for chirp calculation
* @param weights Empty array for weight calculation
* @param size Number of samples/data points
* @see https://en.wikipedia.org/wiki/Chirp_Z-transform
*/
void fftChirpZ(const float complex* dataIN, float complex* dataOUT, float complex* chirps, float complex* weights, size_t size);
/**
* This is the inverse FFT of the 'fftChirpZ'
*
* @param dataIN Samples/Data array of size 'size'
* @param dataOUT Result array of size 'size'
* @param chirps Array of size 'size' for chirp calculation
* @param weights Array of size 'size' for weight calculation
* @param size Number of samples/data points
* @see https://en.wikipedia.org/wiki/Chirp_Z-transform
*/
void inverseFFTChirpZ(const float complex* dataIN, float complex* dataOUT, float complex* chirps, float complex* weights, size_t size);
// ==========================================================================
// Convenient API functions
// ==========================================================================
/**
* Deletes given FFTResult struct
*
* @param result FFTResult struct to be deleted
*/
void deleteFFTResult(struct FFTResult* result);
/**
* Executes FFT with desired algorithm
*
* NOTE: Use this convenient function call only if you're allowed to place more complex data structures on the stack without any problems.
* If you are not allowed to do this, you should call the individual FFT functions directly, with pre-initialized arrays.
*
* @param algorithm Algorithm of choice
* @param data Samples/Data array of size 'size'
* @param size Number of samples/data points
* @return Struct with new allocated array (resulting data) & size, or NULL if chosen FFT algorithm was unknown
*/
struct FFTResult* fft(enum FFTAlgorithm algorithm, const float complex* data, size_t size);
/**
* Executes inverse FFT with desired algorithm
*
* NOTE: Use this convenient function call only if you're allowed to place more complex data structures on the stack without any problems.
* If you are not allowed to do this, you should call the individual FFT functions directly, with pre-initialized arrays.
*
* @param algorithm Algorithm of choice
* @param data Samples/Data array of size 'size'
* @param size Number of samples/data points
* @return Struct with new allocated array (resulting data) & size, or NULL if chosen FFT algorithm was unknown
*/
struct FFTResult* inverseFFT(enum FFTAlgorithm algorithm, const float complex* data, size_t size);