复数和朴素傅立叶变换(C ++)

时间:2013-11-18 05:50:34

标签: c++ image transform fft complex-numbers

我正在努力让傅里叶变换工作,我必须为一项任务做这件事,我想我应该把它用于它应该工作的地方,我不知道为什么它不是。我认为它与复杂的数字有关,因为'我'参与其中。我看了很多参考文献,我理解了公式,但是我编程时遇到了麻烦。这就是我到目前为止所拥有的

void NaiveDFT::Apply( Image & img )
{
    //make the fourier transform using the naive method and set that to the image.
    Image dft(img);
    Pixel ** dftData = dft.GetImageData();
    Pixel ** imgData = img.GetImageData();
    for(unsigned u = 0; u < img.GetWidth(); ++u)
    {
        for(unsigned v = 0; v < img.GetHeight(); ++v)
        {
            std::complex<double> sum = 0;
            for(unsigned x = 0; x < img.GetWidth(); ++x)
            {
                for(unsigned y = 0; y < img.GetHeight(); ++y)
                {
                    std::complex<double> i = sqrt(std::complex<double>(-1));
                    std::complex<double> theta = 2 * M_PI * (((u * x) / img.GetWidth()) + ((v * y) / img.GetHeight()));
                    sum += std::complex<double>(imgData[x][y]._red) * cos(theta) + (-i * sin(theta));
                    //sum += std::complex<double>(std::complex<double>(imgData[x][y]._red) * pow(EULER, -i * theta));

                }
            }
            dftData[u][v] = (sum.imag() / (img.GetWidth() * img.GetHeight()));
        }
    }
    img = dft;
}

我有一些测试图像,我正在测试它,我要么像一个全黑的图像或类似的,一个全灰色的图像。

我也尝试过e ^( - i * 2 * PI *(x * u * width + y * v * height)* 1 / width * height的总和得到与预期相同的结果,尽管它仍然是不是那种沮丧的输出。

我也尝试了sum.real()号,看起来不正确

如果有人有任何提示或者可以指出我正确的方向,那就太好了,在这一点上,我只是继续尝试不同的事情并检查输出,直到我得到我应该得到的东西。

感谢。

1 个答案:

答案 0 :(得分:1)

我认为在复杂术语的乘法过程中可能存在问题。这一行:

sum += std::complex<double>(imgData[x][y]._red) * cos(theta) + (-i * sin(theta));

应该是:

sum += std::complex<double>(imgData[x][y]._red) * ( cos(theta) + -i * sin(theta));

此外,在计算θ时你需要使用双精度:

std::complex<double> theta = 2 * M_PI * ((((double)u * x) / (double)(img.GetWidth())) + (((double)v * y) / (double)(img.GetHeight())));