Example: Fast Fourier Transform

The Fourier coefficients of a periodic sequence are computed. The coefficients are then used to reproduce the periodic sequence.
import com.imsl.math.*;

public class FFTEx1 {
    public static void main(String args[]) {
        double	x[] = {1, 2, 3, 4, 5, 6, 7, 8};
        FFT	fft = new FFT(x.length);
        
        double	y[] = fft.forward(x);
        double	z[] = fft.backward(y);
        for (int i = 0; i < x.length; i++) {
            z[i] = z[i] / x.length;
        }
        
        new PrintMatrix("x").print(x);
        new PrintMatrix("y").print(y);
        new PrintMatrix("z").print(z);
    }
}

Output

  x
   0  
0  1  
1  2  
2  3  
3  4  
4  5  
5  6  
6  7  
7  8  

     y
     0     
0  36      
1  -4      
2   9.657  
3  -4      
4   4      
5  -4      
6   1.657  
7  -4      

  z
   0  
0  1  
1  2  
2  3  
3  4  
4  5  
5  6  
6  7  
7  8  

Link to Java source.