Skip to main content

FFT (Fast Fourier Transform)

What is an FFT?​

The FFT (fast Fourier transform) is an algorithm that splits a block of signal samples into the sine waves it contains, and measures how strong each frequency is. It computes the discrete Fourier transform (DFT) far faster than the direct method, which is why spectrum analysers, audio tools and vibration monitors can show a signal's frequencies in real time.

An FFT spectrum analyser in a dark navy theme: a signal of three tones plus noise at the top, and its FFT magnitude spectrum in decibels at the bottom, with peaks at 50 Hz and about 120 Hz and a small peak at 160 Hz above a noise floor

A test signal of three tones plus noise (top) and its FFT spectrum in decibels (bottom), sample data. Try the interactive version, where you can move a tone between bins and change the window.

Key takeaways
  • An FFT turns NN samples into frequency bins spaced Δf=fs/N\Delta f = f_s / N apart, from 0 Hz up to the Nyquist frequency fs/2f_s / 2.
  • It gives the same answer as the DFT, in about Nlog⁡2NN \log_2 N operations instead of N2N^2.
  • Longer records, finer detail: halving the bin width means recording for twice as long.
  • A tone that falls between bins spreads into its neighbours (spectral leakage). A window such as Hann reduces it.
  • Zero padding makes the plot smoother but doesn't separate tones that are too close together.

How the FFT works​

This section explains the algorithm. To read a spectrum first, skip to how to read an FFT plot.

The Fourier transform says that any signal can be built from sine waves of different frequencies, amplitudes and phases. (For the idea itself, see the Fourier transform guide.) A computer works with a list of samples, so it uses the discrete Fourier transform. For NN samples x0…xN−1x_0 \ldots x_{N-1}, bin kk of the DFT is:

Xk=∑n=0N−1xn e−2πi kn/N,k=0,1,…,N−1X_k = \sum_{n=0}^{N-1} x_n \, e^{-2\pi i \, k n / N}, \qquad k = 0, 1, \ldots, N-1

Each bin compares the signal with a sine and cosine that complete exactly kk cycles in the block. A strong match gives a large ∣Xk∣\lvert X_k \rvert; no match gives a value near zero.

Done directly, each of the NN bins needs NN multiplications, so the work grows as N2N^2. Double the block and the work goes up four times.

The radix-2 trick​

The FFT avoids most of that work by reusing it. Its best-known form, the radix-2 version of the algorithm James Cooley and John Tukey published in 1965 (Gauss had used the same idea around 1805, unpublished in his lifetime), works on blocks whose length is a power of 2:

  1. Split the samples into the even-numbered ones and the odd-numbered ones.
  2. Transform each half. Each is a DFT of half the length, and each of those splits again, down to blocks of one sample.
  3. Combine each pair of half-size results with a butterfly: two outputs, a+w ba + w\,b and a−w ba - w\,b, where ww is a fixed complex number called a twiddle factor.

The split only reorders the samples. Splitting into even and odd samples, then splitting each half the same way, ends with the samples in bit-reversed order: write each position as a binary number, read the bits backwards, and that's the sample that lands there. In a 16-sample block, position 1 (0001) gets sample 8 (1000). Code does the whole split in one pass, swapping pairs of samples in place.

Five rows show 16 samples being split. One block of samples 0 to 15 splits into the even samples 0, 2, 4 … 14 and the odd samples 1, 3, 5 … 15, then into four blocks of 4, eight blocks of 2 and finally sixteen single samples in the order 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15. Worked bit reversals: position 1 = 0001 holds sample 1000 = 8, position 2 = 0010 holds 4, position 3 = 0011 holds 12, position 6 = 0110 holds 6, and position 11 = 1011 holds 13. Adapted from Burnett-Thompson (2007)

Adapted from Burnett-Thompson (2007).

The DFT of one sample is that sample, so transforming a single sample costs nothing and the reordered list is already NN one-point spectra. All the work is in combining them.

Each butterfly multiplies the lower of its two values by a twiddle factor and outputs their sum and their difference:

