Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 1-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

EE-384: Digital Signal Processing Spring 2020

Laboratory 1: Introduction to MATLAB


Instructor: Mr Ammar Naseer EE UET New Campus

Aims

This laboratory session illustrate some of fundamental MATLAB commands


related to
 Arithmetic operators.
 Vector and matrix manipulation.
 Symbolic math
 Script file (m-file) and user-defined functions

Pre-Lab:
Operations on variables and plotting graphs in MATLAB
On a PC that is installed with MATLAB, start MATLAB. A command window will
appear where one can type in and execute MATLAB commands. Execute the set
of commands/codes in the boxes and check the results. This self-study method is
one of the fastest ways to master the basic MATLAB commands.

In a report, document what each command does. Focus on the specific actions
and purposes, rather than the execution results. For commands that return an
error message, document the reasons.

1.A. Operation of scalar variables


1. X2 6. X*Y-X*3-Y 11.X=12e6
2. X=12 7. X=Yˆ2 12.clc
3. X=X+2 8. Z=sqrt(Y) 13.x=rand
4. Y=X+3 9. X=2; Y=4; Z=X+Y 14.x=rand
5. Y*6 10.Z=XˆY 15.help rand

Page 1
Laboratory 1: Introduction to MATLAB 1.2

In addition, explain why the same command executed twice in item 13 and item
14 generates different results.

1.B. Operation of complex numbers


1. I 6. Z=X*Y 11. angle(Z)
2. j 7. real(Z) 12. who
3. X=1+3*j 8. imag(Z) 13. whos
4. Y= -2+j; 9. conj(Z) 14. clear
5. Z=X+Y 10. abs(Z) 15. who

1.C. Operation of vectors.


1. X=2 : 2 : 10 12. Y=[ 2; 1; 4; -3] 23. Y=rand(1,5)
2. Y=1 : 5; 13. Z=Y' 24. Y=rand(4)
3. Z=X + Y 14. Z(1) 25. Y=[7 3 -1 2]
4. Z=X.*Y 15. Z(2) 26. mean(Y)
5. Z=X*Y 16. Z(1:3) 27. var(Y)
6. Z=X./Y 17. Z(2:4) 28. min(Y)
7. Z=X/Y 18. length(Z) 29. max(Y)
8. 2*Y 19. X=[2 4 8 16] 30. [a b]=min(Y)
9. Z=0 : 10 20. Y=log2(X) 31. sort(Y)
10. sum (Z) 21. Yˆ2 32. Y=[Y 5]
11. Y=[ 2 1 4 -3] 22. Y.ˆ2 33. Z=[ Y(3:4) X(1:2) ]

1.D. Operation of matrices.


1. X=[3 6 -2 -1;0 5 2 1; 7 -1 4 8]; 11. Y(1,:)=X(2,:) 21. Z=X.ˆ2+3*Y
2. X(2,1) 12. Y(2,:)=X(1,:) 22. max(Z)
3. X(2,3) 13. Y(3,:)=[1 2 3 4] 23. mean(Z)
4. X(1,:) 14. Z=X + Y 24. mean(Y)
5. X(:,2) 15. Z=X*Y 25. max(mean(Z))
6. X(1:2,:) 16. Z=X*Y' 26. min(Y)
7. X(:,2:3) 17. Z=X.* Y 27. max(max(Z))
8. Y=[1 0 2;3 2 1; 2 3 4] 18. Z=Xˆ2 28. X=inv(Z)
9. Y' 19. Z=X.ˆ2 29. Y=X*Z
10.Y=zeros(3,4); 20. Z=2.ˆX 30. size(Z)
Laboratory 1: Introduction to MATLAB 1.3

1.E. Plotting some basic functions.


1. x=0:0.1:10 9. plot(x,y2) 17. plot(x,y1)
2. y1=sin(x) 10. y3=exp(-x) 18. subplot(3,1,2)
3. y2=cos(x); 11. plot(x,y3,'r') 19. plot(x,y2)
4. plot(x) 12. legend('sin(x)', 'cos(x)','exp(-x)') 20. subplot(3,1,3)
5. plot(y1) 13. axis([-5 15 -3 3]) 21. plot(x,y3)
6. plot(x,y1) 14. axis([0 10 -2 2]) 22. semilogy (x,y3)
7. grid 15. figure 23. help plot
8. hold on 16. subplot(3,1,1) 24. help semilogy
Compare the output of command 6 to 5.

1.F. Boolean operations and plotting graphs over a limited range of the
x axis.
1. A=[0 1 2 3 4]; 7. C=([1 0 1 1 1]== [1 0 1 0 13. y=(1<x)&(x<4);
2. A<3 0]) 14.plot(x,y); axis([0
3. B=(A>2) 8. C=([1 0 1 1 1] ≠ [1 0 1 0 10 -2 2]); grid;
4. C=([1 1 0 0] & [ 1 0]) 15.y=1<x<4;
1 1 0]) 9. x=0:0.01:10; 16.plot(x,y); axis([0
5. C=([1 1 0 0] | [ 1 10.y=(x<3); 10 2 2]); grid;
1 1 0]) 11.figure 17.plot(x,y); axis([0
6. C=~ [1 0 1 0 0] 12.plot(x,y); axis([0 10 -2 10 -2 2]); grid;
2]); grid;
In addition, explain the difference between items 13 and 15.

Main Lab
Using Symbolic Math
In the symbolic math in MATLAB, the characters (or words) such as a, b, and temp
are treated as symbolic variables, not numeric variables. Mathematical
expressions can be computed or manipulated in symbolic forms. Find out what
else can be done using symbolic math in the following problems.
Laboratory 1: Introduction to MATLAB 1.4

2.A. Write down what each of the lines in the following box does and capture the
execution result.
>>syms a b c x t
>>y=sin(t);
>>diff(y)
>>int(y)
>>int(y, t, 0, pi)
>>z=int(xˆ2*exp(-x),x,1,3)
>>double(z)
>>limit(sin(t)/t,t,0)
>>symsum(xˆ2, x,1,4)
>>T=solve(a*xˆ2+b*x+c,x)
>>T2=solve(a*xˆ2+b*x+c,b)
>>a=1;b=2;c=3;
>>z=eval(T)
>>a=t;
>>z=eval(T)

2B. Verify the following quantities by using the symbolic math. Capture the
calculation results.

∫ √

∑( ) ( )

( )

Creating and Using a Script File (m-File)


The commands and functions we have covered so far are all executable directly in
the command window. Using a "script file," which is also called an "m-file" in the
earlier versions of MATLAB, users can execute various algorithms or can
implement user-defned functions.
Laboratory 1: Introduction to MATLAB 1.5

3.A Follow the steps below and learn how to create and execute an m-file.
Step 1. Open a new script file editing window.
Step 2. Shown in the box below is an m-file that plots y = x sin(ax), for the
cases of a = 0.1, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7 and 0.8 over the range 0 < x < (10 + D),
where D = the last digit of your student ID number. Write this m-file and save it as
Lab1 3A.m. The m-file name must begin with a letter, files with a name that
begins with a number will not executed in MATLAB. Be sure not to use a space or
mathematical operator in the file name.
clear
x=0:0.1:(10+The last digit of your student ID number);
for n=1:8
a=n/10;
if (a==0.2)
a=0.25;
end
y(n,:)=x.*sin(a*x);
end
plot(x,y)
xlabel('x')
ylabel('y=x sin(ax)')
legend('a=0,1','a=0.25','a=0.3','a=0.4','a=0.5','a=0.6','a=0.7','a=0.8')
grid on

3.A-1 Add a comment to explain each line in the m-file. Capture the commented
m-file.

