简体   繁体   中英

Discrete Fourier Transform (Not FFT) In Java

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 an=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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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