forked from 1chipML/1chipML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_DFT.c
85 lines (68 loc) · 2.47 KB
/
test_DFT.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
#include <1chipml.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int compareFT(const unsigned length, dft_real* incomingReals,
dft_real* incomingImgs, dft_real* expectedReals,
dft_real* expectedImgs) {
const dft_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 knownTestingDFT(const unsigned length, fft_real* inputReals,
fft_real* inputImaginaries, fft_real* expectedReals,
fft_real* expectedImaginaries) {
DFT(length, inputReals, inputImaginaries, 1);
int isSimilar = compareFT(length, inputReals, inputImaginaries, expectedReals,
expectedImaginaries);
printf("Known testing DFT: is the DFT working as intended? ");
int returnCode = 0;
if (isSimilar == 0) {
printf("true\n");
returnCode = 0;
} else {
printf("false\n");
returnCode = 1;
}
return returnCode;
}
static int knownTestingDFTI(const unsigned length, fft_real* inputReals,
fft_real* inputImaginaries, fft_real* expectedReals,
fft_real* expectedImaginaries) {
DFT(length, inputReals, inputImaginaries, -1);
int isSimilar = compareFT(length, inputReals, inputImaginaries, expectedReals,
expectedImaginaries);
printf("Known testing inverse DFT: is the DFT 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 returnCode = 0;
const unsigned length = 4;
fft_real DFTinputReals[] = {8, 4, 8, 0};
fft_real DFTinputImgs[] = {0, 0, 0, 0};
fft_real DFTexpectedReals[] = {20, 0, 12, 0};
fft_real DFTexpectedImgs[] = {0, -4, 0, 4};
returnCode |= knownTestingDFT(length, DFTinputReals, DFTinputImgs,
DFTexpectedReals, DFTexpectedImgs);
fft_real DFTIinputReals[] = {20, 0, 12, 0};
fft_real DFTIinputImgs[] = {0, -4, 0, 4};
fft_real DFTIexpectedReals[] = {8, 4, 8, 0};
fft_real DFTIexpectedImgs[] = {0, 0, 0, 0};
returnCode |= knownTestingDFTI(length, DFTIinputReals, DFTIinputImgs,
DFTIexpectedReals, DFTIexpectedImgs);
return returnCode;
}