fft - Fast Fourier transform (2024)

Fast Fourier transform

collapse all in page

Syntax

Y = fft(X)

Y = fft(X,n)

Y = fft(X,n,dim)

Description

Y = fft(X) computes the discrete Fourier transform (DFT) of X using a fast Fourier transform (FFT) algorithm. Y is the same size as X.

  • If X is a vector, then fft(X) returns the Fourier transform of the vector.

  • If X is a matrix, then fft(X) treats the columns of X as vectors and returns the Fourier transform of each column.

  • If X is a multidimensional array, then fft(X) treats the values along the first array dimension whose size does not equal 1 as vectors and returns the Fourier transform of each vector.

example

Y = fft(X,n) returns the n-point DFT.

  • If X is a vector and the length of X is less than n, then X is padded with trailing zeros to length n.

  • If X is a vector and the length of X is greater than n, then X is truncated to length n.

  • If X is a matrix, then each column is treated as in the vector case.

  • If X is a multidimensional array, then the first array dimension whose size does not equal 1 is treated as in the vector case.

example

Y = fft(X,n,dim) returns the Fourier transform along the dimension dim. For example, if X is a matrix, then fft(X,n,2) returns the n-point Fourier transform of each row.

example

Examples

collapse all

Noisy Signal

Open Live Script

Find the frequency components of a signal buried in noise and find the amplitudes of the peak frequencies by using Fourier transform.

Specify the parameters of a signal with a sampling frequency of 1 kHz and a signal duration of 1.5 seconds.

Fs = 1000; % Sampling frequency T = 1/Fs; % Sampling period L = 1500; % Length of signalt = (0:L-1)*T; % Time vector

Form a signal containing a DC offset of amplitude 0.8, a 50 Hz sinusoid of amplitude 0.7, and a 120 Hz sinusoid of amplitude 1.

S = 0.8 + 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);

Corrupt the signal with zero-mean random noise with a variance of 4.

X = S + 2*randn(size(t));

Plot the noisy signal in the time domain. The frequency components are not visually apparent in the plot.

plot(1000*t,X)title("Signal Corrupted with Zero-Mean Random Noise")xlabel("t (milliseconds)")ylabel("X(t)")

fft - Fast Fourier transform (1)

Compute the Fourier transform of the signal.

Y = fft(X);

Because Fourier transforms involve complex numbers, plot the complex magnitude of the fft spectrum.

plot(Fs/L*(0:L-1),abs(Y),"LineWidth",3)title("Complex Magnitude of fft Spectrum")xlabel("f (Hz)")ylabel("|fft(X)|")

fft - Fast Fourier transform (2)

The plot shows five frequency peaks including the peak at 0 Hz for the DC offset. In this example, the signal is expected to have three frequency peaks at 0 Hz, 50 Hz, and 120 Hz. Here, the second half of the plot is the mirror reflection of the first half without including the peak at 0 Hz. The reason is that the discrete Fourier transform of a time-domain signal has a periodic nature, where the first half of its spectrum is in positive frequencies and the second half is in negative frequencies, with the first element reserved for the zero frequency.

For real signals, the fft spectrum is a two-sided spectrum, where the spectrum in the positive frequencies is the complex conjugate of the spectrum in the negative frequencies. To show the fft spectrum in the positive and negative frequencies, you can use fftshift. For an even length of L, the frequency domain starts from the negative of the Nyquist frequency -Fs/2 up to Fs/2-Fs/L with a spacing or frequency resolution of Fs/L.

plot(Fs/L*(-L/2:L/2-1),abs(fftshift(Y)),"LineWidth",3)title("fft Spectrum in the Positive and Negative Frequencies")xlabel("f (Hz)")ylabel("|fft(X)|")

fft - Fast Fourier transform (3)

To find the amplitudes of the three frequency peaks, convert the fft spectrum in Y to the single-sided amplitude spectrum. Because the fft function includes a scaling factor L between the original and the transformed signals, rescale Y by dividing by L. Take the complex magnitude of the fft spectrum. The two-sided amplitude spectrum P2, where the spectrum in the positive frequencies is the complex conjugate of the spectrum in the negative frequencies, has half the peak amplitudes of the time-domain signal. To convert to the single-sided spectrum, take the first half of the two-sided spectrum P2. Multiply the spectrum in the positive frequencies by 2. You do not need to multiply P1(1) and P1(end) by 2 because these amplitudes correspond to the zero and Nyquist frequencies, respectively, and they do not have the complex conjugate pairs in the negative frequencies.

