Você está na página 1de 21

Analog and Digital Communication

MATLAB Simulations for


Digital Communication
Systems
Submitted to: Dr Asim Samejo

Labs
12/8/2014

Simulation of Communication systems using MATLAB communication toolbox


Introduction:
In this lab report I have simulated some programs using Matlab. Using communication toolbox of
Matlab I have simulated some modulation techniques and their BER. I have simulated the results of
Rayleigh fading and AWGN channel models. Transmitted an image and observed its performance on
different dB signal strength. The results in this report are all simulation of codes for different
programs.

Communication Tool box:


Communications System Toolbox provides algorithms for the design, simulation, and analysis of
communications systems. These capabilities are provided as MATLAB functions, MATLAB System
objects, and Simulink blocks. The system toolbox enables source coding, channel coding,
interleaving, modulation, equalization, synchronization, and channel modeling. You can also analyze
bit error rates, generate eye and constellation diagrams, and visualize channel characteristics. Using
adaptive algorithms, you can model dynamic communications systems that use OFDM, OFDMA, and
MIMO techniques. Algorithms support fixed-point data arithmetic and C or HDL code generation

Constellation Diagram:
A constellation diagram is a representation of a signal modulated by a digital modulation scheme
such as quadrature amplitude modulation or phase-shift keying. It displays the signal as a twodimensional scatter diagram in the complex plane at symbol sampling instants. In a more abstract
sense, it represents the possible symbols that may be selected by a given modulation scheme as
points in the complex plane. Measured constellation diagrams can be used to recognize the type of
interference and distortion in a signal.

3.1 Description of different MATLAB functions which are used in this lab manual.

No:

Function

Working

modulate()

Signal modulation for communications simulations.


Y=MODULATE(X,Fc,Fs,METHOD,OPT)

demodulate()

Y = DEMODULATE(H, X) performs baseband demodulation


of the signal X using demodulator object H.

modem()

awgn()

biterr()

rayleighchan()

symerr()

fec()

H = MODEM.<TYPE>(...) returns a modulation object H of


a particular
TYPE for performing modulation and/or demodulation.
Y = AWGN(..., POWERTYPE) specifies the units of SNR and
SIGPOWER.
POWERTYPE can be 'db' or 'linear'.
Compute number of bit errors and bit error rate.
[NUMBER,RATIO] = BITERR(X,Y) compares the unsigned
binary representation of the elements in the two matrices
X and Y.
Construct a Rayleigh fading channel object. CHAN =
RAYLEIGHCHAN(TS, FD) constructs a frequency-flat
("single path") Rayleigh fading channel object.
Compute number of symbol errors and symbol error rate.
[NUMBER,RATIO] = SYMERR(X,Y) compares the elements
in the two matricesX and Y.
Forward error control code implementation. H =
FEC.<TYPE>(...) returns a forward error control code
object H of a particular TYPE for performing encoding
and/or decoding.

There are some generic functions which are useful for this lab manual description for these
functions is given as:

No:

Function

Working

reshape()

randn()

randi()

de2bi()

RESHAPE(X,M,N) returns the M-by-N matrix whose


elements are taken columnwise from X. An error results if
X does not have M*N elements.
Normally distributed pseudorandom numbers. R =
RANDN(N) returns an N-by-N matrix containing
pseudorandom values drawn from the standard normal
distribution. RANDN(M,N) or RANDN([M,N]) returns an
M-by-N matrix. RANDN(M,N,P,...) or RANDN([M,N,P,...])
returns an M-by-N-by-P-by-... array.
R = randi(IMAX,N) returns an N-by-N matrix containing
pseudorandom
integer values drawn from the discrete uniform
distribution on 1:IMAX.
randi(IMAX,M,N) or randi(IMAX,[M,N]) returns an M-by-N
matrix.
Converts decimal numbers to binary numbers.

bi2de()

Converts binary numbers to decimal numbers

imread()

imshow()

A = imread(FILENAME,FMT) reads a grayscale or color


image from the file
specified by the string FILENAME.
imshow(I,[LOW HIGH]) displays the grayscale image I,
specifying the display
range for I in [LOW HIGH].

3.2 Use the reshape function to re-arrange a matrix as illustrated in the figure below.

3.3 Please analyze in detail the operation of de2bi (), how can we obtain a fixed size binary Output
for given number.

Imaginary Values PSK


Scatter plot

Amplitude

1
0.8

0.6
0.4

10
12
n Values
Real Values PSK

14

16

18

20

Quadrature

-1

0
-0.2
-0.4

1
Amplitude

0.2

-0.6
-0.8

-1
-1

-1
0

10
12
n Values

14

16

18

-0.5

0
In-Phase

0.5

20

de2bi () function in MATLAB converts


decimal numbers into binary numbers
and the output of the function starts from LSB to MSB. Least significant bit to most significant bit.
Example
V=61;
Binary=de2bi(V)
Binary= 1

3.4 For now consider that we are simulating just 1000 QPSK modulated symbols using toolbox
functions. So we can set up some constants at the start of our simulation code.
clear all;
M=4; %Constellation size
Nsym=10000; %Number of symbols to be simulated
mpsk=modem.pskmod(M);
u=randi([0,M-1],1,Nsym);
s=modulate(mpsk,u);
scatterplot(s)
dmpsk=modem.pskdemod(M);
u_hat=demodulate(dmpsk,s);
figure (2)
subplot(2,1,1)
plot(imag(s(1:20)));
xlabel('n Values')
ylabel('Amplitude')
title('Imaginary Values PSK');
axis([0 20 -1.5 1.5])
subplot(2,1,2)
plot(real(s(1:20)));
xlabel('n Values')
ylabel('Amplitude')
title('Imaginary Values PSK');
axis([0 20 -1.5 1.5])

3.5 Now that we have an operational M-PSK transmission system, the same steps can be taken to
simulate an M-QAM modulation scheme by using appropriate sub-funtions of modem command.
Please sketch the constellation diagram for M-QAM and also plot the time domain waveform.

Code : 3.5
M=8;
%M=4;
Nsym=1000;
mqam=modem.qammod(M);
u=randi([0,M-1],1,Nsym); %symbols
s=modulate(mqam,u);
scatterplot(s); %Constellation Diagram
dmqam=modem.qamdemod(M);
u_hat=demodulate(dmqam,s);
figure (2)
subplot(2,1,1)
plot(imag(s(1:20)));
xlabel('n Values')
ylabel('Amplitude')
title('8 QAM modulation Imaginary Values')
axis([0 20 -1.5 1.5])
subplot(2,1,2)
plot(real(s(1:20)));
8 QAM modulation Imaginary Values
Scatter plot

Amplitude

1
3

0
2

-1
2

10
12
14
n Values
8 QAM modulation Real Values

16

18

20

Amplitude

Quadrature

-1

-2

-1

-3

10
12
n Values

14

16

18

20

-3

-2

-1

0
In-Phase

xlabel('n Values')
ylabel('Amplitude')
title('8 QAM modulation Real Values')
axis([0 20 -1.5 1.5])

4.3 Now we can extend this problem to study flat fading rayleigh channel. Flat fading
process can be conveniently realized as h=sqrt(0.5)*(randn()+1i*randn());

y=h*s+n;
The received signal can be equalized using ZF equalizer as

In a typical wired digital communication link the channel impulse response can be assumed
to be fixed or very slowly varying as a function of time. In this simulation we consider a
sample channel impulse response h which is defined in the subsequent psuedo-code.
Task 4_3
clear all
clc
M=2;
Nsym=1000;
mpsk=modem.pskmod(M);
%Creating object of modulation
dmpsk=modem.pskdemod(M);
%Creating object of demodulation
snr=-4:20;
%Defining the range of SNR
tic();
for j=1:length(snr)
ratio=[];
RATIO=[];
for i=1:100
u=randi([0,M-1],1,Nsym);%Generating Random Sequence
bits=de2bi(u); %Converting from decimal to binary
s=modulate(mpsk,u); %Modulating the Signal
s1=awgn(s,snr(j));%Adding Additive White Guassian
y1=demodulate(dmpsk,s1);
%Demodulating data
bits_y1=de2bi(y1);
h=sqrt(0.5)*(randn()+1i*randn());
s2=h.*s;
y=awgn(s2,snr(j));
h_star=conj(h);
s_hat=(h_star.*y)./((abs(h))^2);
u_hat=demodulate(dmpsk,s_hat);
bits_hat=de2bi(u_hat);
[symbol ratio(i)]=biterr(bits,bits_hat);
[symb RATIO(i)]=biterr(bits,bits_y1);
end
ber(j)=mean(ratio);
BER(j)=mean(RATIO);
end
toc()
semilogy(snr,ber,'r',snr,BER,'g')
legend('Rayleigh fading','AWGN','Location','NorthEastOutside')
xlabel('SNR in dB')
ylabel('BitErrorRate')
title('Comparison between Rayleigh Fading and AWGN')

Comparison between Rayleigh Fading and AWGN

10

Rayleigh fading
AWGN
-1

Bit Error Rate

10

-2

10

-3

10

-4

10

-5

10

-5

5
10
SNR in dB

15

20

4.4 In the next we use matlab builtin function to perform adaptive least mean square (LMS)
equalization using a certain initial symbols as pilot. The initial pilot symbols are used to train
the equalization filter. The idea is illustrated in the figure below

10

-2

10

-4

10

10

15

20

25

30

35

40

10

15

20

25

30

35

40

-0.31

10

-0.39

10

-0.47

10

Task 4.4 Code:


clear all
clc
M=4;
Nsymb=1000;
Nsims=10;
Tlen=200;
h = [.986; .845; .37; .137; .123+.31i];
m2psk=modem.pskmod(2);
mpsk=modem.pskmod(M);
dmpsk=modem.pskdemod(M);
%generate data (u) and modulate it (s)
u=randi([0,M-1],1,Nsymb-Tlen);
U=randi([0 1],1,Tlen);
s=modulate(mpsk,u);
p=modulate(m2psk,U);
S=[p s];
eqz=lineareq(8,lms(0.01),mpsk.constellation); %generate LMS equalizer object.
snr=0:4:40;
tic();
demod_noeq=modem.pskdemod(M);
for i=1:length(snr)
ser=[];
for k=1:Nsims
% with Equalizer
r=filter(h,1,S);
% convolve channel with modulated signal.
y=awgn(r,snr(i),'Measured');
[symbolest,yd]=equalize(eqz,y,S(1:Tlen)); % equalize the channel
u_hat=demodulate(dmpsk,yd);
[err_num ser(k)]=symerr(u,u_hat(Tlen+1:Nsymb));
% without Equalizer
u_hat_noeq=demodulate(demod_noeq,y);
[nnoeq,req(k)]=symerr(u_hat_noeq(Tlen+1:Nsymb),u);
end
% for Nsims

SER(i)=mean(ser);
SER_noeq(i)=mean(req);
end% for ebnov
clear figure
subplot(2,1,1)
semilogy(snr,SER)
subplot(2,1,2)
semilogy(snr,SER_noeq)

Bit Error Rate


In digital transmission, the number of bit errors is the number of
received bits of a data stream over a communication channel that
have been altered due to noise, interference, distortion or bit
synchronization errors.
The bit error rate or bit error ratio (BER) is the number of bit errors
divided by the total number of transferred bits during a studied
time interval. BER is a unit less performance measure, often
expressed as a percentage.
Bit error rate, BER is a key parameter that is used in assessing
systems that transmit digital data from one location to another.
When data is transmitted over a data link, there is a possibility of
errors being introduced into the system. If errors are introduced
into the data, then the integrity of the system may be
compromised. As a result, it is necessary to assess the
performance of the system, and bit error rate, BER, provides an
ideal way in which this can be achieved.
In this section we have plotted the graphsEb/N0 (SNR) versus
symbol error rate and Eb/No (SNR) versus bit error rate of different
modulation schemes.
We observed that as we are increasing the SNR the bit error rate
or symbol error rate are being decreased.We also observed that at
the greater value of M the bit error rate or symbol error rate is
higher than the lower values of M.

Consequently, there is tradeoff between the bit error rate and


bandwidth. It depends upon the application either we require
more bandwidth and sacrifice the errors in data or we sacrifice
bandwidth and prefer accurate information.
4-PSK SYMBOL ERROR RATE

10

-1

10

-3

10

-4

10

-5

10

-6

10

-4

-2

4
6
SNR in dB

10

12

14

12

14

4-PSK BIT ERROR RATE

10

-1

10

-2

10

Bit Error Rate

Symbol Error Rate

-2

10

-3

10

-4

10

-5

10

-6

10

-4

-2

4
6
SNR in dB

10

Bit error rate for M-psk


M=2
M=4
M=8
M=16
M=32
M=64

-1

10

-2

-3

10

-4

10

-5

10

-5

5
10
Eb/No in dB

15

20

Symbol error rate for M-psk


M=2
M=4
M=8
M=16
M=32
M=64

-1

10

Symbol error rate

Bit error rate

10

-2

10

-3

10

-4

10

-5

5
10
Eb/No in dB

15

20

Bit error rate for M-QAM

10

M=2
M=4
M=8
M=16
M=32
M=64

-1

10

-2

-3

10

-4

10

-5

10

-6

10

10
15
Eb/No in dB

20

25

Symbol error rate for M-QAM

10

M=2
M=4
M=8
M=16
M=32
M=64

-1

10

Symbol error rate

Bit error rate

10

-2

10

-3

10

-4

10

-5

10

10
15
Eb/No in dB

20

25

4.2
clear all
clc
clear figure
M=4;
mpsk=modem.pskmod(M);
%Creating object of
modulation
dmpsk=modem.pskdemod(M);
%Creating object of
demodulation
snr=-5:5:20;
image_data=imread('abc','jpg'); %Loading the image in
MATLAB
[r c]=size(image_data);
size(image_data)
img_array=reshape(double(image_data),r*c,1);
img_bits=de2bi(img_array,8);
img_vector=reshape(img_bits,1,r*c*8);
w=img_vector;
for i=1:length(w)/2
s(i)=bi2de(img_vector(1:log2(M)),log2(M));
img_vector(1:log2(M))=[];
end
q=1:length(snr);
for j=1:length(snr)
modulated_signal=modulate(mpsk,s); %Modulating Image
data
y=awgn(modulated_signal,snr(j));
%Adding
Additive White Guassian Noise
demodulated_Signal=demodulate(dmpsk,y);%Demodulating
Image data
img_vector_hat=de2bi(demodulated_Signal);
h_hat_vector=reshape(img_vector_hat',1,r*c*8);
img_bits_hat=reshape(h_hat_vector',r*c,8);
img_array_hat=bi2de(img_bits_hat);
imga_Data_hat=reshape(img_array_hat,r,c);
intArray = uint8(imga_Data_hat);
%Converting data
from double to unsigned integer
approximated_Signal=reshape(intArray,60,60,3);
q(j)=subplot(3,3,j);
imshow(approximated_Signal)
title('Received Image ')
end
subplot(3,3,7:9)
imshow(image_data)
title('original image')

Received Image

Received Image

Received Image

Received Image

Received Image

Received Image

original image

In this section we have transmit one grayscale image in the


awgn(Additive White Gaussian Noise) channel noise and observe the
effects at different SNR values as shown in figure below.
As we are increasing the SNR we find clearer image at the receiver.

4.5
Least mean squares (LMS) algorithms are a class of adaptive filter
used to mimic a desired filter by finding the filter coefficients that
relate to producing the least mean squares of the error signal
(difference between the desired and the actual signal). It is a
stochastic gradient descent method in that the filter is only
adapted based on the error at the current time.

Bit error rate with equalization

10
Bit error rate

M=2
M=4
M=8
M=16

-5

10

10

20
SNR in dB

30

40

Bit error rate without equalization

Bit error rate

10

M=2
M=4
M=8
M=16

-1

10

10

20
SNR in dB

30

40

4.6
Forward Error Correction (FEC)
This page describes forward error correction and its application and
mention matlab codes for different forward error correction techniques.
Forward Error Correction is the module used in wireless communication
to correct errors at the receiver end. These errors must have occurred
due to interference, noise or various impairments in the medium
between transmitter and receiver. It is also referred as short form FEC.
As the name suggests this module avoids retransmission of the
corrupted data as it helps in correcting the errors at the receiver.

FEC is not bandwidth efficient as it adds some amount of data as


overhead at the transmitter end.

FEC is power efficient compare to its' non FEC counterpart as with the
same transmit power one can achieve better BER in FEC based
communication system.
There are many techniques invented to be used as forward error
correction techniques such as Convolution coding, Turbo coding, BCH
coding and more.

Bit Error Rate of BCH encoding


Bit Error Rate without BCH encoding
0

Bit Error Performance of Flat Fading Rayleigh Channel using BCH encoding

10

-1

Bit Error Rate

10

-2

10

-3

10

-20

-15

-10

-5

5
SNR in dB

10

15

20

25

4.6
clearall
clc
clearfigure
M=4;
K=131;
N=255;
mpsk=modem.pskmod(M);
modulation

%Creating object of

dmpsk=modem.pskdemod(M);
%Creating object of
demodulation
cc=fec.bchenc(N,K);
%Creating object of BCH
Coder
cd=fec.bchdec(N,K);
%Creating object of BCH
decoder
snr=-20:30;
%Defining the range of
SNR
for j=1:length(snr)
u=randi([0,M-1],1,K);
%Generating random
sequence
b=de2bi(u);
%Converting data from
decimal to binary
s=encode(cc,b);
%Encoding data
m=bi2de(s);
%Converting data from
binary to decimal
msg=modulate(mpsk,m);
%Modulating the Signal
msg_nocode=modulate(mpsk,u);
ratio=1:length(snr);
fori=1:10000
%Imposing Flat Fading Rayleigh Channel to coded
signal
h1=sqrt(0.5)*(randn()+1i*randn());
s2=h1.*msg;
y=awgn(s2,snr(j),'measured');
h_star=conj(h1);
signal=(h_star.*y)./((abs(h1))^2);
%Imposing Flat Fading Rayleigh Channel to uncoded
signal
h2=sqrt(0.5)*(randn()+1i*randn());
s3=h2.*msg_nocode;
y_nocode=awgn(s3,snr(j));
h_star2=conj(h2);
signal2=(h_star2.*y_nocode)./((abs(h2))^2);
%Demodulating data
dem=demodulate(dmpsk,signal2);
msg_hat=demodulate(dmpsk,signal);
bits_nocode=de2bi(dem);
d=de2bi(msg_hat);
%Converting from
decimal to binary
ds=decode(cd,d);
%Decoding the
data
[Number ratio(i)]=biterr(ds,b);
%Simulatin bit
error rate
[number Ratio(i)]=biterr(b,bits_nocode);
end
bercode(j)=mean(ratio);

berno(j)=mean(Ratio);
end
clearfigure
semilogy(snr,bercode)
title('Bit Error Performance of Flat Fading Rayleigh
Channel using BCH encoding')
xlabel('SNR in dB')
ylabel('Bit Error Rate')
holdon
semilogy(snr,berno,'r')
legend('Bit Error Rate of BCH encoding','Bit Error
Rate without BCH encoding','location','NorthOutside')
axis([-20 28 10^(-3.25) 1])

[The End]

Você também pode gostar