Professional Documents
Culture Documents
Biên
www.hutech.edu.vn
*1ï2019ïCAP221*
này, xin - :
tailieuhoctap@hutech.edu.vn
I
...................................................................................................................I
.......................................................................................................... IV
................................................................................................... VI
AB.....................................................................................................................................1
....................................................................................................... 1
1.2 .......................................................................................... 3
................................................................................. 3
.................................................................................................. 5
...................................................................................... 5
....................................................... 5
.......................................................................................................... 6
........................................................................................................ 12
................................................................................... 12
1.2.8 Các file và hàm.......................................................................................................................................15
............................................................................................................ 20
.................................................................................................. 20
.................................................................................................. 21
.............................................................................................. 21
............................................................................................ 22
......................................................................................... 23
...................................................................................... 24
............................................................................... 25
........................................................... 25
........................................................................... 26
:.......................................................................................... 29
.................................................................................................. 34
................................................................................................................ 36
.................................................................................................... 36
M
Tin h k t cho sinh viên
Bài 1. .
Bài 2. GUI.
Bài 3. Simulink.
Bài 4. Symbolic.
c Tin h k t .
ý
V
5 nh, phù h
5 trong 90 phút.
VI
AM Amplitude Modulation:
AWGN Additive
vi phân
FM Frequency Modulation:
IMC
LQG
PM Phase Modulation:
VII
BÀI 1:
- .
- b.
toolboxes
uy
, fuzzy logic
vào icon
Hình 1.1:
2
BÀI
Hình 1.2:
mand window:
Matlab, là
built-in
function m m-files
A = pi/2;
B = sin(A)
B=1
bytes hó trình.
3
BÀI
edit
Hình 1.3:
plot_sin
or:
edit
1.2 CÁC V N
1.2.1 Các phép toán và toán t
Các toán t :
Các to :
pi 3.14159265
eps 2-52
realmin -1022
realmax 1023
inf vô
1.1:
a= 1;
b=-2;
c=1;
delta x1=(-b +
sqrt(delta))/(2*a) x2=(-b -
sqrt(delta))/(2*a) Chú ý:
màn hình.
-
..)
5
BÀI
- i .
-
global
lookfo
clear all %
save % file
load % file
who %
whos %
which
what
save file.
p = rand(1, 10);
q = ones(10);
save(savefile, 'p', 'q');
% n d ng c u trúc:
s1.a = 12.7;
s1.b = {'abc', [4 5; 6 7]};
s1.c = 'Hello!';
save('test.mat', '-struct', 's1');
Khi n trong fi
-append.
p = rand(1, 10);
q = ones(10);
save('test.mat', 'p'); % bi n p
save('test.mat', 'q'); % bi n q p b xóa
save('test.mat', - ); % bi n p và không xóa q
load file.
load('test.mat'); % L y t t c các bi
load('test.mat', 'p'); % L y bi n p trong file test.mat
1.2.5 Ma tr n
1.2.5.1 Nh p ma tr n
vector
- r file
- trong MATLAB
-
7
BÀI
- Bao các ph ]
1.2:
sum(A)
ans = 34 34 34 34
ans =
16 5 9 4
3 10 6 15
2 11 7 14
13 8 12 1
1.2.5.2 Ch s
ý
tham
1.2.5.3 Toán t
Toán t
1:10
ans = 1 2 3 4 5 6 7 8 9 10
100: 5
8
BÀI
ans = 100 93 86 79 72 65 58 51
0: pi/4:
/4
A(:,3)
ans =
2
11
7
14
A(3,:)
ans =
9 6 7 12
B=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
1.2.5.4 T o ma tr n b ng hàm có s n
- linspace: vector
a = linspace vector
a =1.0000 1.2857 1.5714 1.8571 2.1429 2.4286 2.7143 3.0000 3.2857 3.5714 3.8571
4.1429 4.4286 4.7143 5.0000
9
BÀI
- zeros:
z = zeros(2, 4)
z=
0000
0000
- ones
x = ones(2, 3)
x=
111
111
y = 5*ones(2, 2)
y=
55
55
- rand
d = rand(4, 4)
d=
- randn
e = randn(3, 3)
e=
- magic(n) n2
3).
- pascal(n) Pascal.
pascal(4)
ans = 1
111
1234
1 3 6 10
1 4 10 20
- eye(n)
eye(3)
ans =
100
010
001
- eye(m,n)
eye(3,4)
ans = 1
000
0100
0010
1.2.5.5 N i ma tr n
1.3:
a = ones(3, 3);
b = 5*ones(3, 3);
c = [a+2; b]
11
BÀI
c=
333
333
333
555
555
555
xoá hàng .
b(:, 2) = [] ;
b=
55
55
55
b(1:2:5) = [
1.2.5.7 Các l nh x lý ma tr n
:X=A+B
:X=A-B
Nhân: X = A*B
cùng kíc
Chia: X = * inv(B)
X = A\ X = inv(A) * B
12
BÀI
: X = A^2
X = A.^2
X = inv(A)
d = det(A)
1.2.6 a th c
m=[1 -1 4 -5 -1]
c, ta dùng .V
2:
polyval(m,2)
roots(m)
end
N if
else và elseif
1.4:
x = fix(30*rand);
13
BÀI
disp
if g < x
disp( Ban );
elseif g > x
disp( Ban );
else
end
1.2.7.2 Switch
Cú pháp u:
case n1
case n2
...............
case nn
Otherwise
end
1.2.7.3 While
end
14
BÀI
disp( );
g = input(
i = 0;
while i~=g
disp( );
i = i+1
end
1.2.7.4 For
ùng
: :
1.6:
x = fix(100*rand);
n = 7;
t = 1;
for k = 1:1:7
disp( );
if g < x
disp( );
elseif g>x
disp( o );
else
1.2.7.5 Break
hay while
k
Script là m file
f = [1 1];
i = 1;
while(f(i)+f(i+1))<1000
f(i + 2)= f(i) +f(i+1);
i = i + 1;
end
plot(f)
>>fibo và .
16 BÀI 1: B
1.2.8.2 Hàm
Hàm là m-file
function y = tb(x)
- g function y = tb(x
y, tên hàm tb
- MATLAB.
- .
. Tên
ATLAB ta gõ l :
z = 1:99;
tb(z)
global.
file.m
file.
BÀI 1
1.2.8.3 u khi n vào và ra
disp
a = [1 2 3];
disp(a)
123
disp(t)
Xin chao
format % short
format hex %
format + % , - và blank
input
x = input( : )
=4
exp(x) (ex)
sqrt
log(x) logarit
log10(x) logarit
abs(x) module
1 BÀI
angle(x) argument
conj
imag(x)
real(x)
sign
-
fil
e
file ab.m
function y = ab(x)
BÀI 1
y = 2./(x + 1) + 1./(x.^2 + 2) 1 ;
Các
inline
f = inline
x=
f(2)
ans =
0.833333333333333
1.7:
Hình 1.4:
:
0.3).^2+0.01)+1./(x.^2+0.04) 6 );
x = fminbnd(f,0.3,1)
2 BÀI
x = 0.999943561455580
function b = three_var(v)
x = v(1);
y = v(2);
z = v(3);
-0.6, y = -1.2 và z =
0.135:
i m
1.9: T - :
0.3).^2+0.01)+1./(x.^2+0.04)
a = fzero(f,-0.2)
a = -0.443585269991057
1.3 HO
1.3.1 Các l nh v
MATLA
plot: -
plot3: -
loglog: g logarit
21
BÀI
semilogx:
semilogy:
polar
1.3.2 T o hình v
vector thì
t = [0:pi/100:2*pi];
y = sin(t);
->2
1.3.3 Ki ng v
t = [0:pi/100:2*pi];
y = sin(t);
LineWidth
MarkerEdgeColor c
MarkerFaceColor
MarkerSize
- g --
-
22
BÀI
o vòng tròn
s hình
vuông
< tam giác sang trái
1.10:
hold on
1.3.4 V v i hai tr c y
có
23
BÀI
1.11:
Hình 1.6
t = 0:900; A = 1000;b = 0.005;a = 0.005;z2 = sin(b*t);
z1 = A*exp(-a*t);
1.3.5 V ng cong 3D
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t)
Hình 1.7:
24
BÀI
1.3.6 V nhi u tr c to
Hình 1.8:
subplot(2,3,5) %
1.12:
x=linspace(0,2*pi);
x1=linspace(-2*pi,2*pi); y4=sinc(x1);
subplot
Hình 1.9:
heo
x = 0:0.025:pi/2;
;
26
BÀI
1.13:
x = -pi:.1:pi; y = sin(x);
plot(x,y)
pi:pi/2:p);
zlabel t
x = -pi:.1:pi; y = sin(x);
plot(x,y);
\ ;
title( \it{G ;
t k
27
BÀI
1.14: V t = 0.005:
t = 0:900;
plot(t,0.25*exp( 0.005*t))
gf
\bf
\it nghiêng
\fontname{fontname} .
\fontsize{fontsize} .
\color(colorSpec)
28
BÀI
\tau)').
1.15:
Hình 1.10
-project.org/
1.3.10 ho c bi t:
a.
bar
barh
ngang.
bar3
3D.
bar3h
30
BÀI
1.16:
subplot(211);bar(y);
subplot(212);bar3(y);
Hình 1.11:
b.
1.17:
ngay = 0:5:35;
bar(ngay,nhdo);
; ;
31
BÀI
Hình 1.12:
trí
:
h1 = gca;
plot(ngay
sL
c. g:
32
BÀI
area(Y)
Hình 1.13:
d.
. 2D và 3D.
subplot(221);pie(x1,explode);
subplot(222);pie3(x1,explode);
Hình 1.14:
33
BÀI
e.
- D
hình.
- D
- D ình.
1.18:
T mv.m
axis equal
M = moviein(16,gcf);
1,
for j = 1:16
plot(fft(eye(j + 16))); ;
M(:,j) = getframe(gcf);
end
clf;
movie(M,30)
:
34
BÀI
t = linspace(0,2*pi,10000); y = exp(sin(t));
h = plot(t,y);
for k = 1:0.01:10
y = exp(sin(t.*k));
h.YData = y;
drawnow
end
1.3.11 ho 3D
a.
b.
[x,y] = meshgrid(-8:.5:8);
r = sqrt(x.^2 + y.^2);
35
BÀI
z = sin(r)./r;mesh(z)
0.5
-0.5
40
30 40
20 30
20
1 10
0 0
Hình 1.15:
36
BÀI
Trong bà
CÂU H
Câu 1: :
a.
b.
c.
sum(A(:,1))).
d. ng các
Câu 2: ;
sau:
a. A'
b. A(:,[1 4])
d. reshape(A,2,6)
e. A(:)
f. [A A(end,:)]
g. A(1:3,:)
h. [A ; A(1:2,:)]
i. sum(A)
j. sum(A')
37
BÀI
Câu 3: c
a. x(3)
b. x(1:7)
c. x(1:end)
d. x(1:end-1)
e. x(6:-2:1)
f. x([1 6 2 1 1])
g. sum(x)
Câu 4:
Câu 5: ) + 3cos( ).
Câu 6: 2x2 + y2 = 10
Câu 9:
Máy A Máy B
1996 38 48
1997 33 54
1998 36 120
1999 31 92
2000 60 110
2001 70 131
Câu 10:
Câu 11:
không
-GUI (GRAPHIC USER INTERFACE) 39
BÀI
BÀI 2: G -GUI
(GRAPHIC USER INTERFACE)
- .
- .
2.1 T O GUI
K New > App > GUIDE
Hình 2.1:
40
BÀI -GUI (GRAPHIC USER
guide
Blank GUI.
Cá
Hình 2.3:
-GUI (GRAPHIC USER INTERFACE) 41
BÀI
2.2 HO
Pop-up
List box
Toggle Button
Table
Axes
Panel
Button Group
ActiveX
dùng
Hình 2.6:
-GUI (GRAPHIC USER INTERFACE) 43
BÀI
chú ý:
String
Tag
FontName
FontSize: kích
Unit alized.
Callback .m
2.3 T O MENU
Tools > Menu Editor.
Hình 2.7:
44
BÀI -GUI (GRAPHIC USER
NewMenuItem:
hành
Move Selected
Xoá menu
Hình 2.8:
Callback c
pushbutton1, mnuTool
( 2.2 và
2.3)
end
2.5 VÍ D T O GUI
Hình 2.9:
doF = str2num(get(handles.editDoF,'String'));
if get(handles.radiobuttonC,'Value') == 1
% Chuyen oC
set(handles.editKQ,'String',num2str((doF-32)*5/9));
elseif get(handles.radiobuttonR,'Value') == 1
%Chuyen oR
set(handles.editKQ,'String',doF+459.7);
else
%Chuyen oK
set(handles.editKQ,'String',(doF-32)*5/9+273.15);
end
-GUI (GRAPHIC USER INTERFACE) 47
BÀI
set(handles.radiobuttonK,'Value',0);
set(handles.radiobuttonR,'Value',0);
set(handles.text2,'String','Celcius degree');
set(handles.radiobuttonC,'Value',0);
set(handles.radiobuttonK,'Value',0);
set(handles.text2,'String','Rankine degree');
set(handles.radiobuttonR,'Value',0);
set(handles.radiobuttonC,'Value',0);
48
BÀI -GUI (GRAPHIC USER
set(handles.text2,'String','Kelvin degree');
set(handles.editDoF,'String',get(handles.sliderDoF,'Value'));
-GUI (GRAPHIC USER INTERFACE) 49
BÀI
Câu 1:
Draw
Sin
Sinc
Cos
Exit
View
2D ( 2))
3D ( )
Zoom Fill ( )
Zoom Out ( )
Help
About
-
-
-
50
BÀI -GUI (GRAPHIC USER
Câu 2:
Static Text:
String: Amplitude
Edit Text:
String: 5
Tag: editAmp
Static Text:
String: Frequency
Edit Text:
String: 10
Tag: editFreq
Static Text:
String: Function
Pop-up Menu:
String: cos
sin
sinc
Tag: popupmenuFunc
Button:
String: Draw
Tag: pushbuttonDraw
Axes:
NextPlot: replacechildren
XGrid: on
YGrid: on
BÀI 2: GIA -GUI (GRAPHIC USER INTERFACE) 51
ZGrid: on
Tag: axes1
Câu 3:
52
BÀI 3:
BÀI 3: SYMBOLIC
Khi
Tên hàm
syms
sym
diff
simplify
int Tích phân
subs
factor
expand
ezplot
limit Tìm g
findsyms
pretty
collect
solve
dsolve
laplace
BÀI 3: SYMBOLIC 53
Tên hàm
ilaplace
fourier
ifourier
finverse
syms x
y=diff(cos(x))
y=
-sin(x)
diff('x^3+3*x^2+2*x-
ans =
6*x + 6
y = diff('x/(1+x^3)')
y=
1/(x^3 + 1) - (3*x^3)/(x^3 + 1)^2
simplify(y)
ans =
-(2*x^3 - 1)/(x^3 + 1)^2
syms m
z = 2*sin(x) + m^2*cos(x)% z là hàm s theo x,m
diff(z,m,2)
ans =
2*cos(x)
diff(z,x,3)
ans =
sin(x)*m^2 - 2*cos(x)
54
BÀI 3:
y=
atan(x)
do hà
y=
int(log(x)/exp(x^2), x)
y=
¼
y=int(x^3*atan(x),0,pi/4)
y=
pi/16 - atan(pi/4)/4 + (pi^4*atan(pi/4))/1024 - pi^3/768
eval(y)
ans = 0.0529
y = inline('log(x)./exp(x.^2)');
quad(y,0,1)
ans =
-0.9059
3.3 C
.
finverse(1/x)
ans =
1/x
BÀI 3: SYMBOLIC 55
finverse(a*x+b)
ans =
-(b - x)/a
3.4 V TH
syms x
subplot(211);ezplot(sin(x)/x)
subplot(212);ezplot(sin(x)/x,0,4*pi)
Hình 3.1:
ezsurf('x*exp(-x^2 - y^2)')
56
BÀI 3:
3.5
syms s
y=int('sin(s+2*x)','s',pi/2,pi)
y=
cos(2*x) - sin(2*x)
pretty(y)
cos(2 x) - sin(2 x)
f = sym('x^2-1');
factor(f)
ans =
(x - 1)*(x + 1)
y=expand((x+1)^3-2*(x-1)^2)
y=
x^3 + x^2 + 7*x - 1
y=simplify(sin(x)^2+cos(x)^2)
BÀI 3: SYMBOLIC 57
y=
1
y=simplify((1-x^2)/(1-x))
y=
x+1
simplify((1/x^3+6/x^2+12/x+8)^(1/3))
ans =
((2*x + 1)^3/x^3)^(1/3)
Dùn
vi
phân.
y=solve(x^3+2*x^2+1==0)
y=
root(z^3 + 2*z^2 + 1, z, 1)
root(z^3 + 2*z^2 + 1, z, 2)
root(z^3 + 2*z^2 + 1, z, 3)
i
y=vpa(y)
y=
-2.2055694304005903117020286177838
0.10278471520029515585101430889191 - 0.66545695115281347670619061162908i
0.10278471520029515585101430889191 + 0.66545695115281347670619061162908i
eval(y)
ans =
-2.2056 + 0.0000i
0.1028 - 0.6655i
0.1028 + 0.6655i
58
BÀI 3:
syms x1 x2 x3
[y1,y2,y3]=solve(2*x1+3*x2-2*x3==1,x1-x2+3*x3==2,x2-2*x3==4)
y1 =
35/2
y2 =
-19
y3 =
-23/2
[y1,y2,y3]=solve(2*x1+3*x2-2*x3==1,x1-x2+3*x3==2,x2^2-2*x3==4)
y1 =
(7*329^(1/2))/64 + 21/64
21/64 - (7*329^(1/2))/64
y2 =
5/8 - 329^(1/2)/8
329^(1/2)/8 + 5/8
y3 =
49/64 - (5*329^(1/2))/64
(5*329^(1/2))/64 + 49/64
eval([y1 y2 y3])
ans =
2.3120 -1.6423 -0.6514
-1.6558 2.8923 2.1827
syms x y
[x1,x2]=solve(x^2+x*y+y==1,x^2-4*y==3)
x1 =
-1
BÀI 3: SYMBOLIC 59
- 11^(1/2) - 2
11^(1/2) - 2
x2 =
-1/2
11^(1/2) + 3
3 - 11^(1/2)
eval(x1)
ans =
-1.0000
-5.3166
1.3166
eval(x2)
ans =
-0.5000
6.3166
-0.3166
giác:
solve(2*cos(x)+2)
ans =
pi
solve(2*cos(x))
ans =
pi/2
solve(cos(x)=sin(x))
ans =
pi/4
y=solve(exp(x)==tan(x))
y=
-226.19467105846511316931032359612
60
BÀI 3:
y=dsolve('Dy=y*tan(x)+cos(x)',x)
y=
(x/2 + sin(2*x)/4)/cos(x) + C4/cos(x)
y = simplify(y)
y=
(4*C4 + 2*x + sin(2*x))/(4*cos(x))
pretty(y)
4 C4 + 2 x + sin(2 x)
4 cos(x)
y=dsolve('Dy=1+y^2','y(0)=1','x')
y=
tan(pi/4 + x)
y=dsolve('D2y=cos(2*x)-
y=
y= simplify(y)
y=
pretty(y)
/ x \4
cos| - | 8
8 cos(x) \2/
+ sin(x) - +1
3 3
BÀI 3: SYMBOLIC 61
f=
cos(4*x)*exp(3*x)
s=
sin(4*x)*exp(3*x)
syms x a t h
y=limit(sin(x)/x) % x 0
y=
y=limit(sin(x)/x,inf) % x
y=
ans =
Inf
ans =
-Inf
limit((sin(x+h)-sin(x))/h,h,0)
ans =
cos(x)
62
BÀI 3:
Câu 1: 3
Câu 3:
Câu 4: V
Câu 5:
Câu 6:
BÀI 4: SIMULINK 63
BÀI 4: SIMULINK
Simulink.
Hình 4.1:
Blank Model
64
BÀI 4:
Hình 4.4:
BÀI 4: SIMULINK 65
4.1 V SIMULINK
4.1.1
Các kh i
-
66
BÀI 4:
- ình.
- Tag: tr
l.
- 0.
- 0.
-
BÀI 4: SIMULINK 67
Show Names.
-
.
-
68
BÀI 4:
4.1.2
n kh i
nu
: untitled.
menu Edit.
4.1.3
ng
-
BÀI 4: SIMULINK 69
- phân nhánh
4.1.4
Ghi chú
hú
70
BÀI 4:
4.1.5
Làm vi c v i các lo i d li u
double
single
int8
uint8
int16
uint16
int32
uint32
t tín
-
BÀI 4: SIMULINK 71
4.1.6
Làm vi c v i tín hi u ph c
4.1.7
T o h th ng con
Khi mô hì
- ình.
-
ra.
-
72
BÀI 4:
4.2.1
Mô hình hoá m
Ph :
F = (9/5)C + 32
Tr ình:
- Constant Sources .
- Gain Math .
- Sum Math .
- Scope Sinks
Gain và
Constant
Ramp
gán Initial output Gain i Sum
Scope
BÀI 4: SIMULINK 73
Hình 4.5:
Run
Scope.
Hình 4.6:
74
BÀI 4:
4.2.2
Mô hình hoá h n tính
ph
- Display Sink
- Constant t Sources
Hình 4.7:
4.2.3
Gi i c cao:
Xét ph
ình: x2 + 3x + 1
=0
BÀI 4: SIMULINK 75
- Constant Sources
- Add Math
Hình 4.8:
4.2.4
Mô hình hoá h th ng liên t n
ình vi phân:
76
BÀI 4:
- Sum Math
- Scope Sink
Hình 4.9:
4.2.5
Mô hình hoá h c cao
- Scope Sink
Hình 4.10:
4.2.6
Mô hình hoá h u ki u khác không
a. Ph ìn ình:
x'(t) + x(t) = 0
Nh -1 ; C = 1 ; u = 0 ; B = 0, D = 0 và Init = 1
- State-Space Continuous.
u:
Hình 4.11:
b. tr ình:
-2 và x(0) = 1.
Ph
4.2.7
Mô hình hóa h cho b kh i
n sau:
Hình 4.12:
Ta mô
Hình 4.13:
4.2.8
Mô hình hoá h phi tuy n
a. X Pol:
2 =0
1 2 y1(0) = 2 và y2
80
BÀI 4:
b. X vi phân sau:
Hình 4.15:
Double-
là 1.3.
4.2.9
Ví d
m
f u
k intergrator
Continuous.
82
BÀI 4:
v = v1dt v1 = v'.
Gain Math
Sum trong th
Math
-
Gain
Step Sources gõ ra
Scope Sinks
BÀI 4: SIMULINK 83
Start
Simulation > Start
-b/m; B = 1/m; C = 1; D = 0.
State-Space Continuous
Step Sources Scope Sinks
State-Space và
TÓM T
Trong bài này
Câu 1:
b=0
K = Kt =
Ke
BÀI 4: SIMULINK 85
Câu 2: :
M1
M2
x1 , x2
Câu 3:
86
BÀI 4:
m1
m2
K1 )
K2 =
b1
b2
x1 , x2
87
BÀI
BÀI 5:
Sa g bài n
5.1 COMMUNICATION
Communication toolbox
n thông. Toolbox
,
mô hình
Communication toolbox
- Các k
- Hàm MatLab
Toolbox này h
-
5.1.1 Hàm
5.1.1.1
Ngu n d li u, hi n th tín hi u
eyediagram:
5.1:
Fs = 10000; Rs = 100;
88 BÀI 5:
nSamps = Fs/Rs;
rolloff = 0.5; M = 4;
qpskMod = comm.QPSKModulator; u ch QPSK
rctFilt = comm.RaisedCosineTransmitFilter('RolloffFactor', rolloff,
'OutputSamplesPerSymbol', nSamps,'FilterSpanInSymbols', 6, 'Gain',
9.9121);
rcrFilt = comm.RaisedCosineReceiveFilter('RolloffFactor', rolloff,
'InputSamplesPerSymbol', nSamps,'FilterSpanInSymbols', 6,
'DecimationFactor', 1, 'Gain', 0.1009);
% T o tín hi u ch
frameLen = 1000; msgData = randi([0 M-1],frameLen,1);
msgSymbols = qpskMod(msgData); msgTx = rctFilt(msgSymbols);
% T o gi m t
eyeObj = commscope.eyediagram('SamplingFrequency', Fs,
'SamplesPerSymbol', nSamps, 'OperationMode', 'Complex
Signal','PlotType', '2D Line');
update(eyeObj, 0.5*msgTx);
Hình 5.1:
scatterplot
89
BÀI
5.2 u 64-QAM
d = (0:63)';
s = qammod(d,64);
scatterplot(s)
biterr
randi
symerr tí i và SER
wgn
comm.GoldSequence
comm.HadamardCode
comm.KasamiSequence
comm.BarkerCode
comm.WalshCode
comm.PNSequence
90
BÀI 5: CÁC T
5.1.1.2
Mã hóa ngu n
compand:
5.3:
data = 2:2:12
mu_e = compand(mu_c,255,max(data),'mu/expander') % Gi i mã lu t
a_e = compand(a_c,87.6,max(data),'a/expander')% Gi i mã lu t A
data = 2 4 6 8 10 12
dpcmdeco
dpcmopt
5.4
partition = [-1:.1:.9];
codebook = [-1:.1:1];
t = [0:pi/50:2*pi];
x = sawtooth(3*t); % Tín hi u g c
% ng t dùng DPCM.
encodedx = dpcmenco(x,codebook,partition,predictor);
plot(t,x,t,decodedx,'--')
distor = sum((x-decodedx).^2)/length(x)% L
Hình 5.3:
distor = 0.0327
5.5: So sánh
t = [0:pi/50:2*pi];
x = sawtooth(3*t); %
initcodebook = [-1:.1:1];
[predictor,codebook,partition] = dpcmopt(x,1,initcodebook);
% DPCM.
encodedx = dpcmenco(x,codebook,partition,predictor);
decodedx = dpcmdeco(encodedx,codebook,predictor);
distor = sum((x-decodedx).^2)/length(x)
92
BÀI
distor = 8.6801e-04
5.4.
huffmandict
huffmandeco
5.6
symbols = 1:6;
p = [.5.125.125.125.0625.0625];
dict = huffmandict(symbols,p);
dsig = huffmandeco(comp,dict); % Gi i mã
binarySig = de2bi(sig);
binaryComp = de2bi(comp);
ans =
logical
seqLen =
300
encodedLen =
208
lloyds g dùng th
93
BÀI
quantiz
5.1.1.3
Phát hi n và s a sai
1. Mã CRC
comm.CRCDetector:
comm.CRCGenerator:
5.7:
err =
err1 =
2. Mã
comm.BCHDecoder
comm.RSDecoder -
Solomon
94
BÀI
cyclgen
cyclpoly
decode
encode
gfweight
gen2par
hammgen
bchdec
bchgenpoly
bchnumerr mã BCH
rsgenpoly -Solomon
rsgenpolycoeffs -Solomon
n = 7; % Chi u dài mã
k = 4; % Chi p
data = randi([0 1],k,1) % Th p ng u nhiên
pol = cyclpoly(n,k);% T o ma tr n sinh và ma tr n ki m tra cho mã vòng
parmat = cyclgen(n,pol);
genmat = gen2par(parmat);
encData = encode(data,n,k,'linear/binary',genmat);
RevData1 = encData; % D li u không sai
decData1 = decode(RevData1,n,k,'linear/binary',genmat)
RevData2 = encData;
RevData2(3) = ~RevData2(3); % Sai 1 bit
95
BÀI
decData2 = decode(RevData2,n,k,'linear/binary',genmat)
96BÀI 5: CÁC TOOLBOX
BÀI
95
S2 = RevData2'*parmat';S2 = mod(S2,2) % Vector s a sai
RevData3 = encData;
RevData3(2) = ~RevData3(2); % Sai 2 bit
RevData3(1) = ~RevData3(1);
decData3 = decode(RevData3,n,k,'linear/binary',genmat)
S3 = RevData3'*parmat';S3 = mod(S3,2) % Vector s a sai
data = 1 1 0 1
decData1 = 1 1 0 1
decData2 = 1 1 0 1
S2 = 0 0 1
decData3 = 1 1 1 1
S3 = 1 1 0
3. Mã
comm.TurboDecoder
comm.ViterbiDecoder
distspec
vitdec
Viterbi
5.9
M = 64; % 64-QAM
k = log2(M);
EbNoVec = (4:10)'; % Eb/No (dB)
numSymPerFrame = 1000; % S symbol QAM symbols trên 1 frame
berEstSoft = zeros(size(EbNoVec)); % Kh i t ng BER
berEstHard = zeros(size(EbNoVec));
trellis = poly2trellis(7,[171 133]); % T i và chi u dài
tbl = 32;
rate = 1/2;
for n = 1:length(EbNoVec)
snrdB = EbNoVec(n) + 10*log10(k*rate); % Chuy n Eb/No sang SNR
[numErrsSoft,numErrsHard,numBits] = deal(0);
while numErrsSoft < 100 && numBits < 1e7
% T o d li u ng u nhiên
dataIn = randi([0 1],numSymPerFrame*k,1);
% Mã hóa d li u
dataEnc = convenc(dataIn,trellis);
% u ch QAM
txSig = qammod(dataEnc,M,'InputType','bit');
% hi u qua kênh truy n có
AWGN rxSig =
awgn(txSig,snrdB,'measured');
% Gi u ch
rxDataHard = qamdemod(rxSig,M,'OutputType','bit');
rxDataSoft = qamdemod(rxSig,M,'OutputType','approxllr',
'NoiseVariance',10.^(snrdB/10));
% Gi i mã Viterbi
dataHard = vitdec(rxDataHard,trellis,tbl,'cont','hard');
dataSoft = vitdec(rxDataSoft,trellis,tbl,'cont','unquant');
numErrsInFrameHard = biterr(dataIn(1:end-tbl),
dataHard(tbl+1:end));
numErrsInFrameSoft = biterr(dataIn(1:end-tbl),
dataSoft(tbl+1:end));
numErrsHard = numErrsHard + numErrsInFrameHard;
numErrsSoft = numErrsSoft + numErrsInFrameSoft;
97
BÀI
Hình 5.4
semilogy(EbNoVec,[berEstSoft berEstHard],'-*')
hold on
semilogy(EbNoVec,berawgn(EbNoVec,'qam',M))
legend('Soft','Hard','Uncoded','location','best')
grid
xlabel('Eb/No (dB)')
ylabel('Bit Error Rate')
5.1.1.4
u ch
1.
comm.GeneralQAMDemodulator ò
comm.GeneralQAMModulator
comm.RectangularQAMDemodulator
comm.RectangularQAMModulator
comm.CPFSKDemodulator
comm.CPFSKModulator
comm.CPMDemodulator
comm.CPMModulator
comm.GMSKDemodulator
comm.GMSKModulator
comm.MSKDemodulator
comm.MSKModulator
comm.OFDMDemodulator
comm.OFDMModulator
comm.BPSKDemodulator
comm.BPSKModulator
comm.DBPSKDemodulator DBPSK
comm.DBPSKModulator
comm.DPSKDemodulator -ary
comm.DQPSKDemodulator
comm.DQPSKModulator
99
BÀI
comm.OQPSKDemodulator: g
comm.OQPSKModulator
100
BÀI
comm.PSKDemodulator -ary
comm.QPSKDemodulator
comm.QPSKModulator
comm.GeneralQAMTCMDemodulator
comm.GeneralQAMTCMModulator
comm.PSKTCMDemodulator
chòm sao M-ary PSK
comm.PSKTCMModulator
sao M-ary PSK
comm.RectangularQAMTCMDemodulator
comm.RectangularQAMTCMModulator i
pamdemod
pammod
qamdemod
qammod
mskdemod
mskmod
fskdemod:
fskmod
dpskdemod
dpskmod
101
BÀI
oqpskdemod
oqpskmod
pskdemod
pskmod
5.11:
M = 8;
freqSep = 100;
fskMod = comm.FSKModulator(M,freqSep);
fskDemod = comm.FSKDemodulator(M,freqSep);
ch = comm.AWGNChannel('NoiseMethod','Signal to noise ratio
(SNR)','SNR',-2);
err = comm.ErrorRate;
for counter = 1:100
data = randi([0 M-1],50,1);
modSignal = step(fskMod,data);
noisySignal = step(ch,modSignal);
receivedData = step(fskDemod,noisySignal);
errorStats = step(err,data,receivedData);
end
es = 'Error rate = %4.2e\nNumber of errors = %d\nNumber of symbols =
%d\n';
fprintf(es,errorStats)
Number of errors = 70
2.
comm.FMDemodulator
comm.FMModulator
5.12:
102
BÀI
amdemod
ammod
fmdemod
fmmod
pmdemod
pmmod
ssbdemod
103
BÀI
ssbmod iên
5.13
fs = 100;
t = (0:1/fs:100)';
fc = 10;
x = sin(2*pi*t);
ydouble = ammod(x,fc,fs);
ysingle = ssbmod(x,fc,fs);
plot(ysingle);
sa = dsp.SpectrumAnalyzer('SampleRate',fs, 'PlotAsTwoSidedSpectrum',
false, 'YLimits',[-60 40]);
step(sa,ysingle)
plot(ydouble);
sb = dsp.SpectrumAnalyzer('SampleRate',fs,
'PlotAsTwoSidedSpectrum',false, 'YLimits',[-60 40]);
step(sb,ydouble)
Hình 5.6: Tí
104
BÀI
Hình 5.7:
5.1.1.5
Kênh truy n
comm.MemorylessNonlinearity
comm.PhaseNoise
comm.ThermalNoise
pskModulator = comm.PSKModulator;
modData = pskModulator(randi([0 7],2000,1));
channel = comm.AWGNChannel('EbNo',20,'BitsPerSymbol',3);
channelOutput = channel(modData);
105
BÀI
scatterplot(modData)
scatterplot(channelOutput)
Hình 5.8: -
5.1.2 Simulink
Hình 4.4.
5.1.2.1
Ngu n d li u, hi n th tín hi u
-1]
Kasami Sequence
Generator
Generator
107
BÀI
Constellation Diagram
Eye Diagram
108
BÀI
Ví d 5.15
Hình 5.9:
5.1.2.2
Mã hóa ngu n
A-Law Expander
Differential
Decoder
109
BÀI
Differential Encoder
Mu-Law
Compressor
Mu-Law Expander: g µ
5.1.2.3
Phát hi n và s a sai
1. Mã CRC
CRC-N Generator
CRC-N
Syndrome Detector
5.1
2. Mã
109
BÀI
3.
5.17 dù
,144
11 BÀI
5.1
,0 0018
5.1.2.4
u ch
1.
AM:
BÀI 11
CPM:
FM:
OFDM:
PM:
TCM:
112
BÀI
2.
5.19:
Hình 5.10:
113
BÀI
3. thông
5.20
Hình 5.11:
114
BÀI
5.1.2.5
Kênh truy n
Hình 5.12:
115
BÀI
5.1.3 Ví d
5.22: Mô ph
625 s nh sang t
Hình 5.13:
116
BÀI
Hình 5.16:
Hình 5.17:
117
BÀI
5.23 -B
Mô hình -
trong các -S có
PPM
118
BÀI
s s
squitter s
sau: ng , dung
(CA), , CRC.
c -S có s,
s hình.
Hình 5.19: -S
userInput.Duration = 10;
userInput.FrontEndSampleRate = 2.4e6;
userInput.RadioAddress = '0';
userInput.LogFileName = 'adsb_messages.txt';
symbolDuration = 1e-6;
chipsPerSymbol = 2;
longPacketDuration = 112e-6;
shortPacketDuration = 56e-6;
preambleDuration = 8e-6;
% T o tín hi u ngu n
bbFileName = 'adsb_capture_01.bb';
sigSrc = comm.BasebandFileReader(bbFileName);
frontEndSampleRate = sigSrc.SampleRate;
adsbParam.isSourceRadio = false;
adsbParam.FrontEndSampleRate = frontEndSampleRate;
chipRate = chipsPerSymbol/symbolDuration;
[n,d]=rat(frontEndSampleRate/chipRate);
119
BÀI
if d>2
interpRate = d;
else
if n <= 1
interpRate = 2*d;
else
interpRate = d;
end
end
adsbParam.InterpolationFactor = interpRate;
sampleRate = frontEndSampleRate * interpRate;
adsbParam.SampleRate = sampleRate;
adsbParam.SamplesPerSymbol = int32(sampleRate * symbolDuration);
adsbParam.SamplesPerChip = adsbParam.SamplesPerSymbol / chipsPerSymbol;
adsbParam.MaxPacketLength = int32((preambleDuration+longPacketDuration)
* sampleRate);
targetSamplesPerFrame = 50000;
maxPacketDuration = (preambleDuration+longPacketDuration);
maxPacketLength = maxPacketDuration*frontEndSampleRate;
minNumSamplesPerFrame = lcm(256,maxPacketLength);
adsbParam.SamplesPerFrame = round(targetSamplesPerFrame /
minNumSamplesPerFrame)*minNumSamplesPerFrame;
adsbParam.MaxNumPacketsInFrame = floor(adsbParam.SamplesPerFrame /
maxPacketLength / 4);
adsbParam.FrameDuration = adsbParam.SamplesPerFrame /
frontEndSampleRate;
adsbParam.LongPacketLength = int32(longPacketDuration*sampleRate);
adsbParam.PreambleLength = int32(preambleDuration*sampleRate);
120
BÀI
adsbParam.LongPacketNumBits = int32(longPacketDuration /
symbolDuration);
adsbParam.ShortPacketNumBits = int32(shortPacketDuration /
symbolDuration);
b = rcosdesign(0.5, 3, double(adsbParam.SamplesPerChip));
adsbParam.InterpolationFilterCoefficients = single(b);
adsbParam.SyncSequence = [1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0];
adsbParam.SyncSequenceLength = length(adsbParam.SyncSequence);
adsbParam.SyncSequenceHighIndices = find(adsbParam.SyncSequence);
adsbParam.SyncSequenceNumHighValues =
length(adsbParam.SyncSequenceHighIndices);
adsbParam.SyncSequenceLowIndices = find(~adsbParam.SyncSequence);
adsbParam.SyncSequenceNumLowValues =
length(adsbParam.SyncSequenceLowIndices);
syncSignal = reshape(ones(adsbParam.SamplesPerSymbol/2,1)
*adsbParam.SyncSequence, 16*adsbParam.SamplesPerSymbol/2, 1);
adsbParam.SyncDownsampleFactor = 2;
adsbParam.SyncFilter = single(flipud(2*
syncSignal(1:adsbParam.SyncDownsampleFactor:end)-1));
sigSrc.SamplesPerFrame = adsbParam.SamplesPerFrame;
viewer = helperAdsbViewer('LogFileName', userInput.LogFileName);
startMapUpdate(viewer);
msgParser = helperAdsbRxMsgParser(adsbParam);
start(viewer)
radioTime = 0;
if adsbParam.isSourceRadio
[rcv,~,lost,late] = sigSrc();
121
BÀI
else
rcv = sigSrc();
lost = uint32(0);
late = uint32(1);
end
[msg,msgCnt] = msgParser(pkt,pktCnt);
stop(viewer)
release(sigSrc)
Hình 5.20:
122
BÀI
Hình 5.21:
Hình 5.22:
5.2.1 Bodies
- 2.
123
BÀI
Input gravity a
Hình 5.24:
dùng
chung thôn
-
124
BÀI
5.2.1.3 Ground
này v a
tr
ground point.
Thô un
Hình 5.25:
125
BÀI
5.2.1.4 Body
- th
- - CSs)
- êm
- Các port
an.
h g
- ong Body và
126
BÀI
- nt và ellipsoid
-
tr
Mass Properties
- t
và
127
BÀI
-
có
World.
hay gián t
128
BÀI
y
radian hay vòng quay
- Euler:
- 3-by-3 Transform:
R-1 = RT
- Quaternion:
n = (nx, ny,nz
129
BÀI
5.2.2 Joints
Hình 5.27:
5.2.2.1 Prismatic
Kh
Hình 5.28: ng
Prismatic
130
BÀI
Axes: xác
5.2.2.2 Revolute
Hình 5.30:
BÀI 5: CÁC 131
5.2.2.3 Spherical
ác
uay the
5.2.2.4 Planar
planar
i hai t
Prismatic song song nhau.
132
BÀI
5.2.2.5 Universal
B
Revolute nguyên thu
Hình 5.33:
5.2.2.6 Cylindrical
olute.
Hình 5.34:
5.2.2.7 Gimbal
ng nhau
Hình 5.35:
Sperical.
133
BÀI
5.2.2.9 Weld
do). Hai Bo
Hình 5.36:
5.2.2.10 Telescoping
o qua
Hình 5.37:
5.2.2.11 In-plane
5.2.2.12 Bushing
và 3 k
134
BÀI
5.2.2.13 Bearing
Khi
Hình 5.39:
g
5.2.2.14 Six-DoF
5.2.2.15 Screw
do quay tron
Hình 5.40:
0
scape .
Hình 5.41:
Disassembled prismatic
135
BÀI
Prismatic di
Simscape
Simscape
Simscape
Hình 5.45: ng
Revolute Revolute
Hình 5.46:
Revolute Spherical
Hình 5.47:
Spherical Spherical
Hình 5.48:
137
BÀI
Cho r1, r2
1 r2| = d(t=0) + f(t).
tance Driver
= (t=0) + f(t)
Angl
World Axis:
138
BÀI
5.2.3.4 Velocity
-Curve Constraint.
Breakpoints:
End conditions
Not-a-
B, aF là 2 vector nh
tr
|aBaF| = |aB||aF|
1, r2, 1 , 2 là bán
1 r1 = 2 r2
Hình 5.51:
- Body tr
141
BÀI
Hình 5.53:
- Khô
-
142
BÀI
5.2.3
2f(t)/dt2
i Drive (Distance
Driver, Linear Driver, Velocity Driver) hay quay (Angle Driver).
143
BÀI
m port vào k
Hình 5.56:
-
T
= RTR = I.
144
BÀI
- ismatic
trí,
quay.
Hình 5.57: V ensor
-
Hình 5.58:
i Constraint &
Drive
-
145
BÀI
Hình 5.59:
-
int Sensor.
-
146
BÀI
Hình 5.60:
A, rB là
A rB
Hình 5.61:
-k(x x0 ) bv
Quay: F = -k( 0) b
5.2.6 Ví d
5.2.6.1
- .
- Ground
147
BÀI
C Ground eck
box
Show Machine Environment port Ground Machine
Environment Format > Flip Block Ctrl+I
- Body
148
BÀI
Current base
- spen.mdl.
Machine of model:
spen Model > Body Geometries > Ellipsoids.
152
BÀI
- Body 1:
Thôn Gi
Mass 2.5
Inertia [1.17 0 0;0 1.17 0;0 0 2.25]
CG [0 0 0] World
CS1 [0 0 0] CG
CS2 [0 0 0] - CG
- Body 2:
Mass 2.5
Inertia [0.13 0 0;0 0.13 0;0 0 0.25]
CG [2 0 0] World
CS1 [0 0 0] CG
CS2 [0 0 0] - CG
- Ground 1: Location: [0 0 0]
- Ground 2: Location: [2 0 0]
153
BÀI
- Gear constraint:
à bi
ù SISO
- , bi
ng thái khác.
5.3.1 Mô hình h th ng
Control System
, và
154
BÀI
5.3.1.1 n
tf
zpk -
không
ss
frd
pid
pidstd
pid2 ong song
pidstd2
pade Padé
absorbDelay z=0
thiran Thiran
totaldelay th LTI
delayss
5.24
num = [ 1 0 ]; % T s : s
den = [ 1 2 10]; % M u s : s^2 + 2 s + 10
H1 = tf(num,den);
subplot(311);step(H1);title('Transfer function');
z = 0; p = [-1+3*i -1-3*i]; m c c
k = 1; l i
H2 = zpk(z,p,k);
subplot(312);step(H2);title('Zero-pole-gain');
A = [ -2 -10 ; 1 0 ]; B = [ 1 ; 0 ]; C = [ 1 0 ]; D = 0;
H3 = ss(A,B,C,D);
subplot(313);step(H3);title('State-space');
155
BÀI
s = tf('s'); H1 = s/(s^2+2*s+10);
s = zpk('s'); H2 =
s/(s^2+2*s+10);
156
BÀI
H = H1*H2
H = H1+H2
H = feedback(H1,H2)
H = lft(H1,H2)
feedback
157
BÀI
connect
sumblk c
series
parallel
lft Redheffer
5.25
s = tf('s');
F = 1/(s+1);
G = 100/(s^2+5*s+100);
C = 20*(s^2+s+60)/s/(s^2+40*s+400);
S = 10/(s+10);
T = F * feedback(G*C,S); % Mô hìn
step(T)
Hình 5.64:
158
BÀI
Hình 5.65
step
stepinfo
impulse
initial
lsim tk
lsiminfo
gensig
bode Bode
bodemag
159
BÀI
nyquist Nyquist
nichols Nichols
sigma iá t ng
evalfr
dcgain LTI
5.27
Hình 5.66:
s = tf('s');
H1 = 1/(s+1); H2 = s/(s^2+5*s+100);
H = H2*H1;
t = 0:0.01:4;
u = exp(-t); % Ngõ vào
v = lsim(F,u,t); % Ngõ ra c a H1
y = lsim(G,v,t); % L y ngõ ra H1 -> H2 và tính ngõ ra c a H2
y1 = lsim(H,u,t); % Ngõ ra c a h th
subplot(121);plot(t,y);
subplot(122);plot(t,y1);
160
BÀI
5.28
s = tf('s'); H = (s^2+2*s+3)/(s^3+2*s^2+10);
figure;bode(H);grid
figure;nyquist(H);grid
figure;nichols(H);grid
Hình 5.67:
161
BÀI
5.29
Hình 5.68:
s = tf('s');
G = -(2*s+1)/(s^2+3*s+2);
k1 = 0.4;
G1 = feedback(G*k1,1);
k2 = 0.7;
G2 = feedback(G*k2,1);
k3 = 1;
G3 = feedback(G*k3,1);
step(G1,'b',G2,'r',G3,'g')
legend('k = 0.4','k = 0.7','k = 1')
s = tf('s'); G = -(2*s+1)/(s^2+3*s+2);
162
BÀI
rlocus(G); grid
hình m
và k = 1.6:
163
BÀI
G = zpk(-5,[-1 -2 -3 -4],6,'OutputDelay',1);
Hình 5.73: C
Show Paramters
Hình 5.74:
1.9 6.28
165
BÀI
Hình 5.75:
(Response Time)
0.3.
166
BÀI
Hình 5.76:
và Ki = 0.30314.
167
BÀI
5.32 feedback-
Hình 5.77:
Hình 5.78:
step(Tfb), grid on
title('Response to step change in temperature setpoint T_ {sp}')
ylabel('Tank temperature')
margin(Gp*C), grid
step(Tfb,'b', feedback(ss(Gp*C1),1),'r')
legend('Kc = 1.23','Kc = 0.9')
170
BÀI
Hình 5.79:
Gd = exp(-35*s)/(25*s+1);
F = -(21.3*s+1)/(25*s+1) * exp(-25*s);
Tff = Gp * ss(F) + Gd;
step(Tff), grid
bodemag(Tfb,'b',Tffb,'r--',{1e-3,1e1})
legend('Feedback only','Feedforward + feedback')
172 BÀI 5: C
173
BÀI
Hình 5.81: h
Hình 5.82:
174
BÀI
Hình 5.83: u
Hình 5.84:
175
BÀI
Hình 5.85: -
1.
không.
2. :
3.
5.33:
-
176
BÀI
- (rise time)
- (settling time)
- (overshoot) 0%.
G = tf(1,[1,1]);
controlSystemDesigner(G);
Hình 5.86:
Hình 5.87:
OK
Hình 5.88:
Hình 5.89:
-2.
l Bode Editor,
179
BÀI
Hình 5.90: n
180
BÀI
TÓM T
T
MatLab.
ystem
dùng cho sinh viên ngàn -
-
-
-
-
-
Câu 3: Dùng công c Simscape Multibody mô ph ng c ch 4 thanh sau:
181
BÀI
au:
2. Won Y. Yang, Yong S. Cho, Won G. Jeon, Jeong W. Lee, Jong H. Paik, Jae K. Kim,
Mi-Hyun Lee, Kyu I. Lee, Kyung W. Park, Kyung S. Woo, MatLab/Simulink for
Digital Communication, A-Jin Publishing Co.
3. Bill Messner, Dawn Tilbury, Control Tutorials for MATLAB and Simulink, Addison-
Wesley Publishing Company, Inc.