Islamic University of Technology (IUT)
Organization of Islamic Cooperation (OIC)
Department of Electrical and Electronic Engineering
Course Code: EEE 4702
Course Name: Digital Signal Processing Lab
Experiment 02: Study of sampling theorem, conversion of sampling rate, impulse and
frequency response of linear systems
Objectives
• Verification of the sampling theorem and aliasing
• Analysis of downsampling, upsampling of discrete signals
• Study of impulse and frequency responses of systems
Part A: Sampling & Aliasing
Sampling is the process of converting a continuous-time signal into a discrete-time signal by taking
measurements at specific intervals. The goal is to choose a sampling rate high enough so that no
information is lost, ensuring that the original continuous-time signal can be accurately reconstructed
from its discrete samples.
Sampling Theorem: A continuous signal can be completely reconstructed from its discrete samples if
the sampling frequency is at least twice the highest frequency present in the signal.
𝑁𝑦𝑞𝑢𝑖𝑠𝑡 𝑅𝑎𝑡𝑒, 𝑓𝑁 = 2𝑓𝑚𝑎𝑥 𝐻𝑧
If a continuous-time signal 𝑥(𝑡) is sampled at sampling frequency 𝑓𝑆 ≥ 𝑓𝑁 , the signal will be
1 2 𝑛
sampled at 𝑡 = 0, 𝑓 , 𝑓 , … , 𝑓 , … where n is the sample index of resulting discrete signal 𝑥(𝑛).
𝑠 𝑠 𝑠
MATLAB Code for Sampling a Continuous-time signal 𝑥(𝑡) to 𝑥(𝑛):
clc;
clear all;
close all;
F=input('Enter the frequency of the Continuous time signal: ');
T = 1/F; % Analog signal's period
%% -------Generation of a continuous time signal
t=0:(T/100):4*T; % visualizing four full cycle
x_analog=cos(2*pi*F*t);
subplot(2,2,1);
plot(t,x_analog);
title('Continuous Time Signal');
xlabel('Continuous time(s)');
ylabel('x(t)');
%% ------Generation of Discrete time sequence when fs > 2F
fs_high=6*F; %fs > 2*F
n1= 0:t(end)*fs_high; % discrete time axis: last index corresponds to the final time of cont. signal
disc_f_high = F / fs_high; % discrete signal frequency: cycle/sample
x_high=cos(2*pi*disc_f_high*n1);
subplot(2,2,2);
stem(n1,x_high,'filled')
title('Sampling above Nyquist Rate');
xlabel('Discrete time(n)');
ylabel('x(n)');
%% -----Generation of Discrete time sequence when fs=2F
fs_nyq=2*F; %fs = 2*F
n2=0:t(end)*fs_nyq;
disc_f_nyq = F / fs_nyq;
x_nyq=cos(2*pi*disc_f_nyq*n2);
subplot(2,2,3);
stem(n2,x_nyq,'filled')
title('Sampling at Nyquist Rate');
xlabel('Discrete time(n)');
ylabel('x(n)');
%% -------Generation of Discrete time sequence when fs<2F
fs_low=F; %fs < 2*F
n3=0:t(end)*fs_low;
disc_x_f_low = F / fs_low;
x_low=cos(2*pi*disc_x_f_low*n3);
subplot(2,2,4);
stem(n3,x_low,'filled')
ylim([-1, 1])
title('Sampling below Nyquist Rate');
xlabel('Discrete time(n)');
ylabel('x(n)');
Expected Outputs:
Part B: Conversion of Sampling Rate
In order to interface between two digital systems with different sampling frequencies 𝑓𝑆1 and 𝑓𝑆2 ,
resampling is required to convert the signal from one sampling rate to the other.
1. Decimation
Downsampling is the process of reducing the sampling rate by a factor of 𝐷 . In order to downsample a
discrete time sequence 𝑥(𝑛) by a factor 𝐷 , we keep every 𝐷 th sample and discard the remaining D-1
samples. This technique is useful for signal compression, thereby reducing computational efforts.
The resulting downsampled signal spectrum expands in the frequency domain. To ensure zero
𝜋
decimation error, aliasing must be avoided. For that, the input signal should be band limited to 𝐷.
Ideally, we use a lowpass anti-aliasing filter before downsampling to eliminate the input signal
𝜋
spectrum within the range of 𝐷 < 𝑤𝑥 < 𝜋. The process of filtering and downsampling is collectively
called decimation.
MATLAB Code:
clc;
clear all;
close all;
%Get the decimation factor
D=input('enter the decimation factor: ');
%Get the signal length
N=input('enter the length of the input signal: '); %integer multiple of D
%Get the normalized frequency of the sinusoid
f1=input('enter the normalized frequency of the sinusoid: ');
%Generate discrete time signal
n=0:N-1; %discrete time axis
x=cos(2*pi*f1*n);
subplot(3,1,1);
stem(n,x(1:N),'filled');
title('Input signal');
xlabel('time(n)');
ylabel('x(t)');
%Downsample the discrete time signal
m=0:(N/D)-1; %discrete time axis
y1=downsample(x,D);
subplot(3,1,2)
stem(m,y1(1:N/D),'filled');
title('Downsampled discrete signal');
xlabel('time(m)');
ylabel('y1(t)');
%Decimate the discrete time signal
y2=decimate(x,D); %uses default IIR filter of order 30
subplot(3,1,3)
m=0:(N/D)-1; %discrete time axis
stem(m,y2(1:N/D),'filled');
title('Decimated discrete signal');
xlabel('time(m)');
ylabel('y2(t)');
Expected Inputs:
enter the decimation factor: 2
enter the length of the input signal: 100
enter the normalized frequency of the sinusoidal: 0.05
Expected Outputs:
2. Interpolation
Upnsampling means increasing the sampling rate by a factor of 𝐼 . If we want to upsample a discrete
time signal by a factor of 𝐼 , we insert 𝐼 − 1 zeros between the successive samples. Unlike
downsampling, this upsampling ensures that there is no information loss.
Because of zero-insertion, the upsampled signal spectrum gets compressed in the frequency domain
and produces spectral images. For getting a clear, image-free spectrum we pass the upsampled signal
through a low-pass anti-imaging filter. The process of upsampling followed by low-pass filtering is
known as interpolation.
MATLAB Code:
clc;
clear all;
close all;
%Get the interpolation factor
I=input('enter the interpolation factor: ');
%Get the signal length
N=input('enter the length of the input signal: ');
%Get the normalized frequency of the sinusoid
f1=input('enter the normalized frequency of the sinusoid: ');
%Generate discrete time signal
n=0:N-1; %discrete time axis
x=cos(2*pi*f1*n);
subplot(2,1,1);
stem(n,x(1:N),'filled');
title('Input signal');
xlabel('time(n)');
ylabel('x(t)');
%Interpolate the discrete time signal
m=0:(N*I)-1; %discrete time axis
y=interp(x,I,5);
subplot(2,1,2)
stem(m,y(1:N*I),'filled');
title('Interpolated signal');
xlabel('time(m)');
ylabel('y(t)');
Expected Inputs:
enter the interpolation factor: 3
enter the length of the input signal: 50
enter the normalized frequency of the sinusoid: 0.05
Expected Outputs:
3. Resampling
Resampling means changing the sampling rate of a discrete signal by a rational factor. The
conversion of sampling rate by a rational factor of 𝐷𝐼 requires cascading of a interpolator
(upsampler + LPF) and a decimator (LPF+downsampler). If 𝐷𝐼 is greater than 1, we have net
𝐼
interpolation. On the other hand, whenever 𝐷 is less than 1, we observe net decimation. For
preserving the spectral characteristics of discrete signal 𝑥(𝑛), interpolation needs to be done
first and then decimation.
MATLAB Code:
clc;
clear all;
close all;
%Get the decimation factor
D=input('enter the decimation factor: ');
%Get the interpolation factor
L=input('enter the interpolation factor: ');
%Get the signal length
N=input('enter the length of the input signal: ');
%Get the frequencies of the sinusoids
f1=input('enter the frequency of first sinusoidal: '); %should be less than 0.5
f2=input('enter the frequency of second sinusoidal: ');
%Generate discrete time sequence
n=0:N-1; %discrete time axis
x=cos(2*pi*f1*n)+cos(2*pi*f2*n);
subplot(2,1,1);
stem(n,x(1:N),'filled');
title('Input sequence');
xlabel('time(n)');
ylabel('x(t)');
%Generate Resampled Sequence
resample_length= ceil((N*L)/D);
m=0:resample_length-1; %discrete time axis
y=resample(x,L,D);
subplot(2,1,2)
stem(m,y(1:resample_length),'filled');
title('Output sequence ');
xlabel('time(m)');
ylabel('y(t)');
Expected Inputs:
enter the decimation factor: 3
enter the interpolation factor: 5
enter the length of the input signal: 20
enter the frequency of first sinusoidal: 0.1
enter the frequency of second sinusoidal: 0.01
Expected Outputs:
+--
Class Task:
1. 𝑥(𝑡) = 2 sin(4 𝜋𝑡) is an analog signal
a. Plot the continuous time signal.
b. Sample the at the Nyquist rate to generate discrete signal x[n], Plot it. Now add a
𝜋
phase of 3 to the signal and plot it again.
c. Upsample the discrete signal x[n] by a factor of 4 using built-in function
‘upsample’.
d. Interpolate the signal x[n] by the same factor used in ‘c’. Which discrete signal is
smoother and why?
2. A discrete time signal, 𝑥[𝑛] = 2 sin(0.2𝜋𝑛)
Decimate the signal by a factor of 8. Observe and explain the output.
Part C:
1. Impulse Response
Impulse response is the output of a differential equation/dynamic system when a unit impulse
𝛿(𝑛) excitation is applied. If impulse response ℎ(𝑛) is known for an LTI system, then for an
arbitrary input 𝑥(𝑛) ,the response of the system is
𝑦(𝑛) = ∑ 𝑥(𝑘)ℎ(𝑛 − 𝑘)
𝑘
MATLAB Code for Impulse Response
clc;
close all;
clear all;
n=0:20;
%impulse response of first order system y(n)-0.7y(n-1)=2x(n)
a=[1 -.7 0]; b=[2 0 0];
y= dimpulse(b,a,length(n));
subplot(2,1,1);
stem(n,y, 'filled');
xlabel('n');
ylabel('h(n)');
title('Impulse response of first order system');
%impulse response of second order system y(n)-1.6y(n-1)+0.8y(n-2)=x(n)
b=[1 0 0]; a=[1 -1.6 .8];
y1= dimpulse(b,a,length(n));
subplot(2,1,2);
stem(n,y1, 'filled');
xlabel('n');
ylabel('h(n)');
title('impulse response of second order system');
Expected Outputs:
2. Frequency Response
The output of a linear system in response to a sinusoidal excitation is also a sinusoidal excitation.
Let the following be our linear system of interest:
𝑦(𝑛) − .5𝑦(𝑛 − 1) = 𝑥(𝑛)
If input excitation 𝑥(𝑛) = 𝑒 𝑖𝜔𝑛 is applied, we get 𝑦(𝑛) = 𝐻(𝜔)𝑒 𝑖𝜔𝑛 which is also a sinusoid with
some added scaling and phase. Here 𝐻(𝜔) is the frequency response whose
• Magnitude |𝐻(𝜔)| = scaling of output response
• Phase ∠𝐻(𝜔) = delay of output response
The output 𝑦(𝑛) differs from the input only in amplitude and phase. Different frequencies will
be amplified(attenuated) and phase shifted differently (hence, H is a function of 𝜔). The goal is
to study how the magnitude and phase of H changes with respect to frequency.
MATLAB Code for Frequency Response
clc;
clear all;
close all;
b=input('Enter the numerator coefficients:');
a=input('Enter the denominator coefficients:');
[h,w]=freqz(b,a);
subplot(2,1,1);
plot(w/pi,abs(h)); // normalized frequency
grid;
xlabel('\omega/\pi ');
ylabel('Magnitude(dB)');
title('Magnitude Response');
subplot(2,1,2);
plot(w/pi,angle(h));
grid;
xlabel('Normalised Frequency');
ylabel('phase in radians');
title('Phase Response');
Expected Inputs:
Enter the numerator coefficients: [1]
Enter the denominator coefficients: [1 -0.7 2.1]
Expected Outputs:
Assignments:
Task-1: You have 120 samples of a discrete signal
𝑥[𝑛] = sin(2 𝜋𝑓1 𝑛) + 0.3 cos(2 𝜋𝑓2 𝑛)
1 1
where 𝑓1 = 40 , 𝑓2 = 4cycles/sample respectively.
a. Plot the given signal. Can you see both frequencies?
b. Now down sample the signal by a factor of 3. Show the plot.
c. Finally, up sample the down sampled signal by the same factor. Show the plot. Can you
explain the result?
Task-2: In this task, we shall study how a second order system can be designed into a bandpass filter.
a. Keep numerator b=[1] fixed and denominator [1 0 var]. Vary var within a suitable range and
observe the frequency response characteristics of the system. For what value of var the system
shows the maximum peak?
b. Keep the var= where maximum peak is found(call it var_max), and vary the 2nd coefficient of
a=[1 var_2 var_max]. How does the response change? Try both negative and positive
coefficients.
𝜋
c. Design a 2nd order system whose peak magnitude is 20 dB at 𝜔 = 3 .
Task-3: Find the impulse response of the 2nd order system you designed in Task 2 for 50 samples.
Plot it.
For a general LTI system, the output of the system is the convolution of the system’s impulse
response and the input excitation . Find the output of the system, if x[n]= [0 0 0.3 0.6 0.9 0.6 0.3 0 0 0
0]. Use your own convolution function (from EXP 1).
Note: you must include all the codes you wrote, screenshots of your plots in your report.
Prepared by:
Abrar Al Shadid Abir
Junior Lecturer, EEE, IUT
Fatema Tasnim Oyshi
Junior Lecturer, EEE, IUT