P2 = abs(Y/L);P1 = P2(1:L/2+1);P1(2:end-1) = 2*P1(2:end-1);

Define the frequency domain f for the single-sided spectrum. Plot the single-sided amplitude spectrum P1. As expected, the amplitudes are close to 0.8, 0.7, and 1, but they are not exact because of the added noise. In most cases, longer signals produce better frequency approximations.

f = Fs/L*(0:(L/2));plot(f,P1,"LineWidth",3) title("Single-Sided Amplitude Spectrum of X(t)")xlabel("f (Hz)")ylabel("|P1(f)|")

fft - Fast Fourier transform (4)

Now, take the Fourier transform of the original, uncorrupted signal and retrieve the exact amplitudes at 0.8, 0.7, and 1.0.

Y = fft(S);P2 = abs(Y/L);P1 = P2(1:L/2+1);P1(2:end-1) = 2*P1(2:end-1);plot(f,P1,"LineWidth",3) title("Single-Sided Amplitude Spectrum of S(t)")xlabel("f (Hz)")ylabel("|P1(f)|")

fft - Fast Fourier transform (5)

Gaussian Pulse

Open Live Script

Convert a Gaussian pulse from the time domain to the frequency domain.

Specify the parameters of a signal with a sampling frequency of 44.1 kHz and a signal duration of 1 ms. Create a Gaussian pulse with a standard deviation of 0.1 ms.

Fs = 44100; % Sampling frequencyT = 1/Fs; % Sampling periodt = -0.5:T:0.5; % Time vectorL = length(t); % Signal lengthX = 1/(0.4*sqrt(2*pi))*(exp(-t.^2/(2*(0.1*1e-3)^2)));

Plot the pulse in the time domain.

plot(t,X)title("Gaussian Pulse in Time Domain")xlabel("Time (t)")ylabel("X(t)")axis([-1e-3 1e-3 0 1.1]) 

fft - Fast Fourier transform (6)

The execution time of fft depends on the length of the transform. Transform lengths that have only small prime factors result in significantly faster execution time than those that have large prime factors.

In this example, the signal length L is 44,101, which is a very large prime number. To improve the performance of fft, identify an input length that is the next power of 2 from the original signal length. Calling fft with this input length pads the pulse X with trailing zeros to the specified transform length.

n = 2^nextpow2(L);

Convert the Gaussian pulse to the frequency domain.

Y = fft(X,n);

Define the frequency domain and plot the unique frequencies.

f = Fs*(0:(n/2))/n;P = abs(Y/sqrt(n)).^2;plot(f,P(1:n/2+1)) title("Gaussian Pulse in Frequency Domain")xlabel("f (Hz)")ylabel("|P(f)|")

fft - Fast Fourier transform (7)

Cosine Waves

Open Live Script

Compare cosine waves in the time domain and the frequency domain.

Specify the parameters of a signal with a sampling frequency of 1 kHz and a signal duration of 1 second.

Fs = 1000; % Sampling frequencyT = 1/Fs; % Sampling periodL = 1000; % Length of signalt = (0:L-1)*T; % Time vector

Create a matrix where each row represents a cosine wave with scaled frequency. The result, X, is a 3-by-1000 matrix. The first row has a wave frequency of 50, the second row has a wave frequency of 150, and the third row has a wave frequency of 300.

x1 = cos(2*pi*50*t); % First row wavex2 = cos(2*pi*150*t); % Second row wavex3 = cos(2*pi*300*t); % Third row waveX = [x1; x2; x3];

Plot the first 100 entries from each row of X in a single figure in order and compare their frequencies.

for i = 1:3 subplot(3,1,i) plot(t(1:100),X(i,1:100)) title("Row " + num2str(i) + " in the Time Domain")end

fft - Fast Fourier transform (8)

Specify the dim argument to use fft along the rows of X, that is, for each signal.

dim = 2;

Compute the Fourier transform of the signals.

Y = fft(X,L,dim);

Calculate the double-sided spectrum and single-sided spectrum of each signal.

P2 = abs(Y/L);P1 = P2(:,1:L/2+1);P1(:,2:end-1) = 2*P1(:,2:end-1);

