Download presentation
Presentation is loading. Please wait.
Published byLorin Cunningham Modified over 8 years ago
1
MATLAB Programming
2
Why MATLAB is used ? It can use a simple instruction to compute a very complex mathematical function. It provides high resolution two dimensional and three dimensional graphic plot functions for signal analysis.
3
Why MATLAB is used ? It can play the sound and display an image in a personal computer or a workstation locally or from the computer network with the X window interface. MATLAB programs are portable in many computer systems.
4
Why MATLAB is used ? It provides interface with the programming language C to enhance its programming ability and execution speed. We can write a simple program easily to simulate an application which might takes many hours or many days in C or Fortran.
5
1.MATLAB is very suitable for signal processing. MATLAB programs is portable to any computer if it is installed with MATLAB and its tool-boxes. Once you initiate MATLAB software you see a prompt >> to indicate that MATLAB is waiting for your command. Each command can be interpreted as soon as you type in. To understand a command you can type help name, where name is the name of a command. For example, >> help sinc
6
MATLAB will output the information about the sinc function. A variable in MATLAB can be a scalar, a vector or a matrix; For example, >> x = 3 x = 3 >> y = 1:5 y = 1 2 3 4 5
7
>> z=[1:3;4:6;7:9] z = 1 2 3 4 5 6 7 8 9
8
2.To understand the variables in the MATLAB environment you can type whos to access their information. For example, >> whos Name Size Elements Bytes Density Complex x 1 by 1 1 8 Full No y 1 by 5 5 40 Full No z 1 by 9 9 72 Full No Grand total is 15 elements using 120 bytes
9
3.MATLAB provides the following arithmetic operators to compute the arithmetic operations between a and b. There are several cases that
10
the following operations are valid: (a) both a and b are scalar. (b) a is a vector or a matrix and b is scalar. (c) both a and b are vectors or matrixes with the same dimension.
11
For example, >> a = 5; >> b = 2; >> a+b ans = 7 >> z z = 1 2 3 4 5 6 7 8 9
12
>> a+z ans = 6 7 8 9 10 11 12 13 14 >> a*z ans = 5 10 15 20 25 30 35 40 45
13
>> z*z ans = 30 36 42 66 81 96 102 126 150 >> z^2 ans = 30 36 42 66 81 96 102 126 150
14
4.MATLAB provides the following logic operators to compute the logic operations between a and b, where a and b must have the same dimension. >> a = [0 1] a = 0 1
15
>> b = ~a b = 1 0 >> a & b ans = 0 0 >> a | b ans = 1 1 >> a == b ans = 0 0
16
5.MATLAB provides the following array operators to compute the array operations between a and b, where a and b must have the same dimension. a = 1 2 >> b = [2,3] b = 2 3
17
>> a.^ b ans = 1 8 >> a.* b ans = 2 6 >> a./b ans = 0.5000 0.6667
18
The following program plots 1 Hz cosine signal cos(2 t). Actually, it’s digital signal. However, we can look it as a real-value analog signal. t = - 1:0.01:1; y = cos(2 * pi * t); fig; plot(t,y); xlabel(‘t’); ylabel(‘f(t)’); set(gca, ‘Ylim’,[ - 2,2]); print -deps cos1
19
Suppose the filename of the program is cos1.m you type cos1 to execute the program. For example, >> cos1
20
6.MATLAB provides the following instructions for triangular, hyperbolic, exponential and logarithmic functions,
22
Note that the function atan2(b,a) requires two variables and returns a value in [ - , ] and atan(t) just requires one variable returns a value in [ - /2, /2]. atan( - 0.5) ans = - 0.4636 atan2( - 1,2) ans = - 0.4636
23
atan2(1, - 2) ans = 2.6779 atan(0.5) ans = 0.4636 atan2(1,2) ans = 0.4636 atan2( - 1, - 2) ans = - 2.6779
24
7.MATLAB provides the following special operations to for a complex valued signal.
25
8.MATLAB provides the following special operations to round a real number.
26
9.If a complex can be computed in MATLAB. For example, a = sqrt( - 1) a = 0 + 1.0000i exp(a) ans = 0.5403 + 0.8415i
27
10.The instruction save test store all variables in the file called test.mat. The instruction load test retrieves all variable from test.mat x = 1 y = 2 z = 3 % Save all variable in test.mat save test % Clear the workspace clear
28
% Check all variables are retrieved or not ? load test x y z % Save x and y in test.mat save test x y % Clear the workspace clear % Check all variables are retrieved or not ? load test x y
29
11.A sound file can be stored in the mat-file. Usually, it has two variables Fs and y, where Fs is the sampling rate and the variable y contains the sound signal. To load the sound file sound.mat and plot and play the sound in the following program can be used: load sound % The duration of the sound in second t = (1:length(y))/Fs; % Plot the sound Plot(t,y)
30
% Play the sound sound(y,Fs) In the Unix system there are several mat-files in the directory called /usr/local/matlab4.2c/to These sound files can be retrieved and played.
31
12.The contents of the current figure can be saved as a file with the following instruction: (a) print -dps filename (b) print -dpsc filename (c) print -deps filename (d) print -depsc filename to save a black and white postscript file, a color postscript file, a block and white encapsulated postscript file and a color encapsulated postscript file.
32
13.The instruction ezplot(f) plots the function f, where f is a symbolic expression with the variable t and t [ - 2 , 2 ]. The instruction ezplot(f,[t 1,t 2 ]) plots the function f(t), where t [t 1,t 2 ]. The instruction ezplot(f, [t 1,t 2 ],fig) plots the function f(t), where t [t 1,t 2 ], with the figure number fig. f = ‘x^3’ ezplot(f); ezplot(f,[0,1000])
33
14.The absolute value of x can be computed as y = ‘(x^2)^(1/2)’ ezplot(y)
34
15. The function tri(t), rec(t), u(t), f(t)= |t| can not be defined symbolically. Therefore, it is difficult to use the instruction ezplot to plot these function. In the following program listing there are three main programs called unit.m, rec1.m and tri1.m and there are two functions called rec.m and tri,m.Figure 1.7, 1.8 and 1.9 are plotted by the commands unit, rec1 and tri1, respectively.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.