Discrete Fourier Transform and its Inverse using MATLAB
Last Updated :
04 Jul, 2021
With the advent of MATLAB and all the scientific inbuilt that it has brought, there's been a significant change and simplification of sophisticating engineering scenarios. In fact, this change has contributed to helping build better visualization and practical skills for students pursuing technical studies in the sciences, if not other fields at the very least. Here we look at implementing a fundamental mathematical idea - the Discrete Fourier Transform and its Inverse using MATLAB.
Calculating the DFT
The standard equations which define how the Discrete Fourier Transform and the Inverse convert a signal from the time domain to the frequency domain and vice versa are as follows:
DFT: X(k)=\sum_{n=0}^{N-1}x(n).e^{-j2\pi kn/N} for k=0, 1, 2....., N-1
IDFT: x(n)=\sum_{k=0}^{N-1}X(k).e^{j2\pi kn/N} for n=0, 1, 2....., N-1
The equations being rather straightforward, one might simply execute repetitive/nested loops for the summation and be done with it. However, we should attempt to utilize another method where we use matrices to find the solution to the problem. Many readers would recall that the DFT and IDFT of a time/frequency domain signal may be represented in vector format as the following:
X(k) = \sum_{n=0}^{N-1}x(n).W_{N}^{kn}
x(n)=\frac{1}{N}\sum_{k=0}^{N-1}X(k).W_{N}^{-kn}
W_{N}=e^{-j2\pi/N}
When we take the twiddle factors as components of a matrix, it becomes much easier to calculate the DFT and IDFT. Therefore, if our frequency-domain signal is a single-row matrix represented by XN and the time-domain signal is also a single-row matrix represented as xN......