In the frequency domain, plot the single-sided amplitude spectrum for each row in a single figure.

for i=1:3 subplot(3,1,i) plot(0:(Fs/L):(Fs/2-Fs/L),P1(i,1:L/2)) title("Row " + num2str(i) + " in the Frequency Domain")end

fft - Fast Fourier transform (9)

Phase of Sinusoids

Open Live Script

Create a signal that consists of two sinusoids of frequencies 15 Hz and 40 Hz. The first sinusoid is a cosine wave with phase -π/4, and the second is a cosine wave with phase π/2. Sample the signal at 100 Hz for 1 s.

Fs = 100;t = 0:1/Fs:1-1/Fs;x = cos(2*pi*15*t - pi/4) + cos(2*pi*40*t + pi/2);

Compute the Fourier transform of the signal. Plot the magnitude of the transform as a function of frequency.

y = fft(x);z = fftshift(y);ly = length(y);f = (-ly/2:ly/2-1)/ly*Fs;stem(f,abs(z))title("Double-Sided Amplitude Spectrum of x(t)")xlabel("Frequency (Hz)")ylabel("|y|")grid

fft - Fast Fourier transform (10)

Compute the phase of the transform, removing small-magnitude transform values. Plot the phase as a function of frequency.

tol = 1e-6;z(abs(z) < tol) = 0;theta = angle(z);stem(f,theta/pi)title("Phase Spectrum of x(t)")xlabel("Frequency (Hz)")ylabel("Phase/\pi")grid

fft - Fast Fourier transform (11)

Interpolation of FFT

Open Live Script

Interpolate the Fourier transform of a signal by padding with zeros.

Specify the parameters of a signal with a sampling frequency of 80 Hz and a signal duration of 0.8 s.

Fs = 80;T = 1/Fs;L = 65;t = (0:L-1)*T;

Create a superposition of a 2 Hz sinusoidal signal and its higher harmonics. The signal contains a 2 Hz cosine wave, a 4 Hz cosine wave, and a 6 Hz sine wave.

X = 3*cos(2*pi*2*t) + 2*cos(2*pi*4*t) + sin(2*pi*6*t);

Plot the signal in the time domain.

plot(t,X)title("Signal superposition in time domain")xlabel("t (ms)")ylabel("X(t)")

fft - Fast Fourier transform (12)

Compute the Fourier transform of the signal.

Y = fft(X);

Compute the single-sided amplitude spectrum of the signal.

f = Fs*(0:(L-1)/2)/L;P2 = abs(Y/L);P1 = P2(1:(L+1)/2);P1(2:end) = 2*P1(2:end);

In the frequency domain, plot the single-sided spectrum. Because the time sampling of the signal is quite short, the frequency resolution of the Fourier transform is not precise enough to show the peak frequency near 4 Hz.

plot(f,P1,"-o") title("Single-Sided Spectrum of Original Signal")xlabel("f (Hz)")ylabel("|P1(f)|")

fft - Fast Fourier transform (13)

To better assess the peak frequencies, you can increase the length of the analysis window by padding the original signal with zeros. This method automatically interpolates the Fourier transform of the signal with a more precise frequency resolution.

Identify a new input length that is the next power of 2 from the original signal length. Pad the signal X with trailing zeros to extend its length. Compute the Fourier transform of the zero-padded signal.

n = 2^nextpow2(L);Y = fft(X,n);

Compute the single-sided amplitude spectrum of the padded signal. Because the signal length n increased from 65 to 128, the frequency resolution becomes Fs/n, which is 0.625 Hz.

f = Fs*(0:(n/2))/n;P2 = abs(Y/L);P1 = P2(1:n/2+1);P1(2:end-1) = 2*P1(2:end-1);

Plot the single-sided spectrum of the padded signal. This new spectrum shows the peak frequencies near 2 Hz, 4 Hz, and 6 Hz within the frequency resolution of 0.625 Hz.

plot(f,P1,"-o") title("Single-Sided Spectrum of Padded Signal")xlabel("f (Hz)")ylabel("|P1(f)|")

fft - Fast Fourier transform (14)

Input Arguments

collapse all

XInput array
vector | matrix | multidimensional array

Input array, specified as a vector, matrix, or multidimensional array.

If X is an empty 0-by-0 matrix, then fft(X) returns an empty 0-by-0 matrix.

