Sei sulla pagina 1di 117

SOLVING

TELECOMMUNICATION
RESEARCH PROBLEMS
USING MATLAB

22 JANUARY 2015

Prof. Dr. Mahamod Ismail


mahamod@eng.ukm.my

1
2
This mini laboratory provide an insight to the
MATLAB technical computing environment.
The course is intended for students whom are
beginning users and those looking for a review
and no prior programming experience or
knowledge of MATLAB is assumed. The
laboratory is structured to allow thorough
assimilation of ideas through hands-on examples
and exercises with an emphasis on solving
telecommunication problems.

3
1. Ability to appreciate MATLAB technical
computing environment
2. Providing hands-on experience using the features
in the MATLAB
3. Ability to utilize various MATLAB subroutines to
solve various telecommunication problems

4
Introduction
Simulation with Matlab
Hands-on with Matlab
Exercises

5
Solving
Telecommunication
Research
Problems

6
Depends upon time,
resources, and
desired level of accuracy
Analytic/mathematical
modeling : Quick, less
accurate
Simulation : Medium effort,
medium accuracy
Measurement : Typical most
effort, most accurate
Note, above are all
typical
but can be reversed in
some cases!

7
Depends upon time, resources, and
desired level of accuracy
Analytic/mathematical modeling : Quick, less
accurate
Simulation : Medium effort, medium accuracy
Measurement : Typical most effort, most
accurate
Note, above are all typical
but can be reversed in some cases!

8
8
MATLAB is a high-level language and
interactive environment for numerical
computation, visualization, and
programming.
Using MATLAB (communication toolbox), we
can analyze data, develop algorithms, and
create models and applications, e.g. C&C
Scilab - a free software package for
scientific and engineering computing, quite
similar to Matlab Scilab and downloadable
from www.scilab.org

9
Symbolic versus numerical; Open and
closed loop
Software package
Mathematica
Simscript
Scilab
Mathcad

10
1. How to simulate a wireless
communication in MATLAB? I want to
create wireless nodes and simulate a
communication between them.
2. I wonder if anybody has an idea of how I
can simulate a mobile wireless network
(Ad-Hoc) of a few nodes using MATLAB.
3. How do I simulate and model network
traffic in Matlab?

11
12
13
What is Matlab?
Matlab or MathLab?
MATLAB= MATrixLABoratory
A High Level and Fourth Generation
programming language developed by MathWorks
and widely used in Academic and Research
Institutions as well as Industrial Enterprises
An interactive technical computing environment.
Algorithm Development
Data Analysis and Visualization
Numerical Computation

14
15
16
Release
Version Year Release Date Notes
name
MATLAB 1.0 1984
MATLAB 2 1986
MATLAB 3 1987
Ran on MS-DOS but required at least
MATLAB 3.5 1990 a 386 processor. Version 3.5m
required math coprocessor
MATLAB 4 1992
Ran on Windows 3.1. Required a math
MATLAB 4.2c R7 1994
coprocessor
MATLAB 5.0 R8 1996 December, 1996
MATLAB 5.1 R9 May, 1997
1997
MATLAB 5.1.1 R9.1
MATLAB 5.2 R10 March, 1998
1998
MATLAB 5.2.1 R10.1

17
Release
Version Year Release Date Notes
name
MATLAB 6.0 R12 2000 November, 2000
MATLAB 6.1 R12.1 2001 June, 2001
MATLAB 6.5 R13 2002 July, 2002
MATLAB 6.5.1 R13SP1
2003
MATLAB 6.5.2 R13SP2
MATLAB 7 R14 June, 2004
2004
MATLAB 7.0.1 R14SP1 October, 2004
MATLAB 7.0.4 R14SP2 March 7, 2005
2005
MATLAB 7.1 R14SP3 September 1, 2005
MATLAB 7.2 R2006a March 1, 2006
2006
MATLAB 7.3 R2006b September 1, 2006 HDF5-based MAT-file support
MATLAB 7.4 R2007a March 1, 2007
Last release for Windows 2000
2007 and PowerPC Mac. License
MATLAB 7.5 R2007b September 1, 2007
Server support for Windows
Vista

18
Release
Version Year Release Date Notes
name
MATLAB 7.6 R2008a March 1, 2008
2008
MATLAB 7.7 R2008b October 9, 2008
First release for 32-bit &
MATLAB 7.8 R2009a March 6, 2009
64-bit Windows 7.
2009 First release for Intel 64-bit
MATLAB 7.9 R2009b September 4, 2009 Mac, and last for Solaris
SPARC.
MATLAB 7.9.1 R2009bSP1 April 1, 2010
Last release for Intel 32-bit
MATLAB 7.10 R2010a 2010 March 5, 2010
Mac.
MATLAB 7.11 R2010b September 3, 2010
MATLAB 7.11.1 R2010bSP1 March 17, 2011
MATLAB 7.12 R2011a 2011 April 8, 2011
MATLAB 7.13/ R2011b/
September 1, 2011
7.14 R2012a
19
20
Release
Version Year Release Date Notes
name
MATLAB 8 R2012b 2012 September 11, 2012
MATLAB 8.1 R2013a 2013 March 7, 2013 Running on Windows 8
MATLAB 8.2 R2013b 2013 Sept. 6, 2013
Simplified compiler setup
MATLAB 8.3 R2014a 2014 March 6, 2014 for building MEX-files.
LTE Toolbox

21
22
23
MATLAB/Simulink Product Family
Fixed-Point Modeling
Simulink Fixed Point
Event-Based Modeling
Stateflow
SimEvents
Physical Modeling
Simscape
SimMechanics
SimDriveline
SimHydraulics
SimRF
SimElectronics
SimPowerSystems

24
MATLAB product family
Parallel Computing
Parallel Computing Toolbox
MATLAB Distributed Computing Server
Math, Statistics, and Optimization
Symbolic Math Toolbox
Partial Differential Equation Toolbox
Statistics Toolbox
Curve Fitting Toolbox
Optimization Toolbox
Global Optimization Toolbox
Neural Network Toolbox
Model-Based Calibration Toolbox

25
MATLAB product family
Control System Design and Analysis
Control System Toolbox
System Identification Toolbox
Fuzzy Logic Toolbox
Robust Control Toolbox
Model Predictive Control Toolbox
Aerospace Toolbox
Image Processing and Computer Vision
Image Processing Toolbox
Computer Vision System Toolbox
Image Acquisition Toolbox
Mapping Toolbox

26
MATLAB product family
Signal Processing and Communications
Signal Processing Toolbox
DSP System Toolbox
Communications System Toolbox
Wavelet Toolbox
Fixed-Point Toolbox
RF Toolbox
Phased Array System Toolbox
Computational Biology
Bioinformatics Toolbox
SimBiology

27
MATLAB product family
Test and Measurement
Data Acquisition Toolbox
Instrument Control Toolbox
Image Acquisition Toolbox
OPC Toolbox
Vehicle Network Toolbox
Computational Finance
Financial Toolbox
Econometrics Toolbox
Datafeed Toolbox
Fixed-Income Toolbox
Financial Derivatives Toolbox

28
MATLAB/Simulink Product Family
Code Generation
MATLAB Coder
Filter Design HDL Coder
Application Deployment
MATLAB Compiler
MATLAB Builder NE (for Microsoft .NET Framework)
MATLAB Builder JA (for Java language)
MATLAB Builder EX (for Microsoft Excel)
Spreadsheet Link EX (for Microsoft Excel)
Database Connectivity and Reporting
Database Toolbox
MATLAB Report Generator

29
MATLAB/Simulink Product Family
Fixed-Point Modeling
Simulink Fixed Point
Event-Based Modeling
Stateflow
SimEvents
Physical Modeling
Simscape
SimMechanics
SimDriveline
SimHydraulics
SimRF
SimElectronics
SimPowerSystems

30
MATLAB/Simulink Product Family
Control System Design and Analysis
Simulink Control Design
Simulink Design Optimization
Aerospace Blockset
Signal Processing and Communications
DSP System Toolbox
Communications System Toolbox
SimRF
Computer Vision System Toolbox
Simulation Graphics and Reporting
Simulink 3D Animation
Gauges Blockset
Simulink Report Generator

31
MATLAB/Simulink Product Family
Code Generation
Simulink Coder / Embedded Coder
Simulink HDL Coder / Simulink PLC Coder
DO Qualification Kit (for DO-178)
IEC Certification Kit (for ISO 26262 and IEC 61508)
Rapid Prototyping and HIL Simulation
xPC Target / xPC Target Embedded Option
Real-Time Windows Target
Verification, Validation, and Test
Simulink Verification and Validation
Simulink Design Verifier
System Test
EDA Simulator Link
Simulink Code Inspector

32
MATLAB Version 8.3 (R2014a)
Simulink Version 8.3 (R2014a)
Aerospace Blockset Version 3.13 (R2014a)
Aerospace Toolbox Version 2.13 (R2014a)
Bioinformatics Toolbox Version 4.4 (R2014a)
Communications System Toolbox Version 5.6 (R2014a)
Computer Vision System Toolbox Version 6.0 (R2014a)
Control System Toolbox Version 9.7 (R2014a)
Curve Fitting Toolbox Version 3.4.1 (R2014a)
DO Qualification Kit Version 2.3 (R2014a)
DSP System Toolbox Version 8.6 (R2014a)
Data Acquisition Toolbox Version 3.5 (R2014a)
Database Toolbox Version 5.1 (R2014a)
Datafeed Toolbox Version 4.7 (R2014a)
Econometrics Toolbox Version 3.0 (R2014a)
Embedded Coder Version 6.6 (R2014a)

