4 as Powerpoint

advertisement
TLTE.3120 Computer Simulation in Communication
and Systems (5 ECTS)
http://www.uva.fi/~timan/TLTE3120/
Lecture 4 – 30.9.2015
Timo Mantere
Professor, Telecommunications
University of Vaasa
http://www.uva.fi/~timan
timan@uva.fi
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
This time





More about plotting and data visualization
Interactivity
Functions
Handling files, read&write
Matlab help
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Plotting






Visualization means to do something apparent to the sense of sight.
In visualization it is essential to do the formation so that they are based
on internal models of human (i.e. how people perceive things).
Visualization can be defined more generally to refer to any data to be
presented in a way or form that it supports the person's own
understanding.
Visualization tools are such as pictures (cave painting, 3D images,
technical drawings), tables and animation.
Map visualizes the terrain or location information, a spreadsheet
program to chart calculation results etc.
Visualization is important tool in science, engineering, education,
multimedia, marketing and medicine.
 E.g., animation can be used to illustrate in the medicine very complex and
dynamic phenomena. In technological field visualizations are used a lot in
the operating instructions, marketing materials and multimedia.
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Plotting and the visualization of things


Good visualization
Edward Tufte (1986) has argued that a good information visualization
consists of the following elements:
 High data-ink ratio: the presentation of information used in the printing ink
divided by the number of all the amount of ink used
 Diagram waste minimization: a diagram waste are such diagram elements
that do not really share information, but are mostly decorations
 Clarify the use of multifunctional elements
 A large data density: A number of the individual figures in a chart divided
by the number of their surface area (note, too packed not good)
 Eesthetics
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
plot


Values of only one input
(parameter) vector are
plotted on y-axis with a
consecutive numbering
on x-axis
t = 1:0.1:2*pi;
y = sin(t);
plot(y);
First vector is plotted on
y-axis and second on xaxis when given two
input vectors
plot(t,y);
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
plot

Command hold on defines that the plots are to be drawn in the
same figure.
t = 1:0.1:2*pi;
y = sin(t);
plot(t,y);
hold on;
1
0.5
0
-0.5
x = 0:0.1:3;
y2 = x - 2;
plot(x, y2);
hold off;
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
-1
-1.5
-2
0
1
2
3
4
5
6
7
plot

It is possible to draw multiple lines with only one command by giving
multiple parameter pairs as input
t = (1:0.1:2*pi)';
y = sin(t);
z = cos(t);
1
0.5
0
x = (0:0.1:3)';
y2 = x - 2;
plot(t, [y z], x, y2);
-0.5
-1
-1.5
-2
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
0
1
2
3
4
5
6
7
plot

It is possible to define multiple features such as colors and line types
1
plot(t, y, 'c-.', x, y2, 'r:')
0.5
See: help plot
0
-0.5
-1
-1.5
-2
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
0
1
2
3
4
5
6
7
plot

One can also define the data point notation
plot(t, y, 'k-o', x, y2, 'k-*')
1
0.5
0
-0.5
-1
-1.5
-2
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
0
1
2
3
4
5
6
7
title & (x/y/z)label



Figure header can be added by using command title
Axis explinations can be added by typing xlabel, ylabel and zlabel
Line comments are added with command legend


Construction lines are shown by typing grid on
Scale of the axes can be altered using command axis
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Figure Comments
title(’y = sin(x)’);
y = sin(x)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
0
1
2
3
4
5
6
7
8
9
10
Figure Comments
xlabel(‘x’);
ylabel(‘y’);
y = sin(x)
1
0.8
0.6
0.4
y
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
0
1
2
3
4
5
x
6
7
8
9
10
Figure Comments
hold on;
y2 = cos(x);
plot(x,y2,':r');
legend('y = sin(x)','y = cos(x)');
title('Kahden kuvaajan vertailu');
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
subplot

It is possible to draw multiple figures to the sane window by using
command subplot
t = 0:pi/20:2*pi;
subplot(2,2,1);
plot(cos(t), sin(t));
axis equal;
1
0.5
0
-0.5
-1
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
-1
-0.5
0
0.5
1
subplot

Changing the active figure to the second one of this subplot
subplot(2,2,2);
[x,y] = meshgrid(t);
z = sin(x) + cos(y);
plot(t, z);
axis([0 2*pi -2 2]);
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
1
2
0.5
1
0
0
-0.5
-1
-1
-1
-0.5
0
0.5
1
-2
0
2
4
6
subplot

Continue plotting by filling the lower row also with two figures
subplot(2,2,3);
z = sin(x).*cos(y);
plot(t, z);
axis([0 2*pi -1 1]);
1
2
0.5
1
0
0
-0.5
-1
-1
subplot(2,2,4);
z = (sin(x).^2)-(cos(y).^2);
plot(t, z);
axis([0 2*pi -1 1]);
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
-1
-0.5
0
0.5
-2
1
1
1
0.5
0.5
0
0
-0.5
-0.5
-1
0
2
4
6
-1
0
0
2
2
4
4
6
6
8
LaTeX-compatibility

