
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Fast Fourier Transform
The Fast Fourier Transform (FFT) is a widely used mathematical algorithm in signal processing and other fields for efficiently computing the Discrete Fourier Transform (DFT) and its inverse. The DFT converts a signal from its original domain (typically time or space) to a representation in the frequency domain.
The main advantage of the Fast Fourier Transform over the standard DFT is its speed. While the standard DFT algorithm has a computational complexity of O(N^2), where N is the number of samples in the signal, the FFT reduces this to O(N log N), making it much faster for large datasets.
Working with Fast Fourier Transform Using fft() Function in Matlab
The fft() function in MATLAB is used to compute the Fast Fourier Transform (FFT) of a signal. It takes a vector representing a signal in the time domain as input and returns a vector representing the signal in the frequency domain.
Syntax
Y = fft(X) Y = fft(X,n) Y = fft(X,n,dim)
Syntax Explanation
Y = fft(X) − Function in MATLAB calculates the Fourier transform of a signal. If you provide a vector, it computes the transform for that vector. If you provide a matrix, it calculates the transform for each column of the matrix. For multidimensional arrays, it treats the values along the first non-singleton dimension as vectors and computes the transform for each vector.
Y = fft(X,n) − The fft() function in MATLAB can calculate a specific length Fourier transform called the n-point DFT.
If you provide a vector X that is shorter than n, it adds zeros to the end of X to make it length n. If X is longer than n, it cuts off the extra values.
If X is a matrix, it processes each column as if it were a vector.
For multidimensional arrays, it treats the first non-singleton dimension as if it were a vector.
Y = fft(X,n,dim) − The fft() function in MATLAB can calculate the Fourier transform along a specific dimension of a matrix or multidimensional array.For example, if you have a matrix X and you use fft(X,n,2), it will calculate the n-point Fourier transform of each row of the matrix.
Let us see a few examples for syntaxes we explained above.
Example 1: Visualize the Fourier transform of a Simple Input Signal
The code we have is as follows −
x = [0, 1, 0, -1]; Y = fft(x); % Plotting figure; subplot(2,1,1); stem(x); title('Input Signal'); xlabel('Time'); ylabel('Amplitude'); subplot(2,1,2); stem(abs(Y)); title('Magnitude of Fourier Transform'); xlabel('Frequency'); ylabel('Magnitude');
In this example, we have a simple input signal x represented by a vector [0, 1, 0, -1].
The fft(x) function calculates the discrete Fourier transform (DFT) of the input signal x using the fast Fourier transform (FFT) algorithm. The result is stored in the variable Y.
After computing the Fourier transform, we create a figure to plot the input signal and its Fourier transform for visualization purposes.
In the first subplot (subplot(2,1,1)), we use the stem() function to plot the input signal x in the time domain. The x-axis represents time, and the y-axis represents the amplitude of the signal.
In the second subplot (subplot(2,1,2)), we use the abs() function to calculate the magnitude of the Fourier transform Y since the Fourier transform can have complex values. We then use stem() to plot the magnitude of the Fourier transform. Here, the x-axis represents frequency, and the y-axis represents the magnitude of the Fourier coefficients.
The output on execution is as follows −

Example 2: Fast Fourier Transform Using Matrix
The code we have is −
X = [1, 2, 3; 4, 5, 6; 7, 8, 9]; Y = fft(X); % Plotting figure; subplot(2,1,1); stem(X(:)); title('Input Matrix (Vectorized)'); xlabel('Index'); ylabel('Value'); subplot(2,1,2); stem(abs(Y(:))); title('Magnitude of Fourier Transform (Vectorized)'); xlabel('Frequency Index'); ylabel('Magnitude');
In this example, the first subplot shows the input matrix X (vectorized) with the matrix elements along the x-axis and their values on the y-axis. The second subplot shows the magnitude of the Fourier transform Y of the matrix (vectorized), with the frequency index on the x-axis and the magnitude of the Fourier coefficients on the y-axis.
On execution the output is as follows −

Example 3: Fast Fourier Transform Using Syntax Y = fft(x,n)
The code we have is as follows −
x = [0, 1, 0, -1]; n = 8; % Desired length of the Fourier transform Y = fft(x, n); % Plotting figure; subplot(2,1,1); stem(x); title('Input Signal'); xlabel('Time'); ylabel('Amplitude'); subplot(2,1,2); stem(abs(Y)); title('Magnitude of Fourier Transform (n-point DFT)'); xlabel('Frequency'); ylabel('Magnitude');
In this example, we have a vector x representing our input signal. We want to compute an 8-point discrete Fourier transform (DFT) of x using the syntax Y = fft(x, n).
Since the length of x is 4, which is shorter than n (8), MATLAB adds zeros to the end of x to make it length 8 before computing the Fourier transform.
After computing the Fourier transform, we create a figure to plot the input signal and its Fourier transform for visualization.
In the first subplot (subplot(2,1,1)), we use the stem() function to plot the input signal x in the time domain. The x-axis represents time, and the y-axis represents the amplitude of the signal.
In the second subplot (subplot(2,1,2)), we use the abs() function to calculate the magnitude of the Fourier transform Y since the Fourier transform can have complex values. We then use stem() to plot the magnitude of the Fourier transform. Here, the x-axis represents frequency, and the y-axis represents the magnitude of the Fourier coefficients.
On execution the output we have is −

Example 4: Fourier Transform of Each Row of a Matrix
The code we have is as follows −
X = [1, 2, 3; 4, 5, 6]; % Compute the 4-point Fourier transform of each row n = 4; % Desired length of the Fourier transform Y = fft(X, n, 2); % Display the result disp('Matrix X:'); disp(X); disp('Fourier Transform of Each Row (n-point DFT):'); disp(Y);
In this code, we first define a matrix X with two rows, where each row represents a different signal. We then compute the 4-point Fourier transform of each row of X using fft(X, n, 2), where n is the desired length of the Fourier transform. Finally, we display the original matrix X and the Fourier transform of each row.
When the code the executed the output is −
X = [1, 2, 3; 4, 5, 6]; % Compute the 4-point Fourier transform of each row n = 4; % Desired length of the Fourier transform Y = fft(X, n, 2); % Display the result disp('Matrix X:'); disp(X); disp('Fourier Transform of Each Row (n-point DFT):'); disp(Y); Matrix X: 1 2 3 4 5 6 Fourier Transform of Each Row (n-point DFT): 6.0000 + 0.0000i -2.0000 - 2.0000i 2.0000 + 0.0000i -2.0000 + 2.0000i 15.0000 + 0.0000i -2.0000 - 5.0000i 5.0000 + 0.0000i -2.0000 + 5.0000i