Lab1: Introduction to MATLAB
Creating vectors
x = [3 4 7 11] % create a row vector(spaces)
- if the statement does not end with a semicolon, the results will be printed on the Command Window.
- If the statement ends with a semicolon, the results will NOT be printed on the Command Window. Two ways to view the results
- Type the name of the variable in the Command Window.
- Double-click on the name of the variable on the Workspace.
x = 3:10;
x = 3:2:21;
x = 8:-1:0
x = linspace(0, 1, 21) % creates 21 points equally spaced between 0 and 1
x = 0:0.05:1
length(x) % returns the number of entries in vector x
x(3) % return the 3rd element of x. Note that Matlab labels the array(vector and matrices) beginning with 1
- Create a vector of even whole numbers between 31 and 75.
x = 32:2:75
- Let
x = [2 5 1 6];
- Add 16 to each element
a = x + 16;
- Compute the sum of all elements in x
b = sum(x);
- Compute the square root of each element
c = sqrt(x); or c = x .^ (0.5);
- Compute the square of each element
c = x .^ 2; or c = x .* 2;
- Add 16 to each element
- Element-wise operations: “.*”, “./”, “.^”. Let
x = [3 2 6 8]
; andy = [4 1 3 5]';
(i.e, x and y should be column vectors). Compare the following operations.x' * y
andy' * x
x .* y
x * y
x .^ y
- Create a vector x with the elements
x n = ( − 1 ) n + 1 2 n − 1 , n = 1 , 2 , . . . , 100 x_n={(-1)^{n+1}\over2n-1},\ n\ =\ 1,2,...,100 xn=2n−1(−1)n+1, n = 1,2,...,100
and calculate the sum of all elements in xn = 1:100; x = ((-1) .^ (n + 1)) ./ (2 * n - 1); % note the use of .^ and ./ y = sum(x);
- We can use a for loop to do the task but it is not efficient.
Plotting Continuous Signals
- MATLAB is dedicated to discrete signals. In order to work with a continuous signal, we need to generate enough samples over the interval of interest.
- Plot the exponential signal x ( t ) = 2 e − 2 t x(t)=2e^{-2t} x(t)=2e−2tover the interval t ∈ [ − 1 , 1 ] t\in [-1,1] t∈[−1,1].
- Plot another expoenential signal x ( t ) = 2 e − 4 t x(t)=2e^{-4t} x(t)=2e−4tover the interval t ∈ [ − 1 , 1 ] t\in[-1,1] t∈[−1,1] on the same figure.
- Plot the sinusoid
x 1 ( t ) = cos ( 2 π f c t ) x_1(t)=\cos(2\pi f_ct) x1(t)=cos(2πfct)
where f c = 4000 f_c=4000 fc=4000 Hz for two periods, i.e. t ∈ [ − T c , T C ] t\in [-T_c,T_C] t∈[−Tc,TC] and where T c T_c Tc is the fundamental periodfc = 4000; % frequency Tc = 1 / fc; % the fundamental period t = linspace(-Tc, Tc, 200); % 200 times instants from -Tc to Tc x1 = cos(2 * pi * fc * t); plot(t, x1, 'LineWidth', 2);
-
Plot another sinusoid given by
x 2 ( t ) = 1.2 cos ( 2 π f c ( t − t 0 ) ) x_2(t)=1.2\cos(2 \pi f_c(t-t_0)) x2(t)=1.2cos(2πfc(t−t0))
where f c = 4000 f_c=4000 fc=4000 Hz and t 0 = T / 4 t_0=T/4 t0=T/4 for t ∈ [ − T c , T c ] t\in[-T_c,T_c] t∈[−Tc,Tc], Note that x 2 ( t ) x_2(t) x2(t) is an amplitude-scaled and time-shifted version of x ( t ) x(t) x(t) in the previous slide.fc = 4000; Tc = 1 / fc; t0 = Tc / 4; t = linspace(-Tc, Tc, 200); x2 = 1.2 * cos(2 * pi * fc * (t - t0)); plot(t, x2, 'LineWidth', 2);
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GHAPrKxZ-1665365279526)(media/untitled-2.png)]
-
Plot the sum: x 3 ( t ) = x 1 ( t ) + x 2 ( t ) x_3(t)=x_1(t)+x_2(t) x3(t)=x1(t)+x2(t)
x3 = x1 + x2
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ECBvAWGH-1665365279526)(media/untitled-3.png)]
-
It is better to plot x 1 , x 2 , x 3 x1, x2, x3 x1,x2,x3 in the same figure.
Use the subplot command. Type help subplot in Matlab to know the details.subplot(3, 1, 1); plot(t, x1, 'LineWidth', 2); xlabel('Time (second)'); ylabel('x1') subplot(3, 1, 2); plot(t, x2, 'LineWidth', 2); xlabel('Time (second)'); ylabel('x2'); subplot(3, 1, 3); plot(t, x3, 'LineWidth', 2); xlabel('Time (second)'); ylabel('x3');
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NSsSPdrW-1665365279526)(media/untitled-1.png)]
Task 1
-
Plot the discrete sinusoid
x [ n ] = cos ( 1 6 π n ) x[n]=\cos({1\over6}\pi n) x[n]=cos(61πn)
for n = 0 , 1 , 2 , . . . , 20 n=0,1,2,...,20 n=0,1,2,...,20 and conclude the period of the signal. Verify your observation by theoretical analysis(cf. Lecture 2).T = 2 π 1 6 π = 12 T = {2\pi\over{1\over 6}\pi}=12 T=61π2π=12
n = 0:20; x = cos(1 / 6 * pi * n); plot(n, x, 'LineWidth', 2);
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p1MXX39K-1665365279526)(media/untitled-6.png)]
-
Plot the discrete sinusoid
x [ n ] = cos ( 2 n ) x[n]=\cos(2n) x[n]=cos(2n)
to show that this signal is not periodic. Explain the reason.We can observe from the figure that this discrete signal is not periodic.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ft0Zqvp5-1665365279527)(media/untitled-5.png)]
Task 2
Plot the discrete signal
cos
(
1
6
π
n
)
+
cos
(
1
3
π
n
)
\cos({1\over 6}\pi n)+\cos({1\over3} \pi n)
cos(61πn)+cos(31πn)
and conclude the period of the signal.
We can observe that the period of the signal is 12
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9J1YEC7w-1665365279527)(media/untitled-7.png)]
Task 3
Let
f
0
=
20
f_0=20
f0=20 Hz and
T
0
=
1
f
0
T_0={1\over f_0}
T0=f01. Compute and plot the following signal
x
(
t
)
=
∑
n
=
−
N
N
a
n
e
j
2
π
n
f
0
t
x(t)=\sum_{n=-N}^{N}a_ne^{j2\pi nf_0t}
x(t)=n=−N∑Nanej2πnf0t
where
N
=
5
N=5
N=5 and
a
n
a_n
an is given by
a
n
=
{
0
n
e
v
e
n
−
j
2
n
n
o
d
d
a_n= \left\{ \begin{array}{ll} 0 & n\ even\\ -{j\over 2n} & n\ odd \end{array} \right.
an={0−2njn evenn odd
for
t
∈
[
0
,
2
T
0
]
t\in[0,2T_0]
t∈[0,2T0]
f0 = 20; T0 = 1 / f0; N = 5; t = linspace(0, 2 * T0, 2000); x = 0; for n = -N:N if mod(n, 2) == 0 an = 0; else an = -1i / (2 * n); end x = x + an * exp(1i * 2 * pi * n * f0 * t); end plot(t, x, 'LineWidth', 2);
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mkNruPKx-1665365279527)(media/untitled-8.png)]
Task 4
Let
f
1
=
20
f_1=20
f1=20 Hz and
T
1
=
1
f
1
T_1={1\over f_1}
T1=f11. Compute and plot the following signal
f
(
t
)
=
8
A
π
2
[
sin
(
ω
1
t
)
−
1
9
sin
(
3
ω
1
t
)
+
1
25
sin
(
5
ω
1
t
)
−
.
.
.
+
(
−
1
)
k
−
1
2
k
2
sin
(
k
ω
1
t
)
+
.
.
.
]
f(t)={8A\over \pi ^2}[\sin(\omega_1t)-{1\over9}\sin(3\omega_1t)+{1\over 25}\sin(5\omega_1t)-...+{(-1)^{{k-1}\over2}\over k^2}\sin(k\omega_1t)+...]
f(t)=π28A[sin(ω1t)−91sin(3ω1t)+251sin(5ω1t)−...+k2(−1)2k−1sin(kω1t)+...]
where
k
k
k is odd,
A
A
A is a constant,
k
=
5
k=5
k=5, and
t
∈
[
0
,
2
T
1
]
t\in[0,2T_1]
t∈[0,2T1]
f1 = 20; T1 = 1 / f1; w1 = 2 * pi / T1; A = 1; t = linspace(0, 2 * T1, 2000); f = 0; for k = 1:2:5 f = f + 8 * A / (pi ^ 2) * (-1) ^ ((k - 1) / 2) / (k ^ 2) * sin(k * w1 * t); end plot(t, f, 'LineWidth', 2);
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gDCSIipQ-1665365279527)(media/untitled-9.png)]
Task 5
Increase the value of N N N in Task 3 3 3 to N = 10 N=10 N=10 and N = 50 N=50 N=50 and N = 100 N=100 N=100. Give your comments on the results signal.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YHyFmSBl-1665365279527)(media/untitled-12.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I4gcsBfG-1665365279528)(media/untitled-11.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZaZvqTBG-1665365279528)(media/untitled-13.png)]
Increase the value of k k k in Task 3 3 3 to k = 11 k = 11 k=11 and k = 51 k = 51 k=51 and k = 101 k = 101 k=101. Give your comments on the results signal.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xVZ1Ix0R-1665365279528)(media/untitled-14.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o3NH41mu-1665365279528)(media/untitled-15.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qcK80YH0-1665365279528)(media/untitled-16.png)]