0% found this document useful (0 votes)
280 views

Simulation Experiments-Com Lab

The document contains Matlab code to simulate and generate various digital modulation techniques including: 1) NRZ, RZ, half sinusoid and raised cosine pulses and generate their corresponding eye diagrams for binary polar signaling. 2) Pulse code modulation (PCM) for signal modulation, quantization, encoding, and demodulation and display the resulting waveforms. 3) Calculation of the probability of bit error for coherent binary ASK, FSK and PSK modulation over an AWGN channel and comparison with their theoretical performance curves. 4) Simulation of QPSK and DPSK modulation involving generation of in-phase and quadrature carrier signals, binary data mapping, modulated signal generation and plotting of

Uploaded by

Sanjana M P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
280 views

Simulation Experiments-Com Lab

The document contains Matlab code to simulate and generate various digital modulation techniques including: 1) NRZ, RZ, half sinusoid and raised cosine pulses and generate their corresponding eye diagrams for binary polar signaling. 2) Pulse code modulation (PCM) for signal modulation, quantization, encoding, and demodulation and display the resulting waveforms. 3) Calculation of the probability of bit error for coherent binary ASK, FSK and PSK modulation over an AWGN channel and comparison with their theoretical performance curves. 4) Simulation of QPSK and DPSK modulation involving generation of in-phase and quadrature carrier signals, binary data mapping, modulated signal generation and plotting of

Uploaded by

Sanjana M P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

B.N.M.I.T Dept.

of ECE Communication Lab – 18ECL67

Simulation experiments using MATLAB (Part-B)

1. Write a Matlab code to simulate NRZ, RZ, half-sinusoid and raised cosine pulses
and generate eye diagram for binary polar signaling.

NRZ polar signal:

Close all; clc;


Clear all;
x=randi([0 1],1,10);
for ii=1:length(x)
if x(ii)==1;
x1(ii)=5;
else
x1(ii)=-5;
end;
end;
p=1;
t=0:0.01:length(x1)
for j=1:length(t)
if t(j)<=p
y(j)=x1(p)
else
p=p+1;
y(j)=x1(p);
end;
end;
plot(t,y,'r');axis([0 length(x1) -6 6])

RZ polar signal:

clc;clear all;close all;


x=randi([0 1],1,10); for
ii=1:length(x)
if x(ii)==1 x1(ii)=5;
else
x1(ii)=-5;
end;
end;
p=1;
a=0;
b=0.5;
t=0:0.01:length(x1);for
j=1:length(t)

BNMIT/T Feb – July 2021 1


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67
if t(j)>=a && t(j)<=b
y(j)=x1(p);
elseif t(j)>=b && t(j)<=p
y(j)=0;
else p=p+1;
a=a+1;
b=b+1;
end;
end;
plot(t,y,'r');axis([0 length(x1) -6 6]);

Sample eye diagram using sinc function:

N=100;
x=2*round(rand(N,1))-1;

x1 = x';
a = -5.5:1/100:1.5;
b = -3.5:1/100:3.5;
c = -1.5:1/100:5.5;
for i = 1:1:(N+2-11);
d = x1(1,i)*sinc(b);
e = x1(1,i+1)*sinc(b);f
= x1(1,i+2)*sinc(b);
hold on, plot(a,f); hold
on, plot(b,e); hold on,
plot(c,d); end

title('Eye Diagram - Before Equalization (W=3)');


xlabel('Time(s)');
ylabel('Amplitude');
axis([-1 1 -2 2])

code for half wave sine:

clc; close
all;clear
all;
t=linspace(0,pi,100);
y=sin(t);
plot(t,y);

BNMIT/T Feb – July 2021 2


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67

code for raised cosine pulse:

span = 10; % Filter span


rolloff = 0.2; % Rolloff factor
sps = 8; % Samples per symbol
M = 4; % Modulation alphabet sizek
= log2(M); % Bits/symbol
phOffset = pi/4; % Phase offset (radians)
n = 1; % Plot every nth value of the signal
offset = 0; % Plot every nth value of the signal, starting from offset+1

%Create the filter coefficients using the rcosdesign function.


