001package algs91; // section 9.9
002import stdlib.*;
003import algs12.Complex;
004/* ***********************************************************************
005 *  Compilation:  javac FFT.java
006 *  Execution:    java FFT N
007 *  Dependencies: Complex.java
008 *
009 *  Compute the FFT and inverse FFT of a length N complex sequence.
010 *  Bare bones implementation that runs in O(N log N) time. Our goal
011 *  is to optimize the clarity of the code, rather than performance.
012 *
013 *  Limitations
014 *  -----------
015 *   -  assumes N is a power of 2
016 *
017 *   -  not the most memory efficient algorithm (because it uses
018 *      an object type for representing complex numbers and because
019 *      it re-allocates memory for the subarray, instead of doing
020 *      in-place or reusing a single temporary array)
021 *
022 *************************************************************************/
023
024public class FFT {
025
026        // compute the FFT of x[], assuming its length is a power of 2
027        public static Complex[] fft(Complex[] x) {
028                int N = x.length;
029
030                // base case
031                if (N == 1) return new Complex[] { x[0] };
032
033                // radix 2 Cooley-Tukey FFT
034                if (N % 2 != 0) { throw new Error("N is not a power of 2"); }
035
036                // fft of even terms
037                Complex[] even = new Complex[N/2];
038                for (int k = 0; k < N/2; k++) {
039                        even[k] = x[2*k];
040                }
041                Complex[] q = fft(even);
042
043                // fft of odd terms
044                Complex[] odd  = even;  // reuse the array
045                for (int k = 0; k < N/2; k++) {
046                        odd[k] = x[2*k + 1];
047                }
048                Complex[] r = fft(odd);
049
050                // combine
051                Complex[] y = new Complex[N];
052                for (int k = 0; k < N/2; k++) {
053                        double kth = -2 * k * Math.PI / N;
054                        Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
055                        y[k]       = q[k].plus(wk.times(r[k]));
056                        y[k + N/2] = q[k].minus(wk.times(r[k]));
057                }
058                return y;
059        }
060
061
062        // compute the inverse FFT of x[], assuming its length is a power of 2
063        public static Complex[] ifft(Complex[] x) {
064                int N = x.length;
065                Complex[] y = new Complex[N];
066
067                // take conjugate
068                for (int i = 0; i < N; i++) {
069                        y[i] = x[i].conjugate();
070                }
071
072                // compute forward FFT
073                y = fft(y);
074
075                // take conjugate again
076                for (int i = 0; i < N; i++) {
077                        y[i] = y[i].conjugate();
078                }
079
080                // divide by N
081                for (int i = 0; i < N; i++) {
082                        y[i] = y[i].times(1.0 / N);
083                }
084
085                return y;
086
087        }
088
089        // compute the circular convolution of x and y
090        public static Complex[] cconvolve(Complex[] x, Complex[] y) {
091
092                // should probably pad x and y with 0s so that they have same length
093                // and are powers of 2
094                if (x.length != y.length) { throw new Error("Dimensions don't agree"); }
095
096                int N = x.length;
097
098                // compute FFT of each sequence
099                Complex[] a = fft(x);
100                Complex[] b = fft(y);
101
102                // point-wise multiply
103                Complex[] c = new Complex[N];
104                for (int i = 0; i < N; i++) {
105                        c[i] = a[i].times(b[i]);
106                }
107
108                // compute inverse FFT
109                return ifft(c);
110        }
111
112
113        // compute the linear convolution of x and y
114        public static Complex[] convolve(Complex[] x, Complex[] y) {
115                Complex ZERO = new Complex(0, 0);
116
117                Complex[] a = new Complex[2*x.length];
118                for (int i = 0;        i <   x.length; i++) a[i] = x[i];
119                for (int i = x.length; i < 2*x.length; i++) a[i] = ZERO;
120
121                Complex[] b = new Complex[2*y.length];
122                for (int i = 0;        i <   y.length; i++) b[i] = y[i];
123                for (int i = y.length; i < 2*y.length; i++) b[i] = ZERO;
124
125                return cconvolve(a, b);
126        }
127
128        // display an array of Complex numbers to standard output
129        public static void show(Complex[] x, String title) {
130                StdOut.println(title);
131                StdOut.println("-------------------");
132                for (Complex element : x) {
133                        StdOut.println(element);
134                }
135                StdOut.println();
136        }
137
138
139        /* *******************************************************************
140         *  Test client and sample execution
141         *
142         *  % java FFT 4
143         *  x
144         *  -------------------
145         *  -0.03480425839330703
146         *  0.07910192950176387
147         *  0.7233322451735928
148         *  0.1659819820667019
149         *
150         *  y = fft(x)
151         *  -------------------
152         *  0.9336118983487516
153         *  -0.7581365035668999 + 0.08688005256493803i
154         *  0.44344407521182005
155         *  -0.7581365035668999 - 0.08688005256493803i
156         *
157         *  z = ifft(y)
158         *  -------------------
159         *  -0.03480425839330703
160         *  0.07910192950176387 + 2.6599344570851287E-18i
161         *  0.7233322451735928
162         *  0.1659819820667019 - 2.6599344570851287E-18i
163         *
164         *  c = cconvolve(x, x)
165         *  -------------------
166         *  0.5506798633981853
167         *  0.23461407150576394 - 4.033186818023279E-18i
168         *  -0.016542951108772352
169         *  0.10288019294318276 + 4.033186818023279E-18i
170         *
171         *  d = convolve(x, x)
172         *  -------------------
173         *  0.001211336402308083 - 3.122502256758253E-17i
174         *  -0.005506167987577068 - 5.058885073636224E-17i
175         *  -0.044092969479563274 + 2.1934338938072244E-18i
176         *  0.10288019294318276 - 3.6147323062478115E-17i
177         *  0.5494685269958772 + 3.122502256758253E-17i
178         *  0.240120239493341 + 4.655566391833896E-17i
179         *  0.02755001837079092 - 2.1934338938072244E-18i
180         *  4.01805098805014E-17i
181         *
182         *********************************************************************/
183
184        public static void main(String[] args) {
185                int N = Integer.parseInt(args[0]);
186                Complex[] x = new Complex[N];
187
188                // original data
189                for (int i = 0; i < N; i++) {
190                        x[i] = new Complex(i, 0);
191                        x[i] = new Complex(-2*Math.random() + 1, 0);
192                }
193                show(x, "x");
194
195                // FFT of original data
196                Complex[] y = fft(x);
197                show(y, "y = fft(x)");
198
199                // take inverse FFT
200                Complex[] z = ifft(y);
201                show(z, "z = ifft(y)");
202
203                // circular convolution of x with itself
204                Complex[] c = cconvolve(x, x);
205                show(c, "c = cconvolve(x, x)");
206
207                // linear convolution of x with itself
208                Complex[] d = convolve(x, x);
209                show(d, "d = convolve(x, x)");
210        }
211
212}