S&S Lab1-13 PDF
S&S Lab1-13 PDF
The >> symbol indicates that MATLAB is ready to accept input and it is called a prompt. Type in
the following and press enter:
>> x = 3
You will see two changes: First, MATLAB will output the value now stored in x. Second, on the
right, x will appear in the list of variables. When there is a long process taking place, the prompt
will disappear and appear again when the calculation has completed. Usually when you add a
semicolon after a statement, it means that the output won’t be shown on the screen.
1.2.1.2 The MATLAB Editor
When writing long programs or writing programs you may want to edit or run over and over, it is
better to create a script in the editor. Click “New Script” near the top left to open the editor.
The editor is where you will write and save all your programs. For example, type The following
code in the editor:
clc;
x = inv(x);
disp(x);
And press “Run”. Once you do that, it will ask you where to save your script. Save it somewhere.
You should see something similar to the following:
cosh(x) 𝑒𝑥 +𝑒−𝑥
cosh 𝑥 = 2
𝑥 −𝑥
tanh(x) sinh 𝑥
tanh 𝑥 = cosh = 𝑒𝑒𝑥 −𝑒
𝑥 +𝑒−𝑥
i 𝑖 = √−1
real(z) Re{𝑧}
imag(z) Im{𝑧}
abs(z) |𝑧|
angle(z) ∠𝑧
conj(z) 𝑧∗
rand Random number between 0 and 1.
randn Random number that follows a standard Normal (Gaussian)
distribution.
randi([min max]) Random integer between min and max.
1.2.4 Binary Operations in MATLAB
MATLAB has a lot of operators and functions you can use to check relationships between two
numbers. Each of them returns 0 or 1, for false and true, respectively.
The second way is to type in help <function name>. This way, you can quickly look up the
syntax or what a function does without opening the Help Centre. Type in help inv. You will see
something like the following:
You can also jump directly to the help section of a function by typing doc <function name>.
A similar thing applies for binary operators. You need to use & instead of && and | instead of ||
when working with arrays.
Here are some special commands for generating arrays, along with their explanation. Note that
commands that have a different behavior as compared to scalars are repeated below for clarity.
MATLAB Expression Explanation
a:b An array starting at a and ending at b with steps of 1.
a:step:b An array starting at a and ending at b with a step size of
step.
linspace(a, b, n) An array starting at a, ending at b, with a total of n points.
rand([1 n]) An array of n points where each element is a random
number between 0 and 1.
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
The r means red and the g means green, as you might have guessed. The meanings of the other
symbols are explained below:
Symbol Color Symbol Line Type Symbol Point Type
B Blue - Solid . Point
G Green : Dotted o Circle
R Red -. Dashed- x Cross
Dotted
C Cyan _ Dashed + Plus
M Magneta * Star
Y Yellow s Square
K Black d Diamond
W White < or > Triangle
P Pentagram
h Hexagram
Beware of using the point types when there are too many points on your plot. It will result in a plot
that’s very crowded.
Below are some other commonly used plotting functions and their explanations. In this table, parts
inside square brackets are optional.
MATLAB Expression Explanation
plot(x1, y1, [x2, y2, …, xn, yn]) Plot all the functions on the same plot.
figure(n) Select the figure with ID n. If it doesn’t
exist, open a new figure.
stem(x1, y1, [x2, y2, …, xn, yn]) Make a stem plot of the functions
xlabel('label') Change the x-axis label.
ylabel('label') Change the y-axis label.
xlim([min max]) Change the x-range of the plot that is
displayed.
1.5
0.5
-0.5
-1
-1.5
-2
0 2 4 6 8 10 12 14 16 18 20
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
0 10 20 30 40 50 60 70 80
In the special case that 𝑥(𝑡) is periodic with a period 𝑇, its power can also be computed by:
1 𝑡0 +𝑇
𝑃𝑥 = ∫ |𝑥(𝑡)|2 𝑑𝑡
𝑇 𝑡0
Where 𝑡0 ∈ ℝ.
For a discrete-time signal 𝑥(𝑛),
∞ 𝑁
1
𝐸𝑥 = ∑ |𝑥(𝑛)|2 , 𝑃𝑥 = lim ∑ |𝑥(𝑛)|2
𝑁→∞ 2𝑁 + 1
𝑛=−∞ 𝑛=−𝑁
where 𝑛0 ∈ ℤ. A signal 𝑥(𝑡) or 𝑥(𝑛) is called an energy signal if 𝐸𝑥 is finite and a power signal if
𝑃𝑥 is finite and 𝑃𝑥 ≠ 0.
Example: Prove that 𝑥1 (𝑡) = sin 𝑥 is a power signal and 𝑥2 (𝑡) = 𝑡𝑒 −𝑡 𝑢(𝑡) is an energy signal.
>> syms t T
>> x1 = sin(t);
>> limit(1/(2*T)*int(x1^2, t, -T, T), T, inf)
ans =
1/2
-1
-2
-3
0 1 2 3 4 5 6 7 8 9 10
-1
-2
-3
-4
-1.5 -1 -0.5 0 0.5 1 1.5
-1
-2
-3
-4
-1.5 -1 -0.5 0 0.5 1 1.5
As you can see, the signal has been flipped (mirrored) about the y-axis.
4.2.2 Time Scaling
4.2.2.1 Time Compression
Next, we will apply a time compression to a function.
>> f = @(t) cos(t);
>> t = -2*pi:0.01:2*pi;
>> figure(1); plot(t, f(t))
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-8 -6 -4 -2 0 2 4 6 8
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-8 -6 -4 -2 0 2 4 6 8
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-8 -6 -4 -2 0 2 4 6 8
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
-6 -4 -2 0 2 4 6
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
-6 -4 -2 0 2 4 6
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
-6 -4 -2 0 2 4 6
6.2 Pre-Lab
The discussion about linear time invariant system and integral convolution
6.2.1 The Impulse Response and the Convolution Integral
The impulse response of a continuous-time system 𝑆, denoted by ℎ(𝑡), is defined as
ℎ(𝑡) ≜ 𝑆{𝛿(𝑡)}.
In words, if a continuous-time system is given a Dirac delta function as an input, the resulting
output is known as the impulse response. It turns out that for LSI (Linear Shift-Invariant) systems,
knowing the impulse response ℎ(𝑡) and the input signal 𝑥(𝑡) is enough to find the output 𝑦(𝑡). In
this case, the following equation holds:
𝑦(𝑡) = (𝑥 ∗ ℎ)(𝑡)
∞
≜ ∫ 𝑥(𝜏)ℎ(𝑡 − 𝜏) 𝑑𝜏
−∞
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-1 0 1 2 3 4 5
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.5 1 1.5 2 2.5 3
Here, ts is the sampling time and the conv function calculates the discrete-time convolution. We
will learn more about this in the next experiment.
You should submit a *.mat file that calculates the answers and a handwritten sheet with the
answer to part c.
7.2 Pre-Lab
The discussion about convolution sum.
7.2.1 The Impulse Response and the Convolution Sum
The impulse response of a discrete-time system 𝑆, denoted by ℎ(𝑛), is defined as
ℎ(𝑛) ≜ 𝑆{𝛿(𝑛)}.
In words, if a discrete-time system is given a Kronecker delta function as an input, the resulting
output is known as the impulse response. It turns out that for LSI (Linear Shift-Invariant) systems,
knowing the impulse response ℎ(𝑛) and the input signal 𝑥(𝑛) is enough to find the output 𝑦(𝑛).
In this case, the following equation holds:
𝑦(𝑛) = (𝑥 ∗ ℎ)(𝑛)
∞
≜ ∑ 𝑥(𝑘)ℎ(𝑛 − 𝑘)
𝑘=−∞
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-4 -2 0 2 4 6 8 10
-1
-2
-3
-4
-5
0 5 10 15 20 25 30
8.2 Pre-Lab
The basics knowledge of discrete and continuous time Fourier series
8.2.1 The Discrete-Time Fourier Series
If a discrete-time signal is periodic with a fundamental period of 𝑁, then its Discrete-Time Fourier
Series (DTFS) is given by
𝑁−1
1
𝑋(𝑘) = ℱ{𝑥(𝑛)} = ∑ 𝑥(𝑛)𝑒 −𝑗2𝜋𝑘𝑛⁄𝑁 ,
𝑁
𝑛=0
and it is also periodic with a fundamental period of 𝑁. To get 𝑥(𝑛) back from 𝑋(𝑘), we use the
formula
𝑁−1
𝑥(𝑛) = ℱ −1 {𝑋(𝑘)}
= ∑ 𝑋(𝑘)𝑒 𝑗2𝜋𝑘𝑛⁄𝑁 .
𝑘=0
This formula is also known as the discrete-time Fourier series representation of a signal. The since
both the series and its inverse are periodic, it is always sufficient to look at them for one time
period only. We evaluate the Fourier series in MATLAB with the fft function and its inverse
with the ifft function.
>> n = 0:7;
>> x = [1 5 3 4 0 7 2 6];
>> X = fft(x);
>> figure; plot(n, x); xlim([0 7]);
>> figure; plot(n, abs(X)); xlim([0 7]);
>> figure; plot(n, angle(X)); xlim([0 7]);
>> x_recovered = ifft(X);
>> figure; plot(n, x_recovered); xlim([0 7]);
1 𝑇⁄2
𝑋(𝑘) = ∫ 𝑥(𝑡)𝑒 −𝑗𝑘𝛺0 𝑡 𝑑𝑡,
𝑇 −𝑇⁄2
Where 𝛺0 = 2𝜋 𝑇
is known as the fundamental frequency of the signal 𝑥(𝑡). This series is generally
not periodic, and therefore has to be considered symbolically. The reverse transform is given by
∞
9.2 Pre-Lab
Basics of continuous time Fourier transform
9.3 The Continuous-Time Fourier Transform
If a continuous-time signal 𝑥(𝑡) satisfies the property that
∞
∫ |𝑥(𝑡)| 𝑑𝑡 ∈ ℝ+
0
−∞
which is known as the frequency domain representation of the signal 𝑥(𝑡). To represent this, we
ℱ
write 𝑥(𝑡) → 𝑋(𝑗𝛺). To recover the time-domain signal from its frequency-domain representation,
we use the equation
1 ∞
𝑥(𝑡) = ℱ −1 {𝑋(𝑗𝛺)} = ∫ 𝑋(𝑗𝛺)𝑒 𝑗𝛺𝑡 𝑑𝛺.
2𝜋 −∞
ℱ −1
In this case, we write 𝑋(𝑗𝛺) → 𝑥(𝑡).
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-40 -30 -20 -10 0 10 20 30 40
-1
-2
-3
-4
-40 -30 -20 -10 0 10 20 30 40
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-1 -0.5 0 0.5 1 1.5 2 2.5 3
1. 𝑥1 (𝑡) = 1, 0 ≤ 𝑡 ≤ 5
2. 𝑥2 (𝑡) = 2𝑒 −2𝑡 , 0 ≤ 𝑡 ≤ 5
3. 𝑥3 (𝑡) = 𝑢(𝑡) − 𝑢(𝑡 − 5)
4. 𝑥4 (𝑡) = 𝑒 −𝑡 𝑢(𝑡)
5. 𝑥5 (𝑡) = cos(𝑡) 𝑤ℎ𝑒𝑟𝑒 𝑡 = −2𝜋 𝑡𝑜 2𝜋
6. 𝑥6 (𝑡) = sin(𝑡) 𝑤ℎ𝑒𝑟𝑒 𝑡 = −2𝜋 𝑡𝑜 2𝜋
7. 𝑥7 = ℎ1 (𝑡)ℎ2 (𝑡) 𝑤ℎ𝑒𝑟𝑒 ℎ1 (𝑡) = 4, 0 ≤ 𝑡 ≤ 4 𝑎𝑛𝑑 ℎ2 (𝑡) = sin(2𝑡) , 0 ≤ 𝑡 ≤ 4
1
8. 𝑥8 = 𝑎ℎ1 (𝑡) + 𝑏ℎ2 (𝑡) 𝑤ℎ𝑒𝑟𝑒 𝑎 = 5, 𝑏 = 10, ℎ1 (𝑡) = (𝜋) 𝑡, 0 ≤ 𝑡 ≤ 4 𝑎𝑛𝑑 ℎ2 (𝑡) =
2𝑒 −2𝑡 , 0 ≤ 𝑡 ≤ 4
a) 𝑥1 (𝑡) = 𝛿(𝑡)
b) 𝑥2 (𝑡) = cos 𝑡
c) 𝑥3 (𝑡) = 𝑒 −3𝑡 𝑢(𝑡)
You should submit a *.m file that displays each result.
9.6.2 Task 2
Compute the inverse Fourier transform of the following frequency-domain representations:
a) 𝑋1 (𝑗𝛺) = 2𝜋𝛿(𝛺)
b) 𝑋2 (𝑗𝛺) = 𝑢(𝛺 + 12) − 𝑢(𝛺 − 12)
You should submit a *.m file that displays each result.
9.6.3 Task 3
For the signal 𝑥(𝑡) = 𝑢(𝑡) − 𝑢(𝑡 − 1),
ℱ
a) Compute the Fourier transform of 𝑥(−𝑡). Verify that the property 𝑥(−𝑡) → 𝑋(−𝑗𝛺) holds.
b) Compute the Fourier transform of 𝑥(𝑡 − 1). Verify that the property 𝑥(𝑡 − 𝑡0 )
ℱ
→ 𝑋(𝑗𝛺)𝑒 −𝑗𝛺𝑡0 holds.
You should submit a *.m file that displays each result.
10.2 Pre-Lab
Basics of discrete time Fourier transform
10.2.1 The Discrete-Time Fourier Transform
If a discrete-time signal 𝑥(𝑛) satisfies the property that
∞
∑ |𝑥(𝑛)| ∈ ℝ+
0
𝑛=−∞
which is known as the frequency domain representation of the signal 𝑥(𝑛). To represent this, we
ℱ
write 𝑥(𝑛) → 𝑋(𝑒 𝑗𝜔 ). The DTFT of a signal is always 2𝜋-periodic.
0
-4 -3 -2 -1 0 1 2 3 4
-1
-2
-3
-4 -3 -2 -1 0 1 2 3 4
a) 𝑥1 (𝑛) = 𝛿(𝑛)
b) 𝑥2 (𝑛) = 2−𝑛 𝑢(𝑛)
𝜋𝑛
c) 𝑥3 (𝑛) = 3−𝑛 cos 5 𝑢(𝑛)
10.4.2 Task 2
For the signal 𝑥(𝑛) = 𝑢(𝑛) − 𝑢(𝑛 − 6),
a) Compute the Fourier transform of 𝑥(𝑛)𝑒 𝑗2𝑛 . Verify that the property 𝑥(𝑛)𝑒 𝑗𝜔0 𝑛
ℱ
→ 𝑋(𝑒 𝑗(𝜔−𝜔0 ) ) holds.
b) Compute the Fourier transform of 𝑥(𝑛 − 1). Verify that the property 𝑥(𝑛 − 𝑛0 )
ℱ
→ 𝑋(𝑒 𝑗𝜔 )𝑒 −𝑗𝜔𝑛0 holds.
You should submit a *.m file that displays each result.
11.2 Pre-Lab
The discussions about the inverse discrete time Fourier transform
We can compute the inverse discrete-time Fourier transform (IDTFT) using the int command and
using the formula.
>> syms n omega
>>X = @(omega) heaviside(omega + pi/2) - heaviside(omega - pi/2);
>> simplify(sym('1/(2*pi)') * int(X(omega) .* exp(j * omega * n),
omega, -pi, pi))
ans =
sin((pi*n)/2)/(n*pi)
>>x = @(n) sin((pi*n)/2)./(n*pi);
>> nr = -10:10;
>> figure; stem(nr, x(nr))
0.35
0.3
0.25
0.2
0.15
0.1
0.05
-0.05
-0.1
-0.15
-10 -8 -6 -4 -2 0 2 4 6 8 10
a) 𝑋1 (𝑒 𝑗𝜔 ) = 2𝜋𝛿(𝜔) , − 𝜋 ≤ 𝜔 ≤ 𝜋
1
b) 𝑋2 (𝑒 𝑗𝜔 ) = 1−2𝑒 −𝑗𝜔
You should submit a *.m file that displays each result.
11.4.2 Task 2
For the signal 𝑋(𝑒 𝑗𝜔 ) = 𝑢(𝜔 + 𝜋2) − 𝑢(𝜔 − 𝜋2), defined for −𝜋 ≤ 𝜔 ≤ 𝜋 and periodic otherwise,
a. Compute the inverse Fourier transform of 𝑋(𝑒 𝑗𝜔 )𝑒 −𝑗2𝜔 . Verify that the property
ℱ −1
𝑋(𝑒 𝑗𝜔 )𝑒 𝑗𝜔𝑛0 → 𝑥(𝑛 − 𝑛0 ) holds.
b. Compute the inverse Fourier transform of 𝑋(𝑒 𝑗(𝜔−1) ). Verify that the property
ℱ −1
𝑋(𝑒 𝑗(𝜔−𝜔0 ) ) → 𝑥(𝑛)𝑒 −𝑗𝜔0 𝑛 holds.
You should submit a *.m file that displays each result.
12.2 Pre-Lab
The basic concepts of Laplace transform.
12.2.1 The Definition of the Laplace Transform
The bilateral Laplace Transform 𝑋(𝑠) of a continuous-time signal 𝑥(𝑡) is defined as
∞
𝑋(𝑠) = ∫ 𝑥(𝑡)𝑒 −𝑠𝑡 𝑑𝑡.
−∞
ℒ
We write 𝑥(𝑡) → 𝑋(𝑠) or 𝑋(𝑠) = ℒ{𝑥(𝑡)} to represent this. The complex frequency 𝑠 has two
parts: 𝑠 = 𝜎 + 𝑗𝛺. Re{𝑠} = 𝜎 is the decay factor and Im{𝑠} = 𝛺 is the oscillatory factor. If there
is no decay factor (𝜎 = 0), the Laplace transform simplifies to the continuous-time Fourier
transform.
12.2.2 The Laplace Transform in MATLAB
In MATLAB, the Laplace transform is implemented using the command laplace.
>> clearvars
>> syms s t
>> x = @(t) cos(t) .* exp(-3*t) .* heaviside(t);
>> laplace(x(t), t, s)
ans =
(s + 3)/((s + 3)^2 + 1)
>> X = @(s) (s + 3)./((s + 3).^2 + 1);
>> pretty(X(s))
s + 3
------------
2
(s + 3) + 1
12.3 In-Lab Tasks
Compute the Laplace Transform for:
1. 𝑥1 (𝑡) = 1, 0 ≤ 𝑡 ≤ 5
2. 𝑥2 (𝑡) = 2𝑒 −2𝑡 , 0 ≤ 𝑡 ≤ 5
3. 𝑥3 (𝑡) = 𝑢(𝑡) − 𝑢(𝑡 − 5)
4. 𝑥4 (𝑡) = 𝑒 −𝑡 𝑢(𝑡)
5. 𝑥5 (𝑡) = cos(𝑡) 𝑤ℎ𝑒𝑟𝑒 𝑡 = −2𝜋 𝑡𝑜 2𝜋
6. 𝑥6 (𝑡) = sin(𝑡) 𝑤ℎ𝑒𝑟𝑒 𝑡 = −2𝜋 𝑡𝑜 2𝜋
7. 𝑥7 = ℎ1 (𝑡)ℎ2 (𝑡) 𝑤ℎ𝑒𝑟𝑒 ℎ1 (𝑡) = 4, 0 ≤ 𝑡 ≤ 4 𝑎𝑛𝑑 ℎ2 (𝑡) = sin(2𝑡) , 0 ≤ 𝑡 ≤ 4
1
8. 𝑥8 = 𝑎ℎ1 (𝑡) + 𝑏ℎ2 (𝑡) 𝑤ℎ𝑒𝑟𝑒 𝑎 = 5, 𝑏 = 10, ℎ1 (𝑡) = (𝜋) 𝑡, 0 ≤ 𝑡 ≤ 4 𝑎𝑛𝑑 ℎ2 (𝑡) =
2𝑒 −2𝑡 , 0 ≤ 𝑡 ≤ 4
a. 𝑥1 (𝑡) = 𝛿(𝑡)
b. 𝑥2 (𝑡) = 𝑒 𝑗2𝜋𝑡 𝑢(𝑡)
c. 𝑥3 (𝑡) = 𝑒 3𝑡 cos(2𝑡) 𝑢(𝑡)
You should submit an m-file that prints each Laplace transform using the pretty function.
Hint: You can pass the output of the laplace function directly to the pretty function.
12.4.2 Task 2
For the signal 𝑥(𝑡) = 𝑒 −𝑡 𝑢(𝑡), compute the Laplace transform of the following signals
a. 𝑥(𝑡 − 3)
b. 𝑒 3𝑡 𝑥(𝑡)
c. 𝑒 𝑗3𝑡 𝑥(𝑡)
You should submit an m-file that prints each Laplace transform using the pretty function.
13.2 Pre-Lab
The basic concepts of Inverse Laplace transform.
13.2.1 The Definition of the Inverse Laplace Transform
The inverse Laplace Transform 𝑥(𝑡) of a complex signal 𝑋(𝑠) is defined as
1 𝜎+𝑗∞
𝑥(𝑡) = ∫ 𝑋(𝑠)𝑒 𝑠𝑡 𝑑𝑠
𝑗2𝜋 𝜎−𝑗∞
where the value 𝜎 can be any real value within the region of convergence of 𝑋(𝑠). We write 𝑋(𝑠)
ℒ −1
→ 𝑥(𝑡) or 𝑥(𝑡) = ℒ −1 {𝑋(𝑠)} to represent this. Usually, this integration is complex and difficult
to perform, so we use tables of Laplace transform pairs to perform the inverse transformation.
13.2.2 The Inverse Laplace Transform in MATLAB
In MATLAB, the inverse Laplace transform is implemented using the command ilaplace.
>> syms s t
>> X = @(s) 3./(s .* (s + 3));
>> ilaplace(X(s), s, t)
ans =
1 - exp(-3*t)
>> pretty(ans)
1 - exp(-3 t)
13.3 In-Lab Tasks
Compute the inverse Laplace Transform for:
1
1. 𝑋1 (𝑠) = 𝑠2 +𝑏2
1
2. 𝑋2 (𝑠) = (𝑠+𝑎)2 +𝑏2
1
3. 𝑋3 (𝑠) = 𝑠𝑛
1
4. 𝑋4 (𝑠) = (𝑠−𝑎)𝑛
𝑒 −5𝑠 (𝑠+1)
5. 𝑋5 (𝑠) = (𝑠+1)2 +16
𝑒 −10𝑠
6. 𝑋6 (𝑠) = (𝑠−3)2
7. Prove for ℎ(𝑡) = 𝑒 −4𝑡
a) 𝑒 −𝑎𝑡 × ℎ(𝑡) = 𝐻(𝑠 + 𝑎)
𝑑
b) 𝑑𝑡 ℎ(𝑡) = 𝑠𝐻(𝑠)
∞ 1
c) ∫−∞ ℎ(𝑡) = 𝑠 𝐻(𝑠)