Pregunta

I've already read quite a few posts about the Discrete Fourier transform, but I find myself struggling to apply it on a simple cosine wave. I'm using the kiss_fft library to calculate the DFT of a set of data and a bitmap library to visualize the result.

Here's the C++ code:

#define FIXED_POINT 32
#include "kiss_fft.h"

int main()
{    
    const int width = 512;
    const int height = 512;
    const int align_center = 256;
    const int fft_siz = width;
    const int is_inverse = 0;

    Bitmap bmp_t("fft_time.bmp", width, height);
    Bitmap bmp_f("fft_frq.bmp", width, height);

    kiss_fft_cpx* in = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * fft_siz);
    kiss_fft_cpx* out= (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * fft_siz);
    kiss_fft_cfg cfg = kiss_fft_alloc(fft_siz, is_inverse, NULL, NULL);

    // initialize input data
    for(int i = 0; i < fft_siz; i++)
    {
        // I think I shouldn't add align_center to in[i].r
        // but should wait till line (1*)
        in[i].r = align_center + 128 * cos(0.128f * i);
        in[i].i = 0;

        // line (1*)
        bmp_t.setPixel(i, in[i].r, 255, 255, 255);
    }

    kiss_fft(cfg, in, out);

    // visualize output
    for(int i = 1; i < fft_siz; i ++)
        bmp_f.setPixel(out[i].r, out[i].i, 255, 255, 255);

    free(in);
    free(out);
    free(cfg);

    kiss_fft_cleanup();

    bmp_t.write();
    bmp_f.write();

    return 0;
}

Here is the input:

enter image description here

and what I'm getting as output:

enter image description here

The result doesn't feel right at all. What is that I'm doing wrong ?

¿Fue útil?

Solución

Plotting just the real component of the FFT output is not very meaningful - plot the magnitude instead (sqrt(re*re+im*im)). Even better, plot the log magnitude in dB (10*log10(re*re+im*im))

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top