It is possible to use LaTeX-script directly to create the figure titles and
subtitles
t = 0:0.1:1000;
y = sin(200.*t*10^-3).*exp(-5.*t*10^-3); plot(t, y);
title('{\itAe}^{-\alpha\itt}sin\beta{\itt}
\alpha<<\beta');
Ae sint  << 
1
xlabel('Time \musec.');
ylabel('Amplitude');
0.5
Amplitude
-t
0
-0.5
-1
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
0
200
400
600
Time sec.
800
1000
LaTeX-compatibility

Explination for the row title:

Subscripts are added by using underline character ( _ ):
 For example, A0 is given in form A_0
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Editor


Type edit or edit file_name to open Matlab editor
It’s possible to run command string files from the command line by
typing the file’s name. Command string files can also be run by
pressing F5 or the button marked with the red arrow while the editor
window is open
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Interactivity


Simplest way to achieve
some printing on the screen
is to leave out the semi-colon
used to end rows
Function disp works in the
same manner with the
exception that it does not
print the variable names
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
» A = rand(4);
»A
A=
0.9355
0.9169
0.4103
0.8936
0.0579
0.3529
0.8132
0.0099
0.1389
0.2028
0.1987
0.6038
0.2722
0.1988
0.0153
0.7468
» disp(A)
0.9355
0.9169
0.4103
0.8936
0.0579
0.3529
0.8132
0.0099
0.1389
0.2028
0.1987
0.6038
0.2722
0.1988
0.0153
0.7468
Interactivity

Function disp is often used in a following manner:
 Note the brackets, apostrophes and the utilization of function num2str
(number to string) which is handy for printing numbers
A = rand(4);
[m,n] = size(A);
disp(['A is following ' num2str(m) 'x' ...
num2str(n) '-matrix']);
disp(A);
A is following 4x4-matrix
0.4451
0.9318
0.4660
0.4186
UNIVERSITY of VAASA
0.8462
0.5252
0.2026
0.6721
Communications and Systems
Engineering Group
0.8381
0.0196
0.6813
0.3795
0.8318
0.5028
0.7095
0.4289
Interactivity

User can be asked for input, eg. parameters by using the inputfunction
 the question to be asked from the user is given as a parameter to the
input-function
 function returns the answer of the user
 note the line feed (\n) in the question below to obtain question and
answer neatly on different rows
age = input(‘How old are you?\n');
 use parameter 's' to express that you want the return value to be of type
character string
name = input(‘What is your name?\n', 's');
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Creating Functions in Matlab
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Functions


Functions are used to make the code more easily readable and to
implement new features in Matlab
Differences between functions and command string files:
 function can be given parameters which affect the execution of the
function
 function can have one or more return values or in other words the ”result”
of the function.
 every function has its own workspace created when the function is called
and destroyed when the execution of the function ends
 functions can not directly utilize variables declared in Matlab’s workspace
or internal variables in other functions
 internal variables of the function are lost if they are not explicitly returned
or saved
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Matlab



Matlab is based on the utilization of functions
Function is a small program meant for one particular task
Function’s task can be figured out from its name
 mean counts the mean value, min finds the smallest element etc.

Function’s inputs are called parameters. Function use of utilize the
parameters in some manner and then gives an output called return value
Parameter(s)
Function
Return value(s)
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Parameter
>> x = [1 2 3 4 5];
>> ka = mean(x)
ka =
3
Return value
Basic Structure
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Parameters


Function is usually given one or more parameters which it needs to
execute its task
The following function counts the average value of the numbers given in
parameter vector and prints it to the command line
function avg(x)
% Getting the size of the vector
N = length(x);
% Counting the mean
xBar = 1 / N * sum(x);
% Printing the mean to command line
xBar

Note! Function name = name of the m-file!
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Parameters

When the function is saved as avg.m, it can be called from the
command line in the usual manner
» numbers= [1 2 3 4 5];
» avg(numbers)
xBar =
3

Functions can be given multiple parameters by listing them all in
brackets after the function name
function weightedAvg(x, w)
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Return Values

The value function returns is declared as follows:
function xBar = avg(x)

Variable xBar gets the value it has when the function execution ends

Returning multiple values is done in the same manner:
function [xBar, N] = avg(x)
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
return

Function execution can be interrupted with command return
function d = det(A)
%DET det(A) is the determinant of A.
if isempty(A)
d = 1;
return
else
...
end
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Handling Files
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Files and Variables

Data (measurements etc.) is usually stored to files.

Characteristics of text files:
 ASCII- format
 Can be opened with Excel, Notepad or any other text editor
 Simple and general

Characteristics of binary files:
 Program-spesific coding of information, eg. mat-files
 It is possible to save the workspace or part of it in Matlab. The variables
are ready to be used with the same names when the workspace is
loaded
 Compressing the information in more compact form
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Reading Files
Command
Explanation
load
Files containing only numerical data, separated with tabulator and
without header rows. Also .mat-files.
textread
Numbers and characters in the same file. Data types for each variable
can be defined. Header rows can be passed. Very versatile function.
dlmread
Files containing numerical information, separated by any possible
character. Header rows and columns can be passed.
importdata
General function for reading data. Can be used for reading any
supported file type. Type help fileformats for supported file formats.
fscanf
Numbers and characters in the same file. Data types for each variable
can be defined. Useful for files with rows of varying lengths or otherwise
irregular shape.
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Writing Files
Command Explanation
save
Saves variables to a file. It is possible to save in Matlab-fomat
(.mat-files) in which case variables can be directly loaded to the
workspace. It is also possible to save text files.
dlmwrite
Saves numerical data. Column delimiter can be any character.
fprintf
Saves arbitrary character strings.
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
load

load variables.mat

Loads variables stored in
variables.mat to Matlab’s
workspace
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
load

data = load(fileName)
satunnaisluvut = random numbers
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
data = load('satunnaisluvut.txt')
data =
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000
10.0000
11.0000
12.0000
13.0000
14.0000
15.0000
0.4398
0.3400
0.3142
0.3651
0.3932
0.5915
0.1197
0.0381
0.4586
0.8699
0.9342
0.2644
0.1603
0.8729
0.2379
dlmread
data = dlmread(fileName,
delimiter, hRows, hColumns)
data =
dlmread('mittaukset.txt','@',3,0)
data =
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000
10.0000
11.0000
12.0000
13.0000
14.0000
15.0000
0.4398
0.3400
0.3142
0.3651
0.3932
0.5915
0.1197
0.0381
0.4586
0.8699
0.9342
0.2644
0.1603
0.8729
0.2379
indexit = indices
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
mittaukset = measurements
textread
[x,y,z,...] = textread(fileName,
dataType, parameters)

[indeksit, mittaukset, alkuaine] =
textread('tulokset.txt', '%f%f%s',
'headerlines',4)
indeksit =
tulokset = results
mittaukset = measurements
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
indexit = indices
alkuaine = element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mittaukset =
0.4398
0.3400
0.3142
0.3651
0.3932
0.5915
0.1197
0.0381
0.4586
0.8699
0.9342
0.2644
0.1603
0.8729
0.2379
alkuaine =
'Fe'
'Fe'
'Zn'
'Zn'
'Cu'
'Fe'
'Cu'
'Zn'
'Zn'
'Cu'
'Fe'
'Ni'
'Fe'
'Ni'
'Cu'
save


X = [indeksit mittaukset];
save variables.mat;


save variables.mat X Y;


Saves all the variables in workspace
in .mat-form to a file variables.mat
Saves variables X and Y to a file
variables.mat
save uudet_mittaukset.txt X -ascii;

Saves variable X (indices and
measurements in columns) in ASCIIform or in other words in text form to
a file uudet_mittaukset.txt
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
importdata





UNIVERSITY of VAASA
Communications and Systems
Engineering Group
f = importdata('flowers.tif');
size(f)
ans =
362 500 3
imshow(f)
s = importdata(‘q110a.wav')
s=
data: [70001x1 double]
fs: 22050
soundsc(s.data,s.fs)
Plotting etc. Extra information

You cn find Mathworks illustration of plotting functions from:
 http://www.mathworks.se/videos/using-basic-plotting-functions-69018.html

Matlab file handling
 http://en.wikibooks.org/wiki/MATLAB_Programming/Basic_Reading_and_
Writing_data_from_a_file

Matlab course in UVA by Petri Välisuo
 http://autopedia.uwasa.fi/autopedia/index.php/MATLAB
 http://autopedia.uwasa.fi/autopedia/index.php/M1_MATLAB_Basics
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Matlab Help
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
How to Get Help?

Command line help
 every function has its own instructions
 you can also write instructions for your own functions! (the commented rows
after function declaration)
 used when the name of the function is known but the details on how to
use it (parameters, return values) are not known or have been forgotten
 help command

Documentation (”HTML”-help)
 more in-depth instructions, more examples, hyperlinks to related
commands
 several search operations
 used to learn new things
 doc command or press the question mark in the upper part of the
Matlab’s main window or press F1
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
Matlab’s Toolboxes


Matlab’s special feature are its numerous toolboxes which are meant
for different application areas
Examples:
 Control System toolbox
 Data Acquisition toolbox
card)
 Fuzzy Logic toolbox
 Image Processing toolbox
 Neural Network toolbox
 Signal Processing toolbox
 System Identification toolbox
UNIVERSITY of VAASA
Communications and Systems
Engineering Group
(state-space models, transfer functions,
control design)
(measurement and control using PC’s
(fuzzy logic)
(image processing )
(neural networks)
(signal filtering and processing)
(building a process model from
measurement data)
Download