fft

Discrete Fourier transform computed with a fast algorithm.

Category: Mathematics > Fourier Analysis and Filtering

Syntax

Y = fft(X)

Transforms along the first non-singleton dimension.

Y = fft(X, n)

Zero-pads or truncates the data to length n first.

Y = fft(X, n, dim)

Transforms along dimension dim.

Description

For a vector x of length n, Y(k) is the sum over j of x(j)*exp(-2*pi*1i*(j-1)*(k-1)/n). Element k measures how much of the frequency (k-1)/n cycles per sample is present. For real input the second half of the result mirrors the first as complex conjugates.

The transform is not normalized: ifft divides by n to undo it. A matrix is treated column by column. Padding with zeros, as in fft(x, 1024), interpolates the spectrum but adds no new information.

Input arguments

X
Numeric vector, matrix or N-D array. Real or complex.
n
Transform length. Use [] to keep the default.
dim
Dimension to operate along.

Output arguments

Y
Frequency-domain data, complex in general. Y(1) is the sum of the samples (the zero-frequency term).

Examples

Spectrum of a short sequence

fft([1 2 3 4])
fft([1 0 0 0], 6)
Output
ans = 1×4 complex

   10.0000 + 0.0000i   -2.0000 + 2.0000i   -2.0000 + 0.0000i   -2.0000 - 2.0000i
ans = 1×6

     1     1     1     1     1     1

Finding a tone

fs = 64;
t = (0:fs-1)/fs;
x = sin(2*pi*5*t);
P = abs(fft(x));
[~, k] = max(P(1:fs/2));
fprintf('peak at %d Hz\n', k-1)
Output
peak at 5 Hz

See also

ifft fft2 fftshift abs filter