Hello. I have been testing my Discrete Fourier Transform program for 1 dimensional data (written in using C++).
For the following test input data(X), the corresponding DFT(X) output is expected. The first number in each separate column is the REAL and the second number is the IMAGINARY.
My program produces the output below for the same input test data.
The program code is below. I would appreciate to learn where the error lies and how to fix this, thank you!
For the following test input data(X), the corresponding DFT(X) output is expected. The first number in each separate column is the REAL and the second number is the IMAGINARY.
Code:
X DFT X
( 1.000, 0.000) ( 4.000, 0.000)
( 1.000, 0.000) ( 1.000,-2.414)
( 1.000, 0.000) ( 0.000, 0.000)
( 1.000, 0.000) ( 1.000,-0.414)
( 0.000, 0.000) ( 0.000, 0.000)
( 0.000, 0.000) ( 1.000, 0.414)
( 0.000, 0.000) ( 0.000, 0.000)
( 0.000, 0.000) ( 1.000, 2.414)Code:
(0.5, 0)
(0.125, -0.302)
(-2.3e-017, -2.78e-017)
(0.125, -0.0518)
(0, -3.06e-017)
(0.125, 0.0518)
(4.11e-017, -4.16e-017)
(0.125, 0.302)The program code is below. I would appreciate to learn where the error lies and how to fix this, thank you!
Code:
bool inverse = false;
int n = 8;
double gRe[8] = {0,1,0,0,0,0,0,0};
double gIm[8] = {0,0,0,0,0,0,0,0};
double GRe[8] = {0,0,0,0,0,0,0,0};
double GIm[8] = {0,0,0,0,0,0,0,0};
for(int w = 0; w < n; w++)
{
GRe[w] = GIm[w] = 0;
for(int x = 0; x < n; x++)
{
double a = -2 * pi * w * x / float(n);
if(inverse) a = -a;
double ca = cos(a);
double sa = sin(a);
GRe[w] += gRe[x] * ca - gIm[x] * sa;
GIm[w] += gRe[x] * sa + gIm[x] * ca;
}
if(!inverse)
{
GRe[w] /= n;
GIm[w] /= n;
}
}
fstream DFTdata;
DFTdata.open("DFT_Bdata.txt");
for (int i = 0; i<n; i++) // width
{
DFTdata <<"Input: "<< testReal[i]
<<" Real: "<< setprecision(3)<<FReal[i] <<" Imag: "<<setprecision(3)<<FIma[i]<< endl;
}
DFTdata.close();