American International University – Bangladesh
Department of Electrical and Electronic Engineering
EEE3211: Digital Signal Processing Laboratory
Lab Report 3: Discrete-Time Signals and Systems
Course
EEE3211 – Digital Signal Processing Laboratory
Experiment No.
3
Title
Discrete-Time Signals and Systems
Tools Used
MATLAB
Reference
Proakis & Manolakis, Digital Signal Processing, 4th Ed.
Helper Functions
Two helper functions are required before running any of the question functions. Save each as a
separate .m file in your MATLAB working directory.
impseq.m — Unit Impulse Generator
function [x, n] = impseq(n0, n1, n2)
% Generates delta(n - n0) from n1 to n2
n = [n1:n2];
x = [(n - n0) == 0];
end
stepseq.m — Unit Step Generator
function [x, n] = stepseq(n0, n1, n2)
% Generates u(n - n0) from n1 to n2
n = [n1:n2];
x = [(n - n0) >= 0];
end
Question 1: User-Defined Functions for Plotting Sequences
Each function below prompts the user to enter the n-range from the MATLAB command prompt
using the input() function. The n-range controls how many samples are shown in the plot.
1(a) x(n) = 2δ(n+2) − δ(n−3) + 2u(n−1) − 2u(n−5)
This signal is a combination of two shifted unit impulses and two shifted unit step sequences with
different weights. The step components 2u(n−1) and −2u(n−5) create a rectangular pulse of height 2
between n=1 and n=4. The impulse at n=−2 adds a spike of amplitude 2 and the impulse at n=3
subtracts a spike of amplitude 1.
Save as: plot_q1a.m
function plot_q1a()
n1 = input('Enter start of n (n1): ');
n2 = input('Enter end of n (n2): ');
n = n1:n2;
% ── inline impseq ──────────────────────────
d_p2 = double((n - (-2)) == 0); % delta(n+2)
d_m3 = double((n - 3 ) == 0); % delta(n-3)
% ── inline stepseq ─────────────────────────
u_1 = double((n - 1) >= 0);
% u(n-1)
u_5 = double((n - 5) >= 0);
% u(n-5)
% ── combine ────────────────────────────────
x = 2*d_p2 - d_m3 + 2*u_1 - 2*u_5;
figure;
stem(n, x, 'filled');
xlabel('n');
ylabel('x(n)');
title('x(n) = 2delta(n+2) - delta(n-3) + 2u(n-1) - 2u(n-5)');
grid on;
end
How to Run:
>> plot_q1a
Enter start of n (n1): -5
Enter end of n (n2): 10
1(b) x(n) = 2sin(0.2πn) + 0.8cos(0.5πn) + w(n)
This sequence adds two sinusoidal components with different amplitudes and digital frequencies,
corrupted by Gaussian random noise w(n) with mean=0 and variance=1. The sine term has
frequency 0.2π rad/sample (one cycle every 10 samples) and amplitude 2. The cosine term has
frequency 0.5π rad/sample (one cycle every 4 samples) and amplitude 0.8. The noise is generated by
randn() which produces a different realisation every run.
Save as: plot_q1b.m
function plot_q1b()
% Plots x(n) = 2*sin(0.2*pi*n) + 0.8*cos(0.5*pi*n) + w(n)
% w(n) = Gaussian noise, mean=0, variance=1
n1 = input('Enter start of n (n1): ');
n2 = input('Enter end of n (n2): ');
n = n1:n2;
% Gaussian random noise
w = randn(1, length(n));
% Build the sequence
x = 2*sin(0.2*pi*n) + 0.8*cos(0.5*pi*n) + w;
% Plot
figure;
stem(n, x, 'filled');
xlabel('n (sample index)');
ylabel('x(n)');
title('x(n) = 2sin(0.2\pin) + 0.8cos(0.5\pin) + w(n)');
grid on;
end
How to Run:
>> plot_q1b
Enter start of n (n1): 0
Enter end of n (n2): 50
1(c) x(n) = {3, 1, 2, 0.5, 3, 1, 2, 0.5, 3, 1, 2, 0.5, 3} (Periodic)
This is a periodic sequence with fundamental period N = 4. One period is defined as {3, 1, 2, 0.5}.
The repmat() function tiles this one period as many times as needed based on the user-supplied nrange. The result is then trimmed to the exact required length.
Save as: plot_q1c.m
function plot_q1c()
% Plots periodic sequence with one period = {3, 1, 2, 0.5}
% Fundamental period N = 4
n1 = input('Enter start of n (n1): ');
n2 = input('Enter end of n (n2): ');
n = n1:n2;
total_len = length(n);
% Define one period
one_period = [3, 1, 2, 0.5];
N = length(one_period);
%N=4
% Compute number of periods needed and tile
P
= ceil(total_len / N);
x_tiled = repmat(one_period, 1, P);
x
= x_tiled(1:total_len); % Trim to exact length
% Plot
figure;
stem(n, x, 'filled');
xlabel('n (sample index)');
ylabel('x(n)');
title('Periodic Sequence: {3, 1, 2, 0.5} Period N = 4');
grid on;
end
How to Run:
>> plot_q1c
Enter start of n (n1): 0
Enter end of n (n2): 12
Question 2: Convolution and Correlation
Given signals: x(n) = {2, −1, 3, 1, −2} and y(n) = {1, 0.5, −1.5, 2}
Convolution computes the output of a linear time-invariant (LTI) system when x(n) is the input and
y(n) is the impulse response. Correlation measures the similarity between signals at different time
lags.
Full Code — Save as: q2_conv_corr.m
% q2_conv_corr.m
clear all; close all;
% Define signals
x = [2, -1, 3, 1, -2];
y = [1, 0.5, -1.5, 2];
% ══════════════════════════════════
% CONVOLUTION
% ══════════════════════════════════
c
= conv(x, y);
n_conv = 0 : length(c)-1;
figure;
subplot(3,1,1);
stem(0:length(x)-1, x, 'filled');
xlabel('n'); ylabel('x(n)');
title('x(n) = {2, -1, 3, 1, -2}');
grid on;
subplot(3,1,2);
stem(0:length(y)-1, y, 'filled');
xlabel('n'); ylabel('y(n)');
title('y(n) = {1, 0.5, -1.5, 2}');
grid on;
subplot(3,1,3);
stem(n_conv, c, 'filled');
xlabel('n'); ylabel('c(n)');
title('Convolution: c(n) = x(n) * y(n)');
grid on;
% ══════════════════════════════════
% CROSS-CORRELATION
% ══════════════════════════════════
[r_xy, lag_cross] = xcorr(x, y); % Let MATLAB give the lags
% ══════════════════════════════════
% AUTO-CORRELATION
% ══════════════════════════════════
[r_xx, lag_auto] = xcorr(x);
% Let MATLAB give the lags
figure;
subplot(2,1,1);
stem(lag_cross, r_xy, 'filled');
xlabel('Lag'); ylabel('r_{xy}');
title('Cross-Correlation of x(n) and y(n)');
grid on;
subplot(2,1,2);
stem(lag_auto, r_xx, 'filled');
xlabel('Lag'); ylabel('r_{xx}');
title('Autocorrelation of x(n)');
grid on;
How to Run:
>> q2_conv_corr
Convolution result c(n) will have 8 values (n = 0 to 7). The autocorrelation r_xx will always peak at
lag = 0 and be symmetric around it, confirming a fundamental property of autocorrelation.
Discussion
Question 1(a) — Impulse and Step Combination
In plot_q1a(), the helper functions impseq() and stepseq() are used to construct each component of
the signal individually. The variable d_p2 stores the unit impulse shifted to n = -2, producing a
single stem of height 1 at that position, scaled by 2. The variable d_m3 stores the impulse at n = 3,
subtracted once from the signal. The variable u_1 activates from n = 1 onwards with amplitude 2,
while u_5 cancels it from n = 5 onwards, forming a rectangular pulse of height 2 between n = 1 and
n = 4. The resultant figure shows these features clearly: a spike at n = -2 with height 2, a rectangular
region of height 2 from n = 1 to n = 4 with a dip at n = 3 due to the subtracted impulse, and zero
elsewhere within the range.
Question 1(b) — Sinusoidal with Gaussian Noise
In plot_q1b(), the variable n is built from user inputs n1 and n2. The noise vector w is created using
randn(1, length(n)) which produces values drawn from a standard normal distribution — mean 0,
variance 1. The first term 2*sin(0.2*pi*n) oscillates at a lower frequency (0.2π rad/sample),
completing one cycle every 10 samples with a larger amplitude of 2. The second term
0.8*cos(0.5*pi*n) oscillates faster at 0.5π rad/sample, completing one cycle every 4 samples but
with smaller amplitude 0.8. When combined and corrupted by noise, the resultant stem plot shows an
irregular oscillating pattern. Each run produces a slightly different graph because randn() generates a
new noise sequence every time.
Question 1(c) — Periodic Sequence
In plot_q1c(), the one-period vector {3, 1, 2, 0.5} represents the fundamental pattern with N = 4. The
variable P is computed using ceil() to ensure enough full copies are generated even when the total
length is not an exact multiple of 4. The repmat() function tiles the period P times into x_tiled, and
then x is trimmed to the exact required length using indexing. The resultant stem plot shows the
repeating pattern clearly: a tall spike of 3, followed by 1, then 2, then 0.5, repeating across all sample
indices in the given range. When called with n1=0 and n2=12, the output perfectly matches the given
sequence {3,1,2,0.5,3,1,2,0.5,3,1,2,0.5,3}.
Question 2 — Convolution and Correlation
For convolution, the built-in conv(x,y) computes the linear convolution of x(n) = {2,-1,3,1,-2} and
y(n) = {1,0.5,-1.5,2}. The output c(n) has length 5+4-1 = 8 samples spanning n = 0 to 7. The three
subplots clearly display x(n), y(n), and c(n) for direct comparison. Each value in c(n) is the sum of
products of overlapping samples, reflecting how the signal is transformed when passed through the
system whose impulse response is y(n).
For cross-correlation xcorr(x,y), the lag axis runs from -(length(y)-1) = -3 to +(length(x)-1) = +4. A
higher magnitude at a particular lag indicates that x(n) and y(n) align well at that shift. The
autocorrelation xcorr(x) peaks at lag = 0 because a signal is perfectly similar to itself with no shift,
and the symmetric shape around zero confirms the even-symmetry property of autocorrelation. Both
plots are consistent with the theoretical expectations derived from the given sample values.
EEE3211 Digital Signal Processing Lab | Lab Report 3 | AIUB