33
Filter Design HDL Coder Version 2.9.5 (R2014a)
Financial Instruments Toolbox Version 1.3 (R2014a)
Financial Toolbox Version 5.3 (R2014a)
Fixed-Point Designer Version 4.2 (R2014a)
Fuzzy Logic Toolbox Version 2.2.19 (R2014a)
Gauges Blockset Version 2.0.9 (R2014a)
Global Optimization Toolbox Version 3.2.5 (R2014a)
HDL Coder Version 3.4 (R2014a)
HDL Verifier Version 4.4 (R2014a)
IEC Certification Kit Version 3.3 (R2014a)
Image Acquisition Toolbox Version 4.7 (R2014a)
Image Processing Toolbox Version 9.0 (R2014a)
Instrument Control Toolbox Version 3.5 (R2014a)
LTE System Toolbox Version 1.1 (R2014a)

34
MATLAB Builder EX Version 2.5 (R2014a)
MATLAB Builder JA Version 2.3.1 (R2014a)
MATLAB Builder NE Version 4.2.1 (R2014a)
MATLAB Coder Version 2.6 (R2014a)
MATLAB Compiler Version 5.1 (R2014a)
MATLAB Report Generator Version 3.16 (R2014a)
Mapping Toolbox Version 4.0.1 (R2014a)
Model Predictive Control Toolbox Version 4.2 (R2014a)
Model-Based Calibration Toolbox Version 4.7 (R2014a)
Neural Network Toolbox Version 8.2 (R2014a)
OPC Toolbox Version 3.3.1 (R2014a)
Optimization Toolbox Version 7.0 (R2014a)
Parallel Computing Toolbox Version 6.4 (R2014a)
Partial Differential Equation Toolbox Version 1.4 (R2014a)
Phased Array System Toolbox Version 2.2 (R2014a)

35
Polyspace Bug Finder Version 1.1 (R2014a)
Polyspace Code Prover Version 9.1 (R2014a)
RF Toolbox Version 2.14 (R2014a)
Real-Time Windows Target Version 4.4 (R2014a)
Robust Control Toolbox Version 5.1 (R2014a)
Signal Processing Toolbox Version 6.21 (R2014a)
SimBiology Version 5.0 (R2014a)
SimDriveline Version 2.6 (R2014a)
SimElectronics Version 2.5 (R2014a)
SimEvents Version 4.3.2 (R2014a)
SimHydraulics Version 1.14 (R2014a)
SimMechanics Version 4.4 (R2014a)
SimPowerSystems Version 6.1 (R2014a)
SimRF Version 4.2 (R2014a)
Simscape Version 3.11 (R2014a)

36
LTE System Toolbox Version 1.1 (R2014a)
Simulate physical layer of LTE and LTE-Advanced wireless
communications systems
Key Features
Standard-compliant models for LTE and LTE-Advanced
(Releases 8, 9, and 10)
End-to-end physical layer transmit and receive processing
functions, including OFDM (downlink) and SC-FDMA (uplink)
MIMO antenna transmission and UE-specific beamforming
functions
Channel estimation, synchronization, and MIMO receiver
functions
Standard-compliant propagation channel models
Test models and reference measurement channel (RMC)
waveform generators
Interactive tools for conformance and BER testing
Recovery of low-level parameters, such as cell identity

37
Simulink 3D Animation Version 7.1 (R2014a)
Simulink Code Inspector Version 2.1 (R2014a)
Simulink Coder Version 8.6 (R2014a)
Simulink Control Design Version 4.0 (R2014a)
Simulink Design Optimization Version 2.5 (R2014a)
Simulink Design Verifier Version 2.6 (R2014a)
Simulink PLC Coder Version 1.7 (R2014a)
Simulink Real-Time Version 6.0 (R2014a)
Simulink Report Generator Version 3.16 (R2014a)
Simulink Verification and Validation Version 3.7 (R2014a)
Spreadsheet Link EX Version 3.2.1 (R2014a)
Stateflow Version 8.3 (R2014a)
Statistics Toolbox Version 9.0 (R2014a)
Symbolic Math Toolbox Version 6.0 (R2014a)
System Identification Toolbox Version 9.0 (R2014a)
SystemTest Version 2.6.7 (R2014a)
Trading Toolbox Version 2.1 (R2014a)
Vehicle Network Toolbox Version 2.2 (R2014a)
Wavelet Toolbox Version 4.13 (R2014a)

38
39
40
Native
.fig MATLAB Figure
.m MATLAB function, script, or class
.mat MATLAB binary file for storing variables
.mex... MATLAB executable (platform specific, e.g.
".mexmac" for the Mac, ".mexglx" for Linux, etc.)
.p MATLAB content-obscured .m file (result of pcode() )
Third-party
.jkt GPU Cache file generated by Jacket for MATLAB
(AccelerEyes)
.mum MATLAB CAPE-OPEN Unit Operation Model File
(AmsterCHEM)

41
MATLAB Sites
Rob Maunders@Soton
http://users.ecs.soton.ac.uk/rm/
Wireless Network Simulator in Matlab
http://wireless-matlab.sourceforge.net/
Dr. Bernd-Peter Paris@George Mason
http://www.spec.gmu.edu/~pparis/
Prowler: Probabilistic Wireless Network Simulator
http://www.isis.vanderbilt.edu/projects/nest/prowler/
Check Latest Learning Resources
http://www.mathworks.com/academia/student_center/tutoria
ls/launchpad.html

