2

我正在使用这个fftw库。

目前我正在尝试以 e^(-(x^2+y^2)/a^2) 的形式绘制二维高斯曲线。

这是代码:

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;
}

原来负数出现了。我认为高斯的傅里叶变换是另一个高斯,它不应该包含任何负数。

此外,当前原点位于 in[0]

4

1 回答 1

3

编辑:先前的答案是错误的,移动高斯中心无济于事,因为它引入了另一个相移。正确的解决方案是将高指数包装成负指数:

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));

这允许输入覆盖整个高斯而不是四分之一。整个代码附在下面。

#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\n", 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;
}
于 2014-02-25T20:20:05.940 回答