开发者

fftwpp creates a zero filled array after a convolution

I use fftwpp to transform both my data and my convolution kernel into the fourier space, multiply them together like in a scalar prod开发者_JAVA百科uct and convert them back to the real space. When I first run the program, it creates an array which is completely filled with zeros. When I run it again, it gives me the desired results.

While running it, a wisdom3.txt is created. If I delete it, the program takes a lot of time to create a zero filled array again.

What is wrong with my code?

// sx, sy and sz are the dimensions of my data

int szp = sz / 2 + 1;
size_t align = sizeof(Complex);

// creates arrays to store the data in, the double one is for the real data
// the Complex one for the fourier data
array3<double> f(sx, sy, sz, align);
array3<Complex> g(sx, sy, szp, align);

// copying data into double array
for(int k = 0; k < sz; k++)
    for(int j = 0; j < sy; j++)
        for(int i = 0; i < sx; i++)
            f(i, j, k) = data[i + sx * j + sx * sy * k];

// transforming data into fourier space
rcfft3d Forward3(sz, f, g);
Forward3.fft(f, g);


// generate the kernel
array3<double> kernel(sx, sy, sz);
array3<Complex> kernel2(sx, sy, szp, align);
// more code to create the kernel left out ...


// transform the kernel into the fourier space
rcfft3d ForwardKernel3(sz, kernel, kernel2);
ForwardKernel3.fft(kernel, kernel2);


// multiplying data and kernel in fourier space together
for(int k = 0; k < szp; k++)
    for(int j = 0; j < sy; j++)
        for(int i = 0; i < sx; i++)
            g(i, j, k) = g(i, j, k) * kernel2(i, j, k);


// transform back to normal space
crfft3d Backward3(sz, g, f);
Backward3.fftNormalized(g, f);


// putting everything in the results array and normalize
for(int k = 0; k < sz; k++)
    for(int j = 0; j < sy; j++)
        for(int i = 0; i < sx; i++)
            result[i + sx * j + sx * sy * k] =
                (f(i, j, k) >= thresholdValue ? f(i, j, k) : 0);


FFTW++ is a wrapper over FFTW. FFTW requires generating an execution plan to process your data efficiently. Once the plan is generated, it can be reused to process various sets of data. The file you're seeing wisdom3.txt is information about the plan that your program generates on its first run. Once it exists, it is loaded upon successive runs and allows your program to run fast. If you delete it, FFTW has to regenerate it, making your program run slow again.

As to why your output is zero on the first run, is because of this plan generation step. It is explained in the FFTW FAQ.

To conclude, since I haven't used FFTW++ I don't know the exact method you're supposed to call to ensure the plan is generated before performing the FT. But you should call it immediately after defining your input/output arrays and before initializing them with your data. And if you need to run your code multiple times, you should keep the wisdom file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