Fast Fourier Transform in MATLAB (original) (raw)

Last Updated : 28 Apr, 2025

Fast Fourier Transform is an algorithm for calculating the Discrete Fourier Transformation of any signal or vector. This is done by decomposing a signal into discrete frequencies. We shall not discuss the mathematical background of the same as it is out of this article's scope. MATLAB provides a built-in function to calculate the Fast Fourier Transform of a signal, the FFT function.

Syntax:

A = fft(X, n, dim)

Where A stores the Discrete Fourier Transformation (DFT) if signal X, which could be a vector, matrix, or multidimensional array. n specifies the n-point DFT for X, if not specified then, it is for all points in X. 'dim' specifies the dimension across which the DFT is to be calculated in N-D arrays.

Let us see this in practice for vectors as they are the more practical way of signal processing. We will compute the DFT of a dummy sinusoidal wave corrupted with some random error.

Example 1:

Matlab `

% MATLAB code for % Defining axes for multiple plots ax1 = axes('Position',[0.1 0.1 0.45 0.45]); ax2=axes('Position',[0.6 0.6 0.35 0.35]);

% Defining signal freq = 23; %freq afreq = 2pifreq; %angular frequency amp = 0.23; %amplitude phase = pi/4; %phase angle

% Time vector T = linspace(-pi,pi,100);

% Creating signal signal = amp*sin((afreq * T) + phase);

% Adding random error in signal signal = signal + 0.23*random(size(T));

% Plotting original signal in axes 1 plot(ax1,T,signal),title(ax1,"Original signal")

%computing DFT using Fast Fourier Transform y=fft(signal);

%plotting transformed signal in axes 2 plot(ax2,T,y),title(ax2,"FFT signal")

`

Output:

FFT of a random sinusoidal signal

Let us take another example where we will compute the DFT of a rectangular pulse using FFT.

Example 2:

Matlab `

% MATLAB code for % Defining the pulse x=linspace(-2.5,2.5,300); y=rectpuls(x,1);

% Computing the DFT using FFT k=fft(y);

% Defining axes for multiple plots ax1=axes('Position',[0.03 0.03 0.3 0.3]); ax2=axes('Position',[0.39 0.39 0.5 0.5]);

% Plotting original pulse plot(ax1,x,y) axis(ax1,[-1 1 -0.1 1.23]),title(ax1,"Rectangular pulse")

% Plotting the DFT of the rectangular pulse plot(ax2,x,k) axis(ax2,[-1 1 -.9 .9]),title(ax2,"DFT of rectangular pulse")

`

Output:

Now, we will Fast Fourier Transform a matrix.

Example 3:

Matlab `

% MATLAB code for creating a % matrix of exponential of random numbers mat = exprnd(1,3);

% Calculating its DFT using FFT entire matrix fft_mat_entire = fft(mat);

% Calculating its DFT using FFT dimension 1 fft_mat_dim1 = fft(mat,[],1);

% Calculating its DFT using FFT dimension 2 fft_mat_dim2 = fft(mat,[],2);

`

Output:

The output will give us Fourier Transform in three cases:

  1. FFT along the entire matrix.
  2. FFT along dimension 1
  3. FFT along dimension 2