filtCoeff = rcosdesign(rolloff,span,sps);
%Generate random symbols for an alphabet size of M.rng
default
data = randi([0 M-1],5000,1);
%Apply QPSK modulation.
dataMod = pskmod(data,M,phOffset);
%Filter the modulated data.
txSig = upfirdn(dataMod,filtCoeff,sps);
%Calculate the SNR for an oversampled QPSK signal.
EbNo = 20;
snr = EbNo + 10*log10(k) - 10*log10(sps);
%Add AWGN to the transmitted signal.
rxSig = awgn(txSig,snr,'measured');
%Apply the RRC receive filter.
rxSigFilt = upfirdn(rxSig, filtCoeff,1,sps);
%Demodulate the filtered signal.
dataOut = pskdemod(rxSigFilt,M,phOffset,'gray');

h = scatterplot(sqrt(sps)*txSig(sps*span+1:end-sps*span),sps,offset,'g.');hold on
scatterplot(rxSigFilt(span+1:end-span),n,offset,'kx',h) scatterplot(dataMod,n,offset,'r*',h)
legend('Transmit Signal','Received Signal','Ideal','location','best')

eyediagram(txSig(sps*span+1:sps*span+1000),2*sps)
eyediagram(rxSig(sps*span+1:sps*span+1000),2*sps)

BNMIT/T Feb – July 2021 3


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67

Expected waveforms:

NRZ Polar wave form:

RZ Polar wave form:

BNMIT/T Feb – July 2021 4


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67
Half sinusoid wave form:

Eye pattern:

BNMIT/T Feb – July 2021 5


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67

2. Write a Matlab code to simulate the Pulse code modulation and demodulation system and
display the waveforms.

PCM Modulation Code


clc; close
all;clear
all;
n=input ('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
L=2^n;
% % Signal Generation
% x=0:1/100:4*pi;
% y=8*sin(x); % Amplitude Of signal is 8v
% subplot(2,2,1);
% plot(x,y);grid on;
% Sampling Operation
x=0:2*pi/n1:4*pi; % n1 number of samples have to be selected
s=8*sin(x);
subplot(3,1,1);
plot(s);
title('Analog Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(3,1,2);
stem(s);grid on; title('Sampled Sinal'); ylabel('Amplitude--->'); xlabel('Time--->');

% Quantization Process
vmax=8;
vmin=-vmax;
del=(vmax-vmin)/L;
part=vmin:del:vmax; % level are between vmin and vmax with
difference of del
code=vmin-(del/2):del:vmax+(del/2); % Contain Quantized valuses
[ind,q]=quantiz(s,part,code); % Quantization process
% ind contain index number and q
contain quantized values

l1=length(ind);
l2=length(q);

for i=1:l1
if(ind(i)~=0) % To make index as binary decimal so
started from 0 to N
ind(i)=ind(i)-1;
end
i=i+1;

end
for i=1:l2
BNMIT/T Feb – July 2021 6
B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67
if(q(i)==vmin-(del/2))% To make quantize value in between the levels
q(i)=vmin+(del/2);
end
end
subplot(3,1,3);
stem(q);grid on; % Display the Quantize values
title('Quantized Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

% Encoding Process
figure
code=de2bi(ind,'left-msb'); % Convert the decimal to binary
k=1;
for i=1:l1
for j=1:n
coded(k)=code(i,j); % convert code matrix to a coded row vector
j=j+1;
k=k+1;
end i=i+1;
end
subplot(2,1,1); grid on;
stairs(coded); % Display the encoded signal
axis([0 100 -2 3]); title('Encoded Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

% Demodulation Of PCM signal

qunt=reshape(coded,n,length(coded)/n);
index=bi2de(qunt','left-msb'); % Getback the index in decimal form
q=del*index+vmin+(del/2); % getback Quantized values
subplot(2,1,2); grid on;
plot(q); % Plot Demodulated signal
title('Demodulated Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

BNMIT/T Feb – July 2021 7


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67

PCM waveforms:

BNMIT/T Feb – July 2021 8


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67

3. Write a program to Compute of the Probability of bit error for coherent binary ASK, FSK and PSK for
an AWGN Channel and Compare them with their Performance curves.

clc;
clear all;
close all;
EbN0dB=0:20;
EbN0=10.^(EbN0dB/10);
pe_bpsk=0.5*erfc(sqrt(EbN0));
pe_bfsk=0.5*erfc(sqrt(EbN0/2));
pe_bask=0.5*erfc(sqrt(EbN0/4));
semilogy(EbN0dB,pe_bpsk,'r*-', EbN0dB,pe_bfsk,'k+ ',EbN0dB,pe_bask,'m>-');
legend('BPSK-C', 'BFSK-C', 'BASK-C');
xlabel('Eb/N0(dB)');
ylabel('BER');
grid on

Simulation Results:

BNMIT/T Feb – July 2021 9


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67

4. Write a program to Simulate QPSK and DPSK transmitter and receiver.

QPSK

% QPSK Modulation
clc;
clear all;
close all;
%GENERATE QUADRATURE CARRIER SIGNAL
Tb=1;t=0:(Tb/100):Tb;fc=1;
c1=sqrt(2/Tb)*cos(2*pi*fc*t);
c2=sqrt(2/Tb)*sin(2*pi*fc*t);
%generate message signal
N=8;m=rand(1,N);
t1=0;t2=Tb
for i=1:2:(N-1)
t=[t1:(Tb/100):t2]
if m(i)>0.5
m(i)=1;
m_s=ones(1,length(t));
else
m(i)=0;
m_s=-1*ones(1,length(t));
end

%odd bits modulated signal


odd_sig(i,:)=c1.*m_s;
if m(i+1)>0.5
m(i+1)=1;
m_s=ones(1,length(t));
else
m(i+1)=0;
m_s=-1*ones(1,length(t));
end

%even bits modulated signal


even_sig(i,:)=c2.*m_s;
%qpsk signal
qpsk=odd_sig+even_sig;
%Plot the QPSK modulated signal
subplot(3,2,4);plot(t,qpsk(i,:));
title('QPSK signal');xlabel('t --- >');ylabel('s(t)');grid on; hold on;
t1=t1+(Tb+.01); t2=t2+(Tb+.01);
end
hold off
%Plot the binary data bits and carrier signal
subplot(3,2,1);stem(m);
title('binary data bits');xlabel('n ---- >');ylabel('b(n)');grid on;
subplot(3,2,2);plot(t,c1);
BNMIT/T Feb – July 2021 10
B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67
title('carrier signal-1');xlabel('t---- >');ylabel('c1(t)');grid on;
subplot(3,2,3);plot(t,c2);
title('carrier signal-2');xlabel('t --- >');ylabel('c2(t)');grid on;

% QPSK Demodulation
t1=0;t2=Tb
for i=1:N-1
t=[t1:(Tb/100):t2]
%correlator
x1=sum(c1.*qpsk(i,:));
x2=sum(c2.*qpsk(i,:));
%decision device
if (x1>0&&x2>0)
demod(i)=1;
demod(i+1)=1;
elseif (x1>0&&x2<0)
demod(i)=1;
demod(i+1)=0;
elseif (x1<0&&x2<0)
demod(i)=0;
demod(i+1)=0;
elseif (x1<0&&x2>0)
demod(i)=0;
demod(i+1)=1;
end
t1=t1+(Tb+.01); t2=t2+(Tb+.01);
end
subplot(3,2,5);stem(demod);
title('qpsk demodulated bits');xlabel('n ---- >');ylabel('b(n)');grid on;

BNMIT/T Feb – July 2021 11


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67

Simulation results:

BNMIT/T Feb – July 2021 12


B.N.M.I.T Dept. of ECE Communication Lab – 18ECL67

Simulate DPSK transmitter and receiver.

clc;
clear all;
close all;
n=100;
x=[ones(1,20) zeros(1,20) ones(1,20) zeros(1,20) ones(1,20)];
subplot(4,1,1);
plot(x);
title('input signal');
xlabel('number of samples');
ylabel('amplitude');
f=1*10^6;
fs=10*10^6;
for i=0:n-1
d(i+1)=sin(2*pi*(f/fs)*i);
end
subplot(4,1,2);
plot(d);
title('carrier signal');
xlabel('number of samples');
ylabel('amplitude');
for i=0:n-1
if(x(i+1)==0)
x(i+1)=sin(2*pi*(f/fs)*i);
else
x(i+1)=sin(2*pi*(f/fs)*i+pi);
end
end
subplot(4,1,3);
plot(x);
title('PSK Signal');
xlabel('number of samples');
ylabel('amplitude');
for i=0:n-1
if(x(i+1)==sin(2*pi*(f/fs)*i))
x(i+1)=0;
else
x(i+1)=1;
end
end
subplot(4,1,4);
plot(x);
title('demodulated Signal');
xlabel('number of samples');
ylabel('amplitude');

BNMIT/T Feb – July 2021 13


B.N.M.I.T Dept. of ECE Communication
Lab – 18ECL67

DPSK waveforms:

BNMIT/T Feb – July 2021 14

You might also like