繁体   English   中英

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

[英]Complex Numbers and Naive Fourier Transform (C++)

我正在尝试使傅立叶变换正常工作,我必须为工作分配它,我想我已经确定它应该在哪里工作,但我不确定为什么不起作用。 我认为这与复数有关,因为涉及“ i”。 我看过许多参考资料,并且了解该公式,但是在编程时遇到了麻烦。 这就是我到目前为止

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()数字,看起来也不正确

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

谢谢。

我认为在与复数项相乘的过程中可能会出现问题。 该行:

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

此外,在计算theta时,您需要使用双精度:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM