Matlab-Signals and Systems
Matlab-Signals and Systems
Outline
Introduction and where to get MATLAB Data structure: matrices, vectors and operations Basic line plots File I/O
HSEAS IT
Maxwell Dworkin Rooms G107-G111
Mathworks:
Student version is affordable and complete.
What is MATLAB
High level language for technical computing Stands for MATrix LABoratory Everything is a matrix - easy to do linear
algebra
MATLAB Desktop
Menu and toolbar
Workspace
History
Command
mn
Transpose
Vector :
>> a=[1 2 3]; >> a' 1 2 3
Matrix:
>> A=[1 2; 3 4]; >> A' ans = 1 3 2 4
Creating Vectors
Create vector with equally spaced intervals
>> x=0:0.5:pi x = 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
Creating Matrices
zeros(m, n): matrix with all zeros
ones(m, n): matrix with all ones. eye(m, n): the identity matrix
have the same sum, along the row, column and diagonal. pascal(m) : Pascal matrix.
Matrix operations
^: exponentiation
*: multiplication /: division
\: left division. The operation A\B is effectively the same as INV(A)*B, although
Array Operations
Evaluated element by element
.' : array transpose (non-conjugated transpose) .^ : array power .* : array multiplication ./ : array division
But:
>> A.*B 5 21 12 32
Indexing Matrices
Given the matrix:
A = m 0.9501 0.2311 n 0.6068 0.4860 0.4231 0.2774
Then:
A(1,2) = 0.6068 A(3) = 0.6068
index (i 1)m j
] 0.2311
0.4231]
A(1,2:3)=[0.6068
Graphics - 2D Plots
plot(xdata, ydata, marker_style);
For example:
>> x=-5:0.1:5; >> sqr=x.^2; >> pl1=plot(x, sqr, 'r:s');
Gives:
Graphics - Annotation
Use title, xlabel, annotation
>> title('Demo plot'); >> xlabel('X Axis'); >> ylabel('Y Axis'); >> legend([pl1, pl2], 'x^2', 'x^3');
ylabel
Graphics - Annotation
Graphics-Stem()
stem()is to plot discrete sequence data The usage of stem() is very similar to plot()
cos(n/4) 1
0.5
-0.5
-1 -10
-5
0 n
10
subplots
Use subplots to divide a plotting window into several panes.
Cosine 1 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 Sine
>> >> >> >> >> >> >> >> >> >>
x=0:0.1:10; f=figure; f1=subplot(1,2,1); plot(x,cos(x),'r'); grid on; title('Cosine') f2=subplot(1,2,2); plot(x,sin(x),'d'); grid on; title('Sine');
10
10
Save plots
Use saveas(h,'filename.ext') to save a figure to a file.
>> >> >> >> >> >> >> f=figure; x=-5:0.1:5; h=plot(x,cos(2*x+pi/3)); title('Figure 1'); xlabel('x'); saveas(h,'figure1.fig') saveas(h,'figure1.eps')
Useful extension types: bmp: Windows bitmap emf: Enhanced metafile eps: EPS Level 1 fig: MATLAB figure jpg: JPEG image m: MATLAB M-file tif: TIFF image, compressed
Workspace
Matlab remembers old commands And variables as well Each Function maintains its own scope The keyword clear removes all variables from workspace The keyword who lists the variables
File I/O
Matlab has a native file format to save and load workspaces. Use keywords load and save. In addition MATLAB knows a large number of popular formats. Type help fileformats for a listing. In addition MATLAB supports C style low level file I/O. Type help fprintf for more information.
Practice Problems
Plot the following signals in linear scale
x(t ) sin(3t ) y(t ) e 2t 3 5 t 5 0t 5
Plot the real part and imaginary part of the following signal
x(t ) e 0.5t j ( t / 3) 0 t 10
For the signal in previous question, plot its phase and magnitude