42
http://www.eng.cam.ac.uk/help/tpl/programs/matlab.html

43
http://www.math.siu.edu/matlab/tutorials.html

44
http://www.imc.tue.nl/

45
http://www.mathworks.com/help/techdoc/learn_matlab/
bqr_2pl.html

46
Basic Matlab commands
Simulating Analog Communication
Systems Amplitude Modulation (AM)
Simulating Digital Communication
Systems - BPSK

47
48
>matlab (command prompt or run)
Using short cut
Not from *.m files

49
50
51
52
53
54
Note: Variables can change its type through calculations.
Ex: x=true; (x is logical), x=x*2; (x is changed to numerical)

55
Note: Matrices can be visualized as a set of raw vectors
or a set of column vectors.

56
Creating ascending or descending row vectors:
Syntax: var_name=init:increment:teminator

Ex: x=-10:10; (x=[-10 -909 10])

Y=1:0.1:10; (Y=[1 1.1 1.2 10])

Z=10:-0.2:1; (z=[10 9.8 9.61])

57
Vectors can be concatenated to form longer vectors or
matrices:
Ex: x=[1 2 3]; y=[4 5 6]; x_c=[7;8;9]; y_c=[10;11;12];

z=[x, y]; (z=[1 2 3 4 5 6])

w=[x;y]; ( 1 2 3 )
w
(the number of columns
4 5 6 should be equal)

7 10
a=[x_c,y_c]; ( a 8 11 ) (the number of rows

9 12 should be equal)

58
MATLAB command Result
a(1,2) Gives the element on the first row and
second column of a.
a(1,:) (eq. a(1,1:end)) Gives the first row of a.
a(:,3) (eq. a(1:end,3)) Gives the third column of a.
a(:,2)=[10;11;12] Changes the second column of into a
column with [10;11;12]. Only works if a is
a n x 3 array and n 2
b(1) the first element of b in case b contains
either one row or one column.
a(1,[3,1]) an array consisting of the 3rd and 1st
elements of the first row of a.

59
Matrices are interleaved when accessed using single
dimension index.
Ex:
1 15 a(1,1)=1, a(1,2)=15, a(2,1)=2,
a 2 20 a(2,2)=20, a(3,1)=3, a(3,2)=25
3 25
Alternative way

a(1)=1, a(2)=2, a(3)=3,


a(4)=15, a(5)=20, a(6)=25

60
Numeric Types
Integer and floating-point data
Characters and Strings
Text in character arrays
Structures
Arrays with named fields that can contain data of
varying types and sizes
Cell Arrays
Arrays that can contain data of varying types and sizes

61
Class Range Function
Signed 8-bit integer -27 to 27-1 int8
Signed 16-bit integer -215 to 215-1 int16
Signed 32-bit integer -231 to 231-1 int32
Signed 64-bit integer -263 to 263-1 int64
Unsigned 8-bit integer 0 to 28-1 uint8
Unsigned 16-bit integer 0 to 216-1 uint16
Unsigned 32-bit integer 0 to 232-1 uint32
Unsigned 64-bit integer 0 to 264-1 uint64

62
Example Result
x= 325.499; int16(x) x=325
x=325.499; x=x+0.001, int16(x) x=326
int16(325) * 4.39 1427 (int16 type)
str = 'Hello World'; int8(str) [72 101 108 108 111 32
(brain Storming: why the result is a 87 111 114 108 100 ]
vector? What the numbers represent?)
x = uint32([132 347 528 ]) .* uint32
uint32(75); class(x)
x = uint32([132 347 528]) .* 75.49; uint32
class(x)

63
Floating point function Range
double(X) -1.79769e+308 to -2.22507e-308
and
2.22507e-308 to 1.79769e+308
single(X) -3.40282e+38 to -1.17549e-38
and
1.17549e-38 to 3.40282e+38
Note: for integers any value x>intmax or x<intmin will be
trimmed to intmax or intmin

while for floating point any value x>realmax will be inf


and x<realmin will be inf.

64
Example Result
y = int64(-589324077574); x = x = -5.8932e+11
single(y)
y = int64(-589324077574); x = x = -5.8932e+11
double(y)
c = 'uppercase' - 32; class(c) ans = double
char(c) ans = UPPERCASE

Note: when you assign a numeric value to any variable,


the default class will be double.

65
Complex numbers are represented with i or j, ex:
x=2+3i or y=-5.5-2.1j.
Another way to create complex number is by using
complex command, ex:
X=5; Y=10;
Z=complex(x,y); (result z=5+10i)
Real and imaginary parts can be separated by using
real and imag commands, ex;
real(z) (result ans=5)
imag(z) (result ans=10)

66
Inf and inf, are considered numbers, ex:
x=inf; y=5; w= x==inf; (result w=1)
z=y/x; (result z=0) w=isinf(x); (result w=1)

Inf and inf occur when the calculations result in a


value larger or smaller to the data type precision.
Note: when using plot commands to plot a vector
or surface with some elements having inf or inf
value, those elements will not be plotted.

67
NaN stands for Not a Number, NaN is a result of some
computations resulting in:

0/0 or inf/ inf


Any arithmetic operation involving NaN will result in NaN.
Moreover, any logical operation with variables having NaN
will be falls, ex:

X=NaN; Y=NaN; The only exception is the inequality, ex:


z=X==Y; (result z=0) w=X~=Y; (result w=1)

68
A structure is a data type that groups related data using
data containers called fields. Each field can contain
data of any type or size.
Structure can be created as follows:
Syntax: structure_name.filedname=value
Ex:
students.name=Liyth;
students.age=0;
students.course=PhD;
student.cgpa_perc=[50 55 54 60];

69
Alternative way to create structure is by using struct
command, ex:
Syntax: struct_name=struct(filed1_name,field1_value,..)
Ex:
students=struct(name,Liyth,age,0,course,PhD,
cgpa_perc, [50 55 54 60]);

Structures can be arranged in arrays by using indices, ex:


Students(1).name=Liyth; Students(2).name=Mahdi;
Students(1).age=0; Students(2).age=45;
Students(1).course=PhD; Students(2).course=PhD;
Student(1).cgpa_perc=[50 55 54 60]; Student(2).cgpa_perc=[90 95 94 96];

70
A cell array is a data type that groups data of any type
or size.
Cell arrays can be created by using the {} paranthesis,
ex:
myCell = {1, 2, 3; 'text', rand(5,10,2), {11;
22; 33}}
Data can be accessed similar to numeric arrays, ex:
myCell{1,1} (result ans=1); myCell{2,1} (result
ans=text)

71
Structure Cell
Structure arrays contain data in Cell arrays contain data in cells
fields that you access by name that you access by numeric
indexing
Similar to records in other Simply it is array which allows
languages different data types

72
Matrix Array
Operations Operations
x 1 y 4
2 5
3 6
x' 123 y' 456
x+y 5 x-y -3
7 -3
9 -3
x+2 3 x-2 -1
4 0
5 1

73
Matrix Array
Operations Operations
x*y Error x.*y 4
10
18
x'*y 32 x'.*y Error
x*y' 456 x.*y' Error
8 10 12
12 15 18
x*2 2 x.*2 2
4 4
6 6

74
Matrix Array
Operations Operations
x\y 16/7 (brain x.\y 4
storming, why this 5/2
is single value?) 2
2\x 1/2 2./x 2
1 1
3/2 2/3
x/y 0 0 1/6 x./y 1/4
0 0 1/3 2/5
0 0 1/2(brain 1/2
storming, why this
is a matrix?)
x/2 1/2 x./2 1/2
1 1
3/2 3/2
75
Matrix Array
Operations Operations
x^y Error x.^y 1
32
729
x^2 Error x.^2 1
4
9
2^x Error 2.^x 2
4
8
(x+i*y)' 1 - 4i 2 - 5i 3 - 6i
(x+i*y).' 1 + 4i 2 + 5i 3 + 6i

76
Relational Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Note: All the relational operators work for scalars and matrices,
the exception is:
a=1; x=a==[]; (result error because a is 1 by 1 matrix)
Alternatively use isempty(a) function
77
78
Ex:
A = [0 1 1 0 1]; B = [1 1 0 0 1];
Operator Description Result
& Returns 1 for every element location that A & B = 01001
is true (nonzero) in both arrays, and 0 for
all other elements.
| Returns 1 for every element location that A | B = 11101
is true (nonzero) in either one or the other,
or both arrays, and 0 for all other
elements.
~ Complements each element of the input ~A = 10010
array, A.
xor Returns 1 for every element location that xor(A,B) = 10100
is true (nonzero) in only one array, and 0
for all other elements.

79
Ex:
A = [0 1 2; 0 -3 8; 0 5 0];
Operator Description Result
any(A) Returns 1 for a vector where any(A)
any element of the vector is ans = 0 1 1
true (nonzero), and 0 if no
elements are true.
all(A) Returns 1 for a vector where all(A)
all elements of the vector are ans = 0 1 0
true (nonzero), and 0 if all
elements are not true.

Brain storming: why the results of any(A) and all(A) are vectors of
zeros and ones? Why not the results are matrix or scalar?

80
Ex: A = 28; % binary 00011100
B = 21; % binary 00010101
Operator Description Result
bitand Returns the bit-wise AND of two bitand(A,B) = 20
integer arguments. (binary 00010100)
bitor Returns the bit-wise OR of two integer bitor(A,B) = 29
arguments. (binary 00011101)
bitcmp Returns the bit-wise complement, bitcmp(A,'int8') = -29
assuming the second argument specifies (binary 11100011)
the integer data type of the first
argument. By default, MATLAB treats
double values as uint64 integers.
bitxor Returns the bit-wise exclusive OR of bitxor(A,B) = 9
two integer arguments. (binary 00001001)

81
Operator Description
&& Returns logical 1 (true) if both inputs evaluate to true, and
logical 0 (false) if they do not.
|| Returns logical 1 (true) if either input, or both, evaluate to true,
and logical 0 (false) if they do not.
Notes:
Short-circuit operators perform AND and OR operations on logical
expressions containing scalar values. They are short-circuit operators in that
they evaluate their second operand only when the result is not fully
determined by the first operand.
Ex: to avoid divide by zero you can write the condition as:
x = (b ~= 0) && (a/b > 18.5)
Short-circuit operators shouldnt be used interchangeably with the element
wise operators because if the operands are matrices, short-circuit operands
might produce undesirable results.

82
MATLAB offer a rich input commands, the basic input is
input(prompt), Ex:
% MATLAB code
For i=1:5 % loop to read a row vector of 5 elements
X(i)=input(strcat([enter num2str(i) value: ]))
End
% End of the code

Note: strcat is a MATLAB function used to concatenate strings.


num2str is a MATLAB function to change numeric value
to string.

83
Using input command to read multiple inputs is not easy task,
instead inputdlg command can be used.
syntax: answer =
inputdlg(prompt,dlg_title,num_lines,defAns,Resize)
Ex:
prompt = {'Enter matrix size:','Enter colormap name:'};
dlg_title = 'Input for peaks function';
num_lines = 1;
def = {'20','hsv'};
answer = inputdlg(prompt,dlg_title,num_lines,def);

84
Note: Inputs read through inputdlg command are all strings (even numbers)
and are saved in cell array (the answer data type is cell).
i.e.
answer{1} is 20, to convert it to number use str2num command
answer{2} is hsv

85
Input and inputdlg commands are useful for small size
matrices (imagine if you have a matrix of 100 by 100
size, how to read it?).
Currently all measurement devices are able to save the
measurements, images, DEM, DSM or rasters directly
on txt, ascii, comma separated, or excel sheets. This
make it easier to read large set of data using few
commands.
The first step to read the data from a file is to provide
the full file name and path name. Command uigetfile is
used to get the file name and path name.

86
The basic uigetfile command is:
Syntax: uigetfile('FilterSpec')
Ex:
% MATLAB code
[filename, pathname] = uigetfile('*.m', 'Pick an M-file');
if isequal(filename,0)
disp('User selected Cancel')
else
disp(['User selected', fullfile(pathname, filename)])
End
Fullfile_name=fullfile(pathname, filename)
% end of code

87
After identifying the data file full name, pass it to the
appropriate file read command. Some commonly used file
read commands are listed in the table.

All data file reading commands can read mixed data types,
however, in this case all the data might be converted into txt
format and saved in a cell array (also called cell container).

Image and audio reading commands set the uint8 as a default


image or audio data type. For computations which involve
double data type, the image or audio data should be converted
to double .

88
File Content Ext. Import Export
comman command
d
MATLAB formatted MAT load save
data
text any load save-ascii
Excel sheets xls xlsread xlswrite
Extensible markup xml xmlread xmlwrite
lang.
Data acquisition file daq daqread non
Image All formats imread imwrite
Audio All formats audioread Audiowrite
video All formats can videoread videowrite
be read, some
cannot be
written
89
Basic Matlab commands
Simulating Analog Communication
Systems Amplitude Modulation (AM)
Simulating Digital Communication
Systems - BPSK

90
Simulating Analog Communication
Systems Amplitude Modulation (AM)

Source Modulator

Channel

Destination Demodulator

91
Simulating Analog Communication
Systems Amplitude Modulation (AM)

Source Modulator

Channel

Destination Demodulator

Produces message signal e.g. a simple Sinewave

92
Exercise 2
- Simulate a Source

Generate message signal (simple sine wave)


m t Vm sin 2f mt

Define time instants (1000 sample points)


tmin = 0; tmax = 10^(-3); step = (tmax-tmin)/1000;
t = tmin:step:tmax;

Define amplitude and frequency (initial phase is zero)


Vm = 1; % Amplitude
fm = 2*10^3; % Frequency

Construct the Signal


m = Vm*sin(2*pi*fm*t);

View the Signal


plot(t,m,'r');

93
Exercise 2
- Simulate a Source

Complete MATLAB Script [Prog1.m]


tmin = 0; tmax = 10^(-3); step = (tmax-tmin)/1000;
t = tmin:step:tmax;
fm = 2*10^3;
Vm = 1;
m = Vm*sin(2*pi*fm*t);
plot(t,m,'r');

94
Exercise 2
- Simulate a Source

Assignment #1 [Prog2.m], [Prog3.m]

What happens if there is an initial phase?


phi_deg = 45;
phi_rad = phi_deg*pi/180;
m = Vm*sin(2*pi*fm*t+phi_rad);

What happens if the message is not sinusoidal?


