CT 303 Digital Communications Lab 9: Heer Gohil 201901135
CT 303 Digital Communications Lab 9: Heer Gohil 201901135
Communications
Lab 9
Heer Gohil
201901135
1. Problem 5.1
%lab9_1
clear all;
clc;
close all;
k=1:1:10;
var = [0 0.1 1 2];
for j=1:length(var)
sigma_2=var(j);
r0 = 1 + sqrt(sigma_2)*randn(1,10);
r1 = zeros(1,10);
noise = sqrt(sigma_2).*randn(1,10);
for i=1:length(k)
if k(i) <= 5
r1(i) = 1 + noise(i);
else
r1(i) = -1 + noise(i);
end
end
s0 = 1 * ones(1,10);
s1 = zeros(1,10);
for i=1:length(k)
if k(i) <= 5
s1(i) = 1;
else
s1(i) = -1;
end
end
%correlation
figure(j);
out1 = xcorr(r0,s0);
subplot(2,2,1);
plot(k,out1(1:10));
title('Correlation of r0 and s0 for variance = ' + string(var(j)));
out2 =xcorr(r0,s1);
subplot(2,2,2);
plot(k,out2(1:10));
title('Correlation of r0 and s1 for variance = ' + string(var(j)));
out3= xcorr(r1,s0);
subplot(2,2,3);
plot(k,out3(1:10));
title('Correlation of r1 and s0 for variance = ' + string(var(j)));
out4 = xcorr(r1,s1);
subplot(2,2,4);
plot(k,out4(1:10));
title('Correlation of r1 and s1 for variance = ' + string(var(j)));
end
Output:
Variance 0
Variance 0.1
Variance 1
Variance 2
Observations
- As the variance rises, the noise in the channel rises as well, distorting the
correlation.
1. Problem 5.8
%lab9_12
clear all;
clc;
close all;
for i=1:length(sigma)
for j=1:10000
% simulated error rate
[sim_err_prob,rec_sig,noise]=smldpe58(sigma(i));
sym_err_prb(i)=sym_err_prb(i)+sim_err_prob;
end
figure(2*i-1);
plot(rec_sig(1:1000));
title('Received Signal ');
figure(2*i);
plot(noise(1:1000));
title('AWGN Noise');
sym_err_prb(i)=sym_err_prb(i)/10000;
end
theo_err_prb=zeros(1,length(sigma_2));
for i=1:length(sigma_2)
% signal-to-noise ratio
SNR_per_bit=exp(sigma_2(i));
% theoretical error rate
theo_err_prb(i)=(3/2)*qfunc(sqrt((4/5)*(5/(4*(sigma_2(i))))));
end
% Plotting commands follow.
figure(11);
semilogy(sigma_2,theo_err_prb,'r','linewidth',1.5);
hold on;
semilogy(sigma,sym_err_prb,'bo','linewidth',1.5);
ylabel('Symbol Error Probability');
xlabel('Variance');
title('Serr vs Variance');
legend('Theoritical','Simulation');
function [p,rec_sig,noise]=smldpe58(sigmain)
% [p]=smldPe58(snr_in_dB)
% SMLDPE58 simulates the probability of error for the given
% snr_in_dB, signal to noise ratio in dB.
d=1;
sgma=sqrt(sigmain); % sigma, standard deviation of noise
N=10000; % number of symbols being simulated
% Generation of the quaternary data source follows.
for i=1:N
temp=rand; % a uniform random variable over (0,1)
if (temp<0.25)
dsource(i)=0; % With probability 1/4, source output is "00."
elseif (temp<0.5)
dsource(i)=1; % With probability 1/4, source output is "01."
elseif (temp<0.75)
dsource(i)=2; % With probability 1/4, source output is "10."
else
dsource(i)=3; % With probability 1/4, source output is "11."
end
end
% detection, and probability of error calculation
numoferr=0;
rec_sig=zeros(1,N);
noise=zeros(1,N);
for i=1:N
% the matched filter outputs
noise(i)=gngauss(sgma);
if (dsource(i)==0)
r=-3*d+noise(i); % if the source output is "00"
elseif (dsource(i)==1)
r=-d+noise(i); % if the source output is "01"
elseif (dsource(i)==2)
r=d+noise(i); % if the source output is "10"
else
r=3*d+noise(i); % if the source output is "11"
end
% Detector follows.
if (r<-2*d)
decis=0; % Decision is "00."
elseif (r<0)
decis=1; % Decision is "01."
elseif (r<2*d)
decis=2; % Decision is "10."
else
decis=3; % Decision is "11."
end
rec_sig(i)=r;
if (decis~=dsource(i)) % If it is an error, increase the error counter.
numoferr=numoferr+1;
end
end
p=numoferr/N; % probability of error estimate
function y = qfunc(x)
%QFUNC Q function.
% Y = QFUNC(X) returns 1 minus the cumulative distribution function of the
% standardized normal random variable for each element of X. X must be a real
% array. The Q function is defined as:
%
% Q(x) = 1/sqrt(2*pi) * integral from x to inf of exp(-t^2/2) dt
%
% It is related to the complementary error function (erfc) according to
%
% Q(x) = 0.5 * erfc(x/sqrt(2))
%
% See also QFUNCINV, ERF, ERFC, ERFCX, ERFINV, ERFCINV.
if (~isreal(x) || ischar(x))
error(message('comm:qfunc:InvalidArg'));
end
y = 0.5 * erfc(x/sqrt(2));
return;
OUTPUTS:
- As the variance increases the noise in the channel increases and hence the
Probability of error increases.
2. Question
%lab9_2
close all;
clc;
clear all;
oversampling_factor = 4;
m = oversampling_factor;
%parameters for sampled raised cosine pulse
a = 0.5;
length = 10;% (truncated outside [-length*T,length*T])
%raised cosine transmit filter (time vector set to a dummy variable which is not used)
[transmit_filter,time_axis] = raised_cosine(a,m,length);
%NUMBER OF SYMBOLS
nsymbols = 100;
%BPSK SYMBOL GENERATION
symbols = sign(rand(nsymbols,1) -.5);
%UPSAMPLE BY m
nsymbols_upsampled = 1+(nsymbols-1)*m;%length of upsampled symbol sequence
symbols_upsampled = zeros(nsymbols_upsampled,1);%initialize
symbols_upsampled(1:m:nsymbols_upsampled)=symbols;%insert symbols with spacing m
%NOISELESS MODULATED SIGNAL
tx_output = conv(symbols_upsampled,transmit_filter);
receiver_filter = transmit_filter;
output_wav = conv(tx_output,receiver_filter);
output_wav = output_wav(41:517);
figure(1);
plot(tx_output);
hold on;
plot(output_wav);
legend('Transmitter Output Waveform','Receiver Output Waveform');
title('Question 2');
OUTPUT:
OBSERVATIONS:
- As seen above, the receiver's output matches the transmitter due to the
receiver's match filter attribute.
3. AM PAM Signal
%lab9_3
clear all;
close all;
clc;
T=1;
delta_T=T/200; %sampling interval
alpha=1; %rolloff factor
fc=40/T; %carrier frequency
A_m=1; %amplitude
t=-5*T+delta_T:delta_T:5*T; %time axis
N=length(t);
for i=1:N
if (abs(t(i))~=T/(2*alpha))
g_T(i) = sinc(t(i)/T)*(cos(pi*alpha*t(i)/T)/(1-4*alpha^2*t(i)^2/T^2));
else
g_T(i) = 0; % The value of g_T is 0 at t=T/(2*alpha)
end % and at t=-T/(2*alpha).
end
G_T=abs(fft(g_T)); %spectrum of g_T
u_m=A_m*g_T.*cos(2*pi*fc*t); %the modulated signal
U_m=abs(fft(u_m)); %spectrum of the modulated signal
% actual frequency scale
f=-0.5/delta_T:1/(delta_T*(N-1)):0.5/delta_T;
% Plotting commands follow.
figure(1);
plot(f,fftshift(G_T));
axis([-1/T 1/T 0 max(G_T)]);
grid on;
title('Spectrum of the Baseband Signal');
xlabel('Frequency');
ylabel('Magnitude');
figure(2);
plot(f,fftshift(U_m));
grid on;
title('Spectrum of the Modulated Signal');
xlabel('Frequency');
ylabel('Magnitude');
OUTPUT:
Observations:
Lab 10
Problem 7.7
MATLAB Code
T = 1;
Definethefunction
g_T = @(t) 1 / 2 * (1 -cos(2 *pi* t / T));
Definethefrequencyofsignal
f_c = 6 / T;
definethetimestep
dt = 1 / (20 * f_c);
Definethetime
t = 0: dt: T;
M = 8;
subplot(8, 1, 1);
CaluclatethePhaseofeachwave
phase = (0: M - 1) * 2 *pi/ M; i
= 1;
for ph = phase
Calculatethewave
sig = g_T(t) .*cos(2 *pi* f_c * t +
ph); plotthewave
subplot(8
, 1, i);
plot(t,
sig)
i = i + 1;
end
Plots
1. Problem 70.8
cm_sm32.m
MATLAB Code
T = 1;
Definethefunction
g_T = @(t) 1 / 2 * (1 -cos(2 *pi* t / T));
Definethefrequencyofsignal f_c = 6
/ T; definethetimestep
dt = 1 / (20 * f_c);
Definethetime t = 0:
dt: T; M = 8;
subplot(8, 1, 1);
CaluclatethePhaseofeachwave phase =
(0: M - 1) * 2 *pi/ M; i = 1;
forph = phase
Calculatethewave
sig = g_T(t) .*cos(2 *pi* f_c * t + ph);
plotthewave
subplot(8, 1, i);
plot(t, sig)
i = i + 1;
end
Plots
2. Problem 7.14
cm_sm41.m
function[p]=cm_sm41(snr_in_dB)
[p]=cm_sm41(snr_in_dB)
CM_SM41findstheprobabilityoferrorforthegiven
valueofsnr_in_dB,SNRindB.
N=100000;
d=2; min.distancebetweensymbols Eav=5.75*d^2;
energypersymbol snr=10^(snr_in_dB/10);
SNRperbit(given) sgma=sqrt(Eav/(8*snr));
noisevariance M=8;
Generationofthedatasourcefollows. fori=1:N
temp=rand; auniformR.V.between0and1
dsource(i)=1+floor(M*temp); anumberbetween1and8,uniform
end
Mappingtothesignalconstellationfollows.
mapping=[-3*d d;
-d d;
d d;
3*d d;
-3*d -d;
-d -d;
d -d;
3*d -d;];
fori=1:N
qam_sig(i,:)=mapping(dsource(i),:);
end
receivedsignal
fori=1:N
[n(1),n(2)]=gngauss(sgma);
r(i,:)=qam_sig(i,:)+n; end
detectionanderrorprobabilitycalculation
numoferr=0;
fori=1:N
Metriccomputationfollows.
forj=1:M
metrics(j)=(r(i,1)-mapping(j,1))^2+(r(i,2)-mapping(j,2))^2;
end
[min_metric decis] =min(metrics);
if(decis~=dsource(i))
numoferr=numoferr+1;
end
end p=numoferr/(N);
MATLAB Code
SNRindB1=0:2:15;
SNRindB2=0:0.1:15; M=8;
k=log2(M);
smld_err_prb=zeros(1,length(SNRindB1));
fori=1:length(SNRindB1)
smld_err_prb(i) = cm_sm41(SNRindB1(i)); simulatederrorrate
end
theo_err_prb=zeros(1,length(SNRindB2));
fori=1:length(SNRindB2)
SNR=exp(SNRindB2(i) *log(10)/10); signal-to-noiseratio
theoreticalsymbolerrorrate
theo_err_prb(i)=4*qfunc(sqrt(3*k*SNR/(M-1)));
end
Plottingcommandsfollow.
figure(1);
semilogy(SNRindB1,smld_err_prb,’*’);
holdon; semilogy(SNRindB2,theo_err_prb);
xlabel(’Perr’);
ylabel(’SNR(dB)’);
title(’ProbabilityofErrorvsSNRdB’);
legend(’Simulation’,’Theoritical’);
Plots
3. Problem 7.15
cm_sm41vv2.m
function[p]=cm_sm41v2(snr_in_dB)
[p]=cm_sm41(snr_in_dB)
CM_SM41findstheprobabilityoferrorforthegiven
valueofsnr_in_dB,SNRindB.
N=10000;
d=1; min.distancebetweensymbols Eav=4.732;
energypersymbol snr=10^(snr_in_dB/10);
SNRperbit(given) sgma=sqrt(Eav/(8*snr));
noisevariance M=8;
Generationofthedatasourcefollows. fori=1:N
temp=rand; auniformR.V.between0and1
dsource(i)=1+floor(M*temp); anumberbetween1and8,uniform
end
Mappingtothesignalconstellationfollows.
mapping=[(1+sqrt(3)), 0;
-d,d;
d,d;
(-1-sqrt(3)),0;
0,(1+sqrt(3));
-d,-d;
d,-d;
0,(-1-sqrt(3));];
fori=1:N
qam_sig(i,:)=mapping(dsource(i),:);
end
receivedsignal
fori=1:N
[n(1),n(2)]=gngauss(sgma);
r(i,:)=qam_sig(i,:)+n; end
detectionanderrorprobabilitycalculation
numoferr=0;
fori=1:N
Metriccomputationfollows.
forj=1:M
metrics(j)=(r(i,1)-mapping(j,1))^2+(r(i,2)-mapping(j,2))^2;
end
[min_metric decis] =min(metrics);
if(decis~=dsource(i))
numoferr=numoferr+1;
end
end p=numoferr/(N);
MATLAB Code
SNRindB1=0:2:15;
SNRindB2=0:0.1:15; M=8;
k=log2(M);
smld_err_prb=zeros(1,length(SNRindB1));
fori=1:length(SNRindB1)
forj=1:10
smld_err_prb(i)=smld_err_prb(i) + cm_sm41v2(SNRindB1(i)); simulatederror rate
end
smld_err_prb(i)=smld_err_prb(i)/10;
end
theo_err_prb=zeros(1,length(SNRindB2));
fori=1:length(SNRindB2) SNR=exp(SNRindB2(i)*log(10)/10);
signal-to-noiseratio theoreticalsymbolerrorrate
theo_err_prb(i) = qfunct(sqrt(3*k*SNR/(M-1))); end
Plottingcommandsfollow.
figure(1);
semilogy(SNRindB1,smld_err_prb,’*’); holdon;
semilogy(SNRindB2,theo_err_prb);
xlabel(’Perr’);
ylabel(’SNR(dB)’);
title(’ProbabilityofErrorvsSNRdB’);
legend(’Simulation’,’Theoritical’);
Plots
Observation
As can be seen from the Per r of both constellations, the second one has a higher Per r for
the same SNR (dB).