Search code examples
c++audiofftfftw

Discrete Fourier Transform C++ - What to do next?


I am using the fftw library to perform a DFT on an audio signal that I have received from a video recording.

This is my first time using this kind of maths and analysing signals, but I believe I have got the correct values from the data after the calculation.

However, now I am unsure what to do with the complex number array and how I could go about plotting this into a graph.

Please could someone advise on what to do next, here is an example of the before/after data...

(in, out(real;im))
(-12190,          (real:-3103;   im:0))
(-16316,          (real:-3108.465666587691;     im:49.512823501357843))
(-10005,          (real:-3096.7767811251124;    im:99.645004992013128))
(first 3 from a sample of 512)

Also, here is the code I am using to get these results...

    int n = 512;

    double in[512];
    fftw_complex out[512];
    fftw_plan p;

    ifstream rFile ("audioswap.csv");
    string line;

    if (rFile.is_open())
    {
        for (int i = 0; i < n; i++)
        {
            if ( getline (rFile, line))
            {
                in[i] = ::atof(line.c_str());
            }
            else
            {
                break;
            }
        }
    }

    p = fftw_plan_dft_r2c_1d(n, in, out, FFTW_ESTIMATE);

    fftw_execute(p);

    fftw_destroy_plan(p);

If I am doing anything wrong in getting the results please would someone be able to advise.

Thanks in advance to any help at all!!! :)


Solution

  • For almost all purposes, the next thing you do is take the magnitude of each complex value. Phase information in the Fourier domain is a result of (small) timeshifts in the time domain, and generally that doesn't matter. If you change the phase information and transform it back to the time domain, it still sounds the same.

    The magnitude can be graphed directly, that's meaningful.