SyC_lab_1
SyC_lab_1
1 Objectives
In this first laboratory session, you will learn about:
• Basic aspects of Matlab as a high-level programming language.
• Representing discrete-time signals in Matlab.
• Performing simple operations with discrete-time signals.
• Computing the partial mean, power, and energy of discrete-time signals.
• Performing transformation and combination of discrete signals.
2 Evaluation
Students must complete a report following the provided template (Report on
Laboratory session 1). 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
signals of finite length. So we will limit ourselves to working with finite time
signals.
Throughout the lab session, we will work with finite-length discrete-time
signals: they only take non-zero values in a certain set of time points and these
are the values that we store. For any other time point, the signal is equal to
zero and this information does not have to be stored. To represent these kind
of signals we need two vectors of the same length. For instance, assume that
the signal x[n] is equal to 3n for n = -1, 0, 1, ..., 4, 5; and zero otherwise. First,
we need a vector that contains the time values. In this case the vector is n =
[-1, 0, 1, 2, 3, 4, 5]. The second vector contains the value of the signal at these
time points, i.e., x = [-3, 0, 3, 6, 9, 12, 15].
The command that we can use to represent a discrete-time signal is stem.
The following code represents the previously described signal (directly copy-
paste it in the Matlab Command Window):
n = -1:5;
x = 3 * n;
stem(n,x);
xlabel("n");
ylabel("x[n] = 3n");
grid on
The function stem is used to plot discrete-time signals, and in this case we
obtain this result:
If you need help about the usage of any Matlab command, for instance stem,
plot, or grid, simply use the help command included in Matlab, and you will
obtain detailed usage instructions about any Matlab functionality. For instance,
type the following:
help stem
2
4 Transforming the Independent Variable
4.1 Temporal Inversion
A time inversion applied to the signal x[n] means constructing the signal y[n] =
x[−n]. So we need to build a new reversed axis, and also reverse the signal
values. The following Matlab code performs this operation:
% Temporal inversion
y = x(end:-1:1); % Inversion of the vector of signal values
ny = -nx(end:-1:1); % We have to perform the temporal inversion over the vector of time poin
Also observe that we have used the command subplot to represent two sig-
nals in the same figure and the command axis to define the minimum and
maximum values for the x and y axis. If you need further information about
these commands, remember to use the Matlab help command.
3
4.2 Time-Scale Operations (Compression)
Given a signal x[n], we are interested in constructing y[n] = x[3n]. This oper-
ation is not reversible, y[n] is a time-compressed version of x[n] and we have
discarded some samples of x[n]. To implement this operation in Matlab, we can
use the following code:
% We define x[n]
x1 = [0:0.1:1 ones(1,5)];
x = [x1 x1 x1]; % This command concatenates the vector x1 three times
4
x1 = [0:0.1:1 ones(1,5)];
x = [x1 x1];
nx = 1:32;
figure()
subplot(2,1,1)
stem(nx,x);
axis([-1 66 -0.2 1.2]);
xlabel("n");
ylabel("x[n]");
x = [0:0.1:1 ones(1,5)];
nx = 1:16;
figure()
5
subplot(2,1,1)
stem(nx,x);
axis([-1 30 -0.2 1.2]);
xlabel("n");
ylabel("x[n]");
grid on
subplot(2,1,2)
stem(ny,y);
axis([-1 30 -0.2 1.2]);
xlabel("n");
ylabel("y[n]=x[n-10]");
grid on
Energy = sum(abs(x).^2);
Power = mean(abs(x).^2);
6
[x, fs] = audioread("the_sting.wav");
where x stores the signal values of the sound “The sting”, and f s is the sampling
rate, 11025 Hertz in this case. We need both the x and the f s values to reproduce
the sound:
sound(x, fs);
% We build the weighting signal: the first half has amplitude equal to 0.5, the second part
w = [0.5 * ones(N/2, 1); 0.1 * ones(N/2, 1)];
% We obtain the atenuated signal y[n] by pointwise multipling x[n] by the weighting signal w
y = x .* w;
7
x[n] and z[n] in a single figure using subplot. Also apply the same axis to both
subfigures (axis([0, 200000, -0.5, 0.5]);). Finally, reproduce the signal z using
the sound() command.
We are going to create now two weighting signals w[n] and v[n]. To create
w[n] We are going to use a cosine function of the form:
2πn
w[n] = a + b cos
M
Adjust the values for a, b and M such that the weighting function w[n]
verifies the following conditions:
8
• The signal w[n] must reach the maximum value twice, and the minimum
value also twice.
• Define the second weighting function as v[n] = 1 - w[n];
• Plot the two resulting weighting functions w[n] and v[n] in the same figure
using subplot.
Now we are going to use the signals w[n] and v[n] to weight the sounds in
x[n] and y[n], respectively:
• Load the voice (”Blade Runner”) and music (”Third man”) audio files,
respectively, in variables ”voice” and ”background”, respectively.
• Play both sounds to verify that you loaded the correct ones.
• Extend the length of the background by replicating it three times.
• Add zeros at the beginning and the end of the voice so that now both
signals have the same length. Recall that you can use the functions ones
and zeros to create vectors, respectively, filled with 1’s and 0’s. Use ”help
ones” or ”help zeros” if you need it.
• Plot the two resulting signals in the same figure using subplot.
9
TASK 9: Preparing voice and background sounds. Insert the re-
sulting figure into the report.
We finally combine the voice and the background music, and we need to
define a weighting signal to be applied to the music such that:
• Before and after the voice, the music must sound at full volume (the
weighting value must be 1).
• During the voice segment, the music volume must be much lower (the
weighting value must be 0.3)
• Define a weighting signal w[n] to modify the background music, and com-
bine the music with fading with the voice.
• Listen to the result and verify that it sounds as described at the beginning
of the section.
• Plot in a single figure (two subplots) the weighted background music and
the resulting final combination of voice and background music.
TASK 10: Adding the background music. Insert the resulting fig-
ure into the report.
10