package fourier; import java.util.Vector; import util.*; /** Inverse discrete Fourier transform */ public class idft { private int N; private Vector data; /**
Inverse DFT constructor
The inverse DFT constructor is passed a vector of complex objects (note that this differs from the dft constructor which is passed a vector of points).
*/ public idft( Vector d ) { N = 0; data = null; if (d != null) { int len = d.size(); if (len > 0) { // check to see if its a Vector of "complex" complex cx = (complex)d.elementAt( 0 ); // the Vector length is > 0 && the type is correct N = len; data = d; } } } // idft constructor /** Return the inverse DFT point at mThe inverse DFT algorith is an N2 algorithm. For a DFT point at m, there are O(N) calculations. The basic inverse DFT equation for calculating the inverse DFT point at m is shown below. In this equation the complex data values that resulted from a DFT transform are stored in the array "X". X[n] is one element of this array. "j" is the sqrt(-1) a.k.a the imaginary number i.
N-1 __ 1 \ ---/ X[n](cos((2Pi*n*m)/N) + j*sin((2Pi*n*m)/N)) N -- n = 0
At each step in the summation above the complex number X[n] is multiplied by the complex number
cxDft = complex( cos((2Pi*n*m)/N), sin((2Pi*n*m)/N) )*/ public complex iDftPoint( int m ) { final double twoPi = 2 * Math.PI; complex cxRslt = new complex(0.0f,0.0f); if (m >= 0 && m < N) { // At m == 0 the IDFT reduces to the sum of the data if (m == 0) { complex cx; for (int n = 0; n < N; n++) { cx = (complex)data.elementAt( n ); cxRslt = cxRslt.add( cx ); } // for } else { for (int n = 0; n < N; n++) { complex cx = (complex)data.elementAt( n ); double scale = (twoPi * n * m)/N; float R = (float)Math.cos( scale ); float I = (float)Math.sin( scale ); complex cxDft = new complex( R, I ); // cxRslt = cxRslt + cx * cxDft cxRslt = cxRslt.add( cx.mult( cxDft ) ); } // for // cxRslt = cxRslt / N cxRslt = cxRslt.div( N ); } // else } return cxRslt; } // dftPoint } // class idft