Báo - cáo - Nhóm - Tâm - Tài - Trong - (TH tín hiệu và hệ thống)
Báo - cáo - Nhóm - Tâm - Tài - Trong - (TH tín hiệu và hệ thống)
Báo - cáo - Nhóm - Tâm - Tài - Trong - (TH tín hiệu và hệ thống)
1
Đà Nẵng, năm 2022
Thực hành Tín hiệu và Hệ thống 2022
• Tài liệu hướng dẫn thực hành (Tham khảo các bài Lab từ Trường EASTERN
MEDITERRANEAN UNIVERSITY)
2
Thực hành Tín hiệu và Hệ thống 2022
NỘI DUNG
3
EENG226 /INFE226 Labsheet#1
1. General View
>> a = 1.2;
>> b=2.3;
>> c=4.5;
>> d=4;
>> a^3+sqrt(b*d)-4*c
ans = -13.2388
Note the semicolon after each variable assignment. If you omit the semicolon, then
MATLAB echoes back on the screen the variable value.
2. Arithmetic Operations
There are also three other operators that operate on an element by element basis:
.* Multiplication of two vectors, element by element
./ Division of two vectors, element-wise
.^ Raise all the elements of a vector to a power.
>> X=[1,3,4]
>> Y=[4,5,6]
>> X+Y
ans= 5 8 10
For the vectors X and Y the operator + adds the elements of the vectors, one by
one, assuming that the two vectors have the same dimension. In the above example, both
vectors had the dimension 1 × 3, i.e., one row with three columns. An error will occur if
you try to add a 1 × 3 vector to a 3 × 1 vector. The same applies for matrices.
To compute the dot product of two vectors, you can use the multiplication
operator *. For the above example, it is:
>> X*Y’
ans = 43
Note the single quote after Y. The single quote denotes the transpose of a matrix
or a vector. To compute an element by element multiplication of two vectors (or two
arrays), you can use the .* operator:
>> X.*Y
ans = 4 15 24
That is, X.*Y means [1×4, 3×5, 4×6] = [4 15 24]. The ‘.*’ operator is used very
often (and is highly recommended) because it is executed much faster compared to the
code that uses for loops.
3. Complex Numbers
>> z=3 + 4i
>> conj(z) % computes the conjugate of z
>> angle(z) % computes the phase of z
>> real(z) % computes the real part of z
>> imag(z) % computes the imaginary part of z
>> abs(z) % computes the magnitude of z
You can also define the imaginary number with any other variables you like. Try
the following:
>> img=sqrt(-1)
>> z=3+4*img
>> exp(pi*img)
4. Array Indexing
In MATLAB, all arrays (vectors) are indexed starting with 1, i.e., y(1) is the first
element of the array y. Note that the arrays are indexed using parenthesis (.) and not
square brackets [.] as in C/C++. To create an array having as elements the integers 1
through 6, just enter:
>> x=[1,2,3,4,5,6]
>> x=1:6
>> x=1:2:6
Ans = 1 3 5
>> x(3:7)
>> length(x) % gives the size of the array or vector
>> x(2:2:length(x))
5. Allocating Memory
You can allocate memory for one-dimensional arrays (vectors) using the zeros
command. The following command allocates memory for a 100-dimensional array:
>> Y=zeros(100,1);
>> Y(30)
ans = 0
Similarly, you can allocate memory for two-dimensional arrays (matrices). The
command
>> Y=zeros(4,5)
defines a 4 X 5 matrix. Similar to the zeros command, you can use the command ones to
define a vector containing all ones,
>> Y=ones(1,5)
ans= 1 1 1 1 1
Symbol Meaning
pi π(3.14...)
sqrt indicates square root e.g., sqrt(4)=2
ˆ indicates power(e.g., 3ˆ2=9)
abs Absolute value | .| e.g., abs(-3)=3
NaN Not-a-number, obtained when comparing mathematically undefined operations,
such as 0/0
Inf Represents +∞
; Indicates the end of a row in a matrix. It is also used to suppress
printing on the screen (echo off)
% Denotes a comment. Anything to the right of % is ignored by the
MATLAB interpreter and is considered as comments
’ Denotes transpose of a vector or matrix. It’s also used to define strings,
e.g.,str1=’DSP’;
>> x=1:10;
>> length(x)
ans = 10
The function find returns the indices of the vector X that are non-zero. For example,
I= find(X>100), finds all the indices of X when X is greater than 100. So for the
above
>> find(x> 4)
ans = 5 6 7 8 9 10
7. Plotting
You can plot arrays using MATLAB’s function plot. The function plot(.) is
used to generate line plots. The function stem(.) is used to generate “picket-fence” type
of plots.
>> x=1:20;
>> plot(x)
>> stem(x)
More generally, plot(X,Y) plots vector Y versus vector X. Various line types,
plot symbols and colors may be obtained using plot(X,Y,S) where S is a character
string indicating the color of the line, and the type of line (e.g., dashed, solid, dotted,
etc.). Examples for the string S include:
You can insert x-labels, y-labels and title to the plots, using the functions
xlabel(.), ylabel(.) and title(.) respectively. To plot two or more graphs on the
same figure, use the command subplot. For instance, to show the above two plots in the
same figure, type:
The (m,n,p) argument in the subplot command indicates that the figure will be
split in m rows and n columns. The ‘p’ argument takes the values 1, 2 . . . m×n. In the
example above, m = 2, n = 1,and, p = 1 for the top figure and p = 2 for the bottom figure.
**** To get more help on plotting, type: help plot or help subplot.
N=12;
n=0:2*N-1;
M=4; %M=5,7,10
x_M=sin((2*pi*M*n)/N);
figure(1)
stem(n,x_M,'filled')
title('x[n]=sin((2*pi*M*n)/N)')
xlabel('Time')
ylabel('Amplitude')
b) For this part, you will define MATLAB vectors y1 through y4 to represent the
following discrete-time signals:
y 1 n = x n − 2 ,
y 2 [ n ] = x n − 1 ,
y 3 n = x − n ,
y 4 [ n ] = x − n + 1
to do this, you should define y1 through y4 to be equal to x. The key is to define
correctly the corresponding index vectors ny1 through ny4. First, you should figure out
how the index of a given sample of x[n] changes when transforming to yi n. The index
vectors need not span the same of indices as nx, but they should all be at least 11
samples long and include the indices of all nonzero samples of associated signal.
Answer:
n=-3:7;
x= [2 0 1 -1 3 0];
nx=[0 0 0 x 0 0];
ny1=n+2; ny2=n-1;
ny3=-n; ny4=-n+1;
figure(1)
subplot(2,1,1),stem(ny1,nx,'filled')
xlabel('time')
ylabel('x[n-2]')
title('x[n-2]')
subplot(2,1,2),stem(ny2,nx,'filled')
xlabel('time')
ylabel('x[n+1]')
title('x[n+1]')
figure(2)
subplot(2,1,1),stem(ny3,nx,'filled')
xlabel('time')
ylabel('x[-n]')
title('x[-n]')
subplot(2,1,2),stem(ny4,nx,'filled')
xlabel('time')
ylabel('x[-n+1]')
title('x[-n+1]')
The objective of this experiment is to introduce using MATLAB to define an LTI system and
determine on its main features, such as; linearity, causality stability invertibility and time
variance. This is achieved by examining the performance of the system for particular inputs that
will show those characteristics.
1. Linearity:
A system is linear if superposition holds. Specifically, a linear system must satisfy the two
properties:
[][ ]
[][]
Write a MATLAB code to demonstrate this example. This can be done as follows:
• Define the domain of the two signals to be from -3 to 3 and save it as a vector n
• Define the signal x1 as a vector of the values [0 0 0 1 0 0 0]
• Define the signal x2 =2x1
• Evaluate the output corresponding to the x1 input, and label it as y1
• Evaluate the output corresponding to the x2 input, and label it as y2
On the same graph window, plot the signals x1, x2, y1 and y2 using the commands (subplot) and
(stem). Your results should be as depicted in Figure 1.
Answer:
n=-3:3;
x1=[0 0 0 1 0 0 0];
x2=2*x1;
y1=sin((pi/2)*x1);
y2=sin((pi/2)*x2);
figure(1)
subplot(4,1,1),stem(n,x1,'filled')
title('x1[n]')
subplot(4,1,2),stem(n,y1,'filled')
title('y[n]')
subplot(4,1,3),stem(n,x2,'filled')
title('x2[n]')
subplot(4,1,4),stem(n,y2,'filled')
title('y[n]')
Q: Is y2 equal to 2y1, what is your coment?
…………………………………………………………………………………………………………………………
2. Causality
Exercise 2
figure(1)
subplot(3,1,1),stem(n,x1,'filled')
title('x[n]')
subplot(3,1,2),stem(n,x2,'filled')
title('x[n+1]')
subplot(3,1,3),stem(n,y,'filled')
title('y[n]')
2
Q: What is your comment about the system causality?
…………………………………………………………………………………………………………………………
3. Stability
For a stable system, if an input signal is bounded, then the output signal must also be bounded.
Exercise 3
The system y[n]=log(x[n]) is not stable because the (log) function goes to minus infinity at the 0
input. Write a MATLAB code to illustrate this. Proceed as follows:
3
• Declare the output vector as y= log(x). using the(log) function.
• Using stem and subplot commands, plot the input signal x[n] and the corresponding
output signal y[n]. The result should appear as shown in Figure 3.
Answer:
n=-2:3;
x=[1 2 0 3 4 5];
y=log(x);
figure(1)
subplot(2,1,1),stem(n,x,'filled')
title('x[n]')
subplot(2,1,2),stem(n,y,'filled')
title('y[n]')
Q: Comment on your result. How does this result indicate that the system is unstable?
…………………………………………………………………………………………………………………………
3
4. Invertible and Inverse Systems
Invertible System A system is invertible if the input signal can be uniquely determined from
knowledge of the output signal. Therefore, invertibility requires the system to be one-to-one and
generate a distinct output for each input.
Exercise 4
The system y[n] = sin(2πx[n]) where x[n]=[0 1 2 3 4 0] is not invertible. Illustrate this by
showing that the system is not one-to-one. As follows:
figure(1)
subplot(2,1,1),stem(n,x,'filled')
title('x[n]')
subplot(2,1,2),stem(n,y,'filled')
title('y[n]')
4
Q: Comment on the result justifying the claim that the system in not invertible
………………………………………………………………………………………………………
……………………………………………………………………………………………………
The objective of this experiment is to introduce using MATLAB to perform discrete time
convolution, and to simulate continuous time convolution for some common signals.
Convolution:
The convolution of the signals x(t) and h(t) is mathematically defined as:
() () ( ) ∫ () ( )
And for the discrete time signals x[n] and h[n], it is defined as:
[][][]∑ [][]
Exercise 1
[ ] { }
Procedure:
• Define the signal x as a vector of values [ 1 1 1 1 1 1], using the (ones) command.
• Use the (conv) function to evaluate the convolution of x with itself, and name it as y.
• Define the index vector n to range from 0 to 10.
• Using the function (stem), plot y versus n.
• Label the vertical and horizontal axes as (Amplitude) and (Time), respectively.
• Title the figure as (y[n]).
Answer:
x=ones(1,6);
y=conv(x,x);
n=0:10;
figure(1)
stem(n,y,'filled')
xlabel('Time')
ylabel('Amplitude')
title('y[n]')
Prepared by: Mahmoud Nazzal & Qadri Mayyala 1
Q: Comment on the relationship between the length of y[n] and x[n]
…………………………………………………………………………………………………………………………
[ ] { }
[ ] { }
• Use the function (conv) function to evaluate the convolution of x with u, and name it as
y.
• Define the index vector n to range from 0 to 10.
• Using the function (stem), plot y versus n.
• Label the vertical and horizontal axes as (Amplitude) and (Time), respectively.
• Title the figure as (y[n]).
Answer:
x=[0 1 2 3 4 5];
u=ones(1,6);
y=conv(x,u);
n=0:10;
figure(2)
stem(n,y,'filled')
xlabel('Time')
ylabel('Amplitude')
title('y[n]')
……………………………………………………………………………………………………………………
Exercise 3
In this part, continuous time convolution will be simulated in the discrete case. Consider the
continuous time signals x(t) and h(t). Generate these functions using a time step of 0.1.
( ) { }
( ) { }
Procedure:
• Define the domain of x as a vector ranging from 1 to 5, with a time step of 0.1. Name it
as tx.
• Define the domain of h as a vector ranging from 2 to 7, with a time step of 0.1. Name it
…………………………………………………………………………………………………………………………
4 10
Amplitude
Amp.
3
2 5
0
0 1 2 3 4 5 6 7 8 9 10 0
0 1 2 3 4 5 6 7 8 9 10
Time
Time
Exercise 1 Exercise 2
Part -1
3.5
10 3
2.5
Amp.
Amp
5 1.5
0.5
0 0
-5 -4 -3 -2 -1 0 1 2 3 4 5 3 4 5 6 7 8 9 10 11 12
Time Time
Exercise 2 Exercise 3
Part -2
The objective of this experiment is to introduce using MATLAB to evaluate the response of an
LTI system, characterized by a linear constant-coefficient differential equation, to a certain input.
As well as using MATLAB functions to calculate the step and impulse responses of such
systems.
MATLAB function (lsim) can be used to simulate the output of an LTI system characterized by a
linear constant-coefficient differential equation of the form described in equation1.
∑ ∑ …………………………………………….. (1)
The latter equation can be programmed using the function (lsim), as
y=lsim(a,b,x,t)
• The LTI system: is specified by providing the coefficients of y and x as row vectors (a)
and (b), respectively.
• The input signal: specified as a row vector (x).
• The time interval: as a row vector (t) of equally-spaced time values.
Exercise 1
Consider the causal LTI system described by the first order differential equation:
Write a MATLAB code to simulate the step response of this system. This can be done as
follows:
To compare the simulated output to the actual one, plot the solution of the differential equation
versus time as follows:
• Define the time vector t ranging from 0 to 10, with a 1 time increment.
• Plot the function 2(1- ), versus time.
• Label the axes and title the figure as( Exact Output)
• Your answer should be as indicated in Fig1 .
Answer:
a=[1 1/2];
b=1;
t=0:10;
x=ones(1,length(t));
y=lsim(b,a,x,t);
figure(1)
plot(t,y,'--')
title('S(t)')
xlabel('t')
ylabel('S')
Following a similar procedure to that of exercise 1, use (lsim) to compute the response of the
system:
To the input:
figure(1)
plot(t,y)
title('S(t)')
xlabel('t')
ylabel('S')
The MATLAB function (step) can be used to evaluate the step function of a causal LTI system
characterized by equation 1. This function can be called as follows:
y=step(b,a,t)
, where;
a: is the coefficient vector of y
b: is the coefficient vector of x
t: is the time vector
Similarly,
y=impulse(b,a,t)
generates the impulse response of the system characterized by coefficient vectors a and b, on
time interval t.
Exercise 3
Plot the step and impulse responses of the system described by:
figure(1)
subplot(2,1,1), plot(t,s)
title('S(t)')
xlabel('t')
ylabel('S')
subplot(2,1,2), plot(t,i)
title('i(t)')
xlabel('t')
ylabel('i')
The following four figures show the expected results of excersises 1,2,3 and4,
respectively.
s(t)
2
2
1.8
1.8
1.6
1.6
1.4
1.4
1.2 1.2
1 1
s
0.8 0.8
0.6 0.6
0.4
0.4
0.2
0.2
0
0 1 2 3 4 5 6 7 8 9 10 0
0 1 2 3 4 5 6 7 8 9 10
t
Fig 1: Exercise 1 Output Fig2: Exercise 2 Output
s(t)
s(t) 2
0.5
1.5
0.45
1
s
0.4
0.5
0.35
0
0.3 0 1 2 3 4 5 6 7 8 9 10
t
0.25
s
i(t)
1
0.2
0.15
0.5
i
0.1
0.05
0
0 0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9 10 t
t
∑ ( ⁄ ) ................. synthesis
∑ ( ⁄ ) .................. analysis
x: represent one period of an N periodic signal.
X: gives DTFS coefficient as a vector.
MATLAB contains two very efficient routines for computing Analysis and synthesis:
• If x is an N-point vector containing x[n] for the period 0 ≤ n ≤ N −1, then the DTFS of
x[n] can be computed by:
>> ( ⁄ ) .
• If X is an N-point vector containing X[k] for the period 0 ≤ k ≤ N −1, then the synthesis
of DTFS of X[k] can be computed by:
>> .
Exercise 1:
Consider using MATLAB to solve Problem 3.3(a) in (Simon Haykin 2nd Edition) book. For the
DTFS coefficient, the signal:
Procedure:
…………………………………………………………………………………………………………………………
1
2
fig 1
1.8
1.6
0.5
1.4
1.2
1 0
0 5 10 15 20 25
0.8
0.6 fig 3
4
0.4
0.2 2
0
0 5 10 15 20 25 0
Figure 1
-2
1
-4
0 5 10 15 20 25
0.5
1.5
0
0 5 10 15 20 25
1
fig 2 0.5
0.2
0
0 5 10 15 20 25
0.1
fig 4
1
0
0.5
-0.1
0
-0.5
-1
0 5 10 15 20 25
Figure 4
Figure 2
-0.2
0 5 10 15 20 25
Figure 3
◼ the coefficient vectors a and b specify the difference equation using the same format
in lab 5.
◼ Freqz returns in H and the frequencies in omega.
Exercise 2:
Procedure: