Professional Documents
Culture Documents
1. Introduction
2. Basics for MATLAB
3. Signal Basics in MATLAB
2 Projects:
Four students in the same class form a group
Submit one report per project for each group before the deadline
Submit your report through the Blackboard System
请不要迟到,做完了可以提前离开
迟交报告一律不计分
平常上课老师会随机提问
Script Window
(脚本窗口) Workspace
(工作区)
Current Folder
(当前文件夹)
Command Window
(命令行窗口)
%用plot函数画一个方波 x(t)
t=[-1 0 0 1 1 3]; 1
x=[0 0 1 1 0 0];
plot(t,x); t
xlabel('t'); 0 1
ylabel('x(t)');
axis([-1 3 0 2]);
① 直接在命令行窗口一条一条的输入命令
② 建一个名字为 example.M 的文件,然后在命令行
窗口输入文件名回车运行
An array x = [1 5 3 4 2 6] or x = [1,5,3,4,2,6]
1 3 5 C(:,:,1) = C(:,:,2) =
3 6 7
0 5 8 0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
A= ans = ans =
k = 10:-0.5:6
t = 0:8
k=
t=
10.0000 9.5000 9.0000 8.5000
0 1 2 3 4 5 6 7 8 8.0000 7.5000 7.0000 6.5000
6.0000
B= B(1,1) = 1 x=
B(1,3) = 3
1 2 3 0 0.5000 1.0000 1.5000 2.0000
6 5 4
4 5 6 B(:,1) zeros 产生矩阵元素全为0的矩阵
ones 产生矩阵元素全为1的矩阵
B(3,:) ans = eye 产生单位矩阵
rand 产生(0,1)均匀分布随机数矩阵
ans = 1 randn 产生正态分布随机数矩阵
6
4 x = rand(1,3)
4 5 6
x=
1 3 5 8 ans =
>> B = [x;y] 2 2
B=
>> length(B)
1 3
5 8 ans =
Single
MSB LSB
single(a) 01000110001111100111100000000000
single(b) 10110111010011001000010000000000
x=
>> x = 2+ 3*i
1.6294
x=
>> y = rand(1)*3
2.0000 + 3.0000i
y=
'myname@mydomain.com'
X= Y= Z= T=
4 7 5 -2 -3 1 22 29 45 1 4 7
9 8 14 -1 2 -2 55 71 102 2 5 8
10 14 18 4 2 0 88 113 159 3 6 9
1 2 3 3 5 2
4 5 6 5 3 8
7 8 9 3 6 9
Z= ans = ans =
>> who
>> whos
>> clear
>> clc
>> dir
>> pause
M文件的第一行包含function
功能: 建立一个函数,可以同MATLAB的库
函数一样使用
x(t)
1
例:编一个绘制图示波形的函数
t
1、先编写一个与函数同名的M文件 -1 0 1
function myFunction(x)
Statement 1 Condition ?
Condition ?
YES NO YES
Statement 2
Statement 1 Statement 1
Statement 2
Statement 3
s = 0;
n = 1;
eps = 1e-6;
while 1/(n*n) > eps
s = s+1/(n*n);
n = n+1;
end
fprintf ('s=%.5f\n',s)
t = linspace(0,4*pi,512);
plot(t,sin(t),t,cos(t),'-.');
title('my figure');
xlabel('t');
legend('sin(t)','cos(t)');
k = 0:39;
stem(k,cos(0.9*pi*k));
title('cos(0.9\pik)');
syms t;
f=sym(cos(pi*t/2)*[heaviside
(t)-heaviside(t-4)]);
fplot(f,[-2,8]);
sin 𝜋𝑡
𝑠𝑖𝑛𝑐 𝑡 = 𝑡≠0
𝜋𝑡
1 𝑡=0
Help rectpuls
% Generate a 10 Hz square wave
with duty cycle 50%.
t = 0:.0001:0.5;
y = square(2*pi*10*t,50);
plot(t,y);
axis([0,0.5,-1.2,1.2]);
Help square
Help tripuls
t = 0:7;
t1 = -10:15;
f = [zeros(1,10),t,zeros(1,8)];
stem(t1,f);
axis([-10,15,0,10]);
grid on;
𝑥 𝑛 = (−1/2)𝑛 𝑢[𝑛]
n = -3:6;
y = (n>=0);
x = (-0.5).^n.*y;
stem(n,x,'filled')
𝝅
𝒙𝟏 [𝒏] = 𝒆𝒙𝒑 𝒋 𝒏 , 𝒙𝟐 [𝒏] = 𝒆𝒙𝒑 𝒋𝟑𝒏
𝟔
𝒙 𝒕 = 𝑒 𝒋𝝎𝟎 𝑡 𝒙 𝒏 = 𝑒 𝒋𝝎𝟎 𝒏
𝑒 𝒋𝝎𝟎𝑡 ≠ 𝑒 𝒋𝝎𝟏 𝑡 (distinct signals) if 𝑒 𝑗𝜔0 𝑛 = 𝑒 𝑗𝜔1𝑛 (identical signals) if
𝜔0 ≠ 𝜔1 𝜔1 = 𝜔0 + 2𝑘𝜋
2𝑘𝜋
𝑥 𝑡+ = 𝑥(𝑡) periodic for any 𝑥 𝑛 + 𝑘𝑁 = 𝑥[𝑛] periodic only if
𝜔0
𝜔0 = 2𝜋𝑚/𝑁 for some integers N
choice of 𝜔0 and m
Fundamental frequency 𝜔0 Fundamental frequency 𝜔0 /𝑚
Fundamental period: Fundamental period:
𝜔0 = 0: undefined 𝜔0 = 0: undefined
𝜔0 ≠ 0: 2𝜋/𝜔0 𝜔0 ≠ 0: 2𝜋𝑚/𝜔0
% Call stepfun
T = -5:0.01:5; % Use symbolic math function
T0 = 2; syms t;
3 x = stepfun(T,T0); 4 x = heaviside(t-2);
plot(T,x); fplot(x);
axis([-5,5,-0.1,1.1]); axis([-5,5,-0.1,1.1]);
n = -10:10;
y=[zeros(1,10),ones(1,11)];
stem(n,y);
% u[n-2]
n = -10:10;
n0 = 2;
u = ((n-n0)>=0);
stem(n,u);
1 𝑛=0
𝛿𝑛 =
0 𝑛≠0
% Call gauspuls
n = -10:10;
y = gauspuls(n-2);
stem(n,y);
n = -10:10;
n0 = 2;
y = ((n-n0)==0);
stem(n,y);
1
(1)
t 0 t
0
f (t ) (t ) f (0) (t )
t
(1) (6) -
( )d u (t )
d
u (t ) (t )
(2) -
(t ) f (t )dt f (0) (t )dt f (0)
-
(7)
dt
0 t0
(3) f (t ) (t - t0 ) f (t0 ) (t - t0 ) (8) f (t )u (t )
f (t ) t 0
(4) -
(t - t0 ) f (t )dt f (t0 )
(9)
t
( - t0 )d u (t - t0 )
-
(5) (t ) (-t ) d
(10) u (t - t0 ) (t - t0 )
dt
Help subs
n = -6:6;
x1 = (n+3).*((n>=-2)-(n>=1))
+3.*((n>=1)-(n>=4));
subplot(2,2,1);stem(n,x1,'.');
title('x[n]');
x2 = fliplr(x1);
subplot(2,2,2);stem(n,x2,'.');
title('x[-n]');
n3 = n + 2;x3 = x1;
subplot(2,2,3);stem(n3,x3,'.');
title('x[n-2]');
n4 = n -2;x4 = x1; Help fliplr, flipud
subplot(2,2,4);stem(n4,x4,'.');
title('x[n+2]');
1
odd part of signal 𝒇(𝒕) 𝑓𝑜 𝑡 = 𝑓 𝑡 − 𝑓 −𝑡
2
f e (t ) f e (-t ) f o (t ) - f o ( -t )
𝑓 𝑡 = 𝑓𝑒 𝑡 + 𝑓𝑜 𝑡 𝑓 −𝑡 = 𝑓𝑒 𝑡 − 𝑓𝑜 𝑡
fs = 10000; t = -2:1/fs:2; w = 1;
x = tripuls(t,w);
plot(t,x,'b-'); hold on;
x1 =diff(x)*fs;
plot(t(1:length(t)-1),x1,'r-.');hold on;
syms s;
x =sym((2*s+1).*(heaviside(s+0.5)-heaviside(s))+(1-
2*s).*(heaviside(s)-heaviside(s-0.5)));
y = int(x);fplot(y,'m');
axis([-1,1,-2.5,2.5]);
Help int