With this interpretation, all we require to do, is create two arrays upon which we shall issue a matrix multiplication to obtain the output. The output matrix will ALWAYS be a Nx1 order matrix since we take a single-row matrix as our input signal (XN or xN). This is essentially a vector which we may transpose to a horizontal matrix for our convenience.
Algorithm (DFT)
- Obtain the input sequence and number of points of the DFT sequence.
- Send the obtained data to a function which calculates the DFT. It isn't imperative to declare a new function but code legibility and flow become cleaner and apparent.
- Determine the length of the input sequence using the length( ) function and check if the length is greater than the number of points. N must always be equal to or greater than the sequence. If you try to execute the matrix multiplication by not satisfying the condition, you'll be met with an error in your command window.
- Accounting for the difference in lengths of the input sequence and the N-points using a separate array which adds extra zeros to elongate the input sequence. This is done using the zeros(no_of_rows, no_of_columns) function which creates a 2D array composed of zeros.
- Based on the value of N obtained as input, create the WN matrix. To do this, implement 2 'for' loops -quite a basic procedure.
- Simply multiply the two arrays that have been created. This is an array of the required frequency-domain signal samples.
- Plot the magnitude and the phase of the output signal via inbuilt functions abs(function_name) and angle(function_name).
Matlab
% MATLAB code for DFT
clc;
xn=input('Input sequence: ');
N = input('Enter the number of points: ');
Xk=calcdft(xn,N);
disp('DFT X(k): ');
disp(Xk);
mgXk = abs(Xk);
phaseXk = angle(Xk);
k=0:N-1;
subplot (2,1,1);
stem(k,mgXk);
title ('DFT sequence: ');
xlabel('Frequency');
ylabel('Magnitude');
subplot(2,1,2);
stem(k,phaseXk);
title('Phase of the DFT sequence');
xlabel('Frequency');
ylabel('Phase');
function[Xk] = calcdft(xn,N)
L=length(xn);
if(N<L)
error('N must be greater than or equal to L!!')
end
x1=[xn, zeros(1,N-L)];
for k=0:1:N-1
for n=0:1:N-1
p=exp(-i*2*pi*n*k/N);
W(k+1,n+1)=p;
end
end
disp('Transformation matrix for DFT')
disp(W);
Xk=W*(x1.')
end
Output:
>> Input sequence: [1 4 9 16 25 36 49 64 81]
>> Enter the number of points: 9

Algorithm (IDFT)
- Obtain the frequency-domain signal / sequence as input (X(k)). The length of this sequence suffices as a value for N (points).
- Pass this array to a function for computation.
- Run 2 loops in the function to create the matrix. Note that this matrix must be conjugated when being utilized for the calculation. You may choose to explicitly declare another array which is the conjugate of the matrix WN.
- Once, the matrix has been created, obtain the conjugate using '*' and simply multiply it with the input sequence's transpose. We require the transpose as the input is a row matrix. When multiplying with the WN matrix we have created, the number of columns in WN must match the number of rows in X(k).
- Plot this sequence using stem(x_axis, y_axis). DO NOT use plot( ) since this is not a CT signal.
Matlab
% MATLAB code for IDFT
clc;
Xk = input('Input sequence X(k): ');
xn=calcidft(Xk);
N=length(xn);
disp('xn');
disp(xn);
n=0:N-1;
stem(n,xn);
xlabel('time');
ylabel('Amplitude');
function [xn] = calcidft(Xk) %function to calculate IDFT
N=length(Xk);
for k=0:1:N-1
for n=0:1:N-1
p=exp(i*2*pi*n*k/N);
IT(k+1,n+1)=p;
end
end
disp('Transformation Matrix for IDFT');
disp(IT);
xn = (IT*(Xk.'))/N;
end
Output:
>> Enter the input sequence: [1 2 3 4 5 9 8 7 6 5]
Transformation Matrix
The time-domain sequence
Similar Reads
Fast Fourier Transform in MATLAB
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 bui
3 min read
Forward and Inverse Fourier Transform of an Image in MATLAB
In this article, we shall apply Fourier Transform on images. Fourier Transform is a mathematical technique that helps to transform Time Domain function x(t) to Frequency Domain function X(Ï). In this article, we will see how to find Fourier Transform in MATLAB. Properties of Fourier Transform:Linear
4 min read
2-D Inverse Cosine Transform in MATLAB
The 2-D inverse cosine transform is used to decode an image into the spatial domain, which is a more suitable data representation for compression (ICT). ICT-based decoding is the foundation for standards for image and video decompression. or, to put it another way, we can say that the inverse cosine
2 min read
How to Find Interior and Exterior Skeleton of Binary Images Using MATLAB?
Skeletonization is a process for reducing foreground regions in a binary image to a skeletal remnant that largely preserves the extent and connectivity of the original region while throwing away most of the original foreground pixels. In this article, we will see how to find interior and exterior sk
2 min read
Edge detection using first derivative operator in MATLAB
An edge in an image is a significant local change in image intensity, usually associated with a discontinuity in image intensity or the first derivative of image intensity. The discontinuities in the intensity of the image can be stepped discontinuities, in which the intensity of the image changes f
3 min read
Create Mirror Image using MATLAB
Prerequisite: Image representation in MATLAB In MATLAB, Images are stored in matrices, in which each element of the matrix corresponds to a single discrete pixel of the image. We can get the mirror image of the given image if we reverse the order of the pixels (elements of the matrix) in each row. C
2 min read
How to detect duplicate values and its indices within an array in MATLAB?
In this article, we will discuss how to find duplicate values and their indices within an array in MATLAB. It can be done using unique(), length(), setdiff(), and numel() functions that are illustrated below: Using Unique() Unique(A) function is used to return the same data as in the specified array
3 min read
Finding Inverse of a Square Matrix using Cayley Hamilton Theorem in MATLAB
Matrix is the set of numbers arranged in rows & columns in order to form a Rectangular array. Here, those numbers are called the entries or elements of that matrix. A Rectangular array of (m*n) numbers in the form of 'm' horizontal lines (rows) & 'n' vertical lines (called columns), is calle
4 min read
Solving 2nd Order Homogeneous Difference Equations in MATLAB
Difference Equations are Mathematical Equations involving the differences between usually successive values of a function ('y') of a discrete variable. Here, a discrete variable means that it is defined only for values that differ by some finite amount, generally a constant (Usually '1'). A differen
4 min read
How To Detect Face in Image Processing Using MATLAB?
MATLAB Â is a programming platform that is mainly used by engineers and scientists to analyze and design systems. Image processing is a process to perform some operations on an image to get an enhanced image or to extract some useful information from it. Each picture is stored as an array and each pi
5 min read