A radix-2 butterfly. Inputs a and b come from the previous stage; b is multiplied by the twiddle factor w; the upper output is a + w·b and the lower output is a − w·b. The twiddle factor is W_N^R = e^(−i2πR/N) = cos(2πR/N) − i sin(2πR/N), and for N = 8 the values are W⁰ = 1, W¹ = (1 − i)/√2, W² = −i and W³ = −(1 + i)/√2. Adapted from Burnett-Thompson (2007)

Adapted from Burnett-Thompson (2007).

The twiddle factors WNR=e−2πiR/NW_N^R = e^{-2\pi i R / N} are the DFT's own test waves, points spaced evenly around the unit circle. The FFT computes each product w bw\,b once and uses it twice, once added and once subtracted, and that reuse is where the saving comes from. Stage 1 pairs values next to each other, stage 2 pairs values 2 apart, stage 3 pairs values 4 apart, and so on, doubling until one NN-point spectrum is left.

Worked example: an 8-point FFT​

Sample one cycle of a sine wave 8 times, xn=sin⁡(2πn/8)x_n = \sin(2\pi n / 8), which gives [0,22,1,22,0,−22,−1,−22][0, \tfrac{\sqrt{2}}{2}, 1, \tfrac{\sqrt{2}}{2}, 0, -\tfrac{\sqrt{2}}{2}, -1, -\tfrac{\sqrt{2}}{2}].

An 8-point FFT flow graph with three stages of butterflies, worked for x = sin(2πn/8). Input in bit-reversed order: x0 = 0, x4 = 0, x2 = 1, x6 = −1, x1 = √2/2, x5 = −√2/2, x3 = √2/2, x7 = −√2/2. After stage 1: 0, 0, 0, 2, 0, √2, 0, √2. After stage 2: 0, −2i, 0, 2i, 0, √2 − √2i, 0, √2 + √2i. Output in natural order: X0 = 0, X1 = −4i, X2 to X6 = 0, X7 = 4i. Adapted from Burnett-Thompson (2007)

Adapted from Burnett-Thompson (2007).

  1. Reorder. In bit-reversed order (x0,x4,x2,x6,x1,x5,x3,x7x_0, x_4, x_2, x_6, x_1, x_5, x_3, x_7) the input is [0,0,1,−1,22,−22,22,−22][0, 0, 1, -1, \tfrac{\sqrt{2}}{2}, -\tfrac{\sqrt{2}}{2}, \tfrac{\sqrt{2}}{2}, -\tfrac{\sqrt{2}}{2}].
  2. Stage 1 combines neighbours with W0=1W^0 = 1, so each pair becomes its sum and its difference: [0,0,0,2,0,2,0,2][0, 0, 0, 2, 0, \sqrt{2}, 0, \sqrt{2}]. These are four 2-point spectra.
  3. Stage 2 combines values 2 apart with W0=1W^0 = 1 and W2=−iW^2 = -i. The second butterfly, for example, takes 00 and 22 and gives 0+(−i)(2)=−2i0 + (-i)(2) = -2i and 0−(−i)(2)=2i0 - (-i)(2) = 2i. The result is two 4-point spectra, [0,−2i,0,2i][0, -2i, 0, 2i] and [0,2−2i,0,2+2i][0, \sqrt{2} - \sqrt{2}i, 0, \sqrt{2} + \sqrt{2}i].
  4. Stage 3 combines values 4 apart with W0W^0 to W3W^3. Bin 1 is −2i+W1(2−2i)=−2i+(1−i)2=−4i-2i + W^1(\sqrt{2} - \sqrt{2}i) = -2i + (1 - i)^2 = -4i, and bin 5, the same butterfly's difference, is −2i−(−2i)=0-2i - (-2i) = 0. Bin 7 is 2i−W3(2+2i)=2i+2i=4i2i - W^3(\sqrt{2} + \sqrt{2}i) = 2i + 2i = 4i.

The output comes out in natural order and is zero everywhere except X1=−4iX_1 = -4i and X7=4iX_7 = 4i: one wave at one cycle per block, plus its mirror image in bin N−1N - 1. Its amplitude is 2∣X1∣/N=2×4/8=12\lvert X_1 \rvert / N = 2 \times 4 / 8 = 1, and the −i-i marks a sine rather than a cosine. A direct DFT gives the same eight numbers with 64 multiplications; the FFT used 12 butterflies.

