DFT (discrete fourier transform) with C++ STL

13,884
std::cout << abs(sum)/N << std::endl;

Why are you dividing by N?

The coefficients are without the division. See the wiki.

These are the values I get with Matlab:

12.0000000000000 + 0.00000000000000i
-0.971586454726535 - 0.153884176858763i
-4.26246117974981 - 1.38495759172886i   
-0.0712959999079796 - 0.0363271264002681i   
-0.473606797749979 - 0.344095480117793i
0.00000000000000 + 0.00000000000000i
-0.237538820250189 - 0.326944137602412i
0.0185095954079375 + 0.0363271264002681i
-0.0263932022500213 - 0.0812299240582274i
0.0243728592265771 + 0.153884176858763i
0.00000000000000 + 0.00000000000000i    
0.0243728592265771 - 0.153884176858763i
-0.0263932022500213 + 0.0812299240582274i
0.0185095954079375 - 0.0363271264002681i    
-0.237538820250189 + 0.326944137602412i 
0.00000000000000 + 0.00000000000000i    
-0.473606797749979 + 0.344095480117793i 
-0.0712959999079796 + 0.0363271264002681i
-4.26246117974981 + 1.38495759172886i   
-0.971586454726535 + 0.153884176858763i

and I see more or less the same values when I print using std::cout << sum << std::endl;

Share:
13,884
Johannes
Author by

Johannes

Updated on July 25, 2022

Comments

  • Johannes
    Johannes almost 2 years

    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;
    } 
    
  • Johannes
    Johannes almost 12 years
    I shall do this for my exercise. This can not be the reason of the error.
  • Johannes
    Johannes almost 12 years
    Thanks for trying with matlab, then the calculation must be correct... Why is the first value (12.0) that high?
  • BertR
    BertR almost 12 years
    The DC value is the sum of all values. If you sum all values, you'll get 12.
  • BertR
    BertR almost 12 years
    @Johannes: Does this solve your problem? In case it does, can you maybe vote and accept the answer? Thanks :-)
  • Johannes
    Johannes almost 12 years
    Actually, I don't know what's wrong with my results. But your MATLAB output shows that my code is correct. I'll find out more week, but I think you should get an "accept" :)
  • Johannes
    Johannes almost 12 years
    Now I know. I made a copy and paste error getting the input. The algorithm, however, is completely correct. Thanks anyway.