. Digital Signal Processing Lab Instructor: Dr.
Shafayat Abrar
. Lab 01: Discrete Fourier Transform Lab Demonstrator: Ahmad Bilal
In Signals and Systems, you have studied the discrete-time Fourier series (DTFS) for periodic
discrete-time signals, and the discrete-time Fourier transform (DTFT) for nonperiodic discrete-
time signals. The result of DTFT analysis of a discrete-time signal x[n] is a transform X(Ω) which,
if it exists, is a 2π-periodic function of the continuous variable Ω. Storing the DTFT of a signal on a
digital computer is impractical because of the continuous nature of Ω. On the other hand, the DTFS
representation of a signal x̃[n] that is periodic with N samples is a set of coefficients c̃k that is also
periodic with N . While this combination would certainly be suitable for computer implementation and
storage, it is only for periodic signals. We often deal with signals that are not necessarily periodic.
1 Discrete Fourier Transform (One Dimensional)
In the analysis of non-periodic discrete-time signals, sometimes it is desirable to have a transform that
is also discrete. This can be accomplished through the use of the discrete Fourier transform (DFT)
provided that the signal under consideration is finite-length. Consider a signal x[n] the meaningful
samples of which are limited to the index range n = 0, . . . , N − 1, that is,
x[n] = 0 for n < 0 or n ≥ N
We will refer to x[n] as a length-N signal since it has N non-trivial samples.
The discrete Fourier transform (DFT) is defined by the forward transform as follows:
N
X −1
X[k] = DFT {x[n]} = x[n]e−j(2π/N )kn , k = 0, . . . , N − 1 (1)
n=0
The length-N signal x[n] leads to the length-N transform X[k]. It is also possible to obtain the signal
x[n] from the transform X[k] using the inverse DFT relationship
N −1
−1 1 X
x[n] = DFT {X[k]} = X[k]ej(2π/N )kn , n = 0, . . . , N − 1 (2)
N k=0
Example 1.1: Determine the DFT of the discrete-time pulse signal (where N = 10)
x[n] = u[n] − u[n − 10]
= { 1 , 1, 1, 1, 1, 1, 1, 1, 1, 1}
↑
n=0
Solution: The discrete Fourier transform is
9
X
X[k] = e−j(2π/10)kn , k = 0, . . . , 9 (3)
n=0
The signal x[n] and its DFT X[k] are shown in Fig. 1 (below).
Figure 1: The signal x[n] and the transform X[k] for Example 1.1.
Activity 1.1:
(a) Use MATLAB, to obtain two functions, one for the forward DFT, and the other for the inverse
DFT, based on the equations (1) and (2), respectively. Use appropriate names for those functions
like fDFT and iDFT, respectively.
(b) Use MATLAB code, fDFT, to obtain X[k] of the signal x[n] as given above in Example 1.1.
Note: You need to give values of x[n] as input to the function fDFT.
(c) Use MATLAB code, iDFT, to obtain x[n] of the spectrum X[k] as given above in Example 1.1.
Note: You need to give values of X[k] as input to the function iDFT.
(d) Note that the Equation (3) can be put into closed form using the finite-length geometric series
formula n2
X rn1 − rn2 +1
rn =
n=n
1−r
1
Page 2
Show that (
1 − e−j2πk 10, k = 0
X[k] = = (4)
1 − e−j2πk/10 0, k = 1, . . . , 9
(e) Using L’Hospital’s rule, show that X[0] = 10.
Activity 1.2:
Consider the following discrete-time signal x[n],
x[n] = { 0 , 0, 0, 0, 0, 1, 1, 1, 1, 1}
↑
n=0
(a) Obtain its spectrum X[k] using the function fDFT.
(b) Observe that the obtained spectrum X[k] is complex-valued. Provide plots of amplitude and phase
spectrums using MATLAB functions abs and angle, respectively, and provide their stem plots.
(c) Using iDFT, recover x[n] back from X[k], and provide its stem plot.
Figure 2: The signal x[n] for Activity 1.2.
Activity 1.3:
There are built-in functions in MATLAB to obtain forward and inverse DFT; they are fft and ifft,
respectively. Verify that the outcomes of the functions fDFT and iDFT are same as obtained from fft
and ifft, respectively.
2 Computing Linear Convolution Using the DFT With Zero-Padding
Given two finite length signals with Nx and Nh samples respectively
x[n], n = 0, . . . , Nx − 1 and h[n], n = 0, . . . , Nh − 1
Page 3
the linear convolution yℓ [n] = x[n] ∗ h[n] is obtained in MATLAB as
y = conv(x,h);
Alternatively, the linear convolution of x[n] and h[n] can be computed with the aid of DFT as follows:
(1) Anticipating the length of the linear convolution result to be Ny = Nx +Nh −1, extend the length
of each signal to Ny ; this is called zero-padding:
(
x[n], n = 0, . . . , Nx − 1
xp [n] =
0, n = Nx , . . . , Ny − 1
(
h[n], n = 0, . . . , Nh − 1
hp [n] =
0, n = Nh , . . . , Ny − 1
(2) Compute the forward DFTs of the zero-padded signals xp [n] and hp [n].
Ny −1
X
Xp [k] = DFT {xp [n]} = xp [n]e−j(2π/N )kn , for k = 0, . . . , Ny − 1
n=0
Ny −1
X
and Hp [k] = DFT {hp [n]} = hp [n]e−j(2π/N )kn , for k = 0, . . . , Ny − 1
n=0
In MATLAB, this can be done conveniently using the fft function as follows:
Xp [k] = fft( xp [n] ) and Hp [k] = fft( hp [n] )
(3) Multiply the two DFTs to obtain Yp [k].
Yp [k] = Xp [k]Hp [k]
Note: In MATLAB, you need to perform a "dot multiplication".
(4) Compute yp [n] through inverse DFT:
yp [n] = DFT−1 {Yp [k]} = ifft( Yp [k] )
The result yp [n] is the same as the linear convolution of the signals x[n] and y[n].
Activity 1.4:
In this activity, you will develop a function for convolving two signals. Even though the built-in MAT-
LAB function conv accomplishes this task very well, developing our own function that uses the DFT
is still instructive. As discussed above, the linear convolution can be obtained through the use of the
Page 4
DFT as long as the length of the transform is adjusted carefully by zero-padding.
(a) Write a function dft_based_conv that computes the linear convolution of two finite-length sig-
nals x[n] and h[n] the samples of which are stored in vectors "x" and "h". For example, consider:
x = [1,3,2,-4,6,2,1]; % Signal x[n]
h = [5,4,3,2,1]; % Signal h[n]
(b) Verify that the outcome of dft_based_conv is the same as the linear convolution of x and h.
Note that the built-in function for linear convolution conv(x,h) does not require zero-padding.
Note: In real-time speech, image, and video processing, the convolution is mostly achieved by DFT,
not by the orthodox convolution process.
3 DFT (Two-Dimensional) to Filtering Images
In this activity, we will examine the use of the two-dimensional Discrete Fourier Transform (2D-DFT)
in image processing for tasks such as filtering, enhancement, and editing. The process involves apply-
ing the 2D-DFT to the image, performing dot multiplication with the filter’s two-dimensional transfer
function, and then using the inverse Discrete Fourier Transform (2D-IDFT) to obtain the processed or
filtered image. Importantly, the size of the transfer function grid corresponds to the dimensions of the
image, with size referring to the number of rows and columns in the pixel grid.
The transfer function, H[k, ℓ], represents the Fourier transform of the impulse response of the image
processing filter, where k and ℓ are the row and column indices of the grid. Now, consider an image
corrupted by additive noise, specifically salt-and-pepper noise. This type of noise manifests as random
black-and-white pixels scattered across the image.
Activity 1.5(a)-(c):
(a) Load a sample picture:
xn=imread('cameraman.tif'); % Load an image from MATLAB root
(b) Add salt-and-pepper noise:
xn_noisy=imnoise(xn,'salt & pepper',0.05); % 0.05 is the noise density,
% you may increase or decrease it.
(c) Plot the original and noisy images side-by-side. Use imshow to plot images and use subplot to
get side-by-side plots. The desired outcome is shown below in Figure 3.
Salt-and-pepper noise appears abruptly and randomly across the image, exhibiting characteristics
Page 5
Figure 3: The original and noisy images.
similar to high-frequency behavior. If this assumption holds true, it may be possible to remove the
noise by applying a 2D low-pass filter (LPF). You may recall the concept of an LPF from your Signals
and Systems course. The transfer function of an LPF, which is the Fourier transform of its impulse
response, maintains a constant value at lower frequencies and gradually diminishes to zero at higher
frequencies. Figure 4 illustrates the transfer functions of both an ideal LPF and a first-order RC-type
LPF.
Figure 4: The ideal LPF (left) and the first-order LPF (right).
By extending this previous knowledge to a two-dimensional scenario, we may easily design a 2D
LPF transfer function as shown in Fig. 5. Both transfer functions are real-valued, and their matrix
dimensions (the number of rows and columns of pixels) must match those of the noisy image. In Figure
5, black represents a value of zero, white represents a value of one, and gray represents values between
Page 6
Figure 5: The ideal LPF (left) and a sort of first-order LPF (right).
zero and one, depending on the intensity of the gray level. When these transfer functions are visualized
on a two-dimensional plane as images, the resulting plots appear as shown in Figure 6.
Figure 6: The black-and-white filter (H1 left) and the gray filter (H2 right).
Activity 1.5(d)-(k):
(d) Convert the uint8-format noisy image into the double format using the function double, and
obtain its 2D FFT using fft2.
(e) Shift the FFT spectrum using fftshift so that the larger amplitudes of the spectrum appear in the
middle of the grid (the image frame).
X = fftshift(fft2(double(xn_noisy)));
(f) Let (M, N ) be the sizes of the noisy image. This can be obtained in MATLAB as follows:
[M, N] = size(X);
Page 7
Let D1 be the radius of the white circle in the black-and-white filter. Let D2 be the arbitrary radius
of the gray filter. The frequency-domain transfer functions, H1 and H2 , are obtained as follows:
[U, V] = meshgrid(1:N, 1:M);
D1 = 70; D2 = 50; p = 1; % For image size 256 x 256, the values
% of D1 and D2 are usually less than 100, and 0 < p <= 2.
D = sqrt((U - N/2).^2 + (V - M/2).^2);
H1k = D <= D1; % Black-and-white Filter
H2k = exp(-(D/D2).^p); % Gray Filter
Plot H1 and H2 using imshow to obtain Fig. 4.
(g) Dot-multiply the image X with filter transfer functions H1 and H2 to obtain the filtered responses
Y1 and Y2, respectively, in the frequency domain.
(h) Remove FFT shift from Y1 and Y2 using the function ifftshift.
(i) Obtain 2D inverse Fourier transform of the results obtained in step (h) using the function ifft2.
(j) Convert the results obtained in step (i) back into the uint8 format using the function uint8.
(k) Plot results obtained in step (j) side-by-side using imshow and subplot, record your findings,
and show it to your instructor or lab demonstrator.
Attention: Both filters H1 and H2 function as low-pass filters, designed to remove salt-and-pepper
noise, which manifests in the image as high-frequency components due to its sudden, abrupt nature.
Page 8