Data Types: double | single | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical
Complex Number Support: Yes

nTransform length
[] (default) | nonnegative integer scalar

Transform length, specified as [] or a nonnegative integer scalar. Specifying a positive integer scalar for the transform length can improve the performance of fft. The length is typically specified as a power of 2 or a value that can be factored into a product of small prime numbers (with prime factors not greater than 7). If n is less than the length of the signal, then fft ignores the remaining signal values past the nth entry and returns the truncated result. If n is 0, then fft returns an empty matrix.

Example: n = 2^nextpow2(size(X,1))

Data Types: double | single | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical

dimDimension to operate along
positive integer scalar

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension whose size does not equal 1.

  • fft(X,[],1) operates along the columns of X and returns the Fourier transform of each column.

    fft - Fast Fourier transform (15)

  • fft(X,[],2) operates along the rows of X and returns the Fourier transform of each row.

    fft - Fast Fourier transform (16)

If dim is greater than ndims(X), then fft(X,[],dim) returns X. When n is specified, fft(X,n,dim) pads or truncates X to length n along dimension dim.

Data Types: double | single | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical

Output Arguments

collapse all

Y — Frequency domain representation
vector | matrix | multidimensional array

Frequency domain representation returned as a vector, matrix, or multidimensional array.

If X is of type single, then fft natively computes in single precision, and Y is also of type single. Otherwise, Y is returned as type double.

The size of Y is as follows:

  • For Y = fft(X) or Y = fft(X,[],dim), the size of Y is equal to the size of X.

  • For Y = fft(X,n,dim), the value of size(Y,dim) is equal to n, while the size of all other dimensions remains as in X.

If X is real, then Y is conjugate symmetric, and the number of unique points in Y is ceil((n+1)/2).

Data Types: double | single

More About

collapse all

Discrete Fourier Transform of Vector

Y = fft(X) and X = ifft(Y) implement the Fourier transform and inverse Fourier transform, respectively. For X and Y of length n, these transforms are defined as follows:

Y(k)=j=1nX(j)Wn(j1)(k1)X(j)=1nk=1nY(k)Wn(j1)(k1),

where

Wn=e(2πi)/n

is one of n roots of unity.

Tips

  • The execution time of fft depends on the length of the transform. Transform lengths that have only small prime factors (not greater than 7) result in significantly faster execution time than those that are prime or have large prime factors.

  • For most values of n, real-input DFTs require roughly half the computation time of complex-input DFTs. However, when n has large prime factors, there is little or no speed difference.

  • You can potentially increase the speed of fft using the utility function fftw. This function controls the optimization of the algorithm used to compute an FFT of a particular size and dimension.

Algorithms

The FFT functions (fft, fft2, fftn, ifft, ifft2, ifftn) are based on a library called FFTW [1] [2].

References

[2] Frigo, M., and S. G. Johnson. “FFTW: An Adaptive Software Architecture for the FFT.” Proceedings of the International Conference on Acoustics, Speech, and Signal Processing. Vol. 3, 1998, pp. 1381-1384.

Extended Capabilities

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a

See Also

Functions

  • fft2 | fftn | fftw | fftshift | ifft | interpft

Topics

  • Fourier Transforms

External Websites

  • Fourier Analysis (MathWorks Teaching Resources)
  • Convolution in Digital Signal Processing (MathWorks Teaching Resources)

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

fft - Fast Fourier transform (17)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

fft - Fast Fourier transform (2024)

FAQs

Fft - Fast Fourier transform? ›

It converts a signal into individual spectral components and thereby provides frequency information about the signal. FFTs are used for fault analysis, quality control, and condition monitoring of machines or systems.

What is fast Fourier transform FFT used for? ›

It converts a signal into individual spectral components and thereby provides frequency information about the signal. FFTs are used for fault analysis, quality control, and condition monitoring of machines or systems.

What does an FFT tell you? ›

The fast Fourier transform (FFT) is a computational tool that transforms time-domain data into the frequency domain by deconstructing the signal into its individual parts: sine and cosine waves. This computation allows engineers to observe the signal's frequency components rather than the sum of those components.

What is the difference between fast Fourier and Fourier transform? ›

