English 中文(简体)
Why does negative number show up when I do FFT on a Gaussian?
原标题:
  • 时间:2010-06-24 22:44:28
  •  标签:
  • c++
  • fftw
  • fft

I m using this fftw library.

Currently I m trying to plot a 2D Gaussian in the form e^(-(x^2+y^2)/a^2).

Here is the code:

using namespace std;
int main(int argc, char** argv ){
    fftw_complex *in, *out, *data;
    fftw_plan p;
    int i,j;
    int w=16;
    int h=16;
    double a = 2;
    in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*w*h);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*w*h);
    for(i=0;i<w;i++){
        for(j=0;j<h;j++){
            in[i*h+j][0] = exp(- (i*i+j*j)/(a*a));
            in[i*h+j][1] = 0;
        }
    }
    p = fftw_plan_dft_2d(w, h, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
    //This is something that print what s in the matrix
    print_2d(out,w,h);

    fftw_destroy_plan(p);
    fftw_free(in);
    fftw_free(out);
    return 0;
}

Turns out negative numbers shows up. I thought Fourier transform of a Gaussian is another Gaussian, which shouldn t include any negative numbers.

Also, the current origin is at in[0]

问题回答

EDIT: the previous answer is wrong, shifting the center of the Gaussian won t help as it introduces another phase shift. The right solution is to wrap high indices to negative ones:

double x = (i < w*0.5) ? i : (i - w);
double y = (j < h*0.5) ? j : (j - h);
in[i*h+j][0] = exp(-(x*x+y*y)/(a*a));

This allows the input to cover the entire Gaussian instead of a quarter of it. The entire code is attached below.

#include <stdio.h>
#include <math.h>
#include <fftw3.h>

int main(int argc, char** argv)
{
    fftw_complex *in, *out;
    fftw_plan p;
    int i, j, w = 16, h = 16;
    double a = 2, x, y;

    in = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * w * h);
    out = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * w * h);
    for (i = 0; i < w; i++) {
        x = (i < w*0.5) ? i : (i - w);
        for (j = 0; j < h; j++) {
            y = (j < h*0.5) ? j : (j - h);
            in[i*h+j][0] = exp(-1.*(x*x+y*y)/(a*a));
            in[i*h+j][1] = 0;
        }
    }
    p = fftw_plan_dft_2d(w, h, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
    for (i = 0; i < w; i++) {
        for (j = 0; j < h; j++) {
            printf("%4d %4d %+9.4f %+9.4f
", i, j, out[i*h+j][0], out[i*h+j][1]);
        }
    }

    fftw_destroy_plan(p);
    fftw_cleanup();
    fftw_free(in);
    fftw_free(out);
    return 0;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签