forked from 1chipML/1chipML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_FFT.c
176 lines (141 loc) · 4.88 KB
/
test_FFT.c
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
#include <1chipml.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int compareFT(const unsigned length, fft_real* incomingReals,
fft_real* incomingImgs, fft_real* expectedReals,
fft_real* expectedImgs) {
const fft_real epsilon = 1e-10;
for (unsigned i = 0; i < length; ++i) {
if (fabs(incomingReals[i] - expectedReals[i]) > epsilon ||
fabs(incomingImgs[i] - expectedImgs[i]) > epsilon) {
return 1;
}
}
return 0;
}
static int randomTestingFFT(const unsigned maxLengthPower) {
const unsigned length = (unsigned)pow(
2, (unsigned)(linear_congruential_random_generator() * maxLengthPower));
// allocate arrays for testing
fft_real fftReals[length];
fft_real fftImgs[length];
fft_real dftReals[length];
fft_real dftImgs[length];
for (unsigned i = 0; i < length; ++i) {
fftReals[i] = linear_congruential_random_generator();
fftImgs[i] = 0;
dftReals[i] = fftReals[i];
dftImgs[i] = fftImgs[i];
}
clock_t start, end;
double algorithmTime = 0.0;
// Algorithm time approximation
start = clock();
FFT(length, fftReals, fftImgs, 1);
end = clock();
algorithmTime = (double)(end - start) / CLOCKS_PER_SEC;
printf("The elapsed time for the FFT is %f seconds\n", algorithmTime);
start = clock();
DFT(length, dftReals, dftImgs, 1);
end = clock();
algorithmTime = (double)(end - start) / CLOCKS_PER_SEC;
printf("The elapsed time for the DFT is %f seconds\n", algorithmTime);
int isSimilar = compareFT(length, fftReals, fftImgs, dftReals, dftImgs);
printf("Random testing FFT: is the FFT working as intended? ");
int returnCode = 0;
if (isSimilar == 0) {
printf("true\n");
returnCode = 0;
} else {
printf("false\n");
returnCode = 1;
}
return returnCode;
}
static int randomTestingFFTI(const unsigned maxLengthPower) {
const unsigned length = (unsigned)pow(
2, (unsigned)(linear_congruential_random_generator() * maxLengthPower));
// allocate arrays for testing
fft_real reals[length];
fft_real imgs[length];
fft_real fftReals[length];
fft_real fftImgs[length];
for (unsigned i = 0; i < length; ++i) {
reals[i] = linear_congruential_random_generator();
imgs[i] = 0;
fftReals[i] = reals[i];
fftImgs[i] = imgs[i];
}
DFT(length, fftReals, fftImgs, 1);
FFT(length, fftReals, fftImgs, -1);
int isSimilar = compareFT(length, reals, imgs, fftReals, fftImgs);
printf("Random testing inverse FFT: is the FFT working as intended? ");
int returnCode = 0;
if (isSimilar == 0) {
printf("true\n");
returnCode = 0;
} else {
printf("false\n");
returnCode = 1;
}
return returnCode;
}
static int knownTestingFFT(const unsigned length, fft_real* inputReals,
fft_real* inputImaginaries, fft_real* expectedReals,
fft_real* expectedImaginaries) {
FFT(length, inputReals, inputImaginaries, 1);
int isSimilar = compareFT(length, inputReals, inputImaginaries, expectedReals,
expectedImaginaries);
printf("Known testing FFT: is the FFT working as intended? ");
int returnCode = 0;
if (isSimilar == 0) {
printf("true\n");
returnCode = 0;
} else {
printf("false\n");
returnCode = 1;
}
return returnCode;
}
static int knownTestingFFTI(const unsigned length, fft_real* inputReals,
fft_real* inputImaginaries, fft_real* expectedReals,
fft_real* expectedImaginaries) {
FFT(length, inputReals, inputImaginaries, -1);
int isSimilar = compareFT(length, inputReals, inputImaginaries, expectedReals,
expectedImaginaries);
printf("Known testing inverse FFT: is the FFT working as intended? ");
int returnCode = 0;
if (isSimilar == 0) {
printf("true\n");
returnCode = 0;
} else {
printf("false\n");
returnCode = 1;
}
return returnCode;
}
int main() {
int seed = time(NULL);
printf("seed used : %d\n", seed);
set_linear_congruential_generator_seed(seed);
int returnCode = 0;
const unsigned randomLengthPower = 8;
returnCode |= randomTestingFFT(randomLengthPower);
returnCode |= randomTestingFFTI(randomLengthPower);
const unsigned length = 4;
fft_real FFTinputReals[] = {8, 4, 8, 0};
fft_real FFTinputImgs[] = {0, 0, 0, 0};
fft_real FFTexpectedReals[] = {20, 0, 12, 0};
fft_real FFTexpectedImgs[] = {0, -4, 0, 4};
returnCode |= knownTestingFFT(length, FFTinputReals, FFTinputImgs,
FFTexpectedReals, FFTexpectedImgs);
fft_real FFTIinputReals[] = {20, 0, 12, 0};
fft_real FFTIinputImgs[] = {0, -4, 0, 4};
fft_real FFTIexpectedReals[] = {8, 4, 8, 0};
fft_real FFTIexpectedImgs[] = {0, 0, 0, 0};
returnCode |= knownTestingFFTI(length, FFTIinputReals, FFTIinputImgs,
FFTIexpectedReals, FFTIexpectedImgs);
return returnCode;
}