0% found this document useful (0 votes)
103 views7 pages

FFT Algorithm Efficiently.

The program implements the Fast Fourier Transform (FFT) algorithm to efficiently compute the discrete Fourier transform (DFT) of a complex sequence. It defines a Complex class to represent complex numbers, and includes methods to compute the FFT and inverse FFT of a complex sequence. The FFT method recursively divides the input sequence into even and odd terms, computes the FFT of each half, and combines the results. Additional methods implement the circular and linear convolution of two complex sequences using FFT.

Uploaded by

umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views7 pages

FFT Algorithm Efficiently.

The program implements the Fast Fourier Transform (FFT) algorithm to efficiently compute the discrete Fourier transform (DFT) of a complex sequence. It defines a Complex class to represent complex numbers, and includes methods to compute the FFT and inverse FFT of a complex sequence. The FFT method recursively divides the input sequence into even and odd terms, computes the FFT of each half, and combines the results. Additional methods implement the circular and linear convolution of two complex sequences using FFT.

Uploaded by

umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

6.

Design, develop, and run a program in any language to implement the FFT algorithm
efficiently.
using System;
using [Link];
using [Link];
using [Link];
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 = [Link] + [Link];
double imag = [Link] + [Link];
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 = [Link] - [Link];
double imag = [Link] - [Link];
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 = [Link] * [Link] - [Link] * [Link];
double imag = [Link] * [Link] + [Link] * [Link];

return new Complex(real, imag);


}
// scalar multiplication
// return a new object whose value is (this * alpha)
public Complex times(double alpha) {
return new Complex(alpha * re, alpha * im);
}
// return a new Complex object whose value is the conjugate of this
public Complex conjugate() { return new Complex(re, -im); }
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = re*re + im*im;
return new Complex(re / scale, -im / scale);
}
// return the real or imaginary part
public double RE() { return re; }
public double IM() { return im; }
// return a / b
public Complex divides(Complex b) {
Complex a = this;
return [Link]([Link]());
}
// return a new Complex object whose value is the complex exponential
of this
public Complex exp() {
return new Complex([Link](re) * [Link](im), [Link](re) *
[Link](im));
}
// return a new Complex object whose value is the complex Sine of this
public Complex Sin() {
return new Complex([Link](re) * [Link](im), [Link](re) *
[Link](im));
}
// return a new Complex object whose value is the complex Cosine of
this
public Complex Cos() {
return new Complex([Link](re) * [Link](im), -[Link](re) *
[Link](im));
}

// return a new Complex object whose value is the complex tangent of


this
public Complex tan() {
return Sin().divides(Cos());
}
// a static version of plus
public static Complex plus(Complex a, Complex b) {
double real = [Link] + [Link];
double imag = [Link] + [Link];
Complex sum = new Complex(real, imag);
return sum;
}
}
}

using
using
using
using

System;
[Link];
[Link];
[Link];

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 = [Link];
// base case
if (N == 1) return new Complex[] { x[0] };

// radix 2 Cooley-Tukey FFT


if (N % 2 != 0) { throw new Exception("N is not a power of 2"); }
// fft of even terms
Complex[] even = new Complex[N / 2];
for (int k = 0; k < N / 2; k++)
{
even[k] = x[2 * k];
}
Complex[] q = fft(even);
// fft of odd terms
Complex[] odd = even; // reuse the array
for (int k = 0; k < N / 2; k++)
{
odd[k] = x[2 * k + 1];
}
Complex[] r = fft(odd);
// combine
Complex[] y = new Complex[N];
for (int k = 0; k < N / 2; k++)
{
double kth = -2 * k * [Link] / N;
Complex wk = new Complex([Link](kth), [Link](kth));
y[k] = q[k].plus([Link](r[k]));
y[k + N / 2] = q[k].minus([Link](r[k]));
}
return y;
}
// compute the inverse FFT of x[], assuming its Length is a power of 2
public Complex[] ifft(Complex[] x)
{
int N = [Link];
Complex[] y = new Complex[N];
// take conjugate
for (int i = 0; i < N; i++)
{
y[i] = x[i].conjugate();
}
// compute forward FFT
y = fft(y);
// take conjugate again
for (int i = 0; i < N; i++)

{
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 ([Link] != [Link]) { throw new Exception("Dimensions don't
agree"); }
int N = [Link];
// 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 * [Link]];
for (int i = 0; i < [Link]; i++) a[i] = x[i];
for (int i = [Link]; i < 2 * [Link]; i++) a[i] = ZERO;

Complex[] b = new Complex[2 * [Link]];


for (int i = 0; i < [Link]; i++) b[i] = y[i];
for (int i = [Link]; i < 2 * [Link]; i++) b[i] = ZERO;
return cconvolve(a, b);
}
// display an array of Complex numbers to standard output
public void show(Complex[] x, String title) {
[Link](title);
[Link]("-------------------");
for (int i = 0; i < [Link]; i++) {
[Link](x[i].toString());
}
}
}
}

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

You might also like