-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfft.cpp
More file actions
137 lines (132 loc) · 4.13 KB
/
fft.cpp
File metadata and controls
137 lines (132 loc) · 4.13 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
#include "fft.h"
#include <math.h>
#include <complex>
#include <iostream>
#define HI 50.0
#define LO -50.0
#define SAMPLE_LENGTH 16
using namespace std;
void main()
{
complex <float> *pSignal = new complex <float> [SAMPLE_LENGTH];
for(int i = 0; i < SAMPLE_LENGTH; i++)
{
float real = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));
float virt = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));
pSignal[i] = complex <float> (real,virt);
cout<<pSignal[i] <<",";
}
//Apply FFT
CFFT::Forward(pSignal, SAMPLE_LENGTH);
cout<<endl;
for(int i = 0; i < SAMPLE_LENGTH; i++)
{
cout<<pSignal[i] <<",";
}
//i-FFT
CFFT::Inverse(pSignal, SAMPLE_LENGTH);
cout<<endl;
for(int i = 0; i < SAMPLE_LENGTH; i++)
{
cout<<pSignal[i] <<",";
}
//Free memory
delete[] pSignal;
}
void CFFT::Scale(complex<float> *const Data, const unsigned int N)
{
const double Factor = 1. / double(N);
// Scale all data entries
for (unsigned int Position = 0; Position < N; ++Position)
Data[Position] *= Factor;
}
bool CFFT::Inverse(complex<float> *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)
CFFT::Scale(Data, N);
// Succeeded
return true;
}
bool CFFT::Forward(complex <float> *const data, const unsigned int length)
{
//Check input parameters
if (!data || length < 1 || length & (length - 1))
return false;
//Rearrange
Rearrange(data, length);
//Call FFT implementation
Perform(data, length);
//Succeeded
return true;
}
void CFFT::Rearrange(complex <float> *const data, const unsigned int length)
{
//Swap position
unsigned int Target = 0;
//Process all positions of input signal
for (unsigned int Position = 0; Position < length; ++Position)
{
//Only for not yet swapped entries
if (Target > Position)
{
//Swap entries
const complex <float> Temp(data[Target]);
data[Target] = data[Position];
data[Position] = Temp;
}
//Bit mask
unsigned int Mask = length;
//While bit is set
while (Target & (Mask >>= 1))
//Drop bit
Target &= ~Mask;
//The current bit is 0 - set it
Target |= Mask;
}
}
void CFFT::Perform(complex <float> *const data, const unsigned int length,
const bool Inverse /* = false */)
{
const double pi = Inverse ? 3.14159265358979323846 : -3.14159265358979323846;
//Iteration through dyads, quadruples, octads and so on...
for (unsigned int Step = 1; Step < length; Step <<= 1)
{
//Jump to the next entry of the same transform factor
const unsigned int Jump = Step << 1;
//Angle increment
const double delta = pi / double(Step);
//Auxiliary sin(delta / 2)
const double Sine = sin(delta * .5);
//Multiplier for trigonometric recurrence
const complex <float> Multiplier(-2.0 * Sine * Sine, sin(delta));
//Start value for transform factor, fi = 0
complex <float> 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 < length; Pair += Jump)
{
//Match position
const unsigned int Match = Pair + Step;
//Second term of two-point transform
const complex <float> 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;
}
}
}