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

Codes Dc Lab

Digital communication lab codes

Uploaded by

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

Codes Dc Lab

Digital communication lab codes

Uploaded by

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

Gram-Schmidt Orthogonalization: To find orthogonal basis vectors for the

given set of vectors and plot the orthonormal vectors

% Given set of vectors as columns


V = [1 1 0; 1 0 1; 0 1 1]';
% Number of vectors
num_vectors = size(V, 2);
% Initialize orthogonal and orthonormal matrices
U = zeros(size(V));
E = zeros(size(V));
% Gram-Schmidt Process
U(:,1) = V(:,1); % First orthogonal vector is the first input vector
E(:,1) = U(:,1) / norm(U(:,1)); % First orthonormal vector
for i = 2:num_vectors
U(:,i) = V(:,i);
for j = 1:i-1
U(:,i) = U(:,i) - (dot(V(:,i), E(:,j)) * E(:,j)); % Subtract projection onto previous orthonormal vectors
end
E(:,i) = U(:,i) / norm(U(:,i)); % Normalize to get orthonormal vector
end
% Display the orthonormal vectors
disp('Orthonormal basis vectors:');
disp(E);
% Plot the original and orthonormal vectors
figure;
hold on;
grid on;
axis equal;
% Plot original vectors
quiver3(0, 0, 0, V(1,1), V(2,1), V(3,1), 'r', 'LineWidth', 2);
quiver3(0, 0, 0, V(1,2), V(2,2), V(3,2), 'g', 'LineWidth', 2);
quiver3(0, 0, 0, V(1,3), V(2,3), V(3,3), 'b', 'LineWidth', 2);
% Plot orthonormal vectors
quiver3(0, 0, 0, E(1,1), E(2,1), E(3,1), 'r--', 'LineWidth', 2);
quiver3(0, 0, 0, E(1,2), E(2,2), E(3,2), 'g--', 'LineWidth', 2);
quiver3(0, 0, 0, E(1,3), E(2,3), E(3,3), 'b--', 'LineWidth', 2);
xlabel('X');
ylabel('Y');
zlabel('Z');
legend('Original V1', 'Original V2', 'Original V3', 'Orthonormal E1', 'Orthonormal E2', 'Orthonormal
E3');
title('Original and Orthonormal Vectors');
hold off;

Output:
Orthonormal basis vectors:
0.7071 0.4082 -0.5774
0.7071 -0.4082 0.5774
0 0.8165 0.5774
Simulation of binary baseband signals using a rectangular pulse and estimate the
BER for AWGN channel using matched filter receiver
% Simplified Parameters
N = 1e4; % Number of bits
SNR_dB = 0:5:20; % SNR values in dB
pulse_width = 1; % Pulse width for rectangular pulse
% Generate random binary data
data = randi([0 1], N, 1);
% Define the rectangular pulse
t = 0:0.01:pulse_width;
rect_pulse = ones(size(t));
% Initialize BER vector
BER = zeros(length(SNR_dB), 1);
for snr_idx = 1:length(SNR_dB)
% Modulate binary data
tx_signal = [];
for i = 1:N
if data(i) == 1
tx_signal = [tx_signal; rect_pulse'];
else
tx_signal = [tx_signal; zeros(size(rect_pulse'))];
end
end
% Add AWGN
SNR = 10^(SNR_dB(snr_idx) / 10);
noise_power = 1 / (2 * SNR);
noise = sqrt(noise_power) * randn(length(tx_signal), 1);
rx_signal = tx_signal + noise;
% Matched Filter
matched_filter = rect_pulse;
filtered_signal = conv(rx_signal, matched_filter, 'same');
% Sample the output of the matched filter
sample_interval = round(length(filtered_signal) / N);
sampled_signal = filtered_signal(1:sample_interval:end);
% Decision (Threshold = 0.5)
estimated_bits = sampled_signal > 0.5;
% Compute BER
num_errors = sum(estimated_bits ~= data);
BER(snr_idx) = num_errors / N;
end
% Plot BER vs. SNR
figure;
semilogy(SNR_dB, BER, 'b-o');
grid on;
xlabel('SNR (dB)');
ylabel('Bit Error Rate (BER)');
title('BER vs. SNR for Rectangular Pulse Modulated Binary Data');
Generate 16-QAM Modulation and obtain the QAM constellation
close all
m=16
k=log2(m);
n=9e3;
nsamp=1;
x=randint(n,1);
stem(x(1:20),'filled');
title('bit sequence');
xlabel('bit index');
ylabel('bit amplitude');
xsym=bi2de(reshape(x,k,length(x)/k).','left-msb');
figure;
stem(xsym(1:10));
title('symbol plot');
xlabel('symbol index');
ylabel('symbol amplitude');
y=modulate(modem.qammod(m),xsym);
ytx=y;
ebno=10
snr=ebno+10*log(k)-10*log10(nsamp);
yn=awgn(ytx,snr);
yrx=yn;
scatterplot(y);
scatterplot(yrx,30);

Output:
Bit sequence Symbol Plot
x = input (‘Enter the number of symbols:’);
N = 1:x;
disp(‘The number of symbols are N:’);
disp(N);
P = input(‘Enter the probabilities = ‘);
disp(P);
S = sort(P, ‘descend’);
disp(‘The sorted probabilities are:’);
disp(S);
[dict,avglen] = huffmandict(N,S);
disp(‘The average length of the code is:’);
disp(avglen);
H = 0;
for i = 1:x
H = H + (P(i) * log2(1/P(i)));
end
disp(‘Entropy is:’);
disp(H);
disp(‘bits/msg’);
E = (H/avglen) * 100;
disp(‘Efficiency is:’);
disp(E);
codeword = huffmanenco(N,dict);
disp(‘The codewords are:’);
disp(codeword);
decode = huffmandeco(codeword,dict);
disp(‘Decoded output is:’);
disp(decode);
Output:
Enter the number of symbols: 5
The number of symbols is N:
12345
Enter the probabilities =
0.4 0.25 0.2 0.1 0.05
The sorted probabilities are:
0.4000 0.2500 0.2000 0.1000 0.0500
The average length of the code is:
2.1000
Entropy is:
2.0414 bits/msg
Efficiency is:
97.217
The codewords are:
10 100 000 100 011
Decoded output is:
12345

Encoding and Decoding of binary data using a Hamming code.


% Hamming Code Encoding and Decoding Example
% Parameters of the Hamming Code
n = 15; % Codeword length
k = 11; % Message length
% Generate random binary data of length k (message length)
data = randi([0 1], k, 1); % Random message of k bits
disp('Original Message:');
disp(data');
% Encode the data using Hamming code (binary)
encData = encode(data, n, k, 'hamming/binary');
disp('Encoded Message:');
disp(encData');
% Introduce an error in the encoded message
errLoc = randerr(1, n); % Generate random error positions (1-bit error)
encData_corrupted = mod(encData + errLoc', 2); % Corrupt the encoded message
disp('Corrupted Encoded Message:');
disp(encData_corrupted');
% Decode the corrupted message using Hamming decoding
decData = decode(encData_corrupted, n, k, 'hamming/binary');
disp('Decoded Message (after error correction):');
disp(decData');
% Compare original data and decoded data
numerr = biterr(data, decData); % Count number of bit errors between original and decoded data
disp(['Number of bit errors between original and decoded message: ', num2str(numerr)]);
Output:
Original Message:
10110110010
Encoded Message:
101110011110010
Corrupted Encoded Message:
101110011010010
Decoded Message (after error correction):
10110110010
Number of bit errors between original and decoded message: 0

Encoding and Decoding of Convolution code


% Complete Example of Convolutional Code Encoding and Decoding
% Input message to be encoded
msg = [1 0 1 1 0 1 0 0];
% Define constraint length and generator polynomial
constraint_length = 3;
generator_polynomials = [7 5];
% Create trellis
trellis = poly2trellis(constraint_length, generator_polynomials);
% Encode the message
encoded_msg = convenc(msg, trellis);
% Introduce some noise (optional)
% Example: Flip a bit in the encoded message to simulate noise
encoded_msg_noisy = encoded_msg;
encoded_msg_noisy(4) = ~encoded_msg_noisy(4); % Flip the 4th bit
% Decode the noisy message using Viterbi decoder
traceback_length = 5;
decoded_msg = vitdec(encoded_msg_noisy, trellis, traceback_length, 'trunc', 'hard');
% Display results
disp('Original Message:');
disp(msg);
disp('Encoded Message:');
disp(encoded_msg);
disp('Noisy Encoded Message:');
disp(encoded_msg_noisy);
disp('Decoded Message:');
disp(decoded_msg);

Output:
Original Message:
10110100
Encoded Message:
110010001110110
Noisy Encoded Message:
110110001110110
Decoded Message:
10110100

You might also like