Upload
shahzad-jalbani
View
67
Download
4
Embed Size (px)
DESCRIPTION
Analog Digital Communication System. This lab manual describes much about simulating multiple digital systems with the MATLAB.
Citation preview
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 two-
dimensional 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
1 modulate() Signal modulation for communications simulations. Y=MODULATE(X,Fc,Fs,METHOD,OPT)
2 demodulate() Y = DEMODULATE(H, X) performs baseband demodulation of the signal X using demodulator object H.
3 modem() H = MODEM.<TYPE>(...) returns a modulation object H of a particular TYPE for performing modulation and/or demodulation.
4 awgn() Y = AWGN(..., POWERTYPE) specifies the units of SNR and SIGPOWER. POWERTYPE can be 'db' or 'linear'.
5 biterr() 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.
6 rayleighchan() Construct a Rayleigh fading channel object. CHAN = RAYLEIGHCHAN(TS, FD) constructs a frequency-flat ("single path") Rayleigh fading channel object.
7 symerr() Compute number of symbol errors and symbol error rate. [NUMBER,RATIO] = SYMERR(X,Y) compares the elements in the two matricesX and Y.
8 fec() 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
1 reshape() 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.
2 randn() 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.
3 randi() 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.
4 de2bi() Converts decimal numbers to binary numbers.
5 bi2de() Converts binary numbers to decimal numbers
6 imread() A = imread(FILENAME,FMT) reads a grayscale or color image from the file specified by the string FILENAME.
7 imshow() 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.
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 0 1 1 1 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])
0 2 4 6 8 10 12 14 16 18 20
-1
0
1
n Values
Am
plitu
de
Imaginary Values PSK
0 2 4 6 8 10 12 14 16 18 20
-1
0
1
n Values
Am
plitu
de
Real Values PSK
-1 -0.5 0 0.5 1
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Quadra
ture
In-Phase
Scatter plot
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)));
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());
0 2 4 6 8 10 12 14 16 18 20
-1
0
1
n Values
Am
plitu
de
8 QAM modulation Imaginary Values
0 2 4 6 8 10 12 14 16 18 20
-1
0
1
n Values
Am
plitu
de
8 QAM modulation Real Values
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
Quadra
ture
In-Phase
Scatter plot
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')
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
-5 0 5 10 15 2010
-5
10-4
10-3
10-2
10-1
100
SNR in dB
Bit E
rror
Rate
Comparison between Rayleigh Fading and AWGN
Rayleigh fading
AWGN
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
0 5 10 15 20 25 30 35 4010
-4
10-2
100
0 5 10 15 20 25 30 35 4010
-0.47
10-0.39
10-0.31
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 -2 0 2 4 6 8 10 12 1410
-6
10-5
10-4
10-3
10-2
10-1
100
4-PSK SYMBOL ERROR RATE
SNR in dB
Sym
bol E
rror
Rate
-4 -2 0 2 4 6 8 10 12 1410
-6
10-5
10-4
10-3
10-2
10-1
100
4-PSK BIT ERROR RATE
SNR in dB
Bit
Err
or R
ate
-5 0 5 10 15 20
10-4
10-3
10-2
10-1
Eb/No in dB
Sym
bol e
rror
rat
e
Symbol error rate for M-psk
M=2
M=4
M=8
M=16
M=32
M=64
-5 0 5 10 15 20
10-5
10-4
10-3
10-2
10-1
Eb/No in dB
Bit e
rror
rate
Bit error rate for M-psk
M=2
M=4
M=8
M=16
M=32
M=64
0 5 10 15 20 2510
-5
10-4
10-3
10-2
10-1
100
Eb/No in dB
Sym
bol err
or
rate
Symbol error rate for M-QAM
M=2
M=4
M=8
M=16
M=32
M=64
0 5 10 15 20 2510
-6
10-5
10-4
10-3
10-2
10-1
100
Eb/No in dB
Bit e
rror
rate
Bit error rate for M-QAM
M=2
M=4
M=8
M=16
M=32
M=64
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')
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.
Received Image Received Image Received Image
Received Image Received Image Received Image
original image
0 10 20 30 4010
-5
100
Bit error rate with equalization
SNR in dB
Bit e
rror
rate
M=2
M=4
M=8
M=16
0 10 20 30 40
10-1
100
Bit error rate without equalization
SNR in dB
Bit e
rror
rate
M=2
M=4
M=8
M=16
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.
4.6
clearall clc clearfigure M=4; K=131; N=255; mpsk=modem.pskmod(M); %Creating object of
modulation
-20 -15 -10 -5 0 5 10 15 20 25
10-3
10-2
10-1
100
Bit Error Performance of Flat Fading Rayleigh Channel using BCH encoding
SNR in dB
Bit E
rror
Rate
Bit Error Rate of BCH encoding
Bit Error Rate without BCH encoding
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]