Académique Documents
Professionnel Documents
Culture Documents
Design, develop, and run a program in any language to implement the FFT algorithm
efficiently.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LabPrograms
{
public class Complex {
private double re; // the real part
private double im; // the imaginary part
// create a new object with the given real and imaginary parts
public Complex(double real, double imag) {
re = real;
im = imag;
}
// return a string representation of the invoking Complex object
public String toString() {
if (im == 0) return re + "";
if (re == 0) return im + "i";
if (im < 0) return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this;
// invoking object
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace LabPrograms
{
/*
* Compute the FFT and inverse FFT of a length N complex sequence.
* Bare bones implementation that runs in O(N log N) time. Our goal
* is to optimize the clarity of the code, rather than performance.
*
* Limitations
* ----------* - assumes N is a power of 2
*
* - not the most memory efficient algorithm (because it uses
*
an object type for representing complex numbers and because
*
it re-allocates memory for the subarray, instead of doing
*
in-place or reusing a single temporary array)
*/
public class FFT
{
// compute the FFT of x[], assuming its Length is a power of 2
public Complex[] fft(Complex[] x)
{
int N = x.Length;
// base case
if (N == 1) return new Complex[] { x[0] };
{
y[i] = y[i].conjugate();
}
// divide by N
for (int i = 0; i < N; i++)
{
y[i] = y[i].times(1.0 / N);
}
return y;
}
// compute the circular convolution of x and y
public Complex[] cconvolve(Complex[] x, Complex[] y)
{
// should probably pad x and y with 0s so that they have same
Length
// and are powers of 2
if (x.Length != y.Length) { throw new Exception("Dimensions don't
agree"); }
int N = x.Length;
// compute FFT of each sequence
Complex[] a = fft(x);
Complex[] b = fft(y);
// point-wise multiply
Complex[] c = new Complex[N];
for (int i = 0; i < N; i++)
{
c[i] = a[i].times(b[i]);
}
// compute inverse FFT
return ifft(c);
}
// compute the linear convolution of x and y
public Complex[] convolve(Complex[] x, Complex[] y)
{
Complex ZERO = new Complex(0, 0);
Complex[] a = new Complex[2 * x.Length];
for (int i = 0; i < x.Length; i++) a[i] = x[i];
for (int i = x.Length; i < 2 * x.Length; i++) a[i] = ZERO;
Output:
How many Complex no. you need:
2
x
------------------1828582339
1828582339
y = fft(x)
------------------3657164678
0
z = ifft(y)
------------------1828582339
1828582339
c = cconvolve(x, x)
-------------------
6.68742674100542E+18
6.68742674100542E+18
d = convolve(x, x)
------------------3.34371337050271E+18
6.68742674100542E+18 - 204.736631943923i
3.34371337050271E+18
204.736631943923i