line plots · web view• from the command line by using the command help : >> help sqrt sqrt...

22
1 EGE UNIVERSITY ELECTRICAL & ELECTRONICS ENGINEERING DEPARMENT COMMUNICATION SYSTEMS – 1 LABORATORY Simulating Communications Systems in Matlab Qi,Wang 2014-2015 FALL Matlab is an interactive system for doing numerical computations. It is widely used to simulate communications systems. General introductions of Matlab can be easily found by typing matlab introduction in any search engine. In this document, we will give a brief introduction on how to simulate a communication system. 1 Before the Start Help and information on Matlab commands can be found in several ways: from the command line by using the command help <function name>: >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of X. Complex results are produced if X is not positive.

Upload: tranxuyen

Post on 30-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

1

EGE UNIVERSITY ELECTRICAL & ELECTRONICS ENGINEERING DEPARMENT

COMMUNICATION SYSTEMS – 1LABORATORY

Simulating Communications Systemsin Matlab

Qi,Wang

2014-2015 FALL

Matlab is an interactive system for doing numerical computations. It is widely used to simulate communications systems. General introductions of Matlab can be easily found by typing matlab introduction in any search engine. In this document, we will give a brief introduction on how to simulate a communication system.

1 Before the StartHelp and information on Matlab commands can be found in several ways:

• from the command line by using the command help <function name>:

>> help sqrtSQRT Square root.

SQRT(X) is the square root of the elements of X. Complex results are produced if X is not positive.

See also sqrtm, realsqrt, hypot.

Overloaded methods:codistributed/sqrtsym/sqrt

Reference page in Help browser doc sqrt

>>

Page 2: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

2

• from the product Help window found under the menu Help or using the command doc from the command line.

• placing the cursor on a command of interest and pressing F1.

2 Calculation, Variables, Vectors, MatricesIn this section, we provide a list of notations that are commonly used in Matlab . It will be easier to follow if you have Matlab by side and just learn by doing. You should type in commands shown below after the prompt: >>.

2.1 Matlab as a Calculator>> 2+3^2+4*(5-8)*sin(0.1)ans =

9.8020Other built-in functions for calculation include abs, sqrt, exp, log, log10, sin, cos, ...

2.2 VariablesThe result of the calculation is assigned to the variable ans by default. Com- mands are separated by a ”,”. The output of commands is supressed by a”;”.

Legal variable names consist of any combination of letters and digits, starting with a letter. However, you should avoid using already predefined names such as eps, pi, i, j.>> eps, pi, i, j, 3+5, a=3+5, b=3+5;ans = 2.2204e-016 ans = 3.1416ans = 0 + 1.0000i ans = 0 + 1.0000i ans =

8 a =

8>>In the exercises, you are required to use names that represent the meaning of the variables.

Page 3: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

3

2.3 Vectors and MatricesWhen defining a vector, entries must be enclosed in square brackets. Row elements are separated with a space or a ”,”. Column elements are separated with a ”;”.

>> a = [1 5 3 8 -4] %% Comments are written like this. a =

1 5 3 8 -4

>> a = [1, 5, 3, 8, -4] %% a row vector a =

1 5 3 8 -4

>> a = [1; 5; 3; 8; -4] %% a column vector a =

1538

-4

>> length(a) %% get the length of the vector ans =

5

>> b = rand(5,1) %% define a vector with uniformly%% distributed entries.

b =0.81470.90580.12700.91340.6324

>> c = a + j*b %% build a complex-valued vector c =

1.0000 + 0.8147i

5.0000 + 0.9058i

3.0000 + 0.1270i

Page 4: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

4

8.0000 + 0.9134i

-4.0000 + 0.6324i

Page 5: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

>> e = conj(d) %% conjugatee =

1.0000 - 0.8147i

5.0000

- 0.9058i 38.0000 - 0.9134i -4.0000 - 0.6324i

5

>> d = c.’ %% transpose d =1.0000 + 0.8147i 5.0000 + 0.9058i 3.0000 + 0.1270i

8.0000 + 0.9134i -4.0000 + 0.6324i

.0000 - 0.1270i

>> f = c’ %% Hermitian -- transpose + conjugate f =1.0000 - 0.8147i 5.0000 - 0.9058i 3.0000 - 0.1270i

8.0000 - 0.9134i -4.0000 - 0.6324i

>> e == f %% logical operations (>,<,~=,>=,&,|)%% zero--false or one--true

ans = 1 1 1 1 1

>> norm(f) %% norm of a vector ans =

10.8506>> help norm %% check different norms

>> k = ones(5,3) %% define an all-one matrix with%% specified

size%% same with zeros() and nan()

k =1 1 11 1 11 1 11 1 11 1 1

>> size(k) %% return the size of a matrix%% size(k,1) to return only the first dimension

ans =5 3

Page 6: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

6

>> k(5,2) %% the element at 5th row, 2nd column in k ans =

1

>> k(5,:)ans =

1 1 1

%% the 5th row in k

>> k(end,:)ans =

1 1 1

%% the last row

>> k(1:3:end,:) %% every 3th row ans =

1 1 11 1 1

>> rank(k) %% rank of a matrix ans =

1

>> n = eye(3) %% an identity matrix n =

1 0 00 1 00 0 1

>> diag(n) %% diagonal elements of a matrix ans =

111

>> trace(n) %% trace of a matrix ans =

3

>> p = d*k %% product of two matrices%% dimensions of the two have to match.

p =

Page 7: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

7

13.0000 + 3.3933i 13.0000 + 3.3933i 13.0000 + 3.3933i

>> clc %% clear the command window>> clear %% clear the workspace

3 Batch Files and Functions1 If you don’t want the current result of a command at the spot, the line has to be ended with ”;”. In order to execute a number of command at once, we write them in a m-file.

• Open the editor using the command edit or from the menu bar.

• Write your command line by line and save as <yourfilename>.m.

• Press F5 or type <yourfilename> in the command window.

To define a function, the procedure is similar, check the product Help.

4 A Typical Communications SystemIn a communications system, messages are transmitted over a certain chan- nel. An example is shown in Fig. 1.

tx_databits

QPSKmodulation

channelH

noise

+decision

rx_databits

tx_symbols rx_symbols

Figure 1: a typical transmission system

In the following, an example will be given to show how each step is im- plemented in Matlab .

1 If you already have some experience with Matlab , this section can be skipped.

Page 8: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

1 (1 +j) 1 (−1+ j) 1 (1 −

j) 1

s

n 8

Table 1: QPSK symbol mappinginteger number 0 1 2 3

LSB 0 1 0 1MSB 0 0 1 1

symbol alphabet √ √2 2

√2

√2(−1 − j)

4.1 Generate DatabitsThe data information is a stream of binary bits. The occurrence of 0 and 1 are of equal probability. Assume a data stream of length 50000, it can be generated in Matlab as shown below:

tx_databits = round(rand(1,50000));The function rand(m,n) returns a matrix of size m × n with its entries uniformly distributed within the interval [0, 1]. The operation round rounds the arguments to the nearest integers, resulting a vector of integer number 0 and 1.

4.2 QPSK ModulationFor QPSK modulation, the mapping pattern is shown in Table. 1. It can be implemented in Matlab as shown below:

symbol_alphabet = [ 1+1j, -1+1j, 1-1j, -1-1j]/sqrt(2); M = log2(length(symbol_alphabet));symbols_int = 2.^[0:M-1]*reshape(tx_databits, M, []);tx_symbols = symbol_alphabet(symbols_int+1);

In this example, the power per symbol σ2 is normalized to 1.

4.3 Noise DefinitionIn most of the simulations, the noise is assumed to be complex-valued additive white Gaussian. The function randn(m,n) returns a m × n matrix of real numbers which follows the standard normal distribution N (0, 1). Therefore, a complex-valued Gaussian vector with average power per symbol 1 can be built by (randn(m,n) + j*randn(m,n))/sqrt(2).

In order to plot a Bit-Error-Ratio (BER)/Signal-to-Noise (SNR) curve, the transmission scheme is simulated for different SNR level. This is usually

Page 9: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

9

done by fixing the transmitted symbol power to 1 and varying the average noise power σ2 according to SNR levels.

Page 10: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

σσ2

10

The SNR utilizes a dB notation and can be defined as shown below:2

SNR in dB = 10 log10 s

n= 20 log

|σs | .10 E{|σn |}

Therefore, the average amplitude of the noise realization can be found byE{|σn |} = 10−SNR in dB

20 ,

which will be applied to the noise definition.In summary, the noise definition part is implemented as shown below:

SNR = 20; % an example: SNR = 20 dBsigma_v = 10^(-SNR/20);noise = sigma_v*(randn(size(tx_symbols))

+j*randn(size(tx_symbols)))/sqrt(2);

4.4 ChannelIn this example, the channel is assumed to be AWGN, which is implemented as following:

rx_symbols = tx_symbols + noise;

4.5 Check the Scatterplot using scatter (optional) You can check the constellation of the received symbol by

scatter(real(rx_symbols), imag(rx_symbols));

Sometimes, this is a useful tool for debugging.

4.6 Demodulation by a SlicerFor demodulation, we consider hard decision made by a slicer, which is im- plemented as shown below:

rx_databits_temp = [real(rx_symbols)<0; imag(rx_symbols)<0];rx_databits = reshape(rx_databits_temp, 1, []);

Page 11: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

11

4.7 Calculate Bit Error RatioThe Bit Error Ratio (BER) is defined as

BER = number of error bits ,number of transmit bits

which is calculated for each SNR value. It can be implemented in Matlabas

bit_errors = sum(tx_databits ~= rx_databits);ber = bit_errors/length(rx_databits);

4.8 Loop over SNRsUp to Section 4.7, the transmission is complete for one SNR value. In order to obtain the BER for different SNR levels, a loop structure using for command is shown below.

% define a SNR vector you want to simulateSNR_vec = -10:2:20;

% assign a vector for bit error ratio result.% note that is very important to predefine the vector in which% the results are stored. otherwise, if the vector is large, it% is increased in size while running the loop, which can be% incredibly slow.

ber = nan(1,length(SNR_vec));

% main loop over different SNR valuefor ii = 1:length(SNR_vec) % Attention: do not use i,j as loop index!

% because i^2=-1 by default.

% specify the SNR for current loopSNR = SNR_vec(ii);

... ...end

Page 12: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

12

ber(ii) = bit_errors/length(rx_databits);

Page 13: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

-1

-2

-3

-4

-5

Bit

Erro

r R

atio

0

4.9 Show the PlotAfter the simulation over different SNR values, a vector of BER is obtained with respect to the SNR vector previously defined. A simple plot can be generated using the commands below. BER curves are usually plotted in logarithmic scale by semilogy.

figuresemilogy(SNR_vec, ber, ’b-’) xlabel(’SNR [dB]’) ylabel(’Bit Error Ratio’) title(’QPSK AWGN’)grid on

QPSK AWGN10

10

10

10

10

10-10 -5 0 5 10 15

SNR [dB]

Figure 2: a Bit Error Ratio curve

If you want to plot more than one curve in one figure, use the commandhold. For more details, read the help file of the command plot.

2-D, 3-D, Plot & Subplot Features.

Line PlotsTo create two-dimensional line plots, use the plot function. For example, plot the value of the sine function from 0 to  :

Page 14: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

x = 0:pi/100:2*pi;

y = sin(x);

plot(x,y)

You can label the axes and add a title.

xlabel('x')ylabel('sin(x)')title('Plot of the Sine Function')

By adding a third input argument to the plot function, you can plot the same variables using a red dashed line.

plot(x,y,'r--')

Page 15: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

The 'r--' string is a line specification. Each specification can include characters for the line color, style, and marker. A marker is a symbol that appears at each plotted data point, such as a +, o, or *. For example, 'g:*' requests a dotted green line with * markers.Notice that the titles and labels that you defined for the first plot are no longer in the current figure window. By default, MATLAB® clears the figure each time you call a plotting function, resetting the axes and other elements to prepare the new plot.

To add plots to an existing figure, use hold.

x = 0:pi/100:2*pi;

y = sin(x);

plot(x,y)

hold on

y2 = cos(x);plot(x,y2,'r:')legend('sin','cos')

Page 16: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

Until you use hold off or close the window, all plots appear in the current figure window.

1. 3-D PlotsThree-dimensional plots typically display a surface defined by a function in two variables, z = f(x,y) .To evaluate z, first create a set of (x,y) points over the domain of the function using meshgrid.

[X,Y] = meshgrid(-2:.2:2);

Z = X .* exp(-X.^2 - Y.^2);

Then, create a surface plot.

surf(X,Y,Z)

Page 17: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

Both the surf function and its companion mesh display surfaces in three dimensions. surf displays both the connecting lines and the faces of the surface in color. mesh produces wireframe surfaces that color only the lines connecting the defining points.

2. SubplotsYou can display multiple plots in different subregions of the same window using the subplot function.For example, create four plots in a 2-by-2 grid within a figure window.

t = 0:pi/10:2*pi;

[X,Y,Z] = cylinder(4*cos(t));

subplot(2,2,1); mesh(X); title('X');subplot(2,2,2); mesh(Y); title('Y');subplot(2,2,3); mesh(Z); title('Z');subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z');

Page 18: Line Plots · Web view• from the command line by using the command help : >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of

The first two inputs to the subplot function indicate the number of plots in each row and column. The third input specifies which plot is active.

Resources

1- http://www.nt.tuwien.ac.at/fileadmin/courses/389068/MATLAB_introduction.pdf