Professional Documents
Culture Documents
Introduction To DSP Simulations in MATLAB PDF
Introduction To DSP Simulations in MATLAB PDF
version
Lecture: Introduction to
DSP simulations in MATLAB
Konstantin Rykov
konstantin.rykov@tut.fi
• Why you’re at this lecture/lab?
• MATlab is a tool
11.9.2012 2
Сontents
BASICS OF MATLAB
• Mainwindow. How to make m-file? How to save m-file?
• Some basic hints
• Main MATLAB objects (commands, variables…)
• Main operation symbols
• Operation symbols
MATLAB IS AN ADVANCED CALCULATOR
• Complex numbers
• HELP
• Vectors
• Matrices
2D GRAPHS
• Main MATLAB functions for plotting graphs
• General rules of forming graphs
• Main tools of staging graphs
• Controlling graph properties
• LineSpec parameters
OUTER FUNCTIONS IN MATLAB
11.9.2012 3
Сontents
DISCRETE SIGNALS IN MATLAB
• Sequences
• Unit sample sequence, unit step sequence, discrete exp
• Discrete complex harmonic signal
• Functions max, sum and prod
• Generation of signals: rectpuls, tripuls, gauspuls, sinc, square, sawtooth, diric
• Functions rand(1,N) and randn(1, N)
11.9.2012 4
BASICS OF MATLAB
The main MATLAB window
11.9.2012 5
Some basic hints
• help <name> (for example: >> help cos)
• ; blocks automatically output of the variables
• % makes a comment
• to comment a few rows hold Ctrl+R
• to uncomment a few rows Ctrl+T
• Always use: clc,clear all; close all;
TASK
• Type in Editor:
• ===============
• My MATlab Crib
• ===============
• Use CTRL+R to comment it
• clc; clear all; close all;
11.9.2012 6
Main MATLAB objects
11.9.2012 7
Main operation symbols
Symbol Operation
+ Summation
- Difference
* Multiplication of matrixes
.* Multiplication of elements
/ Right division
.’ Transposing
11.9.2012 8
MATLAB IS AN ADVANCED
CALCULATOR
Complex numbers
11.9.2012 9
Use help to find what these commands do
• abs
• angle
• exp
• conj
11.9.2012 10
Type and simulate
• z=3+4i
• r=abs(z)
• fii=angle(z)
• r*exp(i*fii)
• zk=conj(z)
• z*zk-r^2
11.9.2012 11
Vectors
11.9.2012 12
Matriсes
11.9.2012 13
Task
• Try to find an easy way to build a 7*8-matrix whose other entries are
zeros, but in its diagonal and its last column are 5s
11.9.2012 14
• Determine whether the given sets of vectors are linearly
independent/dependent:
11.9.2012 15
2D GRAPHS
Main MATLAB functions for plotting graphs
Function Meaning
plot (x1, y1, x2, y2,…) Linear graphics
stem Sequence graphs
stairs Stairs graphs
loglog Both Logarithmic axis Im and Re
semilogx Logarithmic Re axis
semiloxy Logarithmic Im axis
11.9.2012 16
General rules of forming graphs
• hold off
11.9.2012 17
Generate x=[1 20 3 15 18];
• plot
• stem
• plot
• stem
• stairs
11.9.2012 18
Use semilogx, semilogy, loglog to plot graphs of the following
functions:
1. y=3x^5
2. y=3^(5x-2)
3. y=log10(3x^4)
11.9.2012 19
Main tools of staging graphs
Function
grid
title(‘<text>’)
xlabel (‘<text>’)
ylabel (‘<text>’)
Legend (‘<funct1>’,’<funct2>’,..,Pos)
11.9.2012 20
Generate x1=0:pi/8:8*pi; y1=sin(x1);
• 3rd graph: do the same, but limit Re axis and Im axis by using
xlim([-15 15]) and ylim([-1.5 1.5])
For all graphs: make a grid, title and give names for both axis
Generate y2=0.5*sin(2*x1);
plot(x1,y1,x1,y2),legend(‘sin(x1)’,’0.5sin(2x1)’);
11.9.2012 21
Controlling grpah properies
• stem(x1,y1,…,LineSpec,’fill’,’MarkerSize’,3);
11.9.2012 22
LineSpec parameters
S1 S2 S3
r Red - +
b Blue : *
g Green -. s Square
w White -- d Diamond
k Black (none) v
y Yellow ^
m Magenta <
c Cyan >
p Pentagram
h Hexagram
11.9.2012 23
• Form a vector y = [0 1 2 3 4 5 6 7 8 9]; line width is 2,
use squared black markers, dotted line
11.9.2012 24
OUTER FUNCTIONS IN MATLAB
Function file – is a M-file, which generates outer function
11.9.2012 25
• If you have a few parameters
function [z, p] = F1(x,y)
% Sum of cubes z
% Square root p
z=x.^3+y.^3;
p=sqrt(abs(z));
end
• If you have one parameter
function z = F2(x,y)
% Sum of cubes z
z=x.^3+y.^3;
end
• After making and saving function-file you can use it in other M-files (script
files).
11.9.2012 26
Number of input and output parameters can be formed by commands:
• nargin(’<function name>’)
• nargout(’<function name>’)
If you need to exit compulsory from the body of the outer function use
operator:
• return
11.9.2012 27
Let us remake function F1 to F3 with controlling negative argument of
the square root and appropriation p=0 in this case:
11.9.2012 28
DESCRETE SIGNALS IN MATLAB
Sequences
11.9.2012 29
10 20
15
5
10
0
5
-5 0
1 1.5 2 2.5 3 1 2 3 4 5
n n
150 10
100 5
50 0
0 -5
0 5 10 15 0 0.5 1 1.5 2
n n
11.9.2012 30
Unit sample sequence, unit step sequence, discrete exp,
Form a unit sample sequence, unit step sequence and discrete exp.
11.9.2012 31
Discrete complex harmonic signal
is presented as
or , where
11.9.2012 32
Functions max, sum and prod
We can work only with vectors/matrices, which have the same
length/dimentions.
11.9.2012 33
Generation of signals: rectplus, triplus, gausplus, sinc,
square, sawtooth, diric
y=rectpuls(t,w);
y=tripuls(t,w,s);
y=gauspuls(t,fc, bw);
y=sincpuls(t);
y=squarepuls(t,d);
y=sawtoothpuls(t,width);
y=diricpuls(x,N);
11.9.2012 34
Functions rand(1, N) and randn(1, N)
RAND is a uniformly distributed pseudorandom number.
RANDN is a normally distributed pseudorandom numbers.
(1, N) – number of rows and columns.
Form additive mixture (sum) of sequence x(n)=sin(wn) with the length N=32
with white noise: uniformly distributed and normally distributed.
11.9.2012 35
Thanks for attention!
Questions?
11.9.2012 36