The FFT Fast Fourier Transform is an algorithm used to compute the discrete Fourier transform (DFT) and its inverse more efficiently. The DFT is a transform used in signal processing and image processing, among many other areas, to transform a discrete signal into its frequency domain representation.

When to use an FFT? ›

Applications of FFT

Audio Analysis: FFT is used in audio processing to analyze and manipulate audio signals. It's essential in tasks like audio compression, equalization, and pitch detection. Image Processing: In image processing, FFT is employed for tasks such as image filtering, compression, and feature extraction.

What is the main advantage of FFT? ›

The advantages of the FFT include speed and memory efficiency. The DFT can process sequences of any size efficiently but is slower than the FFT and requires more memory, because it saves intermediate results while processing.

What is a Fourier transform for dummies? ›

The Fourier Transform is a mathematical technique that transforms a function of time, x(t), to a function of frequency, X(ω). It is closely related to the Fourier Series. If you are familiar with the Fourier Series, the following derivation may be helpful.

Why do we go for FFT? ›

The FFT algorithm is used to convert a digital signal (x) with length (N) from the time domain into a signal in the frequency domain (X), since the amplitude of vibration is recorded on the basis of its evolution versus the frequency at that the signal appears [40].

What are the basics of FFT analysis? ›

The basic functions for FFT-based signal analysis are the FFT, the Power Spectrum, and the Cross Power Spectrum. Using these functions as building blocks, you can create additional measurement functions such as frequency response, impulse response, coherence, amplitude spectrum, and phase spectrum.

What does a Fourier transform do? ›

Fourier Transform is a mathematical model which helps to transform the signals between two different domains, such as transforming signal from frequency domain to time domain or vice versa. Fourier transform has many applications in Engineering and Physics, such as signal processing, RADAR, and so on.

What is Fast Fourier Transform for beginners? ›

Fast fourier transform is an algorithm that determines the discrete Fourier transform of an object faster than computing it. This can be used to speed up training a convolutional neural network. The application of Fourier transform isn't limited to digital signal processing.

How accurate is Fast Fourier Transform? ›

Fast Fourier transform (FFT)-based computations can be far more accurate than the slow transforms suggest. Discrete Fourier transforms computed through the FFT are far more accurate than slow transforms, and convolutions computed via FFT are far more accurate than the direct results.

What is the FFT of a real signal? ›

For real signals, the fft spectrum is a two-sided spectrum, where the spectrum in the positive frequencies is the complex conjugate of the spectrum in the negative frequencies. To show the fft spectrum in the positive and negative frequencies, you can use fftshift .

What is the FFT in simple terms? ›

The FFT is an efficient algorithm for computing the DFT. The core idea behind FFT is re-expressing Fourier Matrices as the product of 3 (sparse) matrices, given below. Re-expressing the N by N Fourier Matrix as the product of 3 sparse matrices. Definition of the Permutation matrix, used above.

How to interpret an FFT? ›

Frequency is plotted along the x-axis and amplitude is plotted along the y-axis. FFTs often look like a series of mountain peaks. The horizontal location of peaks indications which frequencies are strongly present in the sound. The valleys show which frequencies are absent.

What is the point of FFT? ›

The Fast Fourier Transform is an efficient algorithm for computing the Discrete Fourier Transform (DFT). It decomposes the Fourier transform of an n-point sequence into smaller subproblems, thus reducing the computational complexity.

What are the practical applications of FFT? ›

Applications. The FFT is used in digital recording, sampling, additive synthesis and pitch correction software. The FFT's importance derives from the fact that it has made working in the frequency domain equally computationally feasible as working in the temporal or spatial domain.

What is the purpose of FFT shift? ›

It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum. For vectors, fftshift(X) swaps the left and right halves of X .

Why FFT is used in image processing? ›

The Fast Fourier Transform (FFT) is commonly used to transform an image between the spatial and frequency domain. Unlike other domains such as Hough and Radon, the FFT method preserves all original data. Plus, FFT fully transforms images into the frequency domain, unlike time-frequency or wavelet transforms.

What is the purpose of the Fourier transform How fast does Fourier transform work explain? ›

The term “Fourier transform” can be used in the mathematical function, and it is also used in the representation of the frequency domain. The Fourier transform helps to extend the Fourier series to the non-periodic functions, which helps us to view any functions in terms of the sum of simple sinusoids.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Trent Wehner

Last Updated:

Views: 6253

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.