# Discrete Fourier Transform Demo

This page demonstrates the discrete Fourier transform, which rewrites a discrete signal as a weighted sum of sines and cosines of various frequencies.  All even functions (when f(x) = f(−x)) only consist of cosines since cosine is an odd function, and all odd functions (when f(x) = −f(−x)) only consist of sines since sine is an odd function, other functions are a mix of sines and cosines.

The demo below performs the discrete Fourier transform on the function f(x).  The first plot shows f(x) from x = −8 to x = 8 sampled in discrete steps (128 by default).  The second plot shows the weights (on the y-axis) versus the frequencies (on the x-axis) of the sines and cosines that make up f(x).  The units on the x-axis of the second plot are “cycles per unit in the spatial domain,” so a cosine wave that repeats once every unit in the spatial domain will show up as a spike at ±1 in the frequency domain.  The third plot shows the inverse discrete Fourier transform, which converts the sines and cosines back into the original function f(x).  It has the same units as the first plot.

Each “spike” on the second plot is the magnitude of the sine or cosine at that frequency.  Here are the first eight cosine waves (click on one to plot it).  The division by 8 is because our signal extends from x = −8 to x = 8, and the cos(x) function here actually repeats every 2 units instead of every 2π units for convenience.

The Fourier transform is actually implemented using complex numbers, where the real part is the weight of the cosine and the imaginary part is the weight of the sine.  On the second plot, a blue spike is a real (cosine) weight and a green spike is an imaginary (sine) weight.  This is why cos(x) shows up blue and sin(x) shows up green.  Most signals have both sines and cosines in them, like triangle(abs(x) + sin(x)).

It turns out that signals and their Fourier transforms come in pairs, called duals, that are each the Fourier transform of the other.  For example, the Fourier transform of cos(2*x) is two spikes at x = ±1 and the Fourier transform of spike(x − 1) + spike(x + 1) is cos(2*x), so cos(2*x) and spike(x ± 1) are duals.

Perhaps the most important dual is box(x) with sinc(x).  The (normalized) sinc function is defined as sinc(x) = sin(πx) / (πx), and is problematic because it is infinite in width.  This is why the Fourier transform of sinc(x) doesn’t quite give a box in the frequency domain, the “ringing” is caused by chopping off the tails of sinc at the sides of the plot (in our case at x = ±8).  Other duals of interest are triangle(x) with sinc2(x) and gaussian(x) with itself.

To see how each spike contributes to the final reconstructed signal, move your mouse around inside the second plot to “chop off” the tails of the signal.  Observe how the third plot (the result of the inverse discrete Fourier transform) changes as you add or remove sines and cosines from the sum, for example in this crazy function.

f(x) =
Your browser doesn't support the HTML5 <canvas> element.

Approximate function with samples (try 32, 64, 128, 256)

Demo made by Evan Wallace in 2010