There are log⁡2N\log_2 N levels of splitting and N/2N/2 butterflies per level, so the total work grows as Nlog⁡2NN \log_2 N rather than N2N^2. The table below counts butterflies, each one complex multiplication, so its last column is about twice the N2/(Nlog⁡2N)N^2 / (N \log_2 N) ratio (102 times for 1,024 samples). The answer is the same as the direct DFT. If anything, the FFT's rounding errors are smaller, because each result passes through fewer operations.

Block size NNDirect DFT (N2N^2 multiplications)Radix-2 FFT (N2log⁡2N\tfrac{N}{2}\log_2 N butterflies)About how many times less work
25665,5361,02464
1,0241,048,5765,120200
4,09616,777,21624,576680
65,5364,294,967,296524,2888,200
1,048,576about 1.1 trillion10,485,760100,000

The FFT made spectrum analysis cheap enough to run continuously, on everything from lab instruments to phones. Other FFT algorithms (mixed-radix, split-radix, Bluestein's) handle block sizes that aren't powers of 2, and FFT libraries pick between them for you.

The inverse FFT​

The inverse DFT differs from the forward one only in the sign of the exponent and a factor of 1/N1/N. So the same FFT code can run it backwards, in four steps:

  1. Conjugate the spectrum: flip the sign of every imaginary part.
  2. Run the forward FFT on the result.
  3. Divide every output by NN.
  4. Conjugate again. For a real signal the imaginary parts left over are zero, apart from rounding errors.

It works because conjugating the input and the output of a sum of complex exponentials flips the sign of the exponent: ∑kXk‾ e−2πikn/N‾=∑kXk e2πikn/N\overline{\sum_k \overline{X_k}\, e^{-2\pi i kn/N}} = \sum_k X_k\, e^{2\pi i kn/N}. Put the spectrum from the 8-point example through these steps and the eight sine samples come back. Some texts and libraries put the 1/N1/N on the forward transform instead of the inverse, so check which convention a library uses before comparing amplitudes.

How to read an FFT plot​

An FFT plot (also called an FFT spectrum or FFT graph) has frequency along the horizontal axis and amplitude or level up the vertical axis. Each peak is a frequency the signal contains; its height is how strong that frequency is.

An annotated FFT spectrum in decibels from 0 to 280 Hz. A peak at 50 Hz reaches 0 dB; a peak at 120.5 Hz reaches about -7 dB because it falls between bins; a small peak at 160 Hz reaches about -49 dB; the rest is a noise floor near -70 dB

Read it in this order:

  1. Check the frequency axis. It runs from 0 Hz to the Nyquist frequency, half the sample rate (fs/2f_s / 2). Nothing above that can be measured; it would have shown up as a false, lower frequency (aliasing) when the signal was sampled.
  2. Check the vertical scale. On a linear scale, a peak's height is the tone's amplitude. On a decibel (dB) scale, each 20 dB is a factor of 10 in amplitude: L=20log⁡10(A)L = 20 \log_{10}(A), so amplitude 1 is 0 dB, 0.5 is about -6 dB, 0.1 is -20 dB and 0.001 is -60 dB. Use dB when you need to see weak tones next to strong ones.
  3. Find the peaks. Each narrow peak is a steady tone. Evenly spaced peaks (for example 50, 100, 150 Hz) are a fundamental and its harmonics, usually a sign that the waveform isn't a pure sine.
  4. Find the noise floor, the uneven level between the peaks. A peak only means something if it stands clearly above it (see signal-to-noise ratio).
  5. Look at the shape of each peak. A wide skirt around a peak is usually leakage from the window, not real content (see windowing).

For a real signal (any measurement from a sensor or microphone), the FFT's output is symmetric: bins above N/2N/2 mirror the ones below, as negative frequencies. So FFT plots show only bins 0 to N/2N/2 (the one-sided spectrum), and double those amplitudes (except at 0 Hz and at the Nyquist frequency) so each tone's energy is counted once.

Amplitude, power or power spectral density?​

An amplitude spectrum (as here) reads the size of each tone directly. A power spectrum shows the square of it. A power spectral density (PSD) divides power by the bin width, so random noise reads the same whatever the FFT size; vibration and noise measurements often use it.

FFT resolution: bins, bin width and FFT size​

An FFT of NN samples taken at a sample rate fsf_s gives bins at

fk=k fsN,Δf=fsN=1Tf_k = k \, \frac{f_s}{N}, \qquad \Delta f = \frac{f_s}{N} = \frac{1}{T}

where Δf\Delta f is the bin width (or frequency resolution) and T=N/fsT = N / f_s is how long the block lasts. The resolution depends only on how long you record, not on how fast you sample. Sampling faster raises the Nyquist frequency; recording longer makes the bins narrower.

For audio sampled at 48,000 samples a second:

FFT size NNBin width Δf\Delta fBlock length TTGood for
1,02446.9 Hz21 msFast-moving level meters and real-time displays
4,09611.7 Hz85 msGeneral audio analysis
16,3842.9 Hz341 msSeparating close musical notes or mains harmonics
65,5360.73 Hz1.4 sFine detail on steady signals, such as machine vibration

The trade-off is between detail in frequency and detail in time. A long block gives narrow bins but blurs anything that changes within it. To follow a signal that changes, take many short FFTs one after another and draw them as a spectrogram.

How close can two tones be?​

As a rule of thumb, two tones of similar size show as separate peaks when they are further apart than a peak's width 6 dB below its top: about 1.2 bins with no window and 2 bins with a Hann window (Harris, 1978). Windowing widens each peak, so it costs some resolution.

Windowing and spectral leakage​

The FFT assumes the block repeats forever. If a tone completes a whole number of cycles in the block, the end joins the start smoothly and the tone lands in exactly one bin. If it doesn't, which is nearly always, the repeat has a jump at the join. That jump spreads the tone's energy into many other bins. This is spectral leakage.

Leakage does two kinds of harm:

  • It hides weak tones. The skirts of a strong peak can be higher than a small peak nearby.
  • It lowers the peak. A tone exactly halfway between two bins reads about 3.9 dB too low with no window. This is called scalloping loss.

A window function fixes most of this. Before the FFT, each sample is multiplied by a weight that is largest in the middle of the block and falls towards the ends, so the jump at the join disappears. It also widens the central peak (less frequency detail) and lowers the overall level, which the amplitude scaling corrects.

Left: the rectangular, Hann, Hamming and Blackman windows across one block of samples. Right: four small charts of the spectrum each window gives one tone, with highest side lobes of -13, -31, -43 and -58 dB

WindowFormula, n=0…N−1n = 0 \ldots N-1Highest side lobeCentral peak width (to first zero)Peak drop between binsBest for
Rectangular (none)11-13 dB±1 bin3.9 dBTones that fit the block exactly; short transients that start and end inside it
Hann (Hanning)0.5−0.5cos⁡2πnN0.5 - 0.5\cos\frac{2\pi n}{N}-31 dB±2 bins1.4 dBA good default for most signals, noise and random vibration
Hamming0.54−0.46cos⁡2πnN0.54 - 0.46\cos\frac{2\pi n}{N}-43 dB±2 bins1.8 dBLower close-in leakage than Hann, often used in speech processing
Blackman0.42−0.5cos⁡2πnN+0.08cos⁡4πnN0.42 - 0.5\cos\frac{2\pi n}{N} + 0.08\cos\frac{4\pi n}{N}-58 dB±3 bins1.1 dBFinding weak tones near strong ones

We measured these figures with the FFT code on this page. They match the standard values in Harris (1978) to within rounding; see Sources. Far from the peak, leakage falls away quickly with Hann and Blackman but only slowly with Hamming, whose side lobes stay in the -40s and -50s of dB for many bins. For the most accurate peak heights, test equipment also offers a flat-top window, with almost no peak drop but a very wide peak.

In the live example, with the moving tone at 120.5 Hz, switch the window to Rectangular. Its skirt rises over the weak 160 Hz tone. Switch back to Hann or Blackman and the small peak reappears. Then move the tone to exactly 120.0 Hz: with a rectangular window the leakage vanishes, because the tone now fits the block.

Zero padding: a smoother plot, not more detail​

Zero padding means adding zeros to the end of the block before the FFT, for example 1,024 samples plus 3,072 zeros for a 4,096-point FFT. The result has four times as many points, a quarter of a bin apart. That draws the same spectrum at more points, like interpolating between them, so peaks look smoother and their tops are easier to locate. It doesn't improve the resolution: the bin width still depends on the real data, fs/Nf_s / N, so two tones too close to separate in 1,024 samples stay merged however many zeros you add.

Padding is also used to reach the next power of 2 so a radix-2 FFT can run, and before fast convolution so the result doesn't wrap around. For how zero padding relates to leakage, see does zero padding reduce spectral leakage? in the window functions guide.

FFT vs DFT, Fourier transform and spectrogram​

Use an FFT for the spectrum of a block of samples, and one of the related tools below when you need something else.

If you need…UseWhy
The spectrum of a block of samplesFFTThe standard tool: same result as the DFT, far less work
A handful of specific frequencies (for example, detecting phone keypad tones)Direct DFT of those bins, or the Goertzel algorithmCheaper than a full FFT when you need only a few bins
The maths of a continuous signal, on paperFourier transformThe continuous version the DFT and FFT approximate
How the frequencies change over timeSpectrogram (short-time FFT)Many FFTs of short, overlapping blocks, drawn as a heatmap
To filter or smooth a signalA low-pass filter, or FFT-based convolution for long filtersFiltering changes the signal; an FFT only measures it

What FFT analysis is used for​

FFT analysis is used wherever a signal needs to be split into its frequencies, often many times a second.

  • Audio: spectrum analysers, equalisers, pitch detection, room measurement and finding hum or noise.
  • Vibration and machine monitoring: bearing and shaft faults show up as peaks linked to the machine's speed.
  • Oscilloscopes and test equipment: most digital oscilloscopes have an FFT mode that shows the spectrum of the captured waveform.
  • Communications, imaging and science: OFDM (Wi-Fi, 4G and 5G), MRI scanners, astronomy, seismology and spectroscopy all run on forward and inverse FFTs.
  • Fast filtering: long filters and convolution run faster through an FFT than directly.

Common FFT mistakes​

Most FFT mistakes come from the block, the window or the scaling, not the algorithm.

  • No window on a signal that doesn't fit the block. Leakage then hides weak tones and lowers peak heights. Use Hann unless you have a reason not to.
  • Forgetting to scale. Raw FFT magnitudes grow with NN. Divide by the sum of the window (and double the one-sided bins) to read amplitudes.
  • Mixing up 10 and 20 log. Use 20log⁡1020 \log_{10} for amplitude and 10log⁡1010 \log_{10} for power, or every level is off by a factor of two in dB.
  • Aliasing before the FFT. Frequencies above fs/2f_s / 2 fold back into the spectrum as false peaks. Filter them out with an analogue or low-pass filter before sampling or downsampling.
  • Leaving in the DC offset. A constant offset makes a large 0 Hz bin, and its leakage can swamp low frequencies. Subtract the mean first.
  • Reading a single FFT of noise as fact. Noise in one FFT is ragged. Average the spectra of several blocks (Welch's method) for a steady estimate.

How to plot an FFT in JavaScript, React or Angular​

Each time you change a control, this demo builds a new test signal sampled at 1,024 Hz, windows it, takes its FFT and redraws the spectrum. The example below runs the FFT in your browser with the plain JavaScript functions in the code. Move the tone, change the window, the FFT size NN, zero padding and the scale; the line under the controls says where the tone falls among the bins. Hover over the spectrum to read a bin, scroll to zoom into a frequency range, drag to pan and double-click to reset.

Here is the code for this example. The JavaScript tab includes the FFT, window and spectrum functions; the React and Angular tabs reuse them.

// FFT spectrum analyser: a test signal and its FFT magnitude spectrum, in JavaScript with SciChart.js.
// The FFT and window functions below are plain JavaScript with no dependencies, so you can use them anywhere.

// Iterative radix-2 Cooley-Tukey FFT, in place. re and im hold the real and imaginary parts;
// their length must be a power of 2. Afterwards, re[k] and im[k] hold frequency bin k.
function fft(re, im) {
const n = re.length;
if ((n & (n - 1)) !== 0) throw new Error("FFT length must be a power of 2");
// 1. Reorder the samples by bit-reversed index (for n = 8, index 1 = 001 swaps with 4 = 100)
for (let i = 1, j = 0; i < n; i++) {
let bit = n >> 1;
for (; j & bit; bit >>= 1) j ^= bit;
j ^= bit;
if (i < j) {
[re[i], re[j]] = [re[j], re[i]];
[im[i], im[j]] = [im[j], im[i]];
}
}
// 2. Combine pairs of smaller DFTs into bigger ones: sizes 2, 4, 8 ... n (log2(n) stages).
// Each "butterfly" turns a and b into a + w*b and a - w*b, where w is a twiddle factor e^(-2*pi*i*k/size).
for (let size = 2; size <= n; size *= 2) {
const half = size / 2;
const step = (-2 * Math.PI) / size;
for (let start = 0; start < n; start += size) {
for (let k = 0; k < half; k++) {
const wr = Math.cos(step * k);
const wi = Math.sin(step * k);
const a = start + k;
const b = a + half;
const tr = re[b] * wr - im[b] * wi;
const ti = re[b] * wi + im[b] * wr;
re[b] = re[a] - tr;
im[b] = im[a] - ti;
re[a] += tr;
im[a] += ti;
}
}
}
}

// Window functions (the periodic form used for spectrum analysis). They taper the ends of the block
// towards zero, so a tone that doesn't fit a whole number of cycles leaks less into other bins.
function makeWindow(type, n) {
const w = new Float64Array(n);
for (let i = 0; i < n; i++) {
const x = (2 * Math.PI * i) / n;
if (type === "hann") w[i] = 0.5 - 0.5 * Math.cos(x);
else if (type === "hamming") w[i] = 0.54 - 0.46 * Math.cos(x);
else if (type === "blackman") w[i] = 0.42 - 0.5 * Math.cos(x) + 0.08 * Math.cos(2 * x);
else w[i] = 1; // rectangular: no window
}
return w;
}

// One-sided amplitude spectrum: window the samples, zero pad, FFT, and scale so a sine of amplitude A
// centred on a bin reads A. Returns the frequency of each bin (0 to sampleRate / 2) and its amplitude.
function amplitudeSpectrum(samples, sampleRate, windowType, padFactor) {
const n = samples.length;
const size = n * padFactor; // zero padding: extra zeros after the samples
const window = makeWindow(windowType, n);
const re = new Float64Array(size);
const im = new Float64Array(size);
let windowSum = 0;
for (let i = 0; i < n; i++) {
re[i] = samples[i] * window[i];
windowSum += window[i];
}
fft(re, im);
const bins = size / 2 + 1; // bins above sampleRate / 2 mirror these, so drop them
const frequencies = new Float64Array(bins);
const amplitudes = new Float64Array(bins);
for (let k = 0; k < bins; k++) {
frequencies[k] = (k * sampleRate) / size;
const oneSided = k === 0 || k === size / 2 ? 1 : 2; // fold the negative frequencies in
amplitudes[k] = (oneSided * Math.hypot(re[k], im[k])) / windowSum;
}
return { frequencies, amplitudes };
}

// Test signal: three sine tones plus a little seeded random noise, so it looks the same every time.
// 50 Hz (amplitude 1), a tone you can move (amplitude 0.5) and a weak 160 Hz tone (0.003, about -50 dB).
function makeTestSignal(count, sampleRate, movingToneHz) {
let seed = 11;
const random = () => {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
};
const time = new Float64Array(count);
const values = new Float64Array(count);
for (let i = 0; i < count; i++) {
const t = i / sampleRate;
const noise = (random() + random() + random() + random() + random() + random() - 3) * 0.007;
time[i] = t;
values[i] =
Math.sin(2 * Math.PI * 50 * t) +
0.5 * Math.sin(2 * Math.PI * movingToneHz * t) +
0.003 * Math.sin(2 * Math.PI * 160 * t) +
noise;
}
return { time, values };
}

async function createFftCharts(timeDiv, spectrumDiv) {
const {
SciChartSurface,
NumericAxis,
NumberRange,
NumericLabelProvider,
EAutoRange,
EXyDirection,
FastLineRenderableSeries,
XyDataSeries,
SciChartJsNavyTheme,
CursorModifier,
LegendModifier,
MouseWheelZoomModifier,
ZoomPanModifier,
ZoomExtentsModifier
} = SciChart;
// or, for npm, import { SciChartSurface, ... } from "scichart"

const SAMPLE_RATE = 1024; // samples per second, so the highest frequency shown (Nyquist) is 512 Hz

// Top chart: the samples going into the FFT, before and after the window
const timeChart = await SciChartSurface.create(timeDiv, { theme: new SciChartJsNavyTheme() });
const timeSurface = timeChart.sciChartSurface;
const timeXAxis = new NumericAxis(timeChart.wasmContext, { axisTitle: "Time (s)", labelPrecision: 2 });
timeSurface.xAxes.add(timeXAxis);
timeSurface.yAxes.add(
new NumericAxis(timeChart.wasmContext, { axisTitle: "Signal", visibleRange: new NumberRange(-2, 2), labelPrecision: 1 })
);
const rawSeries = new XyDataSeries(timeChart.wasmContext, { dataSeriesName: "Samples" });
const windowedSeries = new XyDataSeries(timeChart.wasmContext, { dataSeriesName: "After the window" });
timeSurface.renderableSeries.add(
new FastLineRenderableSeries(timeChart.wasmContext, { dataSeries: rawSeries, stroke: "#50C7E088" }),
new FastLineRenderableSeries(timeChart.wasmContext, { dataSeries: windowedSeries, stroke: "#F48420" })
);
timeSurface.chartModifiers.add(
new LegendModifier({ showCheckboxes: false }),
new MouseWheelZoomModifier({ xyDirection: EXyDirection.XDirection }),
new ZoomPanModifier({ xyDirection: EXyDirection.XDirection }),
new ZoomExtentsModifier({ xyDirection: EXyDirection.XDirection })
);

// Bottom chart: the FFT magnitude spectrum, from 0 Hz to the Nyquist frequency
const spectrumChart = await SciChartSurface.create(spectrumDiv, { theme: new SciChartJsNavyTheme() });
const spectrumSurface = spectrumChart.sciChartSurface;
spectrumSurface.xAxes.add(
new NumericAxis(spectrumChart.wasmContext, {
axisTitle: "Frequency (Hz)",
visibleRange: new NumberRange(0, SAMPLE_RATE / 2),
labelPrecision: 0
})
);
const levelLabels = new NumericLabelProvider({ labelPrecision: 0 });
const levelAxis = new NumericAxis(spectrumChart.wasmContext, { autoRange: EAutoRange.Never, labelProvider: levelLabels });
let unit = "dB";
spectrumSurface.yAxes.add(levelAxis);
const spectrumSeries = new XyDataSeries(spectrumChart.wasmContext, { dataSeriesName: "Spectrum", dataIsSortedInX: true });
spectrumSurface.renderableSeries.add(
new FastLineRenderableSeries(spectrumChart.wasmContext, { dataSeries: spectrumSeries, stroke: "#50C7E0", strokeThickness: 2 })
);
// Hover to read a bin, scroll to zoom into a frequency range, drag to pan, double-click to reset
spectrumSurface.chartModifiers.add(
new CursorModifier({
showTooltip: true,
showAxisLabels: false,
tooltipDataTemplate: (seriesInfos) =>
seriesInfos.map((info) => `${info.formattedXValue} Hz: ${info.formattedYValue} ${unit}`)
}),
new MouseWheelZoomModifier({ xyDirection: EXyDirection.XDirection }),
new ZoomPanModifier({ xyDirection: EXyDirection.XDirection }),
new ZoomExtentsModifier({ xyDirection: EXyDirection.XDirection })
);

// Recompute everything from the settings: { toneHz, windowType, size, padFactor, decibels }
const update = (settings) => {
const { time, values } = makeTestSignal(settings.size, SAMPLE_RATE, settings.toneHz);
const window = makeWindow(settings.windowType, settings.size);
rawSeries.clear();
rawSeries.appendRange(time, values);
windowedSeries.clear();
windowedSeries.appendRange(time, values.map((v, i) => v * window[i]));
timeXAxis.visibleRange = new NumberRange(0, settings.size / SAMPLE_RATE);

const { frequencies, amplitudes } = amplitudeSpectrum(values, SAMPLE_RATE, settings.windowType, settings.padFactor);
// Decibels relative to an amplitude of 1: 20 * log10(amplitude). A floor avoids log10(0) = -Infinity
const levels = settings.decibels ? amplitudes.map((a) => 20 * Math.log10(Math.max(a, 1e-10))) : amplitudes;
spectrumSeries.clear();
spectrumSeries.appendRange(frequencies, levels);
unit = settings.decibels ? "dB" : "";
levelAxis.axisTitle = settings.decibels ? "Amplitude (dB)" : "Amplitude";
levelLabels.precision = settings.decibels ? 0 : 2;
levelAxis.visibleRange = settings.decibels ? new NumberRange(-100, 5) : new NumberRange(0, 1.1);
spectrumSeries.dataSeriesName = `Spectrum, bin width ${SAMPLE_RATE / settings.size} Hz`;
};
return { update, sampleRate: SAMPLE_RATE };
}

Key points:

  • The spectrum is an XyDataSeries of frequencies and levels, drawn by a FastLineRenderableSeries. Set dataIsSortedInX: true, since FFT bins are already in order.
  • To update it live, keep the data series and call clear() then appendRange() with each new spectrum, as the example's update() function does. SciChart.js redraws on the next frame.
  • A fixed y-axis range (autoRange: EAutoRange.Never and a visibleRange in dB) stops the axis jumping as the noise changes.
  • The FFT itself is plain JavaScript. Swap in any FFT library you prefer; the chart code stays the same.

See it in the demo app: JavaScript audio spectrum analyzer chart · React audio spectrum analyzer chart · Angular audio spectrum analyzer chart. It runs the FFT on your microphone input and draws the waveform, the spectrum and a spectrogram.

Go deeper: SciChart.js line series documentation, and the spectrogram guide for drawing many FFTs over time.

Frequently asked questions​

What does FFT stand for?​

FFT stands for fast Fourier transform. It is a fast way to compute the discrete Fourier transform (DFT) of a block of samples, which tells you how much of each frequency the block contains. FFT analysis means using it to look at a signal's spectrum.

What is the difference between the DFT and the FFT?​

The DFT is the calculation: it turns N samples into N frequency bins. The FFT is a family of algorithms that compute exactly the same result much faster. Computed directly, the DFT takes about N squared operations; an FFT takes about N log2 N, which for 1,024 samples is about 100 times fewer.

What is FFT size, and how do I choose it?​

FFT size is the number of samples N in each block. It sets the bin width, which is the sample rate divided by N. A bigger N gives finer frequency detail but needs a longer recording (N divided by the sample rate) and reacts more slowly to change. Choose N so the bin width is no more than about half the gap between the closest frequencies you need to separate.

Why does my FFT show a mirror image or two peaks for one tone?​

For a real signal, the FFT's second half mirrors the first: bins above N/2 are the negative frequencies. Plot only bins 0 to N/2, which run from 0 Hz to the Nyquist frequency, and double their amplitude (except the first and last) to get the one-sided spectrum.

Does a JavaScript FFT give the same result as NumPy or MATLAB?​

Yes, if the conventions match. A standard JavaScript FFT, numpy.fft.fft and MATLAB fft(x) all compute the same unnormalised DFT with the same sign, so bin kk is identical to within rounding when you use 64-bit numbers (Float64Array). numpy.fft.rfft returns only bins 0 to N/2N/2, the same half you plot; in MATLAB, compute X = fft(x); and keep X(1:N/2+1), since MATLAB counts from 1.

When the numbers don't match, the window is the usual cause. numpy.hanning(N) and MATLAB hann(N) are symmetric, while hann(N, 'periodic') and scipy.signal.get_window('hann', N) are periodic (the demo above uses the periodic form), so use the same one on both sides. Scale amplitudes the same way too: 2∣X[k]∣2|X[k]| divided by the window's sum, except at bin 0 and bin N/2N/2.

How do I plot an FFT in JavaScript?​

Window the samples, run an FFT (a short radix-2 function is enough), keep bins 0 to N/2, and convert each magnitude to an amplitude or decibels. With SciChart.js, put the frequencies and levels in an XyDataSeries and draw them with a FastLineRenderableSeries; call clear() and appendRange() each time new samples arrive. For React or Angular, wrap that chart setup in scichart-react or scichart-angular.

Sources​