You are on page 1of 16

CT 303 Digital

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;

sigma=[0 0.1 0.2 1 2];


sigma_2=0:0.1:2;
sym_err_prb=zeros(1,length(sigma));

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.

% Copyright 1996-2011 The MathWorks, Inc.

if (~isreal(x) || ischar(x))
error(message('comm:qfunc:InvalidArg'));
end
y = 0.5 * erfc(x/sqrt(2));
return;
OUTPUTS:

Received samples and AWGN Noise Samples for variance = 0


Received samples and AWGN Noise Samples for variance = 0.1
Received samples and AWGN Noise Samples for variance = 0.2
Received samples and AWGN Noise Samples for variance = 1
Received samples and AWGN Noise Samples for variance = 2
Observations:

- 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:

PAM (pulse amplitude modulation) is a type of modulation in which the


amplitude of a series of pulses is used to encode a signal. The
amplitude of pulses is monitored at predetermined intervals for
demodulation.

You might also like