3.A-2 Execute the m-file you have created. You can either click 'run' button in the
menu bar of the m-file editor or press the F5 key on the keyboard or type the m-
file name in the command window as

>>CH1 3A
Laboratory 1: Introduction to MATLAB 1.6

3.A-3 Execute the following commands in the MATLAB command window. Based
on the results, document the meanings of the two variables. Do not capture the
execution results.

>>x
>>y

3.B Let us write an m-file to plot sine waveforms of 10 different frequencies by


properly modifying the m-file created in 3.A

3.B-1 Consider 10 sine waveforms whose frequencies are 1; 2; 3; 4; ; 10 Hz. In


your code, calculate the smallest period (the highest frequency) among these
waveforms and denote it by T. Then, overlay the 10 sine waveforms in the range
of -2T < t < 2T, the range of the time axis (x axis) in the graph. Use legend() to
label the 10 waveforms. Use a 'for' loop as done in the m-file in 3.A. Capture the
m-file and the execution result.

User-Defined MATLAB Functions


Similar to many other programming languages, MATLAB also supports the use of
user- defined functions to avoid repeatedly editing the main body of a code. User-
defined functions are similar to the built-in MATLAB commands or functions; they
follow certain syntax and are normally saved in the same folder where the main
m-file is located in, but it can be saved in a different folder. Through the following
problem you will learn how to write and to use user-defined MATLAB functions.

4.A Let us write a MATLAB function that converts a number in linear scale into dB
scale. In MATLAB, open the script file editor and write the following m-file. Save
the m-file as lin2dB.m (if you click "save," the default file name will be lin2dB.m).

function xdB=lin2dB(x)
xdB=10*log10(x);

4.B Let us write a MATLAB function that plots the Gaussian probability density
function.
Laboratory 1: Introduction to MATLAB 1.7

4.B-1 Write the following m-file and save it. Add your comments explaining what
each line does or means.

function plot gaussian(m, v)


x=m+sqrt(v)*(-5:0.01:5);
fx=1/sqrt(2*pi*v)*exp(-(x-m).ˆ2/(2*v));
plot(x,fx)

4.B-2 Execute plot.gaussian(0,1) in the command window and capture the result.

4.C Write a user-defined function swap(A,row0col1,c,d) that swaps two rows (or
columns) of a matrix. If row0col1 is 0, then swap(A, row0col1,c,d) swaps the c-th
row and the d-th row of a matrix A and returns the swapped matrix. If row0col1 is
1, then swap(A,row0col1,c,d) swaps the c-th column and the d-th column of a
matrix A and returns the swapped matrix.

4.C-1 An incomplete version of swap.m is provided below. Complete all parts


marked by ' ?' and add a comment for each line you are completing.

function e=swap(A,row0col1,c,d)
e=A;
if row0col1==0
e(d,:)=A(c,:);
e(?,:)=A(?,:);
end
if row0col1==1
??;
??'
End

4.C-2 Execute the following command lines and capture the results. Check
whether or not your swap function works correctly.

>>x=rand(4,5)
>>y=swap(x,0,2,4)
>>z=swap(y,1,5,1)
Laboratory 1: Introduction to MATLAB 1.8

Rubrics
Punctuality Rubrics No participation Average Excellent
(0 points) (1 point) (2 points)
Attendance T> 15min 07<T<15 min T< 7min
Reaching time Or Absent

No participation Unsatisfactory Fair Good Excellent


(0 points) (1 point) (2 points) (3 points) (4 points)
Most of the Most of the All task
Absent or
main lab task Major flaws in code implemented
Code Function Matlab Code not
not code function implemented with proper
implemented
implemented with minor flaws understanding
Results
Result not
clear due to Have no Major results Graphical axis
Proper results
snippet tool Absent knowledge showing wrong and title not
about results answers defined
implemented
Graphical axis
and title not
defined

Rule violation Any rule violation would be given -1 mark each time
To be observed each time and would be deducted from the total marks earned.

You might also like