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

CT216_Lab1

The document outlines a lab report for the CT216 course at Dhirubhai Ambani Institute, focusing on communication systems. It includes programming tasks related to Bernoulli random variables, product encoders, and Monte Carlo simulations to evaluate decoder success probabilities in a Binary Erasure Channel (BEC). The report emphasizes the importance of originality and proper citation in academic work.

Uploaded by

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

CT216_Lab1

The document outlines a lab report for the CT216 course at Dhirubhai Ambani Institute, focusing on communication systems. It includes programming tasks related to Bernoulli random variables, product encoders, and Monte Carlo simulations to evaluate decoder success probabilities in a Binary Erasure Channel (BEC). The report emphasizes the importance of originality and proper citation in academic work.

Uploaded by

shivampatel9189
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Dhirubhai Ambani Institute of Information and

Communication Technology

CT216: Introduction to Communication Systems

LAB-1

Group-2

Kirtan Chhatbar-202301098
Tirth Kanani-202301075
Honor Code

I, Kirtan Chhatbar(202301098), declare that


→ the work that I am presenting is my own work
→ I have not copied the work (Matlab code, results, etc.) that someone
else has done
→ Concepts, understanding and insights I will be describing are my own
→ Wherever I have relied on an existing work that is not my own, I have
provided a proper reference citation
→ I make this pledge truthfully. I know that violation of this solemn
pledge can carry grave consequences

I, Tirth Kanani(202301075), declare that


→ the work that I am presenting is my own work
→ I have not copied the work (Matlab code, results, etc.) that someone
else has done
→ Concepts, understanding and insights I will be describing are my own
→ Wherever I have relied on an existing work that is not my own, I have
provided a proper reference citation
→ I make this pledge truthfully. I know that violation of this solemn
pledge can carry grave consequences
Q-1.1 Bernoulli Random Variable

1. Write a program to generate (i.e., simulate) the Bernouilli Random Variable (RV) X ∼

Bern(p) that we have defined in the class.

2. Generate Nsim instances of X in a Matlab array, i.e., X = {X1, X2, . . . , XNsim }.

3. Use the functions mean and var in Matlab1 with X to evaluate the simulated mean and

variance of the simulated X. Compare the simulated answer that you obtain for Nsim = 10, 100

and 1000 with the theoretical expressions for E [X] and Var [X] we have defined in the class.

p = 0.7; % success
Nsim_array = [10, 100, 1000]; % no. of samples in each experiment

% Expected value and variance


E_X = p; % Expected value --> 1.p + 0.(1-p)
Var_X = p * (1 - p); % Variance --> (E_Xsq) - (E_X)sq

% Theoretical Mean and Variance


fprintf('Theoretical Mean - %.2f\n', E_X);

Theoretical Mean - 0.70

fprintf('Theoretical Variance - %.2f\n\n', Var_X);

Theoretical Variance - 0.21

% looping Nsim_array
for i = 1:length(Nsim_array)
x = Nsim_array(i); % first iteration x=10 , second x=100 ....
Output = binornd(1, p, [1, x]); % Generating random binomial data

simulated_mean = mean(Output);
simulated_variance = var(Output);

fprintf('Nsim = %d\n', x);


fprintf('Simulated Mean: %.4f\n', simulated_mean);
fprintf('Simulated Variance: %.4f\n', simulated_variance);
fprintf('Difference in Mean: %.4f\n', abs(simulated_mean - E_X)); % taking absoulte values
fprintf('Difference in Variance: %.4f\n\n', abs(simulated_variance - Var_X));
end

Nsim = 10
Simulated Mean: 0.6000
Simulated Variance: 0.2667
Difference in Mean: 0.1000
Difference in Variance: 0.0567
Nsim = 100
Simulated Mean: 0.7100
Simulated Variance: 0.2080
Difference in Mean: 0.0100
Difference in Variance: 0.0020
Nsim = 1000
Simulated Mean: 0.7060
Simulated Variance: 0.2078
Difference in Mean: 0.0060
Difference in Variance: 0.0022

Q-1.2 Product Encoder, BEC, and Decoder

In this section of the Lab, you will simulate rate r = K/N = 4/9 (square) product code with K = 4

bits and N = 9 bits, where K is the number of information bits and N is the number of encoded

bits.

1. Generate K instances of a RV M ∼ Bern(0.5) and store in a vector m that represents the

message or information bits produced by an informative source.

2. Develop the encoder of (N, K) product encoder to generate N encoded bits in an array c.

3. Generate N instances of Y ∼ Bern(q) RV and store in an array y. This models the erasures

introduced by the BEC(q) channel.

4. Erase those bits of the transmitted codeword c where the corresponding bits of the vector y

are ones. Denote the resultant array of N bits received at the output of the BEC as an array

5. Develop the iterative row-by-row and column-by-column decoder of the product code. Pass r

through your iterative product decoding algorithm. The decoder should produce an estimate

mˆ of the transmitted message bits.

6. Generate a decoder success flag T which is set to 1 when the decoded message bitword mˆ

matches with the transmitted word m. Set T = 0 if the decoder fails and there are erasures

left in mˆ .

K = input('Enter no. of information bits (K):');


N = input('Enter no. of encoded bits (N):');

if sqrt(K)~= floor(sqrt(K))
error('K must be a perfect square');
end
if N ~= K +2*sqrt(K)+1
error('Invalid N')
end

% generating K bits randomly


m=round(rand(1,K));
fprintf("info bits are :");
disp(m);

message_matrix=reshape(m,sqrt(K),sqrt(K));%generating matrix
row_parity=mod(sum(message_matrix,2),2); % parity bit along row
col_parity=mod(sum(message_matrix,2),2); %parity bit along coloumn
final_parity=mod(sum(col_parity,1),2);

encoded_matrix=[message_matrix,row_parity ;col_parity',final_parity];

a=encoded_matrix(:);% make a 1d array

%implementing BEC
p=input('enter erasure probability:');
y=rand(1,N) <p;

%erase bitws in a where y =1


r=a;
r(y==1)=-1; % -1 are the erased bits
fprintf('noisy array;')
disp(r);

% Iterative decoding
decoded_matrix = encoded_matrix; % Initialize the decoding matrix

% Iterative row and column decoding


max_iterations = 10; % Define a limit on iterations
for iteration = 1:max_iterations

% Row decoding
for i = 1:size(decoded_matrix, 1) - 1 % Skip the parity row
row = decoded_matrix(i, :);
if sum(row == -1) == 1 % If only one bit is erased
missing_index = find(row == -1); % Find the missing bit index
row(missing_index) = mod(sum(row(row ~= -1)), 2); % Recover the missing bit
decoded_matrix(i, :) = row; % Update the matrix
end
end

% Column decoding
for j = 1:size(decoded_matrix, 2) - 1 % Skip the parity column
col = decoded_matrix(:, j);
if sum(col == -1) == 1 % If only one bit is erased
missing_index = find(col == -1); % Find the missing bit index
col(missing_index) = mod(sum(col(col ~= -1)), 2); % Recover the missing bit
decoded_matrix(:, j) = col; % Update the matrix
end
end

% Check if decoding is complete (no -1 left)


if all(decoded_matrix(:) ~= -1)
break;
end
end
% Extract the decoded message bits
decoded_message = decoded_matrix(1:sqrt(K), 1:sqrt(K));
decoded_message = decoded_message(:)'; % Convert back to a 1D array

fprintf("Decoded message bits are: ");


disp(decoded_message);

% Generate decoder success flag


if isequal(decoded_message, m)
fprintf("Decoding successful! T = 1\n");
else
fprintf("Decoding failed. T = 0\n");
end

Q-1.3 Monte Carlo Simulations

1. Denote a vector of BEC erasure probabilities as qset = {0, 0.1, 0.2, . . . , 1}.

2. For each of eleven values of q ∈ qset,

(a) Perform Nsim experiments of encoding, BEC channel and iterative decoding that you

have developed in Section 1.2. 3

(b) Thus, you will generate Nsim values of the decoder success flag Tn, for n ∈ {1, 2, . . . , Nsim}.

Evaluate the simulated decoder success probability psucc(q) = 1

Nsim

PNsim

n=1 Tn.

3. Plot psucc(q) as function of q ∈ qset. Your plot should match the simulation result in the red

dotted line in Fig. 1.

K = 4;
N = 9;
r = K / N;
p = 0.5;
max_iterations = 100;
Nsim = 1000;
q_values = 0:0.1:1;
psucc = zeros(size(q_values));
for q_idx = 1:length(q_values)
q = q_values(q_idx);
T_values = zeros(1, Nsim);
for n = 1:Nsim
m = rand(1, K) < p;

m_reshaped = reshape(m, 2, 2);


c = [m_reshaped; mod(sum(m_reshaped, 1), 2)];
c = [c, mod(sum(c, 2), 2)];
c = c(:)';
y = rand(1, N) < q;
r = c;
r(y == 1) = NaN;
decoded = r;
for iter = 1:max_iterations
decoded_matrix = reshape(decoded, 3, 3);
changed = false;
for i = 1:3
row = decoded_matrix(i, :);
if sum(isnan(row)) == 1
known_sum = sum(row(~isnan(row)));
row(isnan(row)) = mod(known_sum, 2);
decoded_matrix(i, :) = row;
changed = true;
end
end
for i = 1:3
col = decoded_matrix(:, i);
if sum(isnan(col)) == 1
known_sum = sum(col(~isnan(col)));
col(isnan(col)) = mod(known_sum, 2);
decoded_matrix(:, i) = col;
changed = true;
end
end
decoded = decoded_matrix(:)';
if ~any(isnan(decoded)) || ~changed
break;
end
end
decoded_reshaped = reshape(decoded, 3, 3);
m_hat = decoded_reshaped(1:2, 1:2);
m_hat = m_hat(:)';
T_values(n) = all(m_hat == m);
end
psucc(q_idx) = mean(T_values);
fprintf('Completed q = %.2f, Success probability = %.4f\n', q, psucc(q_idx));

end

Completed q = 0.00, Success probability = 1.0000


Completed q = 0.10, Success probability = 0.9990
Completed q = 0.20, Success probability = 0.9890
Completed q = 0.30, Success probability = 0.9410
Completed q = 0.40, Success probability = 0.8270
Completed q = 0.50, Success probability = 0.6220
Completed q = 0.60, Success probability = 0.4070
Completed q = 0.70, Success probability = 0.2150
Completed q = 0.80, Success probability = 0.0570
Completed q = 0.90, Success probability = 0.0090
Completed q = 1.00, Success probability = 0.0000

figure;
plot(q_values, psucc, 'r-.', 'LineWidth', 2);
grid on;
xlabel('Erasure Probability (q)');
ylabel('Decoder Success Probability');
title('BEC Channel Performance');
axis([0 1 0 1]);

You might also like