Professional Documents
Culture Documents
1. Basics:
1.) Scalar computation:
d = 36*12 + 120;
b=2^5;
b+d
2.) Matrices:
A=[1,2,3; 4,5,6;7,8,9]
A(1,1)
A(3,4)
A(2,1)=0
A
inv(A)
3.) Arrays:
x=[1 2 3]
y=[4; 5; 6]
y(3)
y(3)=0
A*y
4.) Element-wise operations:
a=[1 2 3]
b=[2 3 4]
a.*b a./b a*b
5.) Colon notation:
a=1:10
a=1:2:10
6.) Some special matrices:
A=eye(6);
B=ones(5,3);
C=zeros(4,5);
Exercise:
Exercise 2:
Using Matlab to compute 1^3 + 3^3 + + (2k+1)^3 + 99^3
2. Signals and Systems
1.) How to define a signal. Signal is a function of time.
Example, x(t)=sin(t).
Define a vector for time t, and then compute the vector for x.
t=[-10:0.01:10];
x=sin(t);
y=cos(t);
figure;
hold on;
plot(t,x);
plot(t,y,r);
grid;
xlabel(t);
ylabel(x and y);
title(sin(t) and cos(t));
legend(sin(t), cos(t));
2.) Give an input and a system, find the output using Matlab
y(t)=[x(t)]^2
x(t)=U(t)
t=-1:0.01:1
x=[];
for index=1:length(t)
x=[x 2*unit_step(t(index))];
end
y=x.^2;
figure;
hold on;
plot(t, x);
plot(t,y,r);
Exercise:
grid;
xlabel('t');
ylabel('x(t)');
subplot(3,1,2);
plot(t,h);
axis([-5 5 -2 2]);
grid;
xlabel('t');
ylabel('h(t)');
y=0.001*conv(x,h);
subplot(3,1,3);
plot([-10:0.001:10], y);
axis([-5 5 -2 2]);
grid;
xlabel('t');
ylabel('y(t)');
Exercise:
Please change x(t) to x(t)=t for 0< t<1, 2-t for 1<t<2, and 0
otherwise. Plot the output.
4.) Frequency domain analysis and filtering
xlabel('t');
ylabel('h(t)');
h_w=fft(h);
H_w=fftshift(h_w);
w2=[-(length(H_w)-1)/2:(length(H_w)-1)/2]/length(H_w)*2*pi*1/Ts;
subplot(2,1,2);
plot(w2,abs(H_w));
axis([-400 400 0 2]);
grid;
xlabel('\omega');
ylabel('|H(i\omega)|');
% After filtering
figure;
subplot(3,1,1)
hold on;
plot(w1,abs(X_w));
plot(w2,abs(H_w),'r');
legend('|X(i\omega)|', '|H(i\omega)|');
axis([-400 400 0 10]);
y=conv(x,h);
subplot(3,1,2)
plot([0:99]*Ts, y(1000:1000+99));
axis tight;
Y_w=fft(y);
Y_w=fftshift(Y_w);
xlabel('t');
ylabel('y(t)');
w3=[-(length(Y_w)-1)/2:(length(Y_w)-1)/2]/length(Y_w)*2*pi*1/Ts;
subplot(3,1,3);
plot(w3,abs(Y_w)/800);
axis([-400 400 0 10]);
xlabel('\omega');
ylabel('|Y(i\omega)|');