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

SyC_lab_1

The document outlines the objectives and procedures for a lab session focused on basic signal transformations using Matlab. It covers topics such as representing discrete-time signals, performing operations on them, computing energy and power, and manipulating audio signals. Students are required to complete a report and implement exercises involving audio signals, including scaling amplitude and mixing sounds.

Uploaded by

rempalago 758
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SyC_lab_1

The document outlines the objectives and procedures for a lab session focused on basic signal transformations using Matlab. It covers topics such as representing discrete-time signals, performing operations on them, computing energy and power, and manipulating audio signals. Students are required to complete a report and implement exercises involving audio signals, including scaling amplitude and mixing sounds.

Uploaded by

rempalago 758
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Systems and Circuits

Lab session no. 1: Basic signal transformations

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.

3 Working with Discrete-Time Signals in Mat-


lab
Matlab is a powerful mathematical software for performing matrix calculations.
It is a high-level programming language for numerical calculation, visualization
and application development. Matlab provides the user with a large number
of toolboxes to address different problems, such as mathematical functions for
linear algebra, statistics, Fourier analysis, filtering, optimization, numerical in-
tegration, and solving ordinary differential equations.
Our first objective is to represent signals in discrete time using vectors so that
they can be processed by Matlab. A vector in Matlab is a matrix of dimensions
N × 1 or 1 × N . In principle, a discrete-time signal with infinite duration should
be represented by a vector of infinite size where each element contains the value
of the signal at each time point. However, Matlab only works with discrete time

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:

% Input signal x[n]


x = [0:0.1:1 ones(1,5)]; % Vector with the values of the signal
nx = -3:12; % Vector of time points.

% We represent the signal


subplot(2,1,1)
stem(nx,x);
axis([-25 25 -0.2 1.2]); % We set an adequate zoom in both axis
xlabel("n"); % Horizontal axis
ylabel("x[n]"); % Vertical axis
grid on

% 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

% We represent the signal y[n]


subplot(2,1,2)
stem(ny,y);
axis([-25 25 -0.2 1.2]);
xlabel("n");
ylabel("y[n]=x[-n]");
grid on

Run this code by direct copy-paste in the Matlab Command Win-


dow. Note that we have generated the signal x using a vector concatenation of
two elements:

• 0:0.1:1 is a command that returns a vector of real numbers between 0 and


1 with 0.1 steps.
• ones(1,5) returns a vector of five ones, it is equivalent to [1, 1, 1, 1, 1]

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

% Vector of time points


nx = 1:48;

% We represent the signal


figure()
subplot(2,1,1)
stem(nx,x);
axis([-1 50 -0.2 1.2]);
xlabel("n"); % Horizontal axis
ylabel("x[n]"); % Vertical axis

% We compute the length of y[n]. Floor is a command


% to compute the lowest integer of the argument
Ny = floor(length(x)/3);
for k=1:1:Ny %For loop. y(k) = x(k*3) is repeated for k=1, 2, ..., Ny
y(k) = x(k*3);
end

% Vector of time points for y[n]


ny = 1:Ny;
subplot(2,1,2)
stem(ny,y);
axis([-1 50 -0.2 1.2]);
xlabel("n");
ylabel("y[n]=x[3n]");

Run this code by direct copy-paste in the Matlab Command Win-


dow and observe the results.

4.3 Time-Scale Operations (Expansion)


Given x[n], the operation y[n] = x[n/2] is a reversible transformation since we
do not discard any samples of y[n]. For those time points n such that n/2
is not an integer, we simply assume y[n] = 0. For any even n, then y[n] =
x[n/2]. Therefore, y[n] is twice as long as x[n]. The following code performs
this operation:

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]");

Ny = length(x)*2; % Length of the signal y[n]

% We insert the zeros


for k=1:1:Ny
if rem(k,2) == 0 %If remainder of k/2 is equal to zero
y(k) = x(k/2);
else
y(k) = 0;
end
end

% Vector of time points for y[n]


ny = 1:Ny;
subplot(2,1,2)
stem(ny,y);
axis([-1 66 -0.2 1.2]);
xlabel("n");
ylabel("y[n]=x[n/2]");

Run this code by direct copy-paste in the Matlab Command Win-


dow and observe the results.

4.4 Temporal Shift


This operation is simpler to perform since it only affects the vector of time
points. For instance, to compute the signal y[n] = x[n − 10], we only need
to build a modified time axis using a displacement of 10. So, for instance,
at time instant n = 11, the new signal takes a value y[11], which is equal to
x[11 − 10] = x[1]. Hence, y[n] is obtained by shifting x[n] 10 time steps to the
right. Here is the code that implements this temporal shift:

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