tmin = 0; tmax = 1; step = (tmax-tmin)/1000;
t = tmin:step:tmax;
f = 2;
m = sawtooth(2*pi*f*t);
plot(t,m,'r');

95
Exercise 2
- Simulate Modulation

Source Modulator

Channel

Destination Demodulator

Built-in functions are available (ammod, amdemod etc.)


WYSIWYG?? No

96
Exercise 2
- Simulate Amplitude Modulation

Simulate with built-in functions [Prog4.m]


fs = 8000; % Sampling rate is 8000 samples per second
fc = 300; % Carrier frequency in Hz
t = [0:0.1*fs]'/fs; % Sampling times for 0.1 second
m = sin(20*pi*t); % Representation of the signal
v = ammod(m,fc,fs); % Modulate m to produce v

figure(1)
subplot(2,1,1); plot(t,m); % Plot m on top
subplot(2,1,2); plot(t,v); % Plot v below

mr = amdemod(v,fc,fs); % Demodulate v to produce m

figure(2);
subplot(2,1,1); plot(t,m); % Plot m on top
subplot(2,1,2); plot(t,mr); % Plot mr below

Source: Introduction to Communications Toolbox in MATLAB 7.6.0 (R2008) by Amit Degada


Available: http://amitdegada.weebly.com/download.html

97
Exercise 2
- Simulate Amplitude Modulation

98
Exercise 2
- Simulate Amplitude Modulation

Try this ..

Define message signal, m t Vm sin 2f m t (as done earlier)

tmin = 0; tmax = 10^(-3); step = (tmax-tmin)/1000;


t = tmin:step:tmax;
Vm = 1;
fm = 2*10^3;
m = Vm*sin(2*pi*fm*t);

Define carrier, c t Vc sin 2f c t

Vc = 2; % Amplitude
fc = 10^4; % Frequency
c = Vc*sin(2*pi*fc*t); % Carrier signal

99
Exercise 2
- Simulate Amplitude Modulation

Continued .
Vm
Modulate the Signal, v t Vc 1 sin 2f mt sin 2f ct
Vc
v = (1+m/Vc).*c; % DSB-FC modulation

View Modulated Wave

plot(t,v); % Modulated Wave


hold on;
plot(t,Vc*(1+m/Vc),'r:'); % Upper Envelope
hold on;
plot(t,-Vc*(1+m/Vc),'r:'); % Lower Envelope
hold off ;

100
Exercise 2
- Simulate Amplitude Modulation

Complete MATLAB Script [Prog5.m]

clear all; close all; clc;


tmin = 0; tmax = 10^(-3); step = (tmax-tmin)/1000;
t = tmin:step:tmax; % Time
Vm = 1; Vc = 2; % Amplitude
fm = 2*10^3; fc = 10^4; % Frequency

m = Vm*sin(2*pi*fm*t); % Message
c = Vc*sin(2*pi*fc*t); % Carrier
v = (1+m/Vc).*c; % Modulated Wave

plot(t,v); hold on;


plot(t,Vc*(1+m/Vc),'r:'); hold on; % Upper Envelope
plot(t,-Vc*(1+m/Vc),'r:'); hold off % Lower Envelope

101
Exercise 2
- Simulate Amplitude Modulation

102
Exercise 2
- Simulate Amplitude Modulation

Assignment #2 [Prog6.m]

How to view effect of changing modulation index?


tmin = 0; tmax = 10^(-3); step = (tmax-tmin)/1000;
t = tmin:step:tmax;
Vm = 1; mu = 1.5; Vc = Vm/mu;
fm = 2*10^3; fc = 10^4;

m = Vm*sin(2*pi*fm*t);
c = Vc*sin(2*pi*fc*t);
v = (1+m/Vc).*c;

plot(t,v); hold on;


plot(t,Vc*(1+m/Vc),'r:'); hold on;
plot(t,-Vc*(1+m/Vc),'r:'); hold off

103
Exercise 2
- Simulate DSB-SC Amplitude Modulation

Assignment #2 (Contd) [Prog7.m]


How to simulate DSB-SC modulation?
tmin = 0; tmax = 10^(-3); step = (tmax-tmin)/1000;
t = tmin:step:tmax;
Vm = 2; Vc = 1;
fm = 2*10^3; fc = 10^4;

m = Vm*sin(2*pi*fm*t);
c = Vc*sin(2*pi*fc*t);
v = m.*c;

plot(t,v); hold on;


plot(t,m,'r:'); hold on;
plot(t,-m,'r:'); hold off

104
Exercise 2
- Simulate DSB-SC Amplitude Modulation

Demodulate DSB-SC with filter [Prog8.m]


clear all; close all; clc;
tmin = 0; tmax = 1; step = (tmax-tmin)/(10^3);
t = tmin:step:tmax;
Vm = 2; Vc = 1;
fm = 2; fc = 10^2;

m = Vm*sin(2*pi*fm*t);
c = Vc*sin(2*pi*fc*t);
v = m.*c;

r = v.*c;

[b a] = butter(1,0.01);
mr = filter(b,a,r);

