0% found this document useful (0 votes)
2 views

LabSession2_SyC

The document outlines the objectives and tasks for Lab 2 in a Systems and Circuits course, focusing on implementing discrete-time systems and convolution in Matlab. Students are required to complete a report and implement specific functions for discrete-time systems and convolution, as well as simulate an acoustic echo using audio signals. The lab includes detailed instructions for programming functions and analyzing audio signals with provided examples.

Uploaded by

rempalago 758
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

LabSession2_SyC

The document outlines the objectives and tasks for Lab 2 in a Systems and Circuits course, focusing on implementing discrete-time systems and convolution in Matlab. Students are required to complete a report and implement specific functions for discrete-time systems and convolution, as well as simulate an acoustic echo using audio signals. The lab includes detailed instructions for programming functions and analyzing audio signals with provided examples.

Uploaded by

rempalago 758
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Systems and Circuits

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.

• Simulate an scenario of acoustic echo using LTI systems.

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.

3 Project 1: Implementation of Discrete-Time


Systems as functions in Matlab
As we studied in the last Lab Session, in Matlab we represent signals using pairs
of vectors, one that stores the values of the signal (the values of x[n]) and one
that stores the corresponding time-instants at which x[n] takes such values (the
values of n). Systems are physical devices or simply functions implemented via
software that transform an input signal into an output signal.
In Matlab, we represent a system by a function that takes as input argument
the input signal and returns as output argument the output signal. As an
example, we are going to create a Matlab function that represents the system
defined by the expression:

y[n] = 0.2x[n]2 − 0.3x[n − 1]2 + sin(n)x[n] − 0.4x[n − 2]


To this end, create a Matlab file called system1.m and save it in your working
folder. The file should contain the following lines to implement the above system:

1
function [ny, y] = system1(nx, x)

% y[n] = 0.2 x[n]^2 - 0.3 x[n-1]^2 + sin(n) x[n] - 0.4 x[n-2]

% 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

% Creating the temporal axis of the output signal (ny)


% If x[n] is defined between nx(1) and nx(end), then the last output sample with
% a non-zero value is nx(end)+2, because of the 0.4*x[n-2] term of the equation.
% In the same way, the first sample to use from x[n] is nx(1)-2, which is zero.
% We define the following vectors considering all the above mentioned:

n_init = nx(1) - 2; % First time point at which we need x[n]


n_end = nx(end) + 2; % Last time where y[n] is non-zero
ny = nx(1):1:n_end; % y[n] will be non-zero between nx(1) and n_end

% We transpose the input signal if needed


if size(x,1) > 1
x = x’;
end

% We define the output signal vector (initialized to 0)


y = zeros(1, length(ny));

% The output is obtained by adding 4 signals.


% First signal: 0.2 x[n]^2 term
% First we extend the vector x to have the same length as y
% To this end we simply include zeros for n > nx(end)
x1 = [x, zeros(1,n_end - nx(end))];
y1 = 0.2*x1.^2;

% Second signal: -0.3x[n-1]^2 term


x2 = [0 x 0]; % The first 0 comes from x[n-1]=0 for n=nx(1)-1
y2 = -0.3*x2.^2;

% Third signal: sin(n)x[n] term


y3 = sin(ny).*x1;

% Fourth signal: -0.4x[n-2] term

2
x4 = [zeros(1, nx(1) - n_init), x];
y4 = -0.4*x4;

% We add the four components


y = y1 + y2 + y3 + y4;

Try the performance of this system, which essentially introduces non-linear


distortion to the input signal, using the audio signal provided in Lab Session 1
(bladerunner.wav).
Using the above system and what you learned in the first Lab Session, pro-
gram a function system2.m that implements the system described by the equa-
tion:

y[n] = 0.5x[4 − n] + 0.7x[n − 5] − 0.4 cos(2πx[2 − n])


Keep in mind that:
• The output is the sum of three components.

• Each of these components performs an operation over the independent


variable. This is useful to determine the time instants at which the output
signal is defined.
TASK 1. Try the performance of the system implemented in func-
tion system2.m using the audio signal bladerunner.wav. Plot the input
and output signals. Insert the resulting figures into the report and
answer the corresponding questions.

4 Project 2: Implementation of the Discrete-


Time Convolution between Finite-Length Sig-
nals
LTI systems are perfectly defined by the impulse response h[n]. Given h[n],
the output signal for any arbitrary input signal is given by the convolution
operation:

X
y[n] = x[k]h[n − k] (1)
k=−∞

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,

x[n] = x1 [n] ∗ δ[n − 4]

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)

Given the above result, the function discreteconvolution.m should con-


tain the following steps:

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,

x[n] = 2δ[n − 1] + 9δ[n − 2] + 5δ[n − 3] + 8δ[n − 5]


h[n] = 3δ[n − 4] + 7δ[n − 5] + 6δ[n − 6] + 3δ[n − 7] + 4δ[n − 8]
Also, compute the convolution of x[n] and h[n] (yconv [n]) using the
conv function provided by Matlab. Plot the signals x[n], h[n], y[n] and
yconv [n]. Insert the resulting figures into the report and answer the
corresponding question.

5 Project 3: Simulation of an Audio Signal with


Echo
Given what you have learned so far, we can simulate an audio signal with an
acoustic echo. Assume someone is giving a speech in the countryside, close to
a mountain. The signal received by a listener is the sum of the original audio
signal and a degraded version of this signal, that has been delayed and modified
after being reflected in the mountain.
The echo can be seen as an interconnection of systems in parallel. One of the
branches consists of an LTI system whose output is equal to the input signal,
i.e., an LTI with impulse response δ[n]. The other branch is the one causing the
echo. This one can be modeled as a chain of three LTI systems:
• The first system models the delay between the person giving the speech
and the mountain. In discrete time, we model this effect by introducing
a delay in the audio signal. If the audios that we provided you have
been sampled with fs samples per second and the sound speed is 340m/s,
for every meter travelled by the audio signal we have a delay of fs /340
samples.
• The second system models the degradation introduced into the delayed
signal after it hits the face of the mountain. The output signal of this
system is the sum of all the reflected signals. For this system, we use the
impulse response stored in the file mountain.mat.
• The third system models the attenuation suffered by the signal after being
reflected in the mountain and its way back to the listener. The impulse
response of this system is of the form Aδ[n], where A < 1 models the
attenuation.
In order to simulate this process, program in a file called addecho.m a func-
tion that adds echo to an audio signal. The structure of the function would be
as follows:

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

TASK 3. Test the implemented function using the audio signal


(discursofinal.wav) provided in the first Lab Session. Plot the input
and output audio signals for d = 300 and A = 0.7. Insert the figure
of the output signal into the report and answer the corresponding
question.

You might also like