% We shift the signal 10 time units to the right


ny = nx + 10;

y = x; % The values of y[n] are the same!

subplot(2,1,2)
stem(ny,y);
axis([-1 30 -0.2 1.2]);
xlabel("n");
ylabel("y[n]=x[n-10]");
grid on

Run this code by direct copy-paste in the Matlab Command Win-


dow and observe the results.

5 Computing Energy and Power


The energy of a discrete-time signal is computed as:

Energy = sum(abs(x).^2);

and the power is computed as the average energy:

Power = mean(abs(x).^2);

In the case of periodic signals, the energy is infinite, so we compute the


power as the average energy over a period of the signal.

6 Exercises Using Audio Signals


Using the above examples as a reference, you should implement some simple
operations in Matlab using audio signals. The necessary audio files, which
contain both voice and recorded music (in .wav format) are also provided.

6.0.1 Loading the sound files


You can load an audio file using the Matlab function audioread. The usage of
this function is as follows:

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);

Run this code by direct copy-paste in the Matlab Command Win-


dow and observe the results.

6.1 Scaling the signal amplitude


We are going to modify the amplitude of signal x such that the first half of the
signal has half of the amplitude of the original signal, and the second part of
the signal has one tenth the amplitude of the original signal. To do so, we have
to define a weighting signal, and then multiply both point-wise. Note: as the
signals are very large, for better visualization we are using the plot command
instead of stem, even though we are representing discrete signals. Simply run
this code in Matlab:

N = length(x); % No is the size of the signal

% 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;

% We plot both signals superimposed, with different color:


figure()
plot(x)
hold on
plot(x_modified, "r")
xlabel("n");
ylabel("x[n]");
legend("Original sound", "Atenuated sound")

TASKs 1 & 2: Scaling the signal amplitude. Insert the resulting


figure into the report and answer the corresponding questions.

6.2 Time-Scale Compression


You must transform the loaded signal x using the Time-Scale Compression
described earlier to build a new signal z[n] = x[2n]. Represent both signals

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.

TASKs 3, 4 & 5: Time-Scale Compression. Insert the resulting


figure into the report and answer the corresponding questions.

6.3 Mixing two sounds with different weights (fading).


The goal here is to blend two different musical pieces over time using a time
varying weighting signals, such that in some parts only the first song can be
listened, whereas in other parts only the second one is audible, this is a different
“fading” is applied to the signals at different time instants. We will use ”The
Sting” and ”Star Wars” main themes.

• Load both songs, respectively in signals x[n] and y[n].


• Play both sounds, to verify that they are correctly loaded.
• Compute and observe the length of both signals.
• Cut the longest signal so they are now both of the same length. Recall
that you can extract a portion of a vector by indicating the initial and
final time instants, for example, x(1:10) extracts the first 10 samples of x.
• Represent both signals x[n] and y[n] in a single figure using the command
subplot.

TASK 6: Mixing two sounds with different weights (fading). In-


sert the resulting figure into the report and answer the corresponding
question.

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:

• The value at the origin is 1.


• The maximum value of is 1, and the minimum is 0.
• The length of w[n] is the same as the length of both x[n] and y[n]. Verify
also that they are all vectors with the same shape (use command size) to
obtain the shapes, and remember you can transpose a vector as x’ ).

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.

TASK 7: Weighting functions. Insert the resulting figure into the


report and answer the corresponding question.

Now we are going to use the signals w[n] and v[n] to weight the sounds in
x[n] and y[n], respectively:

• Create the weighted sounds p[n] and q[n] by respectively multiplying


(point-wise) each sound by each weighting signal (x[n] is point-wise weighted
by w[n] and y[n] is point-wise weighted by v[n]). Important; before point-
wise multiplying verify that both signals have the same shape, otherwise
an error is obtained.
• Also build the signal r[n], which is the sum of both weighted sounds p[n]
and q[n].
• Represent in a single figure using subplot p[n], q[n] and the resulting com-
bined sound r[n].

TASK 8: Combining the weighted sounds. Insert the resulting


figure into the report and answer the corresponding question.

6.4 Adding background music to a voice


Here we are going to add some background music to a voice clip. The objective
is to obtain a combined signal that only plays music at the beginning, then the
volume of the music is reduced to better here the voice, and the volume of the
music raises again when the voice message is finished.

• 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

You might also like