Java 中的离散傅里叶变换(不是 FFT)

发布于 2024-10-20 15:01:31 字数 1250 浏览 1 评论 0原文

我正在为 Java 中的 CSE 类做作业,并正在实现 FFT 和直接 DFT(带有矩阵计算)。我的 FFT 工作正常,但我的直接 DFT 不起作用。我的傅里叶矩阵没有给出正确的值,我想知道这是否是用户错误,或者问题是否出在我正在使用的 Complex 类中(org.apache.commons.math.complex)。对于这样一个主流类,我想这只是用户错误,所以如果有人能指出它,那就太好了。

我的傅里叶矩阵计算工作原理如下:

Complex[][] fmatrix = new Complex[cvector.length][cvector.length]; // Initialize Matrix
    for(int k = 0; k < n; k++) { 
        double val = -2.0 * k * Math.PI / n; // Calculate exponential value
        Complex w = new Complex(0.0,val); // Store that in a Complex value and set imaginary piece to the exponential value
        for (int l = 0; l < n; l++) {
            Complex powerof = new Complex((double) (k*l),0.0); // Calculate the power to take it to
            fmatrix[k][l] = w.exp().pow(powerof); // Take the exponent, then raise it to powerof
        }
    }

出于调试目的,我将一些项目拉入变量中,但代码应该全部按照我的理解工作。

然而,对于长度为 4 的向量,上面的代码会得出以下矩阵:

      Mine                    Desired
[ 1,  1,  1, 1          [  1,  1,  1,  1
  1, -j, -1, j             1, -j, -1,  j
  1,  1,  1, 1     =/=     1, -1,  1, -1
  1, -j, -1, j ]           1,  j, -1, -j ]

非常感谢任何帮助。

I'm doing an assignment for a CSE class in Java and am implementing a FFT and the direct DFT (with matrix calculations). My FFT works fine, but my direct DFT is not working. My Fourier matrix is not coming out with the right values, and I want to know if its a user error, or if the issue instead lies in the Complex class that I'm using (org.apache.commons.math.complex). With such a mainstream class, I imagine its just user error, so if someone could point it out, that would be great.

My Fourier Matrix calculations works as follows:

Complex[][] fmatrix = new Complex[cvector.length][cvector.length]; // Initialize Matrix
    for(int k = 0; k < n; k++) { 
        double val = -2.0 * k * Math.PI / n; // Calculate exponential value
        Complex w = new Complex(0.0,val); // Store that in a Complex value and set imaginary piece to the exponential value
        for (int l = 0; l < n; l++) {
            Complex powerof = new Complex((double) (k*l),0.0); // Calculate the power to take it to
            fmatrix[k][l] = w.exp().pow(powerof); // Take the exponent, then raise it to powerof
        }
    }

I have some of the items pulled out into variables for debugging purposes, but the code should all work from my understanding.

The code above, for a n=4 length vector, however, comes up with the following matrix:

      Mine                    Desired
[ 1,  1,  1, 1          [  1,  1,  1,  1
  1, -j, -1, j             1, -j, -1,  j
  1,  1,  1, 1     =/=     1, -1,  1, -1
  1, -j, -1, j ]           1,  j, -1, -j ]

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2024-10-27 15:01:31

我只是简单地浏览了一下你的代码,决定先看一下数学,因为我已经有一段时间没有完成 FFT 和 DFT 了(对于 DFT 来说已经很长时间了)
我想知道这一行:

double val = -2.0 * k * Math.PI / n; // Calculate exponential value

我不明白为什么 k 在那里,因为我看到的方程是 -2 PI i / n,而且我认为您不需要复杂类中的 i 。我怀疑这是你的问题,只是我的一个问题。如果我发现其他东西,我会再次发布。

I just glanced at your code breafly and decided to look at the math first since it has been awhile since I have done FFT's and DFT's (a very long time for DFT's)
I am wondering about this line:

double val = -2.0 * k * Math.PI / n; // Calculate exponential value

I don't understand why the k is there, since the equation I see is -2 PI i / n, and I don't think you need the i in the complex class. I doubt that is your problem, just a question I had. I will post again if I find something else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文