CT216_Lab1
CT216_Lab1
Communication Technology
LAB-1
Group-2
Kirtan Chhatbar-202301098
Tirth Kanani-202301075
Honor Code
1. Write a program to generate (i.e., simulate) the Bernouilli Random Variable (RV) X ∼
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
% 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);
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
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.
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
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
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ˆ .
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
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];
%implementing BEC
p=input('enter erasure probability:');
y=rand(1,N) <p;
% Iterative decoding
decoded_matrix = encoded_matrix; % Initialize the decoding matrix
% 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
1. Denote a vector of BEC erasure probabilities as qset = {0, 0.1, 0.2, . . . , 1}.
(a) Perform Nsim experiments of encoding, BEC channel and iterative decoding that you
(b) Thus, you will generate Nsim values of the decoder success flag Tn, for n ∈ {1, 2, . . . , Nsim}.
Nsim
PNsim
n=1 Tn.
3. Plot psucc(q) as function of q ∈ qset. Your plot should match the simulation result in the red
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;
end
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]);