-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fast4ier.cpp
183 lines (171 loc) · 5.18 KB
/
Fast4ier.cpp
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
// fft.cpp - impelementation of class
// of fast Fourier transform - FFT
//
// The code is property of LIBROW
// You can use it on your own
// When utilizing credit LIBROW site
// http://www.librow.com/articles/article-10
// Reworked from original from LIBROW (info above) to Arduino Lib in c++
// Include declaration file
#include <fast4ier.h>
// FORWARD FOURIER TRANSFORM
// Input - input data
// Output - transform result
// N - length of both input data and result
bool Fast4::FFT(const complex *const Input, complex *const Output, const unsigned int N)
{
// Check input parameters
if (!Input || !Output || N < 1 || N & (N - 1))
return false;
// Initialize data
Rearrange(Input, Output, N);
// Call FFT implementation
Perform(Output, N);
// Succeeded
return true;
}
// FORWARD FOURIER TRANSFORM, INPLACE VERSION
// Data - both input data and output
// N - length of input data
bool Fast4::FFT(complex *const Data, const unsigned int N)
{
// Check input parameters
if (!Data || N < 1 || N & (N - 1))
return false;
// Rearrange
Rearrange(Data, N);
// Call FFT implementation
Perform(Data, N);
// Succeeded
return true;
}
// INVERSE FOURIER TRANSFORM
// Input - input data
// Output - transform result
// N - length of both input data and result
// Scale - if to scale result
bool Fast4::IFFT(const complex *const Input, complex *const Output, const unsigned int N, const bool Scale /* = true */)
{
// Check input parameters
if (!Input || !Output || N < 1 || N & (N - 1))
return false;
// Initialize data
Rearrange(Input, Output, N);
// Call FFT implementation
Perform(Output, N, true);
// Scale if necessary
if (Scale)
Fast4::Scale(Output, N);
// Succeeded
return true;
}
// INVERSE FOURIER TRANSFORM, INPLACE VERSION
// Data - both input data and output
// N - length of both input data and result
// Scale - if to scale result
bool Fast4::IFFT(complex *const Data, const unsigned int N, const bool Scale /* = true */)
{
// Check input parameters
if (!Data || N < 1 || N & (N - 1))
return false;
// Rearrange
Rearrange(Data, N);
// Call FFT implementation
Perform(Data, N, true);
// Scale if necessary
if (Scale)
Fast4::Scale(Data, N);
// Succeeded
return true;
}
// Rearrange function
void Fast4::Rearrange(const complex *const Input, complex *const Output, const unsigned int N)
{
// Data entry position
unsigned int Target = 0;
// Process all positions of input signal
for (unsigned int Position = 0; Position < N; ++Position)
{
// Set data entry
Output[Target] = Input[Position];
// Bit mask
unsigned int Mask = N;
// While bit is set
while (Target & (Mask >>= 1))
// Drop bit
Target &= ~Mask;
// The current bit is 0 - set it
Target |= Mask;
}
}
// Inplace version of rearrange function
void Fast4::Rearrange(complex *const Data, const unsigned int N)
{
// Swap position
unsigned int Target = 0;
// Process all positions of input signal
for (unsigned int Position = 0; Position < N; ++Position)
{
// Only for not yet swapped entries
if (Target > Position)
{
// Swap entries
const complex Temp(Data[Target]);
Data[Target] = Data[Position];
Data[Position] = Temp;
}
// Bit mask
unsigned int Mask = N;
// While bit is set
while (Target & (Mask >>= 1))
// Drop bit
Target &= ~Mask;
// The current bit is 0 - set it
Target |= Mask;
}
}
// FFT implementation
void Fast4::Perform(complex *const Data, const unsigned int N, const bool Inverse /* = false */)
{
const FLT pi = Inverse ? 3.14159265358979323846 : -3.14159265358979323846;
// Iteration through dyads, quadruples, octads and so on...
for (unsigned int Step = 1; Step < N; Step <<= 1)
{
// Jump to the next entry of the same transform factor
const unsigned int Jump = Step << 1;
// Angle increment
const FLT delta = pi / FLT(Step);
// Auxiliary sin(delta / 2)
const FLT Sine = sin(delta * .5);
// Multiplier for trigonometric recurrence
const complex Multiplier(-2. * Sine * Sine, sin(delta));
// Start value for transform factor, fi = 0
complex Factor(1.);
// Iteration through groups of different transform factor
for (unsigned int Group = 0; Group < Step; ++Group)
{
// Iteration within group
for (unsigned int Pair = Group; Pair < N; Pair += Jump)
{
// Match position
const unsigned int Match = Pair + Step;
// Second term of two-point transform
const complex Product(Factor * Data[Match]);
// Transform for fi + pi
Data[Match] = Data[Pair] - Product;
// Transform for fi
Data[Pair] += Product;
}
// Successive transform factor via trigonometric recurrence
Factor = Multiplier * Factor + Factor;
}
}
}
// Scaling of inverse FFT result
void Fast4::Scale(complex *const Data, const unsigned int N)
{
const FLT Factor = 1. / FLT(N);
// Scale all data entries
for (unsigned int Position = 0; Position < N; ++Position)
Data[Position] *= Factor;
}