-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathfft_stages_loop-top.cpp
103 lines (90 loc) · 2.61 KB
/
fft_stages_loop-top.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
/*
This is traditional 2-radix DIT FFT algorithm implementation.
It is based on conventional 3-loop structure.
INPUT:
In_R, In_I[]: Real and Imag parts of Complex signal
OUTPUT:
In_R, In_I[]: Real and Imag parts of Complex signal
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include "fft_streaming.h"
DTYPE In_R[SIZE], In_I[SIZE];
DTYPE Out_R[SIZE], Out_I[SIZE];
DTYPE WW_R[SIZE], WW_I[SIZE];
int main()
{
FILE *fp;
FILE *fp_r, *fp_i;
printf("GENERATING INPUTS\n");
for(int i=0; i<SIZE; i++){
In_R[i] = i;
In_I[i] = 0.0;
}
//Twiddle factor is calculated here and saved in fft.h to be used in offline.
double e = -6.2831853071795864769;
printf("GENERATING %d TWIDDLE FACTORS\n", SIZE);
fp_r=fopen("tw_r.h", "w");
fp_i=fopen("tw_i.h", "w");
fprintf(fp_r, "const DTYPE W_real[]={");
fprintf(fp_i, "const DTYPE W_imag[]={");
for(int i=0; i<SIZE2; i++)
{
//COMPLEX W; // e^(-j 2 pi/ N)
double w = e*double(i)/double(SIZE);
WW_R[i]=cos(w);
WW_I[i]=sin(w);
//printf("%4d\t%f\t%f\n",i,WW_R[i],WW_I[i]);
fprintf(fp_r, "%.20f,",WW_R[i]);
fprintf(fp_i, "%.20f,",WW_I[i]);
if(i%16==0)
{
fprintf(fp_r, "\n");
fprintf(fp_i, "\n");
}
}
fprintf(fp_r, "};\n");
fprintf(fp_i, "};\n");
fclose(fp_r);
fclose(fp_i);
//Perform FFT
fft_streaming(In_R, In_I, Out_R, Out_I);
//Print output
fp=fopen("out.fft.dat", "w");
printf("Printing FFT Output\n");
for(int i=0; i<SIZE; i++){
//printf("%4d\t%f\t%f\n",i,Out_R[i],Out_I[i]);
fprintf(fp, "%4d\t%f\t%f\n",i,Out_R[i],Out_I[i]);
}
fclose(fp);
printf ("Comparing against output data \n");
std::ifstream golden("out.fft.gold.dat");
DTYPE error = 0.0;
DTYPE maxerror = 0.0;
for(int i=0; i<SIZE; i++) {
DTYPE rx, ix;
int j;
golden >> j >> rx >> ix;
DTYPE newerror = fabs(rx-Out_R[i]) + fabs(ix-Out_I[i]);
error += newerror;
if(newerror > maxerror) {
maxerror = newerror;
fprintf(stdout, "Max Error@%d: %f\n", i, maxerror);
}
}
fprintf(stdout, "Average Error: %f\n",error/SIZE);
if ((error/SIZE) > .05 || maxerror > 2) { // This is somewhat arbitrary. Should do proper error analysis.
fprintf(stdout, "*******************************************\n");
fprintf(stdout, "FAIL: Output DOES NOT match the golden output\n");
fprintf(stdout, "*******************************************\n");
return 1;
} else {
fprintf(stdout, "*******************************************\n");
fprintf(stdout, "PASS: The output matches the golden output!\n");
fprintf(stdout, "*******************************************\n");
return 0;
}
}