LabSession2_SyC
LabSession2_SyC
Lab 2: Systems
1 Objectives
In this second Laboratory Session, we will learn how to:
• Implement discrete-time systems as functions in Matlab.
• Implement a discrete-time convolution in Matlab.
2 Evaluation
Students must complete a report following the provided template (Report Lab
Session2 SyC template.docx). Once completed, the report must be uploaded
to Aula Global in pdf format, along with the corresponding Matlab code used
to solve the exercises.
1
function [ny, y] = system1(nx, x)
% Inputs:
% nx: range of time instants of the input signal
% In this case, we assume the input signal is zero out of this range
% x: values of the input signal at the time instants in nx
% Outputs:
% ny: range of time instants of the output signal
% y: values of the output signal
2
x4 = [zeros(1, nx(1) - n_init), x];
y4 = -0.4*x4;
Despite Matlab already providing the function conv to implement the con-
volution operation, in this exercise we are going to program a version that is
able to work with signals defined with two vectors (one for the time values
and one for the values of the signal). To this end, program in a file called
discreteconvolution.m a function that implements the convolution between
two finite-length signals. The structure of the function would be as follows:
3
function [ny, y] = discreteconvolution(nx, x, nh, h)
% Inputs:
% nx: range of time instants of the input signal
% x: values of the input signal at the time instants in nx
% nh: range of time instants of the impulse response
% h: values of the impulse response at the time instants in nh
% Outputs:
% ny: range of time instants of the output signal
% y: values of the output signal at the time instants in ny
The Matlab function conv computes the convolution between two vectors
of length N , assuming that the first sample is at n = 1 and the last one is at
n = N . Given the pair of input signals x[n] and h[n], which start at sx and
sh , respectively, we can build a pair of signals x1 [n] and h1 [n] starting at n = 1
such that x[n] = x1 [n − sx + 1] and h[n] = h1 [n − sh + 1]. For instance, the
signal,
1, 5 ≤ n ≤ 12
x[n] =
0, otherwise
can be written as follows,
where,
1, 1≤n≤8
x1 [n] =
0, otherwise
Hence, the output of the LTI system y[n] = x[n] ∗ h[n] can be alternatively
computed as follows:
y[n] = x[n] ∗ h[n] = (x1 [n] ∗ δ[n + 1 − sx ]) ∗ (h1 [n] ∗ δ[n + 1 − sh ]) (2)
= (x1 [n] ∗ h1 [n]) ∗ (δ[n + 1 − sx ] ∗ δ[n + 1 − sh ]) (3)
= y1 [n] ∗ δ[n + 2 − (sx + sh )] (4)
1. Transform x[n] and h[n] their equivalents x1 [n] and h1 [n], starting at
n = 1.
2. Compute the convolution of the equivalent signals, y1 [n] = x1 [n] ∗ h1 [n]
using Equation (1).
4
3. Shift the output signal y1 [n] to get the desired signal y[n] according to
Equation (4).
TASK 2. Use the function discreteconvolution.m to compute the
convolution y[n] of the following signals,
5
function [y] = addecho(x, fs, d, A)
% Inputs:
% x: input signal
% fs: sampling frequency
% d: distance between the listener and the mountain (in meters)
% A: attenuation factor
% Outputs:
% y: output signal