You are on page 1of 38

Simulating Communication Systems with MATLAB : An Introduction

Aniruddha Chandra
ECE Department, NIT Durgapur, WB, India. aniruddha.chandra@ieee.org

September 23, 2010

NIT DGP Student Branch

Presentation Outline
Objective of the Lecture Expected Background Simulating Analog Communication Systems Amplitude Modulation (AM) Simulating Digital Communication Systems Binary Phase Shift Keying (BPSK)

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

Presentation Outline
Objective of the Lecture Expected Background Simulating Analog Communication Systems Amplitude Modulation (AM) Simulating Digital Communication Systems Binary Phase Shift Keying (BPSK)

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

Objective of the Lecture


After the Lecture Youll be able to

Sep. 23, 2010

Write your own Matlab Script Make a Analog/ Digital Communication Link Compare your Results with Theoretical Values

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

Presentation Outline
Objective of the Lecture Expected Background Simulating Analog Communication Systems Amplitude Modulation (AM) Simulating Digital Communication Systems Binary Phase Shift Keying (BPSK)

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

Expected Background
I assume that You understand

Sep. 23, 2010

Basic MATLAB Operations (function, matrix) Basics of Communication (modulation) Performance Metrics (BER)

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

Presentation Outline
Objective of the Lecture Expected Background Simulating Analog Communication Systems Amplitude Modulation (AM) Simulating Digital Communication Systems Binary Phase Shift Keying (BPSK)

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

Analog Communication Systems

Sep. 23, 2010

Source

Modulator Channel

Destination

Demodulator

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

Simulate a Source

Sep. 23, 2010

Source

Modulator Channel

Destination

Demodulator

Produces message signal e.g. a simple Sine wave

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

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;

Sep. 23, 2010

Define amplitude and frequency (initial phase is zero)


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

Construct the Signal


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

View the Signal


plot(t,m,'r');

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

10

Simulate a Source
Complete MATLAB Script [Prog1.m]

Sep. 23, 2010

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');

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

11

Simulate a Source

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

12

Simulate a Source
Assignment #1 [Prog2.m], [Prog3.m]

Sep. 23, 2010

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');

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

13

Simulate Modulation

Sep. 23, 2010

Source

Modulator Channel

Destination

Demodulator

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

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

14

Amplitude Modulation
Simulate with built-in functions
fs = 8000; fc = 300; t = [0:0.1*fs]'/fs; m = sin(20*pi*t); v = ammod(m,fc,fs); % % % % %

Sep. 23, 2010

[Prog4.m]

Sampling rate is 8000 samples per second Carrier frequency in Hz Sampling times for 0.1 second Representation of the signal Modulate m to produce v

figure(1) subplot(2,1,1); plot(t,m); subplot(2,1,2); plot(t,v); mr = amdemod(v,fc,fs);

% Plot m on top % Plot v below

% Demodulate v to produce m

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

% Plot m on top % Plot mr below

Source: Introduction to Communications Toolbox in MATLAB 7.6.0 (R2008) by Amit Degada Available: http://amitdegada.weebly.com/download.html

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

15

Amplitude Modulation

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

16

Amplitude Modulation

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

17

Amplitude Modulation
Dont have the feel??? . Try this
Define message signal, m(t ) = Vm sin (2f mt ) (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);

Sep. 23, 2010

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

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

18

Amplitude Modulation
Continued .
Modulate the Signal, v(t ) = Vc 1 +

Sep. 23, 2010

Vm sin (2f mt ) sin (2f c t ) 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 ;

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

19

Amplitude Modulation
Complete MATLAB Script [Prog5.m]

Sep. 23, 2010

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); c = Vc*sin(2*pi*fc*t); v = (1+m/Vc).*c; % Message % Carrier % 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

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

20

Amplitude Modulation

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

21

Amplitude Modulation
Assignment #2 [Prog6.m]

Sep. 23, 2010

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

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

22

Amplitude Modulation
Assignment #2 (Contd) [Prog7.m]

Sep. 23, 2010

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

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

23

Demodulation
Demodulate DSB-SC with filter [Prog8.m]

Sep. 23, 2010

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);

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

24

Demodulation

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

25

Demodulation
Ideal Demodulation of DSB-SC
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;

Sep. 23, 2010

[Prog9.m]

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);

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

26

Demodulation

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

27

Analog Communication Systems

Sep. 23, 2010

Source

Modulator Channel

Destination

Demodulator

Introduces noise Additive White Gaussian Noise

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

28

Simulate Channel
Introducing AWGN
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]) A. Chandra, ECE Deptt., NITD Simulating Communication Systems with MATLAB

Sep. 23, 2010

[Prog10.m]

29

Simulate Channel

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

30

Presentation Outline
Objective of the Lecture Expected Background Simulating Analog Communication Systems Amplitude Modulation (AM) Simulating Digital Communication Systems Binary Phase Shift Keying (BPSK)

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

31

Digital Communication Systems

Sep. 23, 2010

Source

Modulator Channel

Destination

Demodulator

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

32

Simulate BPSK
Simulation
%This program simulates BER clear all; close all; clc; num_bit=100000; max_run=20; Eb=1; SNRdB=0:1:9; SNR=10.^(SNRdB/10);

Sep. 23, 2010

[Prog11.m]
of BPSK in AWGN channel% %Signal length %Maximum number of iterations for a single SNR %Bit energy %Signal to Noise Ratio (in dB)

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

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

33

Simulate BPSK
Simulation (Contd.) [Prog11.m]

Sep. 23, 2010

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); s=2*data-1; N=sqrt(No/2)*randn(1,num_bit); Y=s+N; %Generate binary data source %Baseband BPSK modulation %Generate AWGN %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; avgError=avgError+Error; end %Calculate error/bit %Calculate error/bit for different runs %Termination of loop for different runs

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

34

Simulate BPSK
Simulation (Contd.) [Prog11.m]

Sep. 23, 2010

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

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

35

Simulate BPSK

Sep. 23, 2010

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

36

References
[1] http://www.mathworks.com/matlabcentral/

Sep. 23, 2010

[2] http://www.freewebs.com/acwebpage/teaching.htm [3] B.P. Lathi and Z. Ding, Modern Digital and Analog Communication Systems, Oxford University Press, International 4th edition, 2010. [4] J.G. Proakis, M. Salehi, and G. Bauch, Contemporary Communication Systems using MATLAB, Thomson/ CLEngineering, 2nd edition, 2003.

A. Chandra, ECE Deptt., NITD

Simulating Communication Systems with MATLAB

37

Sep. 23, 2010

Thank You! Questions???


aniruddha.chandra@ieee.org
A. Chandra, ECE Deptt., NITD Simulating Communication Systems with MATLAB 38

You might also like