简单就地离散傅立叶变换(DFT) [英] Simple in-place discrete fourier transform ( DFT )

查看:131
本文介绍了简单就地离散傅立叶变换(DFT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个非常简单的就地DFT。我现在用的公式如下所示:
http://en.wikipedia.org/wiki/Discrete_Fourier_transform#Definition < /一>使用欧拉公式沿着以避免使用复杂的数类只是这个。到目前为止,我有这样的:

I'm writing a very simple in-place DFT. I am using the formula shown here: http://en.wikipedia.org/wiki/Discrete_Fourier_transform#Definition along with Euler's formula to avoid having to use a complex number class just for this. So far I have this:

private void fft(double[] data)
        {
            double[] real = new double[256];
            double[] imag = new double[256];
            double pi_div_128 = -1 * Math.PI / 128;
            for (int k = 0; k < 256; k++)
            {
                for (int n = 0; n < 256; n++)
                {
                    real[k] += data[k] * Math.Cos(pi_div_128 * k * n);
                    imag[k] += data[k] * Math.Sin(pi_div_128 * k * n);
                }
                data[k] = Math.Sqrt(real[k] * real[k] + imag[k] * imag[k]);
            }
        }



但Math.Cos和Math.Sin最终条款去正面和负面的,这样我加入乘以数据[K]这些条款,它们抵消,而我只是得到了一些肮脏的小值。我看到它是如何发生的,但我不能让我的代码怎么可能是误代表的数学意义。任何帮助表示赞赏。仅供参考,我必须写我自己,我知道我可以得到现成的FFT的。

But the Math.Cos and Math.Sin terms eventually go both positive and negative, so as I'm adding those terms multiplied with data[k], they cancel out and I just get some obscenely small value. I see how it is happening, but I can't make sense of how my code is perhaps mis-representing the mathematics. Any help is appreciated. FYI, I do have to write my own, I realize I can get off-the shelf FFT's.

推荐答案

我相信你(; N< 256; N + INT N = 0)
{$ b在本节

I believe You have an error in this section

for (int n = 0; n < 256; n++)
{
    real[k] += data[k] * Math.Cos(pi_div_128 * k * n);
    imag[k] += data[k] * Math.Sin(pi_div_128 * k * n);
}

您应该替换数据[K]数据[N]

You should replace data[k] with data[n]

编辑:

您也破坏了行,您的数据:

You are also destroying Your data in the line:

data[k] = Math.Sqrt(real[k] * real[k] + imag[k] * imag[k]);

您必须存储您的复数或其他地方以后的模量。如果模是你想要的,你想将其存储在数据[]你有计算变换后的代码中的另一个循环,整个数据[]是必要的计算每一个真正的[k]和IMAG [K]。

You have to store the moduli of your complex numbers somewhere else or later. If the modulus is all you want, and You want to store it in data[] You have to code another loop after computing the transform., The whole data[] is necessary for computing every real[k] and imag [k].

这篇关于简单就地离散傅立叶变换(DFT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