figure(1)
subplot(2,1,1); plot(t,m);
subplot(2,1,2); plot(t,mr);

105
Exercise 2
- Simulate DSB-SC Amplitude Modulation

Ideal Demodulation of DSB-SC [Prog9.m]


clear all; close all; clc;
fs = 10^5; N = 10^5;
t = 1/fs:1/fs:N/fs;
fm = 2; fc = 10^3;
m = sin(2*pi*fm*t);
c = sin(2*pi*fc*t);
v = m.*c;

r = zeros(1,N); n =f s/fc;
for k = 1:fc
mr((k-1)*n+1:k*n) = 2*v((k-1)*n+1:k*n)
*c((k-1)*n+1:k*n)'/n;
end

figure(1)
subplot(2,1,1); plot(t,m);
subplot(2,1,2); plot(t,mr);

106
Exercise 2
- Simulate Channel

Source Modulator

Channel

Destination Demodulator

Introduces noise Additive White Gaussian Noise

107
Exercise 2
- Simulate Channel

Introducing AWGN [Prog10.m]


fs = 10^5; N = 10^5;
t = 1/fs:1/fs:N/fs;
fm = 2; fc = 10^3;
m = sin(2*pi*fm*t);
c = sin(2*pi*fc*t);
v = m.*c;

SNRdB = 10; SNR = 10^(SNRdB/10);


vn = var(v)/SNR;
n = sqrt(vn)*randn(1,N);
v = v + n;

r=zeros(1,N); n=fs/fc;
for k=1:fc
mr((k-1)*n+1:k*n)=2*v((k-1)*n+1:k*n)*c((k-1)*n+1:k*n)'/n;
end

figure(1)
subplot(2,1,1); plot(t,m);
subplot(2,1,2); plot(t,mr); axis([0 1 -1 1])

108
Basic Matlab commands
Simulating Analog Communication
Systems Amplitude Modulation (AM)
Simulating Digital Communication
Systems - BPSK

109
Exercise 3
- Simulate Digital Communication

Source Modulator

Channel

Destination Demodulator

110
Exercise 3
- Simulate BPSK

Simulation [Prog11.m]
%This program simulates BER of BPSK in AWGN channel%
clear all; close all; clc;
num_bit=100000; %Signal length
max_run=20; %Maximum number of iterations for a single SNR
Eb=1; %Bit energy
SNRdB=0:1:9; %Signal to Noise Ratio (in dB)
SNR=10.^(SNRdB/10);

hand=waitbar(0,'Please Wait....');
for count=1:length(SNR) %Beginning of loop for different SNR
avgError=0;
No=Eb/SNR(count); %Calculate noise power from SNR

Simulate BPSK
111
Exercise 3
- Simulate BPSK

Simulation (Contd.) [Prog11.m]


for run_time=1:max_run %Beginning of loop for different runs
waitbar((((count-1)*max_run)+run_time-1)/(length(SNRdB)*max_run));
Error=0;

data=randint(1,num_bit); %Generate binary data source


s=2*data-1; %Baseband BPSK modulation

N=sqrt(No/2)*randn(1,num_bit); %Generate AWGN

Y=s+N; %Received Signal

for k=1:num_bit %Decision device taking hard decision and deciding error
if ((Y(k)>0 && data(k)==0)||(Y(k)<0 && data(k)==1))
Error=Error+1;
end
end

Error=Error/num_bit; %Calculate error/bit


avgError=avgError+Error; %Calculate error/bit for different runs
end %Termination of loop for different runs

112
Exercise 3
- Simulate BPSK
Simulation (Contd.) [Prog11.m]
BER_sim(count)=avgError/max_run; %Calculate BER for a particular SNR

end %Termination of loop for different SNR


BER_th=(1/2)*erfc(sqrt(SNR)); %Calculate analytical BER
close(hand);

semilogy(SNRdB,BER_th,'k'); %Plot BER


hold on
semilogy(SNRdB,BER_sim,'k*');
legend('Theoretical','Simulation',3);
axis([min(SNRdB) max(SNRdB) 10^(-5) 1]);
hold off

113
Exercise 3
- Simulate ASK

Simulation [Prog12.m]

114
Exercise 3
- Simulate FSK

Simulation [Prog13.m]

115
http://www.mathworks.com/help/techdoc/
http://www.mathworks.com/matlabcentral/
http://home.hit.no/~hansha/
http://www.imc.tue.nl/
http://www.freewebs.com/acwebpage/teaching.htm
http://www.slideshare.net/AmyraGhazali/askfsk-and-mpsk-using-
matlab?related=1
http://www.freewebs.com/acwebpage/Resources/Codes_Comm_P
PT.zip
B.P. Lathi and Z. Ding, Modern Digital and Analog Communication
Systems, Oxford University Press, International 4th edition, 2010.
J.G. Proakis, M. Salehi, and G. Bauch, Contemporary
Communication Systems using MATLAB, Thomson/ CL-
Engineering, 2nd edition, 2003

116
11
7

Potrebbero piacerti anche