I try to calculate the DFT for this array x_1. It must be dead simple, but my values are way too low. What’s wrong with my code?
Please no links to other examples – just looking for a fix for my own code.
#include <iostream>
#include <complex>
#include <cassert>
int main ()
{
const unsigned int N = 20;
const double x_1[N] = {0, 0.3, 0.6, 0.8, 1, 1, 0.9, 0.7, 0.5, 0.2, 0.2, 0.5, 0.7, 0.9, 1, 1, 0.8, 0.6, 0.3, 0};
for(unsigned int k = 0; k < N; k++)
{
std::complex<double> sum(0.0,0.0);
for(unsigned int j = 0; j < N; j++)
{
int integers = -2*j*k;
std::complex<double> my_exponent(0.0, M_PI/N*(double)integers);
sum += x_1[j] * std::exp(my_exponent);
}
std::cout << abs(sum)/N << std::endl;
}
return 0;
}
Why are you dividing by N?
The coefficients are without the division. See the wiki.
These are the values I get with Matlab:
and I see more or less the same values when I print using
std::